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

git commit: Test newly refactored LoggerOutputStream and LoggerFilterOutputStream.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master f892f849e -> bb5d55b7d


Test newly refactored LoggerOutputStream and LoggerFilterOutputStream.

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

Branch: refs/heads/master
Commit: bb5d55b7da937bff91117e39dd62d46004536d76
Parents: f892f84
Author: Gary Gregory <ga...@gmail.com>
Authored: Tue Sep 2 23:23:09 2014 -0400
Committer: Gary Gregory <ga...@gmail.com>
Committed: Tue Sep 2 23:23:09 2014 -0400

----------------------------------------------------------------------
 .../streams/AbstractLoggerOutputStreamTest.java | 138 +++++++++++++++++++
 .../streams/LoggerFilterOutputStreamTest.java   |  36 +++++
 .../log4j/streams/LoggerOutputStreamTest.java   | 103 +-------------
 .../log4j/streams/LoggerPrintStreamTest.java    |  10 +-
 4 files changed, 189 insertions(+), 98 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bb5d55b7/log4j-streams/src/test/java/org/apache/logging/log4j/streams/AbstractLoggerOutputStreamTest.java
----------------------------------------------------------------------
diff --git a/log4j-streams/src/test/java/org/apache/logging/log4j/streams/AbstractLoggerOutputStreamTest.java b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/AbstractLoggerOutputStreamTest.java
new file mode 100644
index 0000000..9108794
--- /dev/null
+++ b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/AbstractLoggerOutputStreamTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.streams;
+
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+public abstract class AbstractLoggerOutputStreamTest extends AbstractStreamTest {
+    protected OutputStream out;
+    protected ByteArrayOutputStream wrapped;
+
+    protected abstract ByteArrayOutputStream createOutputStream();
+
+    protected abstract OutputStream createOutputStreamWrapper();
+
+    @Before
+    public void createStream() {
+        this.wrapped = createOutputStream();
+        this.out = createOutputStreamWrapper();
+    }
+
+    @Test
+    public void testClose_HasRemainingData() throws IOException {
+        this.out.write(FIRST.getBytes());
+        assertMessages();
+        this.out.close();
+        assertMessages(FIRST);
+        if (this.wrapped != null) {
+            assertEquals(FIRST, this.wrapped.toString());
+        }
+    }
+
+    @Test
+    public void testClose_NoRemainingData() throws IOException {
+        this.out.close();
+        assertMessages();
+        if (this.wrapped != null) {
+            assertEquals("", this.wrapped.toString());
+        }
+    }
+
+    @Test
+    public void testFlush() throws IOException {
+        final OutputStream out = EasyMock.createMock("out", OutputStream.class);
+        out.flush(); // expect the flush to come through to the mocked OutputStream
+        out.close();
+        replay(out);
+
+        final LoggerFilterOutputStream los = new LoggerFilterOutputStream(out, getExtendedLogger(), LEVEL);
+        los.flush();
+        los.close();
+        verify(out);
+    }
+
+    @Test
+    public void testWrite_ByteArray() throws Exception {
+        final byte[] bytes = "byte[]".getBytes();
+        this.out.write(bytes);
+        assertMessages();
+        this.out.write('\n');
+        assertMessages("byte[]");
+        if (this.wrapped != null) {
+            assertEquals("byte[]\n", this.wrapped.toString());
+        }
+    }
+
+    @Test
+    public void testWrite_ByteArray_Offset_Length() throws Exception {
+        final byte[] bytes = "byte[]".getBytes();
+        final int middle = bytes.length / 2;
+        final int length = bytes.length - middle;
+        final String right = new String(bytes, middle, length);
+        this.out.write(bytes, middle, length);
+        assertMessages();
+        this.out.write('\n');
+        assertMessages(right);
+        if (this.wrapped != null) {
+            assertEquals("byte[]".substring(middle, bytes.length) + '\n', this.wrapped.toString());
+        }
+    }
+
+    @Test
+    public void testWrite_IgnoresWindowsNewline() throws IOException {
+        this.out.write(FIRST.getBytes());
+        this.out.write("\r\n".getBytes());
+        this.out.write(LAST.getBytes());
+        this.out.close();
+        assertMessages(FIRST, LAST);
+        if (this.wrapped != null) {
+            assertEquals(FIRST + "\r\n" + LAST, this.wrapped.toString());
+        }
+    }
+
+    @Test
+    public void testWrite_Int() throws Exception {
+        for (final byte b : "int".getBytes()) {
+            this.out.write(b);
+            assertMessages();
+        }
+        this.out.write('\n');
+        assertMessages("int");
+        if (this.wrapped != null) {
+            assertEquals("int" + '\n', this.wrapped.toString());
+        }
+    }
+
+    @Test
+    public void testWrite_MultipleLines() throws IOException {
+        this.out.write((FIRST + '\n' + LAST + '\n').getBytes());
+        assertMessages(FIRST, LAST);
+        if (this.wrapped != null) {
+            assertEquals(FIRST + '\n' + LAST + '\n', this.wrapped.toString());
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bb5d55b7/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerFilterOutputStreamTest.java
----------------------------------------------------------------------
diff --git a/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerFilterOutputStreamTest.java b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerFilterOutputStreamTest.java
new file mode 100644
index 0000000..5b05775
--- /dev/null
+++ b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerFilterOutputStreamTest.java
@@ -0,0 +1,36 @@
+/*
+ * 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.streams;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+
+import org.apache.logging.log4j.Level;
+
+public class LoggerFilterOutputStreamTest extends AbstractLoggerOutputStreamTest {
+
+    @Override
+    protected ByteArrayOutputStream createOutputStream() {
+        return new ByteArrayOutputStream();
+    }
+
+    @Override
+    protected OutputStream createOutputStreamWrapper() {
+        return new LoggerFilterOutputStream(this.wrapped, getExtendedLogger(), Level.ERROR);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bb5d55b7/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerOutputStreamTest.java
----------------------------------------------------------------------
diff --git a/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerOutputStreamTest.java b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerOutputStreamTest.java
index e6450ee..2847a94 100644
--- a/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerOutputStreamTest.java
+++ b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerOutputStreamTest.java
@@ -16,110 +16,21 @@
  */
 package org.apache.logging.log4j.streams;
 
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.assertEquals;
-
 import java.io.ByteArrayOutputStream;
-import java.io.IOException;
 import java.io.OutputStream;
 
 import org.apache.logging.log4j.Level;
-import org.easymock.EasyMock;
-import org.junit.Before;
-import org.junit.Test;
-
-public class LoggerOutputStreamTest extends AbstractStreamTest {
-    protected ByteArrayOutputStream wrapped;
-    protected OutputStream out;
-
-    protected OutputStream createOutputStream() {
-        return new LoggerFilterOutputStream(this.wrapped, getExtendedLogger(), Level.ERROR);
-    }
-
-    @Before
-    public void createStream() {
-        this.wrapped = new ByteArrayOutputStream();
-        this.out = createOutputStream();
-    }
-
-    @Test
-    public void testClose_HasRemainingData() throws IOException {
-        this.out.write(FIRST.getBytes());
-        assertMessages();
-        this.out.close();
-        assertMessages(FIRST);
-        assertEquals(FIRST, this.wrapped.toString());
-    }
 
-    @Test
-    public void testClose_NoRemainingData() throws IOException {
-        this.out.close();
-        assertMessages();
-        assertEquals("", this.wrapped.toString());
-    }
-
-    @Test
-    public void testFlush() throws IOException {
-        final OutputStream out = EasyMock.createMock("out", OutputStream.class);
-        out.flush(); // expect the flush to come through to the mocked OutputStream
-        out.close();
-        replay(out);
-        
-        final LoggerFilterOutputStream los = new LoggerFilterOutputStream(out, getExtendedLogger(), LEVEL);
-        los.flush();
-        los.close();
-        verify(out);
-    }
-
-    @Test
-    public void testWrite_ByteArray() throws Exception {
-        final byte[] bytes = "byte[]".getBytes();
-        this.out.write(bytes);
-        assertMessages();
-        this.out.write('\n');
-        assertMessages("byte[]");
-        assertEquals("byte[]\n", this.wrapped.toString());
-    }
-
-    @Test
-    public void testWrite_ByteArray_Offset_Length() throws Exception {
-        final byte[] bytes = "byte[]".getBytes();
-        final int middle = bytes.length/2;
-        final int length = bytes.length - middle;
-        final String right = new String(bytes, middle, length);
-        this.out.write(bytes, middle, length);
-        assertMessages();
-        this.out.write('\n');
-        assertMessages(right);
-        assertEquals("byte[]".substring(middle, bytes.length) + '\n', this.wrapped.toString());
-    }
+public class LoggerOutputStreamTest extends AbstractLoggerOutputStreamTest {
 
-    @Test
-    public void testWrite_IgnoresWindowsNewline() throws IOException {
-        this.out.write(FIRST.getBytes());
-        this.out.write("\r\n".getBytes());
-        this.out.write(LAST.getBytes());
-        this.out.close();
-        assertMessages(FIRST, LAST);
-        assertEquals(FIRST + "\r\n" + LAST, this.wrapped.toString());
+    @Override
+    protected ByteArrayOutputStream createOutputStream() {
+        return null;
     }
 
-    @Test
-    public void testWrite_Int() throws Exception {
-        for (final byte b : "int".getBytes()) {
-            this.out.write(b);
-            assertMessages();
-        }
-        this.out.write('\n');
-        assertMessages("int");
-        assertEquals("int" + '\n', this.wrapped.toString());
+    @Override
+    protected OutputStream createOutputStreamWrapper() {
+        return new LoggerOutputStream(getExtendedLogger(), Level.ERROR);
     }
 
-    @Test
-    public void testWrite_MultipleLines() throws IOException {
-        this.out.write((FIRST + '\n' + LAST + '\n').getBytes());
-        assertMessages(FIRST, LAST);
-        assertEquals(FIRST + '\n' + LAST + '\n', this.wrapped.toString());
-    }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bb5d55b7/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerPrintStreamTest.java
----------------------------------------------------------------------
diff --git a/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerPrintStreamTest.java b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerPrintStreamTest.java
index 812043a..b1df321 100644
--- a/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerPrintStreamTest.java
+++ b/log4j-streams/src/test/java/org/apache/logging/log4j/streams/LoggerPrintStreamTest.java
@@ -19,15 +19,21 @@ package org.apache.logging.log4j.streams;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
 
+import java.io.ByteArrayOutputStream;
 import java.io.OutputStream;
 
 import org.junit.Test;
 
-public class LoggerPrintStreamTest extends LoggerOutputStreamTest {
+public class LoggerPrintStreamTest extends AbstractLoggerOutputStreamTest {
     private LoggerPrintStream print;
 
     @Override
-    protected OutputStream createOutputStream() {
+    protected ByteArrayOutputStream createOutputStream() {
+        return new ByteArrayOutputStream();
+    }
+
+    @Override
+    protected OutputStream createOutputStreamWrapper() {
         return this.print = new LoggerPrintStream(this.wrapped, getExtendedLogger(), LEVEL);
     }