Move exception matcher to a utils class
[Sone.git] / src / test / java / net / pterodactylus / sone / web / WebTestUtils.java
diff --git a/src/test/java/net/pterodactylus/sone/web/WebTestUtils.java b/src/test/java/net/pterodactylus/sone/web/WebTestUtils.java
new file mode 100644 (file)
index 0000000..d4e7596
--- /dev/null
@@ -0,0 +1,37 @@
+package net.pterodactylus.sone.web;
+
+import javax.annotation.Nonnull;
+
+import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+
+/**
+ * Utilities for testing the <code>web</code> package.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class WebTestUtils {
+
+       @Nonnull
+       public static Matcher<RedirectException> redirectsTo(@Nonnull final String page) {
+               return new TypeSafeDiagnosingMatcher<RedirectException>() {
+                       @Override
+                       protected boolean matchesSafely(RedirectException exception, Description mismatchDescription) {
+                               if (!exception.getTarget().equals(page)) {
+                                       mismatchDescription.appendText("target is ").appendValue(exception.getTarget());
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("target is ").appendValue(page);
+                       }
+               };
+       }
+
+}