org.apache.velocity
velocity
diff --git a/src/main/java/org/takes/facets/hamcrest/EntryMatcher.java b/src/main/java/org/takes/facets/hamcrest/EntryMatcher.java
new file mode 100644
index 000000000..cbc3233f1
--- /dev/null
+++ b/src/main/java/org/takes/facets/hamcrest/EntryMatcher.java
@@ -0,0 +1,78 @@
+/**
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Yegor Bugayenko
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package org.takes.facets.hamcrest;
+
+import java.util.Map;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Matchers;
+import org.hamcrest.TypeSafeMatcher;
+
+/**
+ * Matcher for {@link Map.Entry}.
+ *
+ * The class is immutable and thread-safe.
+ *
+ * @author Dragan Bozanovic (bozanovicdr@gmail.com)
+ * @version $Id$
+ * @since 0.27
+ */
+public final class EntryMatcher extends TypeSafeMatcher> {
+
+ /**
+ * Key matcher.
+ */
+ private final transient Matcher keym;
+
+ /**
+ * Value matcher.
+ */
+ private final transient Matcher valuem;
+
+ /**
+ * Ctor.
+ * @param key Key
+ * @param value Value
+ */
+ public EntryMatcher(final K key, final V value) {
+ super();
+ this.keym = Matchers.equalTo(key);
+ this.valuem = Matchers.equalTo(value);
+ }
+
+ @Override
+ public boolean matchesSafely(final Map.Entry entry) {
+ return this.keym.matches(entry.getKey())
+ && this.valuem.matches(entry.getValue());
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("entry containing [")
+ .appendDescriptionOf(this.keym)
+ .appendText("->")
+ .appendDescriptionOf(this.valuem)
+ .appendText("]");
+ }
+}
diff --git a/src/main/java/org/takes/facets/hamcrest/HmRsHeader.java b/src/main/java/org/takes/facets/hamcrest/HmRsHeader.java
index 0b45ea7d4..02dea94b2 100644
--- a/src/main/java/org/takes/facets/hamcrest/HmRsHeader.java
+++ b/src/main/java/org/takes/facets/hamcrest/HmRsHeader.java
@@ -24,14 +24,17 @@
package org.takes.facets.hamcrest;
import java.io.IOException;
-import java.util.Collections;
+import java.util.Collection;
import java.util.Iterator;
+import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
+import java.util.Map.Entry;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.takes.Response;
+import org.takes.misc.EntryImpl;
/**
* Response Header Matcher.
@@ -43,78 +46,180 @@
* @author Eugene Kondrashev (eugene.kondrashev@gmail.com)
* @version $Id$
* @since 0.23.3
- * @todo #260:30min Implement additional constructors.
- * According to #260 there should be also available such constructors
- * public HmRsHeader(final Matcher extends Map.Entry> mtchr);
- * public HmRsHeader(final String header,
- * final Matcher extends Iterable> mtchr);
- * public HmRsHeader(final String header,
- * final Matcher extends String> mtchr);
- * public HmRsHeader(final String header, final String value);
*/
public final class HmRsHeader extends TypeSafeMatcher {
/**
- * Expected request header matcher.
+ * Expected response header matcher.
*/
- private final transient Matcher extends Map extends CharSequence,
- ? extends CharSequence>> matcher;
+ private final transient HeaderMatcher matcher;
/**
- * Expected matcher.
- * @param mtchr Is expected header matcher.
+ * Ctor.
+ * @param mtchr Matcher
*/
- public HmRsHeader(final Matcher extends Map extends CharSequence,
- ? extends CharSequence>> mtchr) {
+ public HmRsHeader(
+ final Matcher extends Map.Entry> mtchr) {
super();
- this.matcher = mtchr;
+ this.matcher = new EntryHeaderMatcher(mtchr);
}
/**
- * Fail description.
- * @param description Fail result description.
+ * Ctor.
+ * @param header Header name
+ * @param mtchr Matcher
*/
+ public HmRsHeader(final String header,
+ final Matcher extends Iterable> mtchr) {
+ super();
+ this.matcher = new IterableHeaderMatcher(header, mtchr);
+ }
+
+ /**
+ * Ctor.
+ * @param header Header name
+ * @param value Header value
+ */
+ public HmRsHeader(final String header, final String value) {
+ this(new EntryMatcher(header, value));
+ }
+
@Override
public void describeTo(final Description description) {
this.matcher.describeTo(description);
}
- /**
- * Type safe matcher.
- * @param item Is tested element
- * @return True when expected type matched.
- */
@Override
public boolean matchesSafely(final Response item) {
try {
final Iterator headers = item.head().iterator();
- headers.next();
+ if (headers.hasNext()) {
+ headers.next();
+ }
+ return this.matcher.matches(headers);
+ } catch (final IOException ex) {
+ throw new IllegalStateException(ex);
+ }
+ }
+
+ /**
+ * Splits the given header to [name, value] array.
+ * @param header Header
+ * @return Array in which the first element is header name,
+ * the second is header value
+ */
+ private static String[] split(final String header) {
+ return header.split(":", 2);
+ }
+
+ /**
+ * Header matcher.
+ */
+ private interface HeaderMatcher {
+
+ /**
+ * Performs the matching.
+ *
+ * @param headers Headers to check
+ * @return True if positive match
+ */
+ boolean matches(final Iterator headers);
+
+ /**
+ * Generates a description of the matcher.
+ *
+ * @param description The description to be built or appended to
+ */
+ void describeTo(final Description description);
+ }
+
+ /**
+ * Header matcher for {@code Matcher extends Map.Entry>}.
+ */
+ private static class EntryHeaderMatcher implements HeaderMatcher {
+
+ /**
+ * Matcher.
+ */
+ private final transient
+ Matcher extends Map.Entry> matcher;
+
+ /**
+ * Ctor.
+ * @param mtchr Matcher
+ */
+ public EntryHeaderMatcher(
+ final Matcher extends Entry> mtchr) {
+ this.matcher = mtchr;
+ }
+
+ @Override
+ @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
+ public boolean matches(final Iterator headers) {
boolean result = false;
while (headers.hasNext()) {
- if (this.matchHeader(headers.next())) {
+ final String[] parts = HmRsHeader.split(headers.next());
+ final Map.Entry entry =
+ new EntryImpl(
+ parts[0].trim().toLowerCase(Locale.ENGLISH),
+ parts[1].trim()
+ );
+ if (this.matcher.matches(entry)) {
result = true;
- break;
}
}
return result;
- } catch (final IOException ex) {
- throw new IllegalStateException(ex);
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ this.matcher.describeTo(description);
}
}
/**
- * Runs matcher against each header.
- * @param header Is header name and value
- * @return True when expected type matched.
+ * Header matcher for {@code Matcher extends Iterable>}.
*/
- private boolean matchHeader(final String header) {
- final String[] parts = header.split(":", 2);
- return this.matcher.matches(
- Collections.singletonMap(
- parts[0].trim().toLowerCase(Locale.ENGLISH),
- parts[1].trim()
- )
- );
- }
+ private static class IterableHeaderMatcher implements HeaderMatcher {
+ /**
+ * Header.
+ */
+ private final transient String header;
+
+ /**
+ * Matcher.
+ */
+ private final transient Matcher extends Iterable> matcher;
+
+ /**
+ * Ctor.
+ * @param hdr Header
+ * @param mtchr Matcher
+ */
+ public IterableHeaderMatcher(final String hdr,
+ final Matcher extends Iterable> mtchr) {
+ this.header = hdr;
+ this.matcher = mtchr;
+ }
+
+ @Override
+ public boolean matches(final Iterator headers) {
+ final Collection hdrs = new LinkedList();
+ while (headers.hasNext()) {
+ final String[] parts = HmRsHeader.split(headers.next());
+ final String lower = parts[0].trim()
+ .toLowerCase(Locale.ENGLISH);
+ if (lower.equals(this.header)) {
+ hdrs.add(parts[1].trim());
+ }
+ }
+ return this.matcher.matches(hdrs);
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ this.matcher.describeTo(description);
+ }
+ }
}
diff --git a/src/main/java/org/takes/misc/EntryImpl.java b/src/main/java/org/takes/misc/EntryImpl.java
new file mode 100644
index 000000000..53ed977b1
--- /dev/null
+++ b/src/main/java/org/takes/misc/EntryImpl.java
@@ -0,0 +1,73 @@
+/**
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Yegor Bugayenko
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package org.takes.misc;
+
+import java.util.Map;
+
+/**
+ * An immutable and thread-safe implementation of
+ * {@link Map.Entry} interface.
+ *
+ * @author Dragan Bozanovic (bozanovicdr@gmail.com)
+ * @version $Id$
+ * @since 0.27
+ */
+public final class EntryImpl implements Map.Entry {
+
+ /**
+ * Key.
+ */
+ private final transient K key;
+
+ /**
+ * Value.
+ */
+ private final transient V value;
+
+ /**
+ * Ctor.
+ * @param keyy Key
+ * @param val Value
+ */
+ public EntryImpl(final K keyy, final V val) {
+ super();
+ this.key = keyy;
+ this.value = val;
+ }
+
+ @Override
+ public K getKey() {
+ return this.key;
+ }
+
+ @Override
+ public V getValue() {
+ return this.value;
+ }
+
+ @Override
+ public V setValue(final V val) {
+ throw new UnsupportedOperationException("This object is immutable.");
+ }
+}
diff --git a/src/test/java/org/takes/facets/hamcrest/HmRsHeaderTest.java b/src/test/java/org/takes/facets/hamcrest/HmRsHeaderTest.java
index 538439833..074524bd4 100644
--- a/src/test/java/org/takes/facets/hamcrest/HmRsHeaderTest.java
+++ b/src/test/java/org/takes/facets/hamcrest/HmRsHeaderTest.java
@@ -26,8 +26,10 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
+import org.takes.rs.RsEmpty;
import org.takes.rs.RsWithBody;
import org.takes.rs.RsWithHeader;
+import org.takes.rs.RsWithHeaders;
/**
* Test case for {@link HmRsHeader}.
@@ -48,7 +50,11 @@ public void testsHeaderAvailable() throws Exception {
new RsWithBody("Hello"),
"content-encoding: gzip"
),
- new HmRsHeader(Matchers.hasEntry("content-encoding", "gzip"))
+ new HmRsHeader(
+ new EntryMatcher(
+ "content-encoding", "gzip"
+ )
+ )
);
}
@@ -62,7 +68,7 @@ public void testsHeaderNotAvailable() throws Exception {
new RsWithBody(""),
new HmRsHeader(
Matchers.not(
- Matchers.hasEntry(
+ new EntryMatcher(
"cache-control",
"no-cache, no-store"
)
@@ -70,4 +76,55 @@ public void testsHeaderNotAvailable() throws Exception {
)
);
}
+
+ /**
+ * HmRsHeader can test header name and value available.
+ * @throws Exception If some problem inside
+ */
+ @Test
+ public void testsHeaderNameAndValueAvailable() throws Exception {
+ MatcherAssert.assertThat(
+ new RsWithHeader("header1: value1"),
+ new HmRsHeader("header1", "value1")
+ );
+ }
+
+ /**
+ * HmRsHeader can test header name and value not available.
+ * @throws Exception If some problem inside
+ */
+ @Test
+ public void testsHeaderNameAndValueNotAvailable() throws Exception {
+ MatcherAssert.assertThat(
+ new RsWithHeader("header2: value2"),
+ Matchers.not(new HmRsHeader("header2", "value21"))
+ );
+ }
+
+ /**
+ * HmRsHeader can test headers available.
+ * @throws Exception If some problem inside
+ */
+ @Test
+ public void testsHeadersAvailable() throws Exception {
+ MatcherAssert.assertThat(
+ new RsWithHeaders(
+ new RsEmpty(),
+ "header3: value31", "header3: value32"
+ ),
+ new HmRsHeader("header3", Matchers.iterableWithSize(2))
+ );
+ }
+
+ /**
+ * HmRsHeader can test headers not available.
+ * @throws Exception If some problem inside
+ */
+ @Test
+ public void testsHeadersNotAvailable() throws Exception {
+ MatcherAssert.assertThat(
+ new RsWithHeaders(new RsEmpty(), "header4: value4"),
+ new HmRsHeader("header41", Matchers.iterableWithSize(0))
+ );
+ }
}