Refactor SerializationTestUtils into 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/408d0a9a
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/408d0a9a
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/408d0a9a
Branch: refs/heads/master
Commit: 408d0a9ab9e98f8856edccc0a1670b70abaafbdc
Parents: 260a4a0
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Oct 4 11:26:46 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Oct 4 11:26:46 2014 -0500
----------------------------------------------------------------------
.../log4j/AbstractSerializationTest.java | 7 ++-
.../logging/log4j/SerializableMatchers.java | 58 ++++++++++++++++++++
.../logging/log4j/SerializationTestUtils.java | 36 ------------
3 files changed, 63 insertions(+), 38 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/408d0a9a/log4j-api/src/test/java/org/apache/logging/log4j/AbstractSerializationTest.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/AbstractSerializationTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/AbstractSerializationTest.java
index 135c0b7..ea38501 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/AbstractSerializationTest.java
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/AbstractSerializationTest.java
@@ -22,6 +22,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
+import static org.apache.logging.log4j.SerializableMatchers.serializesRoundTrip;
+import static org.junit.Assert.assertThat;
+
/**
* Subclasses tests {@link Serializable} objects.
*/
@@ -37,11 +40,11 @@ public abstract class AbstractSerializationTest {
@Test
public void testSerializationRoundtripEquals() {
- SerializationTestUtils.roundtripEquals(serializable);
+ assertThat(serializable, serializesRoundTrip(serializable));
}
@Test
public void testSerializationRoundtripNoException() {
- SerializationTestUtils.roundtripNoException(serializable);
+ assertThat(serializable, serializesRoundTrip());
}
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/408d0a9a/log4j-api/src/test/java/org/apache/logging/log4j/SerializableMatchers.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/SerializableMatchers.java b/log4j-api/src/test/java/org/apache/logging/log4j/SerializableMatchers.java
new file mode 100644
index 0000000..7545b96
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/SerializableMatchers.java
@@ -0,0 +1,58 @@
+/*
+ * 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;
+
+import java.io.Serializable;
+
+import org.apache.commons.lang3.SerializationUtils;
+import org.hamcrest.FeatureMatcher;
+import org.hamcrest.Matcher;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.hamcrest.core.IsInstanceOf.any;
+
+/**
+ * Hamcrest Matchers for Serializable classes.
+ *
+ * @since 2.1
+ */
+public final class SerializableMatchers {
+
+ public static <T extends Serializable> Matcher<T> serializesRoundTrip(final Matcher<T> matcher) {
+ return new FeatureMatcher<T, T>(matcher, "serializes round trip", "serializes round trip") {
+ @Override
+ protected T featureValueOf(final T actual) {
+ return SerializationUtils.roundtrip(actual);
+ }
+ };
+ }
+
+ public static <T extends Serializable> Matcher<T> serializesRoundTrip(final T expected) {
+ return serializesRoundTrip(equalTo(expected));
+ }
+
+ public static <T extends Serializable> Matcher<T> serializesRoundTrip(final Class<T> clazz) {
+ return serializesRoundTrip(any(clazz));
+ }
+
+ public static Matcher<? super Serializable> serializesRoundTrip() {
+ return serializesRoundTrip(any(Serializable.class));
+ }
+
+ private SerializableMatchers() {
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/408d0a9a/log4j-api/src/test/java/org/apache/logging/log4j/SerializationTestUtils.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/SerializationTestUtils.java b/log4j-api/src/test/java/org/apache/logging/log4j/SerializationTestUtils.java
deleted file mode 100644
index 9009ee8..0000000
--- a/log4j-api/src/test/java/org/apache/logging/log4j/SerializationTestUtils.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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;
-
-import java.io.Serializable;
-
-import org.apache.commons.lang3.SerializationUtils;
-import org.junit.Assert;
-
-/**
- * Tests {@link Serializable} objects.
- */
-public class SerializationTestUtils {
-
- public static void roundtripNoException(final Serializable serializable) {
- SerializationUtils.roundtrip(serializable);
- }
-
- public static void roundtripEquals(final Serializable serializable) {
- Assert.assertEquals(serializable, SerializationUtils.roundtrip(serializable));
- }
-}