summaryrefslogtreecommitdiffstats
path: root/sangria-core/src/test/java/com/tavianator/sangria/core
diff options
context:
space:
mode:
Diffstat (limited to 'sangria-core/src/test/java/com/tavianator/sangria/core')
-rw-r--r--sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java29
-rw-r--r--sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java126
-rw-r--r--sangria-core/src/test/java/com/tavianator/sangria/core/PriorityTest.java100
-rw-r--r--sangria-core/src/test/java/com/tavianator/sangria/core/TypeLiteralsTest.java81
4 files changed, 335 insertions, 1 deletions
diff --git a/sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java b/sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java
index 349dced..f27aea8 100644
--- a/sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java
+++ b/sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java
@@ -32,7 +32,7 @@ import static org.hamcrest.Matchers.*;
* Tests for {@link DelayedError}.
*
* @author Tavian Barnes (tavianator@tavianator.com)
- * @version 1.0
+ * @version 1.1
* @since 1.0
*/
public class DelayedErrorTest {
@@ -83,4 +83,31 @@ public class DelayedErrorTest {
}
});
}
+
+ @Test
+ public void testCancel() {
+ Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ DelayedError error = DelayedError.create(binder(), "Message");
+ error.cancel();
+ }
+ });
+ }
+
+ @Test
+ public void testLateCancel() {
+ final DelayedError[] errorHolder = new DelayedError[1];
+
+ Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ errorHolder[0] = DelayedError.create(binder(), "Message");
+ errorHolder[0].cancel();
+ }
+ });
+
+ thrown.expect(IllegalStateException.class);
+ errorHolder[0].cancel();
+ }
}
diff --git a/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java b/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java
new file mode 100644
index 0000000..c4ccc36
--- /dev/null
+++ b/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java
@@ -0,0 +1,126 @@
+/****************************************************************************
+ * Sangria *
+ * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * Licensed under the Apache License, Version 2.0 (the "License"); *
+ * you may not use this file except in compliance with the License. *
+ * You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, software *
+ * distributed under the License is distributed on an "AS IS" BASIS, *
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
+ * See the License for the specific language governing permissions and *
+ * limitations under the License. *
+ ****************************************************************************/
+
+package com.tavianator.sangria.core;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import javax.inject.Qualifier;
+
+import com.google.inject.CreationException;
+import com.google.inject.Key;
+import com.google.inject.name.Names;
+import org.hamcrest.Matcher;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
+
+/**
+ * Tests for {@link PotentialAnnotation}s.
+ *
+ * @author Tavian Barnes (tavianator@tavianator.com)
+ * @version 1.1
+ * @since 1.1
+ */
+public class PotentialAnnotationTest {
+ @Retention(RetentionPolicy.RUNTIME)
+ @Qualifier
+ private @interface Simple {
+ }
+
+ private final PotentialAnnotation none = PotentialAnnotation.none();
+ private final Annotation nameAnnotation = Names.named("name");
+
+ @Test
+ public void testHasAnnotation() {
+ assertThat(none.hasAnnotation(), is(false));
+ assertThat(none.annotatedWith(Simple.class).hasAnnotation(), is(true));
+ assertThat(none.annotatedWith(nameAnnotation).hasAnnotation(), is(true));
+ }
+
+ @Test(expected = CreationException.class)
+ public void testInvalidAnnotatedWithType() {
+ none.annotatedWith(Simple.class)
+ .annotatedWith(Simple.class);
+ }
+
+ @Test(expected = CreationException.class)
+ public void testInvalidAnnotatedWithInstance() {
+ none.annotatedWith(Names.named("name"))
+ .annotatedWith(Names.named("name"));
+ }
+
+ @Test
+ public void testGetKey() {
+ assertThat(none.getKey(String.class),
+ equalTo(new Key<String>() { }));
+ assertThat(none.annotatedWith(Simple.class).getKey(String.class),
+ equalTo(new Key<String>(Simple.class) { }));
+ assertThat(none.annotatedWith(nameAnnotation).getKey(String.class),
+ equalTo(new Key<String>(nameAnnotation) { }));
+ }
+
+ @Test
+ public void testVisitor() {
+ PotentialAnnotation.Visitor<String> visitor = new PotentialAnnotation.Visitor<String>() {
+ @Override
+ public String visitNoAnnotation() {
+ return "none";
+ }
+
+ @Override
+ public String visitAnnotationType(Class<? extends Annotation> annotationType) {
+ assertThat((Object)annotationType, equalTo((Object)Simple.class));
+ return "type";
+ }
+
+ @Override
+ public String visitAnnotationInstance(Annotation annotation) {
+ assertThat(annotation, equalTo(nameAnnotation));
+ return "instance";
+ }
+ };
+
+ assertThat(none.accept(visitor), equalTo("none"));
+ assertThat(none.annotatedWith(Simple.class).accept(visitor), equalTo("type"));
+ assertThat(none.annotatedWith(nameAnnotation).accept(visitor), equalTo("instance"));
+ }
+
+ @Test
+ public void testToString() {
+ assertThat(none.toString(),
+ equalTo("[no annotation]"));
+ assertThat(none.annotatedWith(Simple.class).toString(),
+ equalTo("@com.tavianator.sangria.core.PotentialAnnotationTest.Simple"));
+ assertThat(none.annotatedWith(nameAnnotation).toString(),
+ equalTo("@com.google.inject.name.Named(value=name)"));
+ }
+
+ /**
+ * Needed to avoid compilation error to to inferred type being anonymous class.
+ */
+ private static <T> Matcher<Key<T>> equalTo(Key<T> key) {
+ return Matchers.equalTo(key);
+ }
+
+ private static <T> Matcher<T> equalTo(T object) {
+ return Matchers.equalTo(object);
+ }
+}
diff --git a/sangria-core/src/test/java/com/tavianator/sangria/core/PriorityTest.java b/sangria-core/src/test/java/com/tavianator/sangria/core/PriorityTest.java
new file mode 100644
index 0000000..0869565
--- /dev/null
+++ b/sangria-core/src/test/java/com/tavianator/sangria/core/PriorityTest.java
@@ -0,0 +1,100 @@
+/****************************************************************************
+ * Sangria *
+ * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * Licensed under the Apache License, Version 2.0 (the "License"); *
+ * you may not use this file except in compliance with the License. *
+ * You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, software *
+ * distributed under the License is distributed on an "AS IS" BASIS, *
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
+ * See the License for the specific language governing permissions and *
+ * limitations under the License. *
+ ****************************************************************************/
+
+package com.tavianator.sangria.core;
+
+import java.util.*;
+
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
+
+/**
+ * Tests for {@link Priority}s.
+ *
+ * @author Tavian Barnes (tavianator@tavianator.com)
+ * @version 1.1
+ * @since 1.1
+ */
+public class PriorityTest {
+ private final Priority defaultPriority = Priority.getDefault();
+ private final Priority one = Priority.create(1);
+ private final Priority oneTwo = Priority.create(1, 2);
+ private final Priority two = Priority.create(2);
+
+ @Test
+ public void testOrdering() {
+ List<Priority> list = Arrays.asList(
+ defaultPriority.next(),
+ two,
+ oneTwo.next(),
+ oneTwo,
+ two.next(),
+ defaultPriority,
+ defaultPriority.next().next(),
+ one,
+ one.next());
+ Collections.sort(list);
+ assertThat(list, contains(
+ defaultPriority,
+ defaultPriority.next(),
+ defaultPriority.next().next(),
+ one,
+ one.next(),
+ oneTwo,
+ oneTwo.next(),
+ two,
+ two.next()));
+
+ assertThat(defaultPriority, equalTo(Priority.getDefault()));
+ assertThat(defaultPriority.next(), equalTo(Priority.getDefault().next()));
+ assertThat(defaultPriority, not(equalTo(Priority.getDefault().next())));
+
+ assertThat(one, equalTo(Priority.create(1)));
+ assertThat(oneTwo, equalTo(Priority.create(1, 2)));
+ assertThat(two, equalTo(Priority.create(2)));
+
+ assertThat(oneTwo.hashCode(), equalTo(Priority.create(1, 2).hashCode()));
+ }
+
+ @Test
+ public void testIsDefault() {
+ assertThat(defaultPriority.isDefault(), is(true));
+ assertThat(defaultPriority.next().isDefault(), is(true));
+
+ assertThat(one.isDefault(), is(false));
+ assertThat(oneTwo.isDefault(), is(false));
+ assertThat(two.isDefault(), is(false));
+ assertThat(two.next().isDefault(), is(false));
+ }
+
+ @Test
+ public void testToString() {
+ assertThat(Priority.getDefault().toString(), equalTo("default priority"));
+ assertThat(Priority.getDefault().next().toString(), equalTo("default priority + 1"));
+ assertThat(Priority.getDefault().next().next().toString(), equalTo("default priority + 2"));
+
+ assertThat(Priority.create(1).toString(), equalTo("priority [1]"));
+ assertThat(Priority.create(1).next().toString(), equalTo("priority [1] + 1"));
+ assertThat(Priority.create(1).next().next().toString(), equalTo("priority [1] + 2"));
+
+ assertThat(Priority.create(1, 2).toString(), equalTo("priority [1, 2]"));
+ assertThat(Priority.create(1, 2).next().toString(), equalTo("priority [1, 2] + 1"));
+ assertThat(Priority.create(1, 2).next().next().toString(), equalTo("priority [1, 2] + 2"));
+ }
+}
diff --git a/sangria-core/src/test/java/com/tavianator/sangria/core/TypeLiteralsTest.java b/sangria-core/src/test/java/com/tavianator/sangria/core/TypeLiteralsTest.java
new file mode 100644
index 0000000..3e7cdb8
--- /dev/null
+++ b/sangria-core/src/test/java/com/tavianator/sangria/core/TypeLiteralsTest.java
@@ -0,0 +1,81 @@
+/****************************************************************************
+ * Sangria *
+ * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * Licensed under the Apache License, Version 2.0 (the "License"); *
+ * you may not use this file except in compliance with the License. *
+ * You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, software *
+ * distributed under the License is distributed on an "AS IS" BASIS, *
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
+ * See the License for the specific language governing permissions and *
+ * limitations under the License. *
+ ****************************************************************************/
+
+package com.tavianator.sangria.core;
+
+import java.util.*;
+
+import javax.inject.Provider;
+
+import com.google.inject.TypeLiteral;
+import org.hamcrest.Matcher;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests for {@link TypeLiterals}.
+ *
+ * @author Tavian Barnes (tavianator@tavianator.com)
+ * @version 1.1
+ * @since 1.1
+ */
+public class TypeLiteralsTest {
+ @Test
+ public void testListOf() {
+ assertThat(TypeLiterals.listOf(String.class),
+ equalTo(new TypeLiteral<List<String>>() { }));
+ assertThat(TypeLiterals.listOf(new TypeLiteral<Class<?>>() { }),
+ equalTo(new TypeLiteral<List<Class<?>>>() { }));
+ }
+
+ @Test
+ public void testSetOf() {
+ assertThat(TypeLiterals.setOf(String.class),
+ equalTo(new TypeLiteral<Set<String>>() { }));
+ assertThat(TypeLiterals.setOf(new TypeLiteral<Class<?>>() { }),
+ equalTo(new TypeLiteral<Set<Class<?>>>() { }));
+ }
+
+ @Test
+ public void testMapOf() {
+ assertThat(TypeLiterals.mapOf(String.class, String.class),
+ equalTo(new TypeLiteral<Map<String, String>>() { }));
+ assertThat(TypeLiterals.mapOf(String.class, new TypeLiteral<Class<?>>() { }),
+ equalTo(new TypeLiteral<Map<String, Class<?>>>() { }));
+ assertThat(TypeLiterals.mapOf(new TypeLiteral<Class<?>>() { }, String.class),
+ equalTo(new TypeLiteral<Map<Class<?>, String>>() { }));
+ assertThat(TypeLiterals.mapOf(new TypeLiteral<Class<?>>() { }, new TypeLiteral<Class<?>>() { }),
+ equalTo(new TypeLiteral<Map<Class<?>, Class<?>>>() { }));
+ }
+
+ @Test
+ public void testProviderOf() {
+ assertThat(TypeLiterals.providerOf(String.class),
+ equalTo(new TypeLiteral<Provider<String>>() { }));
+ assertThat(TypeLiterals.providerOf(new TypeLiteral<Class<?>>() { }),
+ equalTo(new TypeLiteral<Provider<Class<?>>>() { }));
+ }
+
+ /**
+ * Needed to avoid compilation error to to inferred type being anonymous class.
+ */
+ private static <T> Matcher<TypeLiteral<T>> equalTo(TypeLiteral<T> type) {
+ return Matchers.equalTo(type);
+ }
+}