summaryrefslogtreecommitdiffstats
path: root/sangria-test/src/main
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2014-10-04 11:46:20 -0400
committerTavian Barnes <tavianator@tavianator.com>2014-10-04 12:00:41 -0400
commit5a08861fcb8c5bc5831a6b99c192aba0a7933952 (patch)
treecd3522f8159c6ffa9fe164cf46cb6eb153a7209a /sangria-test/src/main
parentba03ed231044de12662be3a616dfb9a52297c514 (diff)
downloadsangria-5a08861fcb8c5bc5831a6b99c192aba0a7933952.tar.xz
test: New sangria-test project.
Diffstat (limited to 'sangria-test/src/main')
-rw-r--r--sangria-test/src/main/java/com/tavianator/sangria/test/AtomicMatcher.java38
-rw-r--r--sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java42
-rw-r--r--sangria-test/src/main/java/com/tavianator/sangria/test/SangriaMatchers.java31
-rw-r--r--sangria-test/src/main/java/com/tavianator/sangria/test/package-info.java25
4 files changed, 136 insertions, 0 deletions
diff --git a/sangria-test/src/main/java/com/tavianator/sangria/test/AtomicMatcher.java b/sangria-test/src/main/java/com/tavianator/sangria/test/AtomicMatcher.java
new file mode 100644
index 0000000..03c897e
--- /dev/null
+++ b/sangria-test/src/main/java/com/tavianator/sangria/test/AtomicMatcher.java
@@ -0,0 +1,38 @@
+package com.tavianator.sangria.test;
+
+import com.google.inject.CreationException;
+import com.google.inject.Guice;
+import com.google.inject.Module;
+import com.google.inject.spi.Elements;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+
+/**
+ * Matcher that checks whether a {@link Module} can be installed multiple times.
+ *
+ * @author Tavian Barnes (tavianator@tavianator.com)
+ * @version 1.2
+ * @since 1.2
+ */
+final class AtomicMatcher extends TypeSafeDiagnosingMatcher<Module> {
+ @Override
+ protected boolean matchesSafely(Module item, Description mismatchDescription) {
+ // Pass through the SPI to make sure the Module is atomic regardless of its equals() implementation
+ // This ensures atomicity even through Modules.override(), for example
+ Module copy1 = Elements.getModule(Elements.getElements(item));
+ Module copy2 = Elements.getModule(Elements.getElements(item));
+
+ try {
+ Guice.createInjector(copy1, copy2);
+ return true;
+ } catch (CreationException e) {
+ mismatchDescription.appendValue(e);
+ return false;
+ }
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("an atomic Module");
+ }
+}
diff --git a/sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java b/sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java
new file mode 100644
index 0000000..36eea97
--- /dev/null
+++ b/sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java
@@ -0,0 +1,42 @@
+package com.tavianator.sangria.test;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.CreationException;
+import com.google.inject.Guice;
+import com.google.inject.Module;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+
+/**
+ * Matcher that checks whether a {@link Module} follows Guice best practices.
+ *
+ * @author Tavian Barnes (tavianator@tavianator.com)
+ * @version 1.2
+ * @since 1.2
+ */
+final class BestPracticesMatcher extends TypeSafeDiagnosingMatcher<Module> {
+ private static final class EnforcerModule extends AbstractModule {
+ @Override
+ protected void configure() {
+ binder().requireAtInjectOnConstructors();
+ binder().requireExactBindingAnnotations();
+ binder().requireExplicitBindings();
+ }
+ }
+
+ @Override
+ protected boolean matchesSafely(Module item, Description mismatchDescription) {
+ try {
+ Guice.createInjector(item, new EnforcerModule());
+ return true;
+ } catch (CreationException e) {
+ mismatchDescription.appendValue(e);
+ return false;
+ }
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("a Module following Guice best practices");
+ }
+}
diff --git a/sangria-test/src/main/java/com/tavianator/sangria/test/SangriaMatchers.java b/sangria-test/src/main/java/com/tavianator/sangria/test/SangriaMatchers.java
new file mode 100644
index 0000000..da8dd74
--- /dev/null
+++ b/sangria-test/src/main/java/com/tavianator/sangria/test/SangriaMatchers.java
@@ -0,0 +1,31 @@
+package com.tavianator.sangria.test;
+
+import com.google.inject.Module;
+import org.hamcrest.Matcher;
+
+/**
+ * Guice-related Hamcrest matchers.
+ *
+ * @author Tavian Barnes (tavianator@tavianator.com)
+ * @version 1.2
+ * @since 1.2
+ */
+public final class SangriaMatchers {
+ private SangriaMatchers() {
+ // Not for instantiating
+ }
+
+ /**
+ * @return A {@link Matcher} that checks whether a {@link Module}'s bindings can be de-duplicated successfully.
+ */
+ public static Matcher<Module> atomic() {
+ return new AtomicMatcher();
+ }
+
+ /**
+ * @return A {@link Matcher} that checks whether a {@link Module} follows Guice best practices.
+ */
+ public static Matcher<Module> followsBestPractices() {
+ return new BestPracticesMatcher();
+ }
+}
diff --git a/sangria-test/src/main/java/com/tavianator/sangria/test/package-info.java b/sangria-test/src/main/java/com/tavianator/sangria/test/package-info.java
new file mode 100644
index 0000000..0375640
--- /dev/null
+++ b/sangria-test/src/main/java/com/tavianator/sangria/test/package-info.java
@@ -0,0 +1,25 @@
+/****************************************************************************
+ * 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. *
+ ****************************************************************************/
+
+/**
+ * {@code sangria-test}: Utilities for testing with Guice.
+ *
+ * @author Tavian Barnes (tavianator@tavianator.com)
+ * @version 1.2
+ * @since 1.2
+ */
+package com.tavianator.sangria.test;