aboutsummaryrefslogtreecommitdiffstats
path: root/container-accesslogging/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'container-accesslogging/src/test/java')
-rw-r--r--container-accesslogging/src/test/java/com/yahoo/container/logging/AccessLogSamplerTest.java63
-rw-r--r--container-accesslogging/src/test/java/com/yahoo/container/logging/CircularArrayAccessLogKeeperTest.java42
-rw-r--r--container-accesslogging/src/test/java/com/yahoo/container/logging/YApacheLogTestCase.java300
-rw-r--r--container-accesslogging/src/test/java/com/yahoo/container/logging/test/LogFileHandlerTestCase.java224
-rw-r--r--container-accesslogging/src/test/java/com/yahoo/container/logging/test/LogFormatterTestCase.java27
5 files changed, 656 insertions, 0 deletions
diff --git a/container-accesslogging/src/test/java/com/yahoo/container/logging/AccessLogSamplerTest.java b/container-accesslogging/src/test/java/com/yahoo/container/logging/AccessLogSamplerTest.java
new file mode 100644
index 00000000000..33a4935d223
--- /dev/null
+++ b/container-accesslogging/src/test/java/com/yahoo/container/logging/AccessLogSamplerTest.java
@@ -0,0 +1,63 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging;
+
+import org.junit.Test;
+
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.hasItem;
+import static org.hamcrest.CoreMatchers.hasItems;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.contains;
+import static org.junit.Assert.assertThat;
+
+public class AccessLogSamplerTest {
+
+ private final List<String> uris = new ArrayList<>();
+ private CircularArrayAccessLogKeeper circularArrayAccessLogKeeperMock = new CircularArrayAccessLogKeeper() {
+ @Override
+ public void addUri(String uri) {
+ uris.add(uri);
+ }
+ };
+ private AccessLogSampler accessLogSampler = new AccessLogSampler(circularArrayAccessLogKeeperMock);
+
+ @Test
+ public void testAFewLines() {
+ accessLogSampler.log(createLogEntry(200, "/search/foo"));
+ accessLogSampler.log(createLogEntry(500, "/search/bar"));
+ accessLogSampler.log(createLogEntry(500, "bar"));
+ accessLogSampler.log(createLogEntry(200, "/search/what"));
+ assertThat(uris, contains("/search/foo", "/search/what"));
+ }
+
+ @Test
+ public void testSubSampling() {
+ for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) {
+ accessLogSampler.log(createLogEntry(200, "/search/" + String.valueOf(i)));
+ }
+ assertThat(uris.size(), is(CircularArrayAccessLogKeeper.SIZE));
+ assertThat(uris, hasItems("/search/0", "/search/1", "/search/2",
+ "/search/" + String.valueOf(CircularArrayAccessLogKeeper.SIZE - 1)));
+ uris.clear();
+ for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) {
+ accessLogSampler.log(createLogEntry(200, "/search/fuzz"));
+ }
+ assertThat(uris, hasItem("/search/fuzz"));
+ assertThat(uris.size(), is(1));
+ for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) {
+ accessLogSampler.log(createLogEntry(200, "/search/ball"));
+ }
+ assertThat(uris, hasItem("/search/ball"));
+ assertThat(uris.size(), is(2));
+ }
+
+ private AccessLogEntry createLogEntry(int statusCode, String uri) {
+ AccessLogEntry accessLogEntry = new AccessLogEntry();
+ accessLogEntry.setStatusCode(statusCode);
+ accessLogEntry.setURI(URI.create(uri));
+ return accessLogEntry;
+ }
+} \ No newline at end of file
diff --git a/container-accesslogging/src/test/java/com/yahoo/container/logging/CircularArrayAccessLogKeeperTest.java b/container-accesslogging/src/test/java/com/yahoo/container/logging/CircularArrayAccessLogKeeperTest.java
new file mode 100644
index 00000000000..d872f62ea82
--- /dev/null
+++ b/container-accesslogging/src/test/java/com/yahoo/container/logging/CircularArrayAccessLogKeeperTest.java
@@ -0,0 +1,42 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.hasItems;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsCollectionContaining.hasItem;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+
+public class CircularArrayAccessLogKeeperTest {
+ private CircularArrayAccessLogKeeper circularArrayAccessLogKeeper = new CircularArrayAccessLogKeeper();
+
+ @Test
+ public void testSizeIsCroppedCorrectly() {
+ for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE - 1; i++) {
+ circularArrayAccessLogKeeper.addUri(String.valueOf(i));
+ }
+ assertThat(circularArrayAccessLogKeeper.getUris().size(), is(CircularArrayAccessLogKeeper.SIZE -1));
+ circularArrayAccessLogKeeper.addUri("foo");
+ assertThat(circularArrayAccessLogKeeper.getUris().size(), is(CircularArrayAccessLogKeeper.SIZE));
+ circularArrayAccessLogKeeper.addUri("bar");
+ assertThat(circularArrayAccessLogKeeper.getUris().size(), is(CircularArrayAccessLogKeeper.SIZE));
+ assertThat(circularArrayAccessLogKeeper.getUris(), hasItems("1", "2", "3", "foo", "bar"));
+ assertThat(circularArrayAccessLogKeeper.getUris(), not(hasItem("0")));
+ }
+
+ @Test
+ public void testEmpty() {
+ assertThat(circularArrayAccessLogKeeper.getUris().size(), is(0));
+ }
+
+ @Test
+ public void testSomeItems() {
+ circularArrayAccessLogKeeper.addUri("a");
+ circularArrayAccessLogKeeper.addUri("b");
+ circularArrayAccessLogKeeper.addUri("b");
+ assertThat(circularArrayAccessLogKeeper.getUris(), contains("a", "b", "b"));
+ }
+} \ No newline at end of file
diff --git a/container-accesslogging/src/test/java/com/yahoo/container/logging/YApacheLogTestCase.java b/container-accesslogging/src/test/java/com/yahoo/container/logging/YApacheLogTestCase.java
new file mode 100644
index 00000000000..3ac4a45b943
--- /dev/null
+++ b/container-accesslogging/src/test/java/com/yahoo/container/logging/YApacheLogTestCase.java
@@ -0,0 +1,300 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging;
+
+import com.yahoo.container.core.AccessLogConfig;
+import org.hamcrest.MatcherAssert;
+
+import java.io.*;
+import java.net.InetSocketAddress;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.core.IsNot.not;
+import static org.junit.Assert.assertThat;
+
+
+/**
+ * @author tonytv
+ */
+public class YApacheLogTestCase extends junit.framework.TestCase {
+
+ private static String ipAddress = "152.200.54.243";
+ private static final String EMPTY_REFERRER = "";
+ private static final String EMPTY_USERAGENT = "";
+
+ public void testIt() throws Exception {
+ AccessLogEntry entry = new AccessLogEntry();
+ addCommonEntries(entry);
+
+ entry.setAdSpaceID("676817");
+
+ AccessLogEntry.AdInfo ad1 = new AccessLogEntry.AdInfo();
+ AccessLogEntry.AdInfo ad2 = new AccessLogEntry.AdInfo();
+ AccessLogEntry.AdInfo ad3 = new AccessLogEntry.AdInfo();
+
+ ad1.setAdID("134263");
+ ad1.setMatchID("29213.323310.2048738.221486");
+
+ ad2.setAdID("127077");
+ ad2.setMatchID("26036.316814.2030021.40354");
+
+ ad3.setAdID("127611");
+ ad3.setMatchID("26036.330708.2043270.64665");
+
+ entry.addAdInfo(ad1);
+ entry.addAdInfo(ad2);
+ entry.addAdInfo(ad3);
+
+ entry.setUserAgent("Mozilla/4.05 [en] (Win95; I)");
+ entry.setWebfactsDigitalSignature("Wk6_cAzC`4IKP\7&)$");
+
+ entry.setRemoteAddress("17.6.5.4");
+
+ String expectedOutput =
+ "98c836f3" +
+ "36e38385" +
+ "0001dc90" +
+ "00002693" +
+ "/Business/Companies/Financial_Services/Investment_Services/Mutual_Funds/" +
+ "\u0005gMozilla/4.05 [en] (Win95; I)" +
+ "\u0005dWk6_cAzC`4IKP\7&)$" +
+ "\u0005AA17.6.5.4\u0001B12345" +
+ "\u0005b\u0001676817" + //adinfo
+ "\u0002134263" + "\u000329213.323310.2048738.221486" +
+ "\u0002127077" + "\u000326036.316814.2030021.40354" +
+ "\u0002127611" + "\u000326036.330708.2043270.64665";
+
+ assertEquals(expectedOutput, new YApacheFormatter(entry).format());
+ }
+
+ private void addCommonEntries(AccessLogEntry entry) throws URISyntaxException {
+ entry.setIpV4Address(ipAddress);
+ entry.setTimeStamp(920880005L*1000);
+ entry.setDurationBetweenRequestResponse(122);
+ entry.setReturnedContentSize(9875);
+ entry.setURI(new URI("/Business/Companies/Financial_Services/Investment_Services/Mutual_Funds/"));
+ entry.setRemotePort(12345);
+ }
+
+ public void test_remote_address_different_from_ip_address() throws Exception {
+ AccessLogEntry entry = new AccessLogEntry();
+ addCommonEntries(entry);
+
+ entry.setRemoteAddress("FE80:0000:0000:0000:0202:B3FF:FE1E:8329");
+
+ assertEquals("98c836f336e383850001dc9000002693/Business/Companies/Financial_Services/Investment_Services/Mutual_Funds/\u0005AAFE80:0000:0000:0000:0202:B3FF:FE1E:8329\u0001B12345",
+ new YApacheFormatter(entry).format());
+ }
+
+ public void test_remote_address_same_as_ip_address_does_not_cause_double_adding() throws Exception {
+ AccessLogEntry entry = new AccessLogEntry();
+ addCommonEntries(entry);
+ entry.setRemoteAddress(ipAddress);
+
+ assertThat(new YApacheFormatter(entry).format(), not(containsString(ipAddress)));
+ }
+
+ public void test_status_code_stored_as_decimal() throws Exception {
+ AccessLogEntry entry = new AccessLogEntry();
+ addCommonEntries(entry);
+ entry.setStatusCode(404);
+
+ assertThat(new YApacheFormatter(entry).format(), containsString("s404"));
+ }
+
+ /**
+ * author someone-else. Please rewrite this.
+ */
+ public void testYApacheAccessLogWithDateNamingScheme() {
+ AccessLogConfig.Builder builder = new AccessLogConfig.Builder().
+ fileHandler(new AccessLogConfig.FileHandler.Builder().
+ pattern("yapachetest/testaccess.%Y%m%d%H%M%S").
+ symlink("testaccess"));
+ AccessLogConfig config = new AccessLogConfig(builder);
+ YApacheAccessLog accessLog = new YApacheAccessLog(config);
+ try {
+ final AccessLogEntry entry = newAccessLogEntry("hans");
+ accessLog.log(entry);
+
+ // wait for the log writing thread to do all its work, then check it did it right
+
+ // check that symlink appears
+ int waitTimeMs=0;
+ while ( ! new File("yapachetest/testaccess").exists()) {
+ Thread.sleep(2);
+ waitTimeMs+=2;
+ if (waitTimeMs>40*1000)
+ throw new RuntimeException("Waited 40 seconds for the configured symlink to be created, giving up");
+ }
+ // ..and check that the log entry is written
+ waitTimeMs=0;
+ while ( ! containsExpectedLine("00000000000000010000271000000008?query=hans","yapachetest/testaccess",0)) {
+ Thread.sleep(2);
+ waitTimeMs+=2;
+ if (waitTimeMs>40*1000)
+ throw new RuntimeException("Waited 40 seconds for a log file entry to be written, giving up");
+ }
+ }
+ catch (IOException e) {
+ throw new RuntimeException("yapache log io exception",e);
+ }
+ catch (InterruptedException e) {
+ throw new RuntimeException("Interruption",e);
+ }
+ finally {
+ accessLog.shutdown();
+ deleteDirectory("yapachetest");
+ }
+ }
+
+ public void testThatQueryWithEncodedCharactersIsLoggedInEncodedForm() {
+ final String query = "%5E%3B%22";
+ final AccessLogEntry entry = new AccessLogEntry();
+ entry.setURI(newQueryUri(query));
+ assertThat(new YApacheFormatter(entry).format(), containsString(query));
+ }
+
+ private AccessLogEntry newAccessLogEntry(final String query) {
+ final AccessLogEntry entry = new AccessLogEntry();
+ entry.setIpV4Address("0.0.0.0");
+ entry.setUser("user");
+ entry.setHttpMethod("GET");
+ entry.setURI(newQueryUri(query));
+ entry.setHttpVersion("HTTP/1.1");
+ entry.setReferer(EMPTY_REFERRER);
+ entry.setUserAgent(EMPTY_USERAGENT);
+ entry.setRemoteAddress(new InetSocketAddress(0));
+ entry.setTimeStamp(1000);
+ entry.setDurationBetweenRequestResponse(10);
+ entry.setReturnedContentSize(8);
+ entry.setHitCounts(new HitCounts(0, 10, 1234, 0, 10));
+ entry.setStatusCode(200);
+ return entry;
+ }
+
+ /**
+ * author someone-else. Please rewrite this.
+ */
+ public void testYApacheAccessLogWithSequenceNamingScheme() throws IOException, InterruptedException {
+ // try without existing files
+ assertCorrectSequenceBehavior(1);
+
+ // try with existing files
+ try {
+ new File("yapachetest2").mkdir();
+ new File("yapachetest2/access.1").createNewFile();
+ new File("yapachetest2/access.2").createNewFile();
+ assertCorrectSequenceBehavior(3);
+ }
+ finally {
+ deleteDirectory("yapachetest2");
+ }
+ }
+
+ // Prefixes that don't collide with any in the specified log format.
+ private static final char FIELD_KEY_REQUEST_EXTRA = '@';
+ private static final char FIELD_KEY_RESPONSE_EXTRA = '#';
+
+
+ private static List<String> subArrayAsList(final String[] fields, final int fromIndex) {
+ return Arrays.asList(fields).subList(fromIndex, fields.length);
+ }
+
+ private static Map<Character, String> makeFieldMap(final Iterable<String> fields) {
+ final Map<Character, String> fieldMap = new HashMap<>();
+ fields.forEach(field -> {
+ final String existingValue = fieldMap.putIfAbsent(field.charAt(0), field.substring(1));
+ MatcherAssert.assertThat("Attempt to insert field " + field + " would overwrite value", existingValue, is(nullValue()));
+ });
+ return fieldMap;
+ }
+
+ /**
+ * author someone-else. Please rewrite this.
+ */
+ private void assertCorrectSequenceBehavior(int startN) throws IOException, InterruptedException {
+ AccessLogConfig.Builder builder = new AccessLogConfig.Builder().
+ fileHandler(new AccessLogConfig.FileHandler.Builder().
+ pattern("yapachetest2/access").
+ rotateScheme(AccessLogConfig.FileHandler.RotateScheme.Enum.SEQUENCE));
+
+ AccessLogConfig config = new AccessLogConfig(builder);
+ YApacheAccessLog accessLog = new YApacheAccessLog(config);
+ try {
+ // log and rotate trice
+ accessLog.log(newAccessLogEntry("query1"));
+ accessLog.rotateNow();
+ accessLog.log(newAccessLogEntry("query2"));
+ accessLog.rotateNow();
+ accessLog.log(newAccessLogEntry("query3.1"));
+ accessLog.log(newAccessLogEntry("query3.2"));
+ accessLog.rotateNow();
+ accessLog.log(newAccessLogEntry("query4"));
+
+ // wait for the last rotation, which should cause us to have an "access" file containing query4
+ int waitTimeMs=0;
+ while ( ! containsExpectedLine("00000000000000010000271000000008?query=query4","yapachetest2/access",0)) {
+ Thread.sleep(2);
+ waitTimeMs+=2;
+ if (waitTimeMs>40*1000)
+ throw new RuntimeException("Waited 40 seconds for the right log file entry to be written, giving up");
+ }
+
+ // Should now have 3 rotated away files
+ assertTrue(containsExpectedLine("00000000000000010000271000000008?query=query1","yapachetest2/access." + (startN+0),0));
+ assertTrue(containsExpectedLine("00000000000000010000271000000008?query=query2","yapachetest2/access." + (startN+1),0));
+ assertTrue(containsExpectedLine("00000000000000010000271000000008?query=query3.1","yapachetest2/access." + (startN+2),0));
+ assertTrue(containsExpectedLine("00000000000000010000271000000008?query=query3.2","yapachetest2/access." + (startN+2),1));
+ }
+ finally {
+ accessLog.shutdown();
+ deleteDirectory("yapachetest2");
+ }
+ }
+
+ private void deleteDirectory(String name) {
+ File dir=new File(name);
+ if (! dir.exists()) return;
+ for (File f : dir.listFiles())
+ f.delete();
+ dir.delete();
+ }
+
+ /**
+ * Returns whether this file contains this line as the first one.
+ * If line is null: Checks that the file is empty.
+ *
+ * author someone-else. Please rewrite this.
+ */
+ private boolean containsExpectedLine(String line,String file,int lineNumber) throws IOException {
+ BufferedReader reader=null;
+ try {
+ reader=new BufferedReader(new FileReader(file));
+ if (line==null) return reader.readLine()==null;
+ while (lineNumber-- > 0) {
+ String l = reader.readLine();
+ }
+ String l = reader.readLine();
+ return l != null && l.startsWith(line);
+ }
+ catch (FileNotFoundException e) {
+ return false;
+ }
+ finally {
+ if (reader!=null)
+ reader.close();
+ }
+ }
+
+ private static URI newQueryUri(final String query) {
+ return URI.create("http://localhost?query=" + query);
+ }
+
+}
diff --git a/container-accesslogging/src/test/java/com/yahoo/container/logging/test/LogFileHandlerTestCase.java b/container-accesslogging/src/test/java/com/yahoo/container/logging/test/LogFileHandlerTestCase.java
new file mode 100644
index 00000000000..99f1132b7bb
--- /dev/null
+++ b/container-accesslogging/src/test/java/com/yahoo/container/logging/test/LogFileHandlerTestCase.java
@@ -0,0 +1,224 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging.test;
+
+import com.yahoo.container.logging.LogFileHandler;
+
+import java.io.File;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.logging.Formatter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.SimpleFormatter;
+
+/**
+ * @author <a href="mailto:travisb@yahoo-inc.com">Bob Travis</a>
+ */
+// TODO: Make these tests wait until the right things happen rather than waiting for a predetermined time
+// These tests take too long, and are not cleaning up properly. See how this should be done in YApacheLogTestCase
+public class LogFileHandlerTestCase extends junit.framework.TestCase {
+
+ public LogFileHandlerTestCase(String name) {
+ super(name);
+ }
+
+ /**
+ * The scenario
+ */
+ public void testIt() {
+ LogFileHandler h = new LogFileHandler();
+ h.setFilePattern("./logfilehandlertest.%Y%m%d%H%M%S");
+ h.setFormatter(new Formatter() {
+ public String format(LogRecord r) {
+ DateFormat df = new SimpleDateFormat("yyyy.MM.dd:HH:mm:ss.SSS");
+ String timeStamp = df.format(new Date(r.getMillis()));
+ return ("["+timeStamp+"]" + " " + formatMessage(r) + "\n");
+ }
+ } );
+ long now = System.currentTimeMillis();
+ long millisPerDay = 60*60*24*1000;
+ long tomorrowDays = (now / millisPerDay) +1;
+ long tomorrowMillis = tomorrowDays * millisPerDay;
+ assertEquals (tomorrowMillis, h.getNextRotationTime(now));
+ long[] rTimes = {1000, 2000, 10000};
+ h.setRotationTimes(rTimes);
+ assertEquals (tomorrowMillis+1000, h.getNextRotationTime(tomorrowMillis));
+ assertEquals (tomorrowMillis+10000, h.getNextRotationTime(tomorrowMillis+3000));
+ boolean okToWrite = false; // don't want regular unit tests to create tiles....
+ if (okToWrite) {
+ LogRecord lr = new LogRecord(Level.INFO, "test");
+ h.publish(lr);
+ h.publish(new LogRecord(Level.INFO, "another test"));
+ h.rotateNow();
+ h.publish(lr);
+ h.flush();
+ }
+ }
+
+ private boolean delete(String fileOrDir) {
+ File file = new File(fileOrDir);
+ return file.delete();
+ }
+
+ // Feeble attempt to get rid of test dirs and files somewhat more reliably in these poorly written tests
+ private void delete2(String fileOrDir) {
+ File file = new File(fileOrDir);
+ file.deleteOnExit();
+ }
+
+ public void testDeleteFileFirst() {
+ String logFilePattern = "./testLogFileG.txt";
+
+ //delete log file
+ delete(logFilePattern);
+
+ //create logfilehandler
+ LogFileHandler h = new LogFileHandler();
+ h.setFilePattern(logFilePattern);
+ h.setFormatter(new SimpleFormatter());
+ h.setRotationTimes("0 5 ...");
+
+ //write log
+ LogRecord lr = new LogRecord(Level.INFO, "testDeleteFileFirst1");
+ h.publish(lr);
+ h.flush();
+
+ //delete log file
+ delete2(logFilePattern);
+ }
+
+ public void testDeleteDirFirst() {
+ //delete log file and dir
+ delete("./testlogsG/foo/bar/testlog");
+ delete("./testlogsG/foo/bar");
+ delete("./testlogsG/foo");
+ delete("./testlogsG");
+
+ //create logfilehandler
+ LogFileHandler h = new LogFileHandler();
+ h.setFilePattern("./testlogsG/foo/bar/testlog");
+ h.setFormatter(new SimpleFormatter());
+ h.setRotationTimes("0 5 ...");
+
+ //write log
+ LogRecord lr = new LogRecord(Level.INFO, "testDeleteDirFirst1");
+ h.publish(lr);
+ h.flush();
+
+ //delete log file and dir
+ delete2("./testlogsG/foo/bar/testlog");
+ delete2("./testlogsG/foo/bar");
+ delete2("./testlogsG/foo");
+ delete2("./testlogsG");
+ }
+
+ public void testDeleteFileDuringLogging() {
+ String logFilePattern = "./testLogFileG.txt";
+
+ //create logfilehandler
+ LogFileHandler h = new LogFileHandler();
+ h.setFilePattern(logFilePattern);
+ h.setFormatter(new SimpleFormatter());
+ h.setRotationTimes("0 5 ...");
+
+ //write log
+ LogRecord lr = new LogRecord(Level.INFO, "testDeleteFileDuringLogging1");
+ h.publish(lr);
+ h.flush();
+
+ //delete log file
+ delete(logFilePattern);
+
+ //write log again
+ lr = new LogRecord(Level.INFO, "testDeleteFileDuringLogging2");
+ h.publish(lr);
+ h.flush();
+
+ //delete log file
+ delete2(logFilePattern);
+ }
+
+ public void testDeleteDirDuringLogging() {
+ //create logfilehandler
+ LogFileHandler h = new LogFileHandler();
+ h.setFilePattern("./testlogsG/foo/bar/testlog");
+ h.setFormatter(new SimpleFormatter());
+ h.setRotationTimes("0 5 ...");
+
+ //write log
+ LogRecord lr = new LogRecord(Level.INFO, "testDeleteDirDuringLogging1");
+ h.publish(lr);
+ h.flush();
+
+ //delete log file and dir
+ delete("./testlogsG/foo/bar/testlog");
+ delete("./testlogsG/foo/bar");
+ delete("./testlogsG/foo");
+ delete("./testlogsG");
+
+ //write log
+ lr = new LogRecord(Level.INFO, "testDeleteDirDuringLogging2");
+ h.publish(lr);
+ h.flush();
+
+ //delete log file and dir
+ delete2("./testlogsG/foo/bar/testlog");
+ delete2("./testlogsG/foo/bar");
+ delete2("./testlogsG/foo");
+ delete2("./testlogsG");
+ }
+
+ public void testSymlink() {
+ LogFileHandler h = new LogFileHandler();
+ h.setFilePattern("./testlogforsymlinkchecking/logfilehandlertest.%Y%m%d%H%M%S%s");
+ h.setFormatter(new Formatter() {
+ public String format(LogRecord r) {
+ DateFormat df = new SimpleDateFormat("yyyy.MM.dd:HH:mm:ss.SSS");
+ String timeStamp = df.format(new Date(r.getMillis()));
+ return ("["+timeStamp+"]" + " " + formatMessage(r) + "\n");
+ }
+ } );
+ h.setSymlinkName("symlink");
+ LogRecord lr = new LogRecord(Level.INFO, "test");
+ h.publish(lr);
+ String f1 = h.getFileName();
+ try {
+ while (f1 == null) {
+ Thread.sleep(1);
+ f1 = h.getFileName();
+ }
+ h.rotateNow();
+ Thread.sleep(1);
+ String f2 = h.getFileName();
+ while (f1.equals(f2)) {
+ Thread.sleep(1);
+ f2 = h.getFileName();
+ }
+ lr = new LogRecord(Level.INFO, "string which is way longer than the word test");
+ h.publish(lr);
+ Thread.sleep(1000);
+ File f = new File(f1);
+ long first = f.length();
+ f = new File(f2);
+ long second = f.length();
+ final long secondLength = 72;
+ for (int n = 0; n < 20 && second != secondLength; ++n) {
+ Thread.sleep(1000);
+ second = f.length();
+ }
+ f = new File("./testlogforsymlinkchecking", "symlink");
+ long link = f.length();
+ assertEquals(secondLength, link);
+ assertEquals(31, first);
+ assertEquals(secondLength, second);
+ delete2(f2);
+ } catch (InterruptedException e) {
+ // just let the test pass
+ }
+ delete2(f1);
+ delete2("./testlogforsymlinkchecking/symlink");
+ delete2("./testlogforsymlinkchecking");
+ }
+
+}
diff --git a/container-accesslogging/src/test/java/com/yahoo/container/logging/test/LogFormatterTestCase.java b/container-accesslogging/src/test/java/com/yahoo/container/logging/test/LogFormatterTestCase.java
new file mode 100644
index 00000000000..f2051a9f9e0
--- /dev/null
+++ b/container-accesslogging/src/test/java/com/yahoo/container/logging/test/LogFormatterTestCase.java
@@ -0,0 +1,27 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.logging.test;
+
+import java.util.Date;
+
+import com.yahoo.container.logging.LogFormatter;
+
+/**
+ * @author <a href="mailto:travisb@yahoo-inc.com">Bob Travis</a>
+ */
+public class LogFormatterTestCase extends junit.framework.TestCase {
+
+ public LogFormatterTestCase(String name) {
+ super(name);
+ }
+
+ public void testIt() {
+ java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("UTC"));
+ @SuppressWarnings("deprecation")
+ long time = new Date(103,7,25,13,30,35).getTime();
+ String result = LogFormatter.insertDate("test%Y%m%d%H%M%S%x",time);
+ assertEquals("test20030825133035Aug",result);
+ result = LogFormatter.insertDate("test%s%T",time);
+ assertEquals("test000"+time, result);
+ }
+
+}