From 355c29d4184fdf08196dba23f0d7c2e5eb5cd77a Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 1 May 2014 21:19:31 -0700 Subject: [PATCH] Some additional test cases and cleanups --- .../activitystreams/actions/Adapters.java | 1 - .../activitystreams/actions/HtmlForm.java | 5 - .../activitystreams/actions/StylesValue.java | 1 - .../actions/test/ActionsTest.java | 149 +++++++++++++++++- 4 files changed, 142 insertions(+), 14 deletions(-) diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/Adapters.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/Adapters.java index 1526904..e42ea55 100644 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/Adapters.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/Adapters.java @@ -22,7 +22,6 @@ package com.ibm.common.activitystreams.actions; import com.ibm.common.activitystreams.ASObject; -import com.ibm.common.activitystreams.TypeValue; import com.ibm.common.activitystreams.actions.StylesValue.Builder; import com.ibm.common.activitystreams.internal.Adapter; import com.ibm.common.activitystreams.util.AbstractDictionaryObjectAdapter; diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/HtmlForm.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/HtmlForm.java index ee30e91..b6fe0ad 100755 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/HtmlForm.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/HtmlForm.java @@ -21,15 +21,10 @@ */ package com.ibm.common.activitystreams.actions; -import static com.ibm.common.activitystreams.Makers.object; -import static com.ibm.common.activitystreams.Makers.type; - import java.io.ObjectStreamException; import com.google.common.base.Supplier; import com.ibm.common.activitystreams.ASObject; -import com.ibm.common.activitystreams.TypeValue; -import com.ibm.common.activitystreams.actions.UrlTemplate.Builder; /** */ diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/StylesValue.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/StylesValue.java index b31f4eb..8f42a92 100755 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/StylesValue.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/StylesValue.java @@ -27,7 +27,6 @@ import java.io.Serializable; import java.util.Map; import com.google.common.collect.ImmutableMap; -import com.ibm.common.activitystreams.TypeValue; import com.ibm.common.activitystreams.util.AbstractDictionaryObject; /** diff --git a/actions/src/test/java/com/ibm/common/activitystreams/actions/test/ActionsTest.java b/actions/src/test/java/com/ibm/common/activitystreams/actions/test/ActionsTest.java index 97eeccb..f16b84a 100644 --- a/actions/src/test/java/com/ibm/common/activitystreams/actions/test/ActionsTest.java +++ b/actions/src/test/java/com/ibm/common/activitystreams/actions/test/ActionsTest.java @@ -1,26 +1,28 @@ package com.ibm.common.activitystreams.actions.test; +import static com.google.common.collect.Iterables.getFirst; +import static com.ibm.common.activitystreams.actions.ActionMakers.embedAction; import static com.ibm.common.activitystreams.actions.ActionMakers.htmlForm; +import static com.ibm.common.activitystreams.actions.ActionMakers.httpAction; +import static com.ibm.common.activitystreams.actions.ActionMakers.intentAction; import static com.ibm.common.activitystreams.actions.ActionMakers.io; import static com.ibm.common.activitystreams.actions.ActionMakers.parameter; +import static com.ibm.common.activitystreams.actions.ActionMakers.styles; import static com.ibm.common.activitystreams.actions.ActionMakers.typedPayload; import static com.ibm.common.activitystreams.actions.ActionMakers.urlTemplate; -import static com.ibm.common.activitystreams.actions.ActionMakers.embedAction; -import static com.ibm.common.activitystreams.actions.ActionMakers.intentAction; -import static com.ibm.common.activitystreams.actions.ActionMakers.httpAction; -import static com.ibm.common.activitystreams.actions.ActionMakers.styles; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import org.junit.Test; -import static com.google.common.collect.Iterables.*; - +import com.google.common.base.Throwables; import com.google.common.net.MediaType; import com.ibm.common.activitystreams.ASObject; import com.ibm.common.activitystreams.LinkValue; @@ -37,6 +39,121 @@ import com.ibm.common.activitystreams.actions.UrlTemplate; public final class ActionsTest { + @Test + public void roundtripHttpActionHandler2() { + + HttpActionHandler hah1 = + httpAction() + .url("http://example.org") + .method("GET") + .get(); + + HttpActionHandler hah2 = + roundTrip2(hah1); + + assertEquals("GET", hah2.method()); + + Iterable i = hah2.url(); + LinkValue lv = getFirst(i,null); + assertNotNull(lv); + assertTrue(lv instanceof SimpleLinkValue); + SimpleLinkValue slv = (SimpleLinkValue) lv; + assertEquals("http://example.org", slv.url()); + + } + + @Test + public void roundtripIntentActionHandler2() { + + IntentActionHandler iah1 = + intentAction() + .id("urn:example:foo:1") + .get(); + + IntentActionHandler iah2 = + roundTrip2(iah1); + + assertEquals("urn:example:foo:1", iah2.id()); + + } + + @Test + public void roundtripEmbedActionHandler2() { + + EmbedActionHandler eah1 = + embedAction() + .style( + styles() + .set("width", "100px") + ) + .get(); + + EmbedActionHandler eah2 = + roundTrip2(eah1); + + Iterable styles = + eah2.styles(); + StylesValue style = + getFirst(styles, null); + assertNotNull(style); + assertTrue(style.has("width")); + assertEquals("100px", style.get("width")); + } + + @Test + public void roundtripHtmlForm2() { + HtmlForm htmlForm1 = + htmlForm() + .parameter("foo", parameter().optional()) + .get(); + + HtmlForm htmlForm2 = + roundTrip2(htmlForm1); + + ParametersValue params = + htmlForm2.parameters(); + assertTrue(params.has("foo")); + + Parameter param = params.get("foo"); + assertFalse(param.required()); + } + + @Test + public void roundtripTypedValue2() { + TypedPayload typedPayload1 = + typedPayload() + .mediaType(MediaType.create("application", "json")) + .get(); + + TypedPayload typedPayload2 = + roundTrip2(typedPayload1); + + assertEquals( + typedPayload1.mediaType(), + typedPayload2.mediaType()); + } + + @Test + public void roundtripUrlTemplate2() { + UrlTemplate template1 = + urlTemplate() + .template("http://example.org{/foo}") + .parameter("foo", "bar") + .get(); + UrlTemplate template2 = + roundTrip2(template1); + assertEquals( + template1.template(), + template2.template()); + ParametersValue value = + template2.parameters(); + assertTrue(value.has("foo")); + } + + + + + @Test public void roundtripHttpActionHandler() { @@ -156,4 +273,22 @@ public final class ActionsTest { new ByteArrayInputStream(out.toByteArray()); return io.readAs(in); } + + @SuppressWarnings("unchecked") + private T roundTrip2(T writable){ + try { + ByteArrayOutputStream out = + new ByteArrayOutputStream(); + ObjectOutputStream oos = + new ObjectOutputStream(out); + oos.writeObject(writable); + ByteArrayInputStream in = + new ByteArrayInputStream(out.toByteArray()); + ObjectInputStream ois = + new ObjectInputStream(in); + return (T)ois.readObject(); + } catch (Throwable t) { + throw Throwables.propagate(t); + } + } }