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 09:34:30 UTC
[6/8] git commit: More literate tests.
More literate tests.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f78a1566
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f78a1566
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f78a1566
Branch: refs/heads/master
Commit: f78a1566438522608051696c8e4acba5bab5b0cb
Parents: 4700549
Author: Matt Sicker <ma...@apache.org>
Authored: Fri Oct 3 02:22:51 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Fri Oct 3 02:22:51 2014 -0500
----------------------------------------------------------------------
.../rolling/RollingAppenderSizeTest.java | 20 +++++++-------
.../rolling/RollingAppenderTimeAndSizeTest.java | 20 +++++++-------
.../rolling/RollingAppenderTimeTest.java | 18 ++++++++----
.../RollingAppenderUncompressedTest.java | 25 +++++++++--------
...RandomAccessFileManagerHeaderFooterTest.java | 15 +++++-----
.../RollingRandomAccessFileManagerTest.java | 29 +++++++++++++-------
6 files changed, 73 insertions(+), 54 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f78a1566/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
index ae4b34f..c490d2c 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
@@ -29,6 +29,13 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
+import static org.apache.logging.log4j.junit.FileMatchers.exists;
+import static org.apache.logging.log4j.junit.FileMatchers.hasFiles;
+import static org.apache.logging.log4j.junit.FileMatchers.hasName;
+import static org.apache.logging.log4j.junit.FileMatchers.that;
+import static org.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.hasItemInArray;
import static org.junit.Assert.*;
/**
@@ -77,17 +84,10 @@ public class RollingAppenderSizeTest {
logger.debug("This is test message number " + i);
}
final File dir = new File(DIR);
- assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
+ assertThat(dir, both(exists()).and(hasFiles()));
final File[] files = dir.listFiles();
- assertTrue("No files created", files.length > 0);
- boolean found = false;
- for (final File file : files) {
- if (file.getName().endsWith(fileExtension)) {
- found = true;
- break;
- }
- }
- assertTrue("No compressed files found", found);
+ assertNotNull(files);
+ assertThat(files, hasItemInArray(that(hasName(that(endsWith(fileExtension))))));
}
private static void deleteDir() {
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f78a1566/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java
index 84966b1..067724e 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java
@@ -25,6 +25,13 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import static org.apache.logging.log4j.junit.FileMatchers.exists;
+import static org.apache.logging.log4j.junit.FileMatchers.hasFiles;
+import static org.apache.logging.log4j.junit.FileMatchers.hasName;
+import static org.apache.logging.log4j.junit.FileMatchers.that;
+import static org.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.hasItemInArray;
import static org.junit.Assert.*;
/**
@@ -57,17 +64,10 @@ public class RollingAppenderTimeAndSizeTest {
}
Thread.sleep(50);
final File dir = new File(DIR);
- assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
+ assertThat(dir, both(exists()).and(hasFiles()));
final File[] files = dir.listFiles();
- assertTrue("No files created", files.length > 0);
- boolean found = false;
- for (final File file : files) {
- if (file.getName().endsWith(".gz")) {
- found = true;
- break;
- }
- }
- assertTrue("No compressed files found", found);
+ assertNotNull(files);
+ assertThat(files, hasItemInArray(that(hasName(that(endsWith(".gz"))))));
}
private static void deleteDir() {
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f78a1566/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeTest.java
index 1f4b188..fab6cb1 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeTest.java
@@ -20,11 +20,19 @@ import java.io.File;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.junit.InitialLoggerContext;
+import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.rules.RuleChain;
+import static org.apache.logging.log4j.junit.FileMatchers.exists;
+import static org.apache.logging.log4j.junit.FileMatchers.hasFiles;
+import static org.apache.logging.log4j.junit.FileMatchers.hasName;
+import static org.apache.logging.log4j.junit.FileMatchers.that;
+import static org.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.hasItemInArray;
import static org.junit.Assert.*;
/**
@@ -55,16 +63,14 @@ public class RollingAppenderTimeTest {
logger.debug("This is test message number " + i + 1);
}
final File dir = new File(DIR);
- assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
+ assertThat(dir, both(exists()).and(hasFiles()));
final int MAX_TRIES = 20;
+ final Matcher<File[]> hasGzippedFile = hasItemInArray(that(hasName(that(endsWith(".gz")))));
for (int i = 0; i < MAX_TRIES; i++) {
final File[] files = dir.listFiles();
- assertTrue("No files created", files.length > 0);
- for (final File file : files) {
- if (file.getName().endsWith(".gz")) {
- return; // test succeeded
- }
+ if (hasGzippedFile.matches(files)) {
+ return; // test succeeded
}
logger.debug("Adding additional event " + i);
Thread.sleep(100); // Allow time for rollover to complete
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f78a1566/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderUncompressedTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderUncompressedTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderUncompressedTest.java
index d019813..99f23eb 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderUncompressedTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderUncompressedTest.java
@@ -23,10 +23,19 @@ import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.status.StatusLogger;
+import org.hamcrest.Matcher;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
+import static org.apache.logging.log4j.junit.FileMatchers.exists;
+import static org.apache.logging.log4j.junit.FileMatchers.hasFiles;
+import static org.apache.logging.log4j.junit.FileMatchers.hasName;
+import static org.apache.logging.log4j.junit.FileMatchers.that;
+import static org.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.hasItemInArray;
+import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.*;
/**
@@ -62,18 +71,12 @@ public class RollingAppenderUncompressedTest {
logger.debug("This is test message number " + i);
}
final File dir = new File(DIR);
- assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
+ assertThat(dir, both(exists()).and(hasFiles()));
final File[] files = dir.listFiles();
- assertTrue("No files created", files.length > 0);
- boolean found = false;
- for (final File file : files) {
- final String name = file.getName();
- if (name.startsWith("test1") && name.endsWith(".log")) {
- found = true;
- break;
- }
- }
- assertTrue("No archived files found", found);
+ assertNotNull(files);
+ final Matcher<File> withCorrectFileName =
+ both(hasName(that(endsWith(".log")))).and(hasName(that(startsWith("test1"))));
+ assertThat(files, hasItemInArray(withCorrectFileName));
}
private static void deleteDir() {
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f78a1566/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerHeaderFooterTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerHeaderFooterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerHeaderFooterTest.java
index a663329..40134eb 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerHeaderFooterTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerHeaderFooterTest.java
@@ -22,12 +22,16 @@ import java.nio.charset.Charset;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.layout.HtmlLayout;
+import org.apache.logging.log4j.core.util.Closer;
import org.apache.logging.log4j.junit.InitialLoggerContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import static org.apache.logging.log4j.junit.FileMatchers.exists;
+import static org.apache.logging.log4j.junit.FileMatchers.hasFiles;
+import static org.hamcrest.Matchers.both;
import static org.junit.Assert.*;
/**
@@ -61,15 +65,15 @@ public class RollingRandomAccessFileManagerHeaderFooterTest {
}
Thread.sleep(50);
final File dir = new File(DIR);
- assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0);
+ assertThat(dir, both(exists()).and(hasFiles()));
final File[] files = dir.listFiles();
- assertTrue("No files created", files.length > 0);
+ assertNotNull(files);
for (final File file : files) {
assertHeader(file);
assertFooter(file);
}
final File logFile = new File(LOGFILE);
- assertTrue("Expected logfile to exist: " + LOGFILE, logFile.exists());
+ assertThat(logFile, exists());
assertHeader(logFile);
}
@@ -102,10 +106,7 @@ public class RollingRandomAccessFileManagerHeaderFooterTest {
}
return result;
} finally {
- try {
- in.close();
- } catch (final Exception ignored) {
- }
+ Closer.closeSilently(in);
}
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f78a1566/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java
index 140dab0..d1bc1ad 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java
@@ -24,9 +24,19 @@ import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.concurrent.locks.LockSupport;
+import org.apache.logging.log4j.core.util.Closer;
import org.apache.logging.log4j.util.Strings;
import org.junit.Test;
+import static org.apache.logging.log4j.junit.FileMatchers.beforeNow;
+import static org.apache.logging.log4j.junit.FileMatchers.hasLength;
+import static org.apache.logging.log4j.junit.FileMatchers.isEmpty;
+import static org.apache.logging.log4j.junit.FileMatchers.lastModified;
+import static org.hamcrest.Matchers.both;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.lessThan;
+import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.*;
/**
@@ -113,7 +123,7 @@ public class RollingRandomAccessFileManagerTest {
final RollingRandomAccessFileManager manager = new RollingRandomAccessFileManager(raf,
file.getName(), Strings.EMPTY, os, append, flushNow, bufferSize, triggerSize, time,
triggerPolicy, rolloverStrategy, null, null);
-
+
// check the resulting buffer size is what was requested
assertEquals(bufferSize, manager.getBufferSize());
}
@@ -123,7 +133,7 @@ public class RollingRandomAccessFileManagerTest {
final boolean isAppend = true;
final File file = File.createTempFile("log4j2", "test");
file.deleteOnExit();
- assertEquals(0, file.length());
+ assertThat(file, isEmpty());
final byte[] bytes = new byte[4 * 1024];
@@ -134,9 +144,9 @@ public class RollingRandomAccessFileManagerTest {
fos.write(bytes, 0, bytes.length);
fos.flush();
} finally {
- fos.close();
+ Closer.closeSilently(fos);
}
- assertEquals("all flushed to disk", bytes.length, file.length());
+ assertThat("all flushed to disk", file, hasLength(bytes.length));
final RollingRandomAccessFileManager manager = RollingRandomAccessFileManager
.getRollingRandomAccessFileManager(
@@ -146,7 +156,7 @@ public class RollingRandomAccessFileManagerTest {
null, null, null);
manager.write(bytes, 0, bytes.length);
final int expected = bytes.length * 2;
- assertEquals("appended, not overwritten", expected, file.length());
+ assertThat("appended, not overwritten", file, hasLength(expected));
}
@Test
@@ -160,7 +170,7 @@ public class RollingRandomAccessFileManagerTest {
final boolean isAppend = false;
final long expectedMin = System.currentTimeMillis();
final long expectedMax = expectedMin + 500;
- assertTrue(file.lastModified() <= expectedMin);
+ assertThat(file, lastModified(lessThanOrEqualTo(expectedMin)));
final RollingRandomAccessFileManager manager = RollingRandomAccessFileManager
.getRollingRandomAccessFileManager(
@@ -168,8 +178,7 @@ public class RollingRandomAccessFileManagerTest {
file.getAbsolutePath(), Strings.EMPTY, isAppend, true, RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE,
new SizeBasedTriggeringPolicy(Long.MAX_VALUE), //
null, null, null);
- assertTrue(manager.getFileTime() < expectedMax);
- assertTrue(manager.getFileTime() >= expectedMin);
+ assertThat(manager.getFileTime(), both(lessThan(expectedMax)).and(greaterThanOrEqualTo(expectedMin)));
}
@Test
@@ -180,7 +189,7 @@ public class RollingRandomAccessFileManagerTest {
LockSupport.parkNanos(1000000); // 1 millisec
final boolean isAppend = true;
- assertTrue(file.lastModified() <= System.currentTimeMillis());
+ assertThat(file, lastModified(beforeNow()));
final RollingRandomAccessFileManager manager = RollingRandomAccessFileManager
.getRollingRandomAccessFileManager(
@@ -188,7 +197,7 @@ public class RollingRandomAccessFileManagerTest {
file.getAbsolutePath(), Strings.EMPTY, isAppend, true, RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE,
new SizeBasedTriggeringPolicy(Long.MAX_VALUE), //
null, null, null);
- assertEquals(file.lastModified(), manager.getFileTime());
+ assertThat(file, lastModified(equalTo(manager.getFileTime())));
}
}