From 6bc45fd509a55b0b003a4e004335ac2d3e91377d Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 1 Apr 2014 18:14:22 -0400 Subject: Add sangria-core module. --- .../tavianator/sangria/core/DelayedErrorTest.java | 80 ++++++++++++++++++++++ .../tavianator/sangria/core/PrettyTypesTest.java | 66 ++++++++++++++++++ .../sangria/core/UniqueAnnotationsTest.java | 50 ++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java create mode 100644 sangria-core/src/test/java/com/tavianator/sangria/core/PrettyTypesTest.java create mode 100644 sangria-core/src/test/java/com/tavianator/sangria/core/UniqueAnnotationsTest.java (limited to 'sangria-core/src/test') 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 new file mode 100644 index 0000000..500b314 --- /dev/null +++ b/sangria-core/src/test/java/com/tavianator/sangria/core/DelayedErrorTest.java @@ -0,0 +1,80 @@ +/********************************************************************* + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * This library is free software. It comes without any warranty, to * + * the extent permitted by applicable law. You can redistribute it * + * and/or modify it under the terms of the Do What The Fuck You Want * + * To Public License, Version 2, as published by Sam Hocevar. See * + * the COPYING file or http://www.wtfpl.net/ for more details. * + *********************************************************************/ + +package com.tavianator.sangria.core; + +import com.google.inject.AbstractModule; +import com.google.inject.CreationException; +import com.google.inject.Guice; +import com.google.inject.Key; +import com.google.inject.spi.Message; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.hamcrest.Matchers.*; + +/** + * Tests for {@link DelayedError}. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.0 + * @since 1.0 + */ +public class DelayedErrorTest { + public @Rule ExpectedException thrown = ExpectedException.none(); + + @Test + public void testFormatString() { + thrown.expect(CreationException.class); + thrown.expectMessage("Test java.lang.String"); + + // We want the messages from our CreationException to get absorbed into the top-level exception + thrown.expectMessage(not(containsString("CreationException"))); + + Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + DelayedError.create(binder(), "Test %s", new Key() { }); + } + }); + } + + @Test + public void testMessage() { + thrown.expect(CreationException.class); + thrown.expectMessage("the message"); + thrown.expectMessage("at the source"); + thrown.expectMessage(not(containsString("CreationException"))); + + Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + DelayedError.create(binder(), new Message("the source", "the message")); + } + }); + } + + @Test + public void testThrowable() { + final Throwable cause = new IllegalStateException(); + + thrown.expect(CreationException.class); + thrown.expectCause(is(cause)); + + Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + DelayedError.create(binder(), cause); + } + }); + } +} diff --git a/sangria-core/src/test/java/com/tavianator/sangria/core/PrettyTypesTest.java b/sangria-core/src/test/java/com/tavianator/sangria/core/PrettyTypesTest.java new file mode 100644 index 0000000..c50163b --- /dev/null +++ b/sangria-core/src/test/java/com/tavianator/sangria/core/PrettyTypesTest.java @@ -0,0 +1,66 @@ +/********************************************************************* + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * This library is free software. It comes without any warranty, to * + * the extent permitted by applicable law. You can redistribute it * + * and/or modify it under the terms of the Do What The Fuck You Want * + * To Public License, Version 2, as published by Sam Hocevar. See * + * the COPYING file or http://www.wtfpl.net/ for more details. * + *********************************************************************/ + +package com.tavianator.sangria.core; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.*; +import javax.inject.Qualifier; + +import com.google.inject.Key; +import com.google.inject.TypeLiteral; +import com.google.inject.name.Names; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +/** + * Tests for {@link PrettyTypes}. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.0 + * @since 1.0 + */ +public class PrettyTypesTest { + @Retention(RetentionPolicy.RUNTIME) + @Qualifier + private @interface Simple { + } + + @Test + public void testClasses() { + assertThat(PrettyTypes.format("Class is %s", PrettyTypesTest.class), + equalTo("Class is com.tavianator.sangria.core.PrettyTypesTest")); + + assertThat(PrettyTypes.format("Class is %s", Simple.class), + equalTo("Class is com.tavianator.sangria.core.PrettyTypesTest.Simple")); + } + + @Test + public void testTypeLiterals() { + assertThat(PrettyTypes.format("TypeLiteral is %s", new TypeLiteral>() { }), + equalTo("TypeLiteral is java.util.List")); + } + + @Test + public void testKeys() { + assertThat(PrettyTypes.format("Key is %s", new Key>() { }), + equalTo("Key is java.util.List")); + + assertThat(PrettyTypes.format("Key is %s", new Key>(Names.named("test")) { }), + equalTo("Key is java.util.List annotated with @com.google.inject.name.Named(value=test)")); + + assertThat(PrettyTypes.format("Key is %s", new Key>(Simple.class) { }), + equalTo("Key is java.util.List annotated with @com.tavianator.sangria.core.PrettyTypesTest.Simple")); + } +} diff --git a/sangria-core/src/test/java/com/tavianator/sangria/core/UniqueAnnotationsTest.java b/sangria-core/src/test/java/com/tavianator/sangria/core/UniqueAnnotationsTest.java new file mode 100644 index 0000000..f08d834 --- /dev/null +++ b/sangria-core/src/test/java/com/tavianator/sangria/core/UniqueAnnotationsTest.java @@ -0,0 +1,50 @@ +/********************************************************************* + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * This library is free software. It comes without any warranty, to * + * the extent permitted by applicable law. You can redistribute it * + * and/or modify it under the terms of the Do What The Fuck You Want * + * To Public License, Version 2, as published by Sam Hocevar. See * + * the COPYING file or http://www.wtfpl.net/ for more details. * + *********************************************************************/ + +package com.tavianator.sangria.core; + +import java.lang.annotation.Annotation; + +import org.junit.Test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +/** + * Tests for {@link UniqueAnnotations}. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.0 + * @since 1.0 + */ +@UniqueAnnotations.UniqueAnnotation(100) +public class UniqueAnnotationsTest { + @Test + public void testUniqueness() { + Annotation a1 = UniqueAnnotations.create(); + Annotation a2 = UniqueAnnotations.create(); + + assertThat(a1, equalTo(a1)); + assertThat(a2, equalTo(a2)); + + assertThat(a1, not(equalTo(a2))); + assertThat(a2, not(equalTo(a1))); + } + + @Test + public void testEquality() { + Annotation real = getClass().getAnnotation(UniqueAnnotations.UniqueAnnotation.class); + Annotation fake = UniqueAnnotations.create(100); + + assertThat(real, equalTo(fake)); + assertThat(fake, equalTo(real)); + } +} -- cgit v1.2.3