You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2014/09/16 17:18:08 UTC

[4/4] git commit: Initial version of JUnit tests for MemoryMappedAppender.

Initial version of JUnit tests for MemoryMappedAppender.

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

Branch: refs/heads/LOG4J2-431
Commit: 9063e0c573091018cb8b62b63bc1698e26a43ce6
Parents: e751c22
Author: rpopma <rp...@apache.org>
Authored: Wed Sep 17 00:16:21 2014 +0900
Committer: rpopma <rp...@apache.org>
Committed: Wed Sep 17 00:16:21 2014 +0900

----------------------------------------------------------------------
 .../MemoryMappedFileAppenderLocationTest.java   | 95 ++++++++++++++++++++
 .../MemoryMappedFileAppenderRemapTest.java      | 95 ++++++++++++++++++++
 .../MemoryMappedFileAppenderSimpleTest.java     | 86 ++++++++++++++++++
 .../MemoryMappedFileAppenderLocationTest.xml    | 18 ++++
 .../MemoryMappedFileAppenderRemapTest.xml       | 18 ++++
 .../resources/MemoryMappedFileAppenderTest.xml  | 18 ++++
 6 files changed, 330 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9063e0c5/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java
new file mode 100644
index 0000000..d9f5d57
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.core.appender;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.util.Integers;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.*;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests that logged strings and their location appear in the file,
+ * that the file size is the next power of two of the specified mapped region length
+ * and that the file is shrunk to its actual usage when done.
+ */
+public class MemoryMappedFileAppenderLocationTest {
+
+    final String LOGFILE = "target/MemoryMappedFileAppenderLocationTest.log";
+
+    @Before
+    public void before() {
+        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, 
+                "MemoryMappedFileAppenderLocationTest.xml");
+    }
+
+    @Test
+    public void testMemMapLocation() throws Exception {
+        final File f = new File(LOGFILE);
+        if (f.exists()) {
+            assertTrue(f.delete());
+        }
+        assertTrue(!f.exists());
+        
+        final int expectedFileLength = Integers.ceilingNextPowerOfTwo(32000);
+        assertEquals(32768, expectedFileLength);
+        
+        final Logger log = LogManager.getLogger();
+        try {
+            log.warn("Test log1");
+            assertTrue(f.exists());
+            assertEquals("initial length", expectedFileLength, f.length());
+            
+            log.warn("Test log2");
+            assertEquals("not grown", expectedFileLength, f.length());
+        } finally {
+            ((LoggerContext) LogManager.getContext(false)).stop();
+        }
+        assertEquals("Shrunk to actual used size", 474, f.length());
+        
+        String line1, line2, line3;
+        final BufferedReader reader = new BufferedReader(new FileReader(LOGFILE));
+        try {
+            line1 = reader.readLine();
+            line2 = reader.readLine();
+            line3 = reader.readLine();
+        } finally {
+            reader.close();
+        }
+        assertNotNull(line1);
+        assertThat(line1, containsString("Test log1"));
+        String location1 = "org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderLocationTest.testMemMapBasics(MemoryMappedFileAppenderLocationTest.java:63)";
+        assertThat(line1, containsString(location1));
+
+        assertNotNull(line2);
+        assertThat(line2, containsString("Test log2"));
+        String location2 = "org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderLocationTest.testMemMapBasics(MemoryMappedFileAppenderLocationTest.java:67)";
+        assertThat(line2, containsString(location2));
+
+        assertNull("only two lines were logged", line3);
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9063e0c5/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java
new file mode 100644
index 0000000..bcb9cdc
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.core.appender;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.util.Arrays;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.*;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests that logged strings appear in the file, that the initial file size is the specified specified region length,
+ * that the file is extended by region length when necessary, and that the file is shrunk to its actual usage when done.
+ */
+public class MemoryMappedFileAppenderRemapTest {
+
+    final String LOGFILE = "target/MemoryMappedFileAppenderRemapTest.log";
+
+    @Before
+    public void before() {
+        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "MemoryMappedFileAppenderRemapTest.xml");
+    }
+
+    @Test
+    public void testMemMapExtendsIfNeeded() throws Exception {
+        final File f = new File(LOGFILE);
+        if (f.exists()) {
+            assertTrue(f.delete());
+        }
+        assertTrue(!f.exists());
+
+        final Logger log = LogManager.getLogger();
+        final char[] text = new char[200];
+        Arrays.fill(text, 'A');
+        try {
+            log.warn("Test log1");
+            assertTrue(f.exists());
+            assertEquals("initial length", 256, f.length());
+
+            log.warn(new String(text));
+            assertEquals("grown", 256 * 2, f.length());
+            
+            log.warn(new String(text));
+            assertEquals("grown again", 256 * 3, f.length());
+        } finally {
+            ((LoggerContext) LogManager.getContext(false)).stop();
+        }
+        assertEquals("Shrunk to actual used size", 664, f.length());
+
+        String line1, line2, line3, line4;
+        final BufferedReader reader = new BufferedReader(new FileReader(LOGFILE));
+        try {
+            line1 = reader.readLine();
+            line2 = reader.readLine();
+            line3 = reader.readLine();
+            line4 = reader.readLine();
+        } finally {
+            reader.close();
+        }
+        assertNotNull(line1);
+        assertThat(line1, containsString("Test log1"));
+
+        assertNotNull(line2);
+        assertThat(line2, containsString(new String(text)));
+
+        assertNotNull(line3);
+        assertThat(line3, containsString(new String(text)));
+
+        assertNull("only three lines were logged", line4);
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9063e0c5/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java
new file mode 100644
index 0000000..bedeb7f
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.core.appender;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.*;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests that logged strings appear in the file,
+ * that the default file size is used if not specified
+ * and that the file is shrunk to its actual usage when done.
+ */
+public class MemoryMappedFileAppenderSimpleTest {
+
+    final String LOGFILE = "target/MemoryMappedFileAppenderTest.log";
+
+    @Before
+    public void before() {
+        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "MemoryMappedFileAppenderTest.xml");
+    }
+
+    @Test
+    public void testMemMapBasics() throws Exception {
+        final File f = new File(LOGFILE);
+        if (f.exists()) {
+            assertTrue(f.delete());
+        }
+        assertTrue(!f.exists());
+        
+        final Logger log = LogManager.getLogger();
+        try {
+            log.warn("Test log1");
+            assertTrue(f.exists());
+            assertEquals("initial length", MemoryMappedFileManager.DEFAULT_REGION_LENGTH, f.length());
+            
+            log.warn("Test log2");
+            assertEquals("not grown", MemoryMappedFileManager.DEFAULT_REGION_LENGTH, f.length());
+        } finally {
+            ((LoggerContext) LogManager.getContext(false)).stop();
+        }
+        assertEquals("Shrunk to actual used size", 190, f.length());
+        
+        String line1, line2, line3;
+        final BufferedReader reader = new BufferedReader(new FileReader(LOGFILE));
+        try {
+            line1 = reader.readLine();
+            line2 = reader.readLine();
+            line3 = reader.readLine();
+        } finally {
+            reader.close();
+        }
+        assertNotNull(line1);
+        assertThat(line1, containsString("Test log1"));
+
+        assertNotNull(line2);
+        assertThat(line2, containsString("Test log2"));
+
+        assertNull("only two lines were logged", line3);
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9063e0c5/log4j-core/src/test/resources/MemoryMappedFileAppenderLocationTest.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/MemoryMappedFileAppenderLocationTest.xml b/log4j-core/src/test/resources/MemoryMappedFileAppenderLocationTest.xml
new file mode 100644
index 0000000..14b6b25
--- /dev/null
+++ b/log4j-core/src/test/resources/MemoryMappedFileAppenderLocationTest.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="off">
+  <Appenders>
+    <MemoryMappedFile name="MemoryMappedFile" 
+        fileName="target/MemoryMappedFileAppenderLocationTest.log" 
+        regionLength="32000" immediateFlush="true" append="false">
+      <PatternLayout>
+        <Pattern>%d %p %c{1.} [%t] %X{aKey} %m %location %ex%n</Pattern>
+      </PatternLayout>
+    </MemoryMappedFile>
+  </Appenders>
+  
+  <Loggers>
+    <Root level="info" includeLocation="true">
+      <AppenderRef ref="MemoryMappedFile"/>
+    </Root>
+  </Loggers>
+</Configuration>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9063e0c5/log4j-core/src/test/resources/MemoryMappedFileAppenderRemapTest.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/MemoryMappedFileAppenderRemapTest.xml b/log4j-core/src/test/resources/MemoryMappedFileAppenderRemapTest.xml
new file mode 100644
index 0000000..02799a5
--- /dev/null
+++ b/log4j-core/src/test/resources/MemoryMappedFileAppenderRemapTest.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="off">
+  <Appenders>
+    <MemoryMappedFile name="MemoryMappedFile" 
+        fileName="target/MemoryMappedFileAppenderRemapTest.log" 
+        regionLength="256" immediateFlush="true" append="false">
+      <PatternLayout>
+        <Pattern>%d %p %c{1.} [%t] %X{aKey} %m%ex%n</Pattern>
+      </PatternLayout>
+    </MemoryMappedFile>
+  </Appenders>
+  
+  <Loggers>
+    <Root level="info">
+      <AppenderRef ref="MemoryMappedFile"/>
+    </Root>
+  </Loggers>
+</Configuration>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9063e0c5/log4j-core/src/test/resources/MemoryMappedFileAppenderTest.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/MemoryMappedFileAppenderTest.xml b/log4j-core/src/test/resources/MemoryMappedFileAppenderTest.xml
new file mode 100644
index 0000000..430c8b1
--- /dev/null
+++ b/log4j-core/src/test/resources/MemoryMappedFileAppenderTest.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="OFF">
+  <Appenders>
+    <MemoryMappedFile name="MemoryMappedFile" 
+         fileName="target/MemoryMappedFileAppenderTest.log" 
+         immediateFlush="false" append="false">
+      <PatternLayout>
+        <Pattern>%d %p %c{1.} [%t] %X{aKey} %m%ex%n</Pattern>
+      </PatternLayout>
+    </MemoryMappedFile>
+  </Appenders>
+  
+  <Loggers>
+    <Root level="info" includeLocation="false">
+      <AppenderRef ref="MemoryMappedFile"/>
+    </Root>
+  </Loggers>
+</Configuration>
\ No newline at end of file