You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/10/03 05:38:57 UTC

[3/6] git commit: More custom Hamcrest matchers.

More custom Hamcrest matchers.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/3f2566dd
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/3f2566dd
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/3f2566dd

Branch: refs/heads/master
Commit: 3f2566dd2259892deb069b6da7af6c17326703db
Parents: 4f2b3d6
Author: Matt Sicker <ma...@apache.org>
Authored: Thu Oct 2 22:14:17 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Thu Oct 2 22:14:17 2014 -0500

----------------------------------------------------------------------
 .../logging/log4j/junit/FileMatchers.java       | 10 +++-
 .../apache/logging/log4j/junit/MapMatchers.java | 62 ++++++++++++++++++++
 2 files changed, 69 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f2566dd/log4j-core/src/test/java/org/apache/logging/log4j/junit/FileMatchers.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/junit/FileMatchers.java b/log4j-core/src/test/java/org/apache/logging/log4j/junit/FileMatchers.java
index 1b3e516..3207a4a 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/junit/FileMatchers.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/junit/FileMatchers.java
@@ -20,20 +20,21 @@ import java.io.File;
 
 import org.hamcrest.FeatureMatcher;
 import org.hamcrest.Matcher;
-import org.hamcrest.core.Is;
+
+import static org.hamcrest.core.Is.is;
 
 /**
  * Hamcrest Matchers that operate on File objects.
  *
  * @since 2.1
  */
-public class FileMatchers {
+public final class FileMatchers {
 
     /**
      * Returns a File Matcher that matches if the File exists.
      */
     public static Matcher<File> exists() {
-        return new FeatureMatcher<File, Boolean>(Is.is(true), "file exists", "file exists") {
+        return new FeatureMatcher<File, Boolean>(is(true), "file exists", "file exists") {
             @Override
             protected Boolean featureValueOf(final File actual) {
                 return actual.exists();
@@ -55,4 +56,7 @@ public class FileMatchers {
         };
     }
 
+    private FileMatchers() {
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3f2566dd/log4j-core/src/test/java/org/apache/logging/log4j/junit/MapMatchers.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/junit/MapMatchers.java b/log4j-core/src/test/java/org/apache/logging/log4j/junit/MapMatchers.java
new file mode 100644
index 0000000..7fd19a8
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/junit/MapMatchers.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.logging.log4j.junit;
+
+import java.util.Map;
+
+import org.hamcrest.FeatureMatcher;
+import org.hamcrest.Matcher;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+
+/**
+ * Hamcrest Matchers for Maps.
+ *
+ * @since 2.1
+ */
+public final class MapMatchers {
+
+    /**
+     * Returns a Map Matcher matching on the size of a Map.
+     *
+     * @param matcher the Matcher to match against the Map size.
+     * @param <K>     the key type.
+     * @param <V>     the value type.
+     */
+    public static <K, V> Matcher<Map<? extends K, ? extends V>> hasSize(final Matcher<Integer> matcher) {
+        return new FeatureMatcher<Map<? extends K, ? extends V>, Integer>(matcher, "map with size", "map with size") {
+            @Override
+            protected Integer featureValueOf(final Map<? extends K, ? extends V> actual) {
+                return actual.size();
+            }
+        };
+    }
+
+    /**
+     * Returns a Map Matcher matching on the exact size of a Map.
+     *
+     * @param size the number of entries to match the Map against.
+     * @param <K>  the key type.
+     * @param <V>  the value type.
+     */
+    public static <K, V> Matcher<Map<? extends K, ? extends V>> hasSize(final int size) {
+        return hasSize(equalTo(size));
+    }
+
+    private MapMatchers() {
+    }
+}