summaryrefslogtreecommitdiffstats
path: root/sangria-test/src/main
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2014-10-04 12:52:32 -0400
committerTavian Barnes <tavianator@tavianator.com>2014-10-04 12:52:32 -0400
commit3fd5777a58f59218e3229c9ea11d816ecc6ae367 (patch)
tree53f937505806e010d2bc3134fa5507d0ff78d32c /sangria-test/src/main
parentb0bb30831a334ff30e3e6a13790bbc324db89de5 (diff)
parent8e24a47b69f940c31ac95451b4584b95de7c8670 (diff)
downloadsangria-3fd5777a58f59218e3229c9ea11d816ecc6ae367.tar.xz
Merge branch 'test'
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;