aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett Chabot <brettchabot@android.com>2012-02-27 14:45:30 -0800
committerBrett Chabot <brettchabot@android.com>2012-02-27 14:45:30 -0800
commit1ecfda91236a8970119144e59e0ba6113dc22c0f (patch)
treee9353ea7a540a5e817843075a06f1e0d5a21923a
parent56d3323ed028becea3e7649e7af69318d540d2f0 (diff)
downloadhamcrest-1ecfda91236a8970119144e59e0ba6113dc22c0f.tar.gz
Import hamcrest 1.1
Change-Id: Iae2781d10cefd13354ac29652cd56514ea60395b
-rw-r--r--Android.mk26
-rw-r--r--LICENSE.txt27
-rw-r--r--MODULE_LICENSE_BSD0
-rw-r--r--NOTICE27
-rw-r--r--README.android1
-rw-r--r--src/org/hamcrest/BaseDescription.java127
-rw-r--r--src/org/hamcrest/BaseMatcher.java23
-rw-r--r--src/org/hamcrest/CoreMatchers.java165
-rw-r--r--src/org/hamcrest/Description.java44
-rw-r--r--src/org/hamcrest/Factory.java17
-rw-r--r--src/org/hamcrest/Matcher.java47
-rw-r--r--src/org/hamcrest/SelfDescribing.java16
-rw-r--r--src/org/hamcrest/StringDescription.java60
-rw-r--r--src/org/hamcrest/core/AllOf.java51
-rw-r--r--src/org/hamcrest/core/AnyOf.java51
-rw-r--r--src/org/hamcrest/core/DescribedAs.java55
-rw-r--r--src/org/hamcrest/core/Is.java68
-rw-r--r--src/org/hamcrest/core/IsAnything.java59
-rw-r--r--src/org/hamcrest/core/IsEqual.java71
-rw-r--r--src/org/hamcrest/core/IsInstanceOf.java44
-rw-r--r--src/org/hamcrest/core/IsNot.java49
-rw-r--r--src/org/hamcrest/core/IsNull.java55
-rw-r--r--src/org/hamcrest/core/IsSame.java40
-rw-r--r--src/org/hamcrest/core/package.html7
-rw-r--r--src/org/hamcrest/internal/ArrayIterator.java28
-rw-r--r--src/org/hamcrest/internal/SelfDescribingValue.java16
-rw-r--r--src/org/hamcrest/internal/SelfDescribingValueIterator.java25
-rw-r--r--src/org/hamcrest/package.html9
28 files changed, 1208 insertions, 0 deletions
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..7fa1f63
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,26 @@
+# Copyright (C) 2012 The Android Open Source Project
+#
+# 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.
+#
+#
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_MODULE := hamcrest-host
+
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_HOST_JAVA_LIBRARY)
diff --git a/LICENSE.txt b/LICENSE.txt
new file mode 100644
index 0000000..5ebe92c
--- /dev/null
+++ b/LICENSE.txt
@@ -0,0 +1,27 @@
+BSD License
+
+Copyright (c) 2000-2006, www.hamcrest.org
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this list of
+conditions and the following disclaimer. Redistributions in binary form must reproduce
+the above copyright notice, this list of conditions and the following disclaimer in
+the documentation and/or other materials provided with the distribution.
+
+Neither the name of Hamcrest nor the names of its contributors may be used to endorse
+or promote products derived from this software without specific prior written
+permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGE.
diff --git a/MODULE_LICENSE_BSD b/MODULE_LICENSE_BSD
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/MODULE_LICENSE_BSD
diff --git a/NOTICE b/NOTICE
new file mode 100644
index 0000000..5ebe92c
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,27 @@
+BSD License
+
+Copyright (c) 2000-2006, www.hamcrest.org
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this list of
+conditions and the following disclaimer. Redistributions in binary form must reproduce
+the above copyright notice, this list of conditions and the following disclaimer in
+the documentation and/or other materials provided with the distribution.
+
+Neither the name of Hamcrest nor the names of its contributors may be used to endorse
+or promote products derived from this software without specific prior written
+permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGE.
diff --git a/README.android b/README.android
new file mode 100644
index 0000000..a1bbfd2
--- /dev/null
+++ b/README.android
@@ -0,0 +1 @@
+unmodified source obtained from http://code.google.com/p/hamcrest/downloads/detail?name=hamcrest-core-1.1.jar&can=2&q=,
diff --git a/src/org/hamcrest/BaseDescription.java b/src/org/hamcrest/BaseDescription.java
new file mode 100644
index 0000000..4c98e5f
--- /dev/null
+++ b/src/org/hamcrest/BaseDescription.java
@@ -0,0 +1,127 @@
+package org.hamcrest;
+
+import static java.lang.String.valueOf;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import org.hamcrest.internal.ArrayIterator;
+import org.hamcrest.internal.SelfDescribingValueIterator;
+
+/**
+ * A {@link Description} that is stored as a string.
+ */
+public abstract class BaseDescription implements Description {
+ public Description appendText(String text) {
+ append(text);
+ return this;
+ }
+
+ public Description appendDescriptionOf(SelfDescribing value) {
+ value.describeTo(this);
+ return this;
+ }
+
+ public Description appendValue(Object value) {
+ if (value == null) {
+ append("null");
+ } else if (value instanceof String) {
+ toJavaSyntax((String) value);
+ } else if (value instanceof Character) {
+ append('"');
+ toJavaSyntax((Character) value);
+ append('"');
+ } else if (value instanceof Short) {
+ append('<');
+ append(valueOf(value));
+ append("s>");
+ } else if (value instanceof Long) {
+ append('<');
+ append(valueOf(value));
+ append("L>");
+ } else if (value instanceof Float) {
+ append('<');
+ append(valueOf(value));
+ append("F>");
+ } else if (value.getClass().isArray()) {
+ appendValueList("[",", ","]", new ArrayIterator(value));
+ } else {
+ append('<');
+ append(valueOf(value));
+ append('>');
+ }
+ return this;
+ }
+
+ public <T> Description appendValueList(String start, String separator, String end, T... values) {
+ return appendValueList(start, separator, end, Arrays.asList(values));
+ }
+
+ public <T> Description appendValueList(String start, String separator, String end, Iterable<T> values) {
+ return appendValueList(start, separator, end, values.iterator());
+ }
+
+ private <T> Description appendValueList(String start, String separator, String end, Iterator<T> values) {
+ return appendList(start, separator, end, new SelfDescribingValueIterator<T>(values));
+ }
+
+ public Description appendList(String start, String separator, String end, Iterable<? extends SelfDescribing> values) {
+ return appendList(start, separator, end, values.iterator());
+ }
+
+ private Description appendList(String start, String separator, String end, Iterator<? extends SelfDescribing> i) {
+ boolean separate = false;
+
+ append(start);
+ while (i.hasNext()) {
+ if (separate) append(separator);
+ appendDescriptionOf(i.next());
+ separate = true;
+ }
+ append(end);
+
+ return this;
+ }
+
+
+ /** Append the String <var>str</var> to the description.
+ * The default implementation passes every character to {@link #append(char)}.
+ * Override in subclasses to provide an efficient implementation.
+ */
+ protected void append(String str) {
+ for (int i = 0; i < str.length(); i++) {
+ append(str.charAt(i));
+ }
+ }
+
+ /** Append the char <var>c</var> to the description.
+ */
+ protected abstract void append(char c);
+
+ private void toJavaSyntax(String unformatted) {
+ append('"');
+ for (int i = 0; i < unformatted.length(); i++) {
+ toJavaSyntax(unformatted.charAt(i));
+ }
+ append('"');
+ }
+
+ private void toJavaSyntax(char ch) {
+ switch (ch) {
+ case '"':
+ append("\\\"");
+ break;
+ case '\n':
+ append("\\n");
+ break;
+ case '\r':
+ append("\\r");
+ break;
+ case '\t':
+ append("\\t");
+ break;
+ default:
+ append(ch);
+ }
+ }
+}
diff --git a/src/org/hamcrest/BaseMatcher.java b/src/org/hamcrest/BaseMatcher.java
new file mode 100644
index 0000000..3fdd6f7
--- /dev/null
+++ b/src/org/hamcrest/BaseMatcher.java
@@ -0,0 +1,23 @@
+/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest;
+
+/**
+ * BaseClass for all Matcher implementations.
+ *
+ * @see Matcher
+ */
+public abstract class BaseMatcher<T> implements Matcher<T> {
+
+ /**
+ * @see Matcher#_dont_implement_Matcher___instead_extend_BaseMatcher_()
+ */
+ public final void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
+ // See Matcher interface for an explanation of this method.
+ }
+
+ @Override
+ public String toString() {
+ return StringDescription.toString(this);
+ }
+}
diff --git a/src/org/hamcrest/CoreMatchers.java b/src/org/hamcrest/CoreMatchers.java
new file mode 100644
index 0000000..fac3acf
--- /dev/null
+++ b/src/org/hamcrest/CoreMatchers.java
@@ -0,0 +1,165 @@
+// Generated source.
+package org.hamcrest;
+
+public class CoreMatchers {
+
+ /**
+ * Decorates another Matcher, retaining the behavior but allowing tests
+ * to be slightly more expressive.
+ *
+ * eg. assertThat(cheese, equalTo(smelly))
+ * vs assertThat(cheese, is(equalTo(smelly)))
+ */
+ public static <T> org.hamcrest.Matcher<T> is(org.hamcrest.Matcher<T> matcher) {
+ return org.hamcrest.core.Is.is(matcher);
+ }
+
+ /**
+ * This is a shortcut to the frequently used is(equalTo(x)).
+ *
+ * eg. assertThat(cheese, is(equalTo(smelly)))
+ * vs assertThat(cheese, is(smelly))
+ */
+ public static <T> org.hamcrest.Matcher<T> is(T value) {
+ return org.hamcrest.core.Is.is(value);
+ }
+
+ /**
+ * This is a shortcut to the frequently used is(instanceOf(SomeClass.class)).
+ *
+ * eg. assertThat(cheese, is(instanceOf(Cheddar.class)))
+ * vs assertThat(cheese, is(Cheddar.class))
+ */
+ public static org.hamcrest.Matcher<java.lang.Object> is(java.lang.Class<?> type) {
+ return org.hamcrest.core.Is.is(type);
+ }
+
+ /**
+ * Inverts the rule.
+ */
+ public static <T> org.hamcrest.Matcher<T> not(org.hamcrest.Matcher<T> matcher) {
+ return org.hamcrest.core.IsNot.not(matcher);
+ }
+
+ /**
+ * This is a shortcut to the frequently used not(equalTo(x)).
+ *
+ * eg. assertThat(cheese, is(not(equalTo(smelly))))
+ * vs assertThat(cheese, is(not(smelly)))
+ */
+ public static <T> org.hamcrest.Matcher<T> not(T value) {
+ return org.hamcrest.core.IsNot.not(value);
+ }
+
+ /**
+ * Is the value equal to another value, as tested by the
+ * {@link java.lang.Object#equals} invokedMethod?
+ */
+ public static <T> org.hamcrest.Matcher<T> equalTo(T operand) {
+ return org.hamcrest.core.IsEqual.equalTo(operand);
+ }
+
+ /**
+ * Is the value an instance of a particular type?
+ */
+ public static org.hamcrest.Matcher<java.lang.Object> instanceOf(java.lang.Class<?> type) {
+ return org.hamcrest.core.IsInstanceOf.instanceOf(type);
+ }
+
+ /**
+ * Evaluates to true only if ALL of the passed in matchers evaluate to true.
+ */
+ public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? extends T>... matchers) {
+ return org.hamcrest.core.AllOf.allOf(matchers);
+ }
+
+ /**
+ * Evaluates to true only if ALL of the passed in matchers evaluate to true.
+ */
+ public static <T> org.hamcrest.Matcher<T> allOf(java.lang.Iterable<org.hamcrest.Matcher<? extends T>> matchers) {
+ return org.hamcrest.core.AllOf.allOf(matchers);
+ }
+
+ /**
+ * Evaluates to true if ANY of the passed in matchers evaluate to true.
+ */
+ public static <T> org.hamcrest.Matcher<T> anyOf(org.hamcrest.Matcher<? extends T>... matchers) {
+ return org.hamcrest.core.AnyOf.anyOf(matchers);
+ }
+
+ /**
+ * Evaluates to true if ANY of the passed in matchers evaluate to true.
+ */
+ public static <T> org.hamcrest.Matcher<T> anyOf(java.lang.Iterable<org.hamcrest.Matcher<? extends T>> matchers) {
+ return org.hamcrest.core.AnyOf.anyOf(matchers);
+ }
+
+ /**
+ * Creates a new instance of IsSame
+ *
+ * @param object The predicate evaluates to true only when the argument is
+ * this object.
+ */
+ public static <T> org.hamcrest.Matcher<T> sameInstance(T object) {
+ return org.hamcrest.core.IsSame.sameInstance(object);
+ }
+
+ /**
+ * This matcher always evaluates to true.
+ */
+ public static <T> org.hamcrest.Matcher<T> anything() {
+ return org.hamcrest.core.IsAnything.anything();
+ }
+
+ /**
+ * This matcher always evaluates to true.
+ *
+ * @param description A meaningful string used when describing itself.
+ */
+ public static <T> org.hamcrest.Matcher<T> anything(java.lang.String description) {
+ return org.hamcrest.core.IsAnything.anything(description);
+ }
+
+ /**
+ * This matcher always evaluates to true. With type inference.
+ */
+ public static <T> org.hamcrest.Matcher<T> any(java.lang.Class<T> type) {
+ return org.hamcrest.core.IsAnything.any(type);
+ }
+
+ /**
+ * Matches if value is null.
+ */
+ public static <T> org.hamcrest.Matcher<T> nullValue() {
+ return org.hamcrest.core.IsNull.nullValue();
+ }
+
+ /**
+ * Matches if value is null. With type inference.
+ */
+ public static <T> org.hamcrest.Matcher<T> nullValue(java.lang.Class<T> type) {
+ return org.hamcrest.core.IsNull.nullValue(type);
+ }
+
+ /**
+ * Matches if value is not null.
+ */
+ public static <T> org.hamcrest.Matcher<T> notNullValue() {
+ return org.hamcrest.core.IsNull.notNullValue();
+ }
+
+ /**
+ * Matches if value is not null. With type inference.
+ */
+ public static <T> org.hamcrest.Matcher<T> notNullValue(java.lang.Class<T> type) {
+ return org.hamcrest.core.IsNull.notNullValue(type);
+ }
+
+ /**
+ * Wraps an existing matcher and overrides the description when it fails.
+ */
+ public static <T> org.hamcrest.Matcher<T> describedAs(java.lang.String description, org.hamcrest.Matcher<T> matcher, java.lang.Object... values) {
+ return org.hamcrest.core.DescribedAs.describedAs(description, matcher, values);
+ }
+
+}
diff --git a/src/org/hamcrest/Description.java b/src/org/hamcrest/Description.java
new file mode 100644
index 0000000..36ddeda
--- /dev/null
+++ b/src/org/hamcrest/Description.java
@@ -0,0 +1,44 @@
+package org.hamcrest;
+
+/**
+ * A description of a Matcher. A Matcher will describe itself to a description
+ * which can later be used for reporting.
+ *
+ * @see Matcher#describeTo(Description)
+ */
+public interface Description {
+
+ /**
+ * Appends some plain text to the description.
+ */
+ Description appendText(String text);
+
+ /**
+ * Appends the description of a {@link SelfDescribing} value to this description.
+ */
+ Description appendDescriptionOf(SelfDescribing value);
+
+ /**
+ * Appends an arbitary value to the description.
+ */
+ Description appendValue(Object value);
+
+ /**
+ * Appends a list of values to the description.
+ */
+ <T> Description appendValueList(String start, String separator, String end,
+ T... values);
+
+ /**
+ * Appends a list of values to the description.
+ */
+ <T> Description appendValueList(String start, String separator, String end,
+ Iterable<T> values);
+
+ /**
+ * Appends a list of {@link org.hamcrest.SelfDescribing} objects
+ * to the description.
+ */
+ Description appendList(String start, String separator, String end,
+ Iterable<? extends SelfDescribing> values);
+}
diff --git a/src/org/hamcrest/Factory.java b/src/org/hamcrest/Factory.java
new file mode 100644
index 0000000..a8bf5f9
--- /dev/null
+++ b/src/org/hamcrest/Factory.java
@@ -0,0 +1,17 @@
+package org.hamcrest;
+
+import static java.lang.annotation.ElementType.METHOD;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+
+/**
+ * Marks a Hamcrest static factory method so tools recognise them.
+ * A factory method is an equivalent to a named constructor.
+ *
+ * @author Joe Walnes
+ */
+@Retention(RUNTIME)
+@Target({METHOD})
+public @interface Factory {
+}
diff --git a/src/org/hamcrest/Matcher.java b/src/org/hamcrest/Matcher.java
new file mode 100644
index 0000000..fd10207
--- /dev/null
+++ b/src/org/hamcrest/Matcher.java
@@ -0,0 +1,47 @@
+/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest;
+
+/**
+ * A matcher over acceptable values.
+ * A matcher is able to describe itself to give feedback when it fails.
+ * <p/>
+ * Matcher implementations should <b>NOT directly implement this interface</b>.
+ * Instead, <b>extend</b> the {@link BaseMatcher} abstract class,
+ * which will ensure that the Matcher API can grow to support
+ * new features and remain compatible with all Matcher implementations.
+ * <p/>
+ * For easy access to common Matcher implementations, use the static factory
+ * methods in {@link CoreMatchers}.
+ *
+ * @see CoreMatchers
+ * @see BaseMatcher
+ */
+@SuppressWarnings({"unused", "UnusedDeclaration"})
+public interface Matcher<T> extends SelfDescribing {
+
+ /**
+ * Evaluates the matcher for argument <var>item</var>.
+ * <p/>
+ * This method matches against Object, instead of the generic type T. This is
+ * because the caller of the Matcher does not know at runtime what the type is
+ * (because of type erasure with Java generics). It is down to the implementations
+ * to check the correct type.
+ *
+ * @param item the object against which the matcher is evaluated.
+ * @return <code>true</code> if <var>item</var> matches, otherwise <code>false</code>.
+ *
+ * @see BaseMatcher
+ */
+ boolean matches(Object item);
+
+ /**
+ * This method simply acts a friendly reminder not to implement Matcher directly and
+ * instead extend BaseMatcher. It's easy to ignore JavaDoc, but a bit harder to ignore
+ * compile errors .
+ *
+ * @see Matcher for reasons why.
+ * @see BaseMatcher
+ */
+ void _dont_implement_Matcher___instead_extend_BaseMatcher_();
+}
diff --git a/src/org/hamcrest/SelfDescribing.java b/src/org/hamcrest/SelfDescribing.java
new file mode 100644
index 0000000..cd53070
--- /dev/null
+++ b/src/org/hamcrest/SelfDescribing.java
@@ -0,0 +1,16 @@
+package org.hamcrest;
+
+/**
+ * The ability of an object to describe itself.
+ */
+public interface SelfDescribing {
+ /**
+ * Generates a description of the object. The description may be part of a
+ * a description of a larger object of which this is just a component, so it
+ * should be worded appropriately.
+ *
+ * @param description
+ * The description to be built or appended to.
+ */
+ void describeTo(Description description);
+} \ No newline at end of file
diff --git a/src/org/hamcrest/StringDescription.java b/src/org/hamcrest/StringDescription.java
new file mode 100644
index 0000000..66709ee
--- /dev/null
+++ b/src/org/hamcrest/StringDescription.java
@@ -0,0 +1,60 @@
+package org.hamcrest;
+
+import java.io.IOException;
+
+/**
+ * A {@link Description} that is stored as a string.
+ */
+public class StringDescription extends BaseDescription {
+ private final Appendable out;
+
+ public StringDescription() {
+ this(new StringBuilder());
+ }
+
+ public StringDescription(Appendable out) {
+ this.out = out;
+ }
+
+ /**
+ * Return the description of a {@link SelfDescribing} object as a String.
+ *
+ * @param selfDescribing
+ * The object to be described.
+ * @return
+ * The description of the object.
+ */
+ public static String toString(SelfDescribing value) {
+ return new StringDescription().appendDescriptionOf(value).toString();
+ }
+
+ /**
+ * Alias for {@link #toString(SelfDescribing)}.
+ */
+ public static String asString(SelfDescribing selfDescribing) {
+ return toString(selfDescribing);
+ }
+
+ protected void append(String str) {
+ try {
+ out.append(str);
+ } catch (IOException e) {
+ throw new RuntimeException("Could not write description", e);
+ }
+ }
+
+ protected void append(char c) {
+ try {
+ out.append(c);
+ } catch (IOException e) {
+ throw new RuntimeException("Could not write description", e);
+ }
+ }
+
+ /**
+ * Returns the description as a string.
+ */
+ public String toString() {
+ return out.toString();
+ }
+}
diff --git a/src/org/hamcrest/core/AllOf.java b/src/org/hamcrest/core/AllOf.java
new file mode 100644
index 0000000..f619a7d
--- /dev/null
+++ b/src/org/hamcrest/core/AllOf.java
@@ -0,0 +1,51 @@
+package org.hamcrest.core;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Matcher;
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+
+import java.util.Arrays;
+
+/**
+ * Calculates the logical conjunction of two matchers. Evaluation is
+ * shortcut, so that the second matcher is not called if the first
+ * matcher returns <code>false</code>.
+ */
+public class AllOf<T> extends BaseMatcher<T> {
+ private final Iterable<Matcher<? extends T>> matchers;
+
+ public AllOf(Iterable<Matcher<? extends T>> matchers) {
+ this.matchers = matchers;
+ }
+
+ public boolean matches(Object o) {
+ for (Matcher<? extends T> matcher : matchers) {
+ if (!matcher.matches(o)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public void describeTo(Description description) {
+ description.appendList("(", " and ", ")", matchers);
+ }
+
+ /**
+ * Evaluates to true only if ALL of the passed in matchers evaluate to true.
+ */
+ @Factory
+ public static <T> Matcher<T> allOf(Matcher<? extends T>... matchers) {
+ return allOf(Arrays.asList(matchers));
+ }
+
+ /**
+ * Evaluates to true only if ALL of the passed in matchers evaluate to true.
+ */
+ @Factory
+ public static <T> Matcher<T> allOf(Iterable<Matcher<? extends T>> matchers) {
+ return new AllOf<T>(matchers);
+ }
+
+}
diff --git a/src/org/hamcrest/core/AnyOf.java b/src/org/hamcrest/core/AnyOf.java
new file mode 100644
index 0000000..e7e9181
--- /dev/null
+++ b/src/org/hamcrest/core/AnyOf.java
@@ -0,0 +1,51 @@
+package org.hamcrest.core;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Matcher;
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+
+import java.util.Arrays;
+
+/**
+ * Calculates the logical disjunction of two matchers. Evaluation is
+ * shortcut, so that the second matcher is not called if the first
+ * matcher returns <code>true</code>.
+ */
+public class AnyOf<T> extends BaseMatcher<T> {
+
+ private final Iterable<Matcher<? extends T>> matchers;
+
+ public AnyOf(Iterable<Matcher<? extends T>> matchers) {
+ this.matchers = matchers;
+ }
+
+ public boolean matches(Object o) {
+ for (Matcher<? extends T> matcher : matchers) {
+ if (matcher.matches(o)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public void describeTo(Description description) {
+ description.appendList("(", " or ", ")", matchers);
+ }
+
+ /**
+ * Evaluates to true if ANY of the passed in matchers evaluate to true.
+ */
+ @Factory
+ public static <T> Matcher<T> anyOf(Matcher<? extends T>... matchers) {
+ return anyOf(Arrays.asList(matchers));
+ }
+
+ /**
+ * Evaluates to true if ANY of the passed in matchers evaluate to true.
+ */
+ @Factory
+ public static <T> Matcher<T> anyOf(Iterable<Matcher<? extends T>> matchers) {
+ return new AnyOf<T>(matchers);
+ }
+}
diff --git a/src/org/hamcrest/core/DescribedAs.java b/src/org/hamcrest/core/DescribedAs.java
new file mode 100644
index 0000000..7b8c151
--- /dev/null
+++ b/src/org/hamcrest/core/DescribedAs.java
@@ -0,0 +1,55 @@
+/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest.core;
+
+import java.util.regex.Pattern;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Factory;
+import org.hamcrest.BaseMatcher;
+
+/**
+ * Provides a custom description to another matcher.
+ */
+public class DescribedAs<T> extends BaseMatcher<T> {
+ private final String descriptionTemplate;
+ private final Matcher<T> matcher;
+ private final Object[] values;
+
+ private final static Pattern ARG_PATTERN = Pattern.compile("%([0-9]+)");
+
+ public DescribedAs(String descriptionTemplate, Matcher<T> matcher, Object[] values) {
+ this.descriptionTemplate = descriptionTemplate;
+ this.matcher = matcher;
+ this.values = values.clone();
+ }
+
+ public boolean matches(Object o) {
+ return matcher.matches(o);
+ }
+
+ public void describeTo(Description description) {
+ java.util.regex.Matcher arg = ARG_PATTERN.matcher(descriptionTemplate);
+
+ int textStart = 0;
+ while (arg.find()) {
+ description.appendText(descriptionTemplate.substring(textStart, arg.start()));
+ int argIndex = Integer.parseInt(arg.group(1));
+ description.appendValue(values[argIndex]);
+ textStart = arg.end();
+ }
+
+ if (textStart < descriptionTemplate.length()) {
+ description.appendText(descriptionTemplate.substring(textStart));
+ }
+ }
+
+ /**
+ * Wraps an existing matcher and overrides the description when it fails.
+ */
+ @Factory
+ public static <T> Matcher<T> describedAs(String description, Matcher<T> matcher, Object... values) {
+ return new DescribedAs<T>(description, matcher, values);
+ }
+}
diff --git a/src/org/hamcrest/core/Is.java b/src/org/hamcrest/core/Is.java
new file mode 100644
index 0000000..f9152e9
--- /dev/null
+++ b/src/org/hamcrest/core/Is.java
@@ -0,0 +1,68 @@
+package org.hamcrest.core;
+
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.hamcrest.core.IsEqual.equalTo;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+
+/**
+ * Decorates another Matcher, retaining the behavior but allowing tests
+ * to be slightly more expressive.
+ *
+ * eg. assertThat(cheese, equalTo(smelly))
+ * vs assertThat(cheese, is(equalTo(smelly)))
+ */
+public class Is<T> extends BaseMatcher<T> {
+
+ private final Matcher<T> matcher;
+
+ public Is(Matcher<T> matcher) {
+ this.matcher = matcher;
+ }
+
+ public boolean matches(Object arg) {
+ return matcher.matches(arg);
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("is ").appendDescriptionOf(matcher);
+ }
+
+ /**
+ * Decorates another Matcher, retaining the behavior but allowing tests
+ * to be slightly more expressive.
+ *
+ * eg. assertThat(cheese, equalTo(smelly))
+ * vs assertThat(cheese, is(equalTo(smelly)))
+ */
+ @Factory
+ public static <T> Matcher<T> is(Matcher<T> matcher) {
+ return new Is<T>(matcher);
+ }
+
+ /**
+ * This is a shortcut to the frequently used is(equalTo(x)).
+ *
+ * eg. assertThat(cheese, is(equalTo(smelly)))
+ * vs assertThat(cheese, is(smelly))
+ */
+ @Factory
+ public static <T> Matcher<T> is(T value) {
+ return is(equalTo(value));
+ }
+
+ /**
+ * This is a shortcut to the frequently used is(instanceOf(SomeClass.class)).
+ *
+ * eg. assertThat(cheese, is(instanceOf(Cheddar.class)))
+ * vs assertThat(cheese, is(Cheddar.class))
+ */
+ @Factory
+ public static Matcher<Object> is(Class<?> type) {
+ return is(instanceOf(type));
+ }
+
+}
+
diff --git a/src/org/hamcrest/core/IsAnything.java b/src/org/hamcrest/core/IsAnything.java
new file mode 100644
index 0000000..c5ca49d
--- /dev/null
+++ b/src/org/hamcrest/core/IsAnything.java
@@ -0,0 +1,59 @@
+/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest.core;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Factory;
+import org.hamcrest.BaseMatcher;
+
+
+/**
+ * A matcher that always returns <code>true</code>.
+ */
+public class IsAnything<T> extends BaseMatcher<T> {
+
+ private final String description;
+
+ public IsAnything() {
+ this("ANYTHING");
+ }
+
+ public IsAnything(String description) {
+ this.description = description;
+ }
+
+ public boolean matches(Object o) {
+ return true;
+ }
+
+ public void describeTo(Description description) {
+ description.appendText(this.description);
+ }
+
+ /**
+ * This matcher always evaluates to true.
+ */
+ @Factory
+ public static <T> Matcher<T> anything() {
+ return new IsAnything<T>();
+ }
+
+ /**
+ * This matcher always evaluates to true.
+ *
+ * @param description A meaningful string used when describing itself.
+ */
+ @Factory
+ public static <T> Matcher<T> anything(String description) {
+ return new IsAnything<T>(description);
+ }
+
+ /**
+ * This matcher always evaluates to true. With type inference.
+ */
+ @Factory
+ public static <T> Matcher<T> any(@SuppressWarnings("unused")Class<T> type) {
+ return new IsAnything<T>();
+ }
+}
diff --git a/src/org/hamcrest/core/IsEqual.java b/src/org/hamcrest/core/IsEqual.java
new file mode 100644
index 0000000..b9f17c5
--- /dev/null
+++ b/src/org/hamcrest/core/IsEqual.java
@@ -0,0 +1,71 @@
+/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest.core;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Factory;
+import org.hamcrest.BaseMatcher;
+
+import java.lang.reflect.Array;
+
+
+/**
+ * Is the value equal to another value, as tested by the
+ * {@link java.lang.Object#equals} invokedMethod?
+ */
+public class IsEqual<T> extends BaseMatcher<T> {
+ private final Object object;
+
+ public IsEqual(T equalArg) {
+ object = equalArg;
+ }
+
+ public boolean matches(Object arg) {
+ return areEqual(object, arg);
+ }
+
+ public void describeTo(Description description) {
+ description.appendValue(object);
+ }
+
+ private static boolean areEqual(Object o1, Object o2) {
+ if (o1 == null || o2 == null) {
+ return o1 == null && o2 == null;
+ } else if (isArray(o1)) {
+ return isArray(o2) && areArraysEqual(o1, o2);
+ } else {
+ return o1.equals(o2);
+ }
+ }
+
+ private static boolean areArraysEqual(Object o1, Object o2) {
+ return areArrayLengthsEqual(o1, o2)
+ && areArrayElementsEqual(o1, o2);
+ }
+
+ private static boolean areArrayLengthsEqual(Object o1, Object o2) {
+ return Array.getLength(o1) == Array.getLength(o2);
+ }
+
+ private static boolean areArrayElementsEqual(Object o1, Object o2) {
+ for (int i = 0; i < Array.getLength(o1); i++) {
+ if (!areEqual(Array.get(o1, i), Array.get(o2, i))) return false;
+ }
+ return true;
+ }
+
+ private static boolean isArray(Object o) {
+ return o.getClass().isArray();
+ }
+
+ /**
+ * Is the value equal to another value, as tested by the
+ * {@link java.lang.Object#equals} invokedMethod?
+ */
+ @Factory
+ public static <T> Matcher<T> equalTo(T operand) {
+ return new IsEqual<T>(operand);
+ }
+
+}
diff --git a/src/org/hamcrest/core/IsInstanceOf.java b/src/org/hamcrest/core/IsInstanceOf.java
new file mode 100644
index 0000000..df20824
--- /dev/null
+++ b/src/org/hamcrest/core/IsInstanceOf.java
@@ -0,0 +1,44 @@
+/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest.core;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Factory;
+import org.hamcrest.BaseMatcher;
+
+
+/**
+ * Tests whether the value is an instance of a class.
+ */
+public class IsInstanceOf extends BaseMatcher<Object> {
+ private final Class<?> theClass;
+
+ /**
+ * Creates a new instance of IsInstanceOf
+ *
+ * @param theClass The predicate evaluates to true for instances of this class
+ * or one of its subclasses.
+ */
+ public IsInstanceOf(Class<?> theClass) {
+ this.theClass = theClass;
+ }
+
+ public boolean matches(Object item) {
+ return theClass.isInstance(item);
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("an instance of ")
+ .appendText(theClass.getName());
+ }
+
+ /**
+ * Is the value an instance of a particular type?
+ */
+ @Factory
+ public static Matcher<Object> instanceOf(Class<?> type) {
+ return new IsInstanceOf(type);
+ }
+
+}
diff --git a/src/org/hamcrest/core/IsNot.java b/src/org/hamcrest/core/IsNot.java
new file mode 100644
index 0000000..cb6946c
--- /dev/null
+++ b/src/org/hamcrest/core/IsNot.java
@@ -0,0 +1,49 @@
+/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest.core;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Factory;
+import org.hamcrest.BaseMatcher;
+
+
+/**
+ * Calculates the logical negation of a matcher.
+ */
+public class IsNot<T> extends BaseMatcher<T> {
+ private final Matcher<T> matcher;
+
+ public IsNot(Matcher<T> matcher) {
+ this.matcher = matcher;
+ }
+
+ public boolean matches(Object arg) {
+ return !matcher.matches(arg);
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("not ").appendDescriptionOf(matcher);
+ }
+
+ /**
+ * Inverts the rule.
+ */
+ @Factory
+ public static <T> Matcher<T> not(Matcher<T> matcher) {
+ return new IsNot<T>(matcher);
+ }
+
+ /**
+ * This is a shortcut to the frequently used not(equalTo(x)).
+ *
+ * eg. assertThat(cheese, is(not(equalTo(smelly))))
+ * vs assertThat(cheese, is(not(smelly)))
+ */
+ @Factory
+ public static <T> Matcher<T> not(T value) {
+ return not(equalTo(value));
+ }
+
+}
diff --git a/src/org/hamcrest/core/IsNull.java b/src/org/hamcrest/core/IsNull.java
new file mode 100644
index 0000000..737dcf2
--- /dev/null
+++ b/src/org/hamcrest/core/IsNull.java
@@ -0,0 +1,55 @@
+/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest.core;
+
+import static org.hamcrest.core.IsNot.not;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Factory;
+import org.hamcrest.BaseMatcher;
+
+/**
+ * Is the value null?
+ */
+public class IsNull<T> extends BaseMatcher<T> {
+ public boolean matches(Object o) {
+ return o == null;
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("null");
+ }
+
+ /**
+ * Matches if value is null.
+ */
+ @Factory
+ public static <T> Matcher<T> nullValue() {
+ return new IsNull<T>();
+ }
+
+ /**
+ * Matches if value is not null.
+ */
+ @Factory
+ public static <T> Matcher<T> notNullValue() {
+ return not(IsNull.<T>nullValue());
+ }
+
+ /**
+ * Matches if value is null. With type inference.
+ */
+ @Factory
+ public static <T> Matcher<T> nullValue(@SuppressWarnings("unused") Class<T> type) {
+ return nullValue();
+ }
+
+ /**
+ * Matches if value is not null. With type inference.
+ */
+ @Factory
+ public static <T> Matcher<T> notNullValue(@SuppressWarnings("unused") Class<T> type) {
+ return notNullValue();
+ }
+}
+
diff --git a/src/org/hamcrest/core/IsSame.java b/src/org/hamcrest/core/IsSame.java
new file mode 100644
index 0000000..b3ad77e
--- /dev/null
+++ b/src/org/hamcrest/core/IsSame.java
@@ -0,0 +1,40 @@
+/* Copyright (c) 2000-2006 hamcrest.org
+ */
+package org.hamcrest.core;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Factory;
+import org.hamcrest.BaseMatcher;
+
+
+/**
+ * Is the value the same object as another value?
+ */
+public class IsSame<T> extends BaseMatcher<T> {
+ private final T object;
+
+ public IsSame(T object) {
+ this.object = object;
+ }
+
+ public boolean matches(Object arg) {
+ return arg == object;
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("same(") .appendValue(object) .appendText(")");
+ }
+
+ /**
+ * Creates a new instance of IsSame
+ *
+ * @param object The predicate evaluates to true only when the argument is
+ * this object.
+ */
+ @Factory
+ public static <T> Matcher<T> sameInstance(T object) {
+ return new IsSame<T>(object);
+ }
+
+}
diff --git a/src/org/hamcrest/core/package.html b/src/org/hamcrest/core/package.html
new file mode 100644
index 0000000..7bb0ffe
--- /dev/null
+++ b/src/org/hamcrest/core/package.html
@@ -0,0 +1,7 @@
+<html>
+<head>
+</head>
+<body>
+ <p>Fundamental matchers of objects and values, and composite matchers.</p>
+</body>
+</html>
diff --git a/src/org/hamcrest/internal/ArrayIterator.java b/src/org/hamcrest/internal/ArrayIterator.java
new file mode 100644
index 0000000..093cdba
--- /dev/null
+++ b/src/org/hamcrest/internal/ArrayIterator.java
@@ -0,0 +1,28 @@
+package org.hamcrest.internal;
+
+import java.lang.reflect.Array;
+import java.util.Iterator;
+
+public class ArrayIterator implements Iterator<Object> {
+ private final Object array;
+ private int currentIndex = 0;
+
+ public ArrayIterator(Object array) {
+ if (!array.getClass().isArray()) {
+ throw new IllegalArgumentException("not an array");
+ }
+ this.array = array;
+ }
+
+ public boolean hasNext() {
+ return currentIndex < Array.getLength(array);
+ }
+
+ public Object next() {
+ return Array.get(array, currentIndex++);
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException("cannot remove items from an array");
+ }
+}
diff --git a/src/org/hamcrest/internal/SelfDescribingValue.java b/src/org/hamcrest/internal/SelfDescribingValue.java
new file mode 100644
index 0000000..0634527
--- /dev/null
+++ b/src/org/hamcrest/internal/SelfDescribingValue.java
@@ -0,0 +1,16 @@
+package org.hamcrest.internal;
+
+import org.hamcrest.Description;
+import org.hamcrest.SelfDescribing;
+
+public class SelfDescribingValue<T> implements SelfDescribing {
+ private T value;
+
+ public SelfDescribingValue(T value) {
+ this.value = value;
+ }
+
+ public void describeTo(Description description) {
+ description.appendValue(value);
+ }
+}
diff --git a/src/org/hamcrest/internal/SelfDescribingValueIterator.java b/src/org/hamcrest/internal/SelfDescribingValueIterator.java
new file mode 100644
index 0000000..58bedf6
--- /dev/null
+++ b/src/org/hamcrest/internal/SelfDescribingValueIterator.java
@@ -0,0 +1,25 @@
+package org.hamcrest.internal;
+
+import java.util.Iterator;
+
+import org.hamcrest.SelfDescribing;
+
+public class SelfDescribingValueIterator<T> implements Iterator<SelfDescribing> {
+ private Iterator<T> values;
+
+ public SelfDescribingValueIterator(Iterator<T> values) {
+ this.values = values;
+ }
+
+ public boolean hasNext() {
+ return values.hasNext();
+ }
+
+ public SelfDescribing next() {
+ return new SelfDescribingValue<T>(values.next());
+ }
+
+ public void remove() {
+ values.remove();
+ }
+}
diff --git a/src/org/hamcrest/package.html b/src/org/hamcrest/package.html
new file mode 100644
index 0000000..143c704
--- /dev/null
+++ b/src/org/hamcrest/package.html
@@ -0,0 +1,9 @@
+<html>
+<head>
+</head>
+<body>
+ <p>The stable API defining Matcher and its associated interfaces and classes.
+ Hamcrest sub-projects define their convenience classes in the org.hamcrest package.
+ </p>
+</body>
+</html>