http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/078f8c87/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java index 14fd4c6..2b5edd7 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java @@ -82,8 +82,8 @@ public class ThrowableProxyTest { } private boolean allLinesContain(final String text, final String containedText) { - String[] lines = text.split("\n"); - for (String line : lines) { + final String[] lines = text.split("\n"); + for (final String line : lines) { if (line.isEmpty()) { continue; } @@ -298,7 +298,7 @@ public class ThrowableProxyTest { final Throwable throwable = new IllegalArgumentException("This is a test"); final ThrowableProxy proxy = new ThrowableProxy(throwable); - String suffix = "some suffix"; + final String suffix = "some suffix"; assertTrue(allLinesContain(proxy.getExtendedStackTraceAsString(suffix), suffix)); } @@ -307,18 +307,18 @@ public class ThrowableProxyTest { final Throwable throwable = new RuntimeException(new IllegalArgumentException("This is a test")); final ThrowableProxy proxy = new ThrowableProxy(throwable); - String suffix = "some suffix"; + final String suffix = "some suffix"; assertTrue(allLinesContain(proxy.getExtendedStackTraceAsString(suffix), suffix)); } @Test public void testSuffix_getExtendedStackTraceAsStringWithSuppressedThrowable() throws Exception { - IllegalArgumentException cause = new IllegalArgumentException("This is a test"); + final IllegalArgumentException cause = new IllegalArgumentException("This is a test"); final Throwable throwable = new RuntimeException(cause); throwable.addSuppressed(new IOException("This is a test")); final ThrowableProxy proxy = new ThrowableProxy(throwable); - String suffix = "some suffix"; + final String suffix = "some suffix"; assertTrue(allLinesContain(proxy.getExtendedStackTraceAsString(suffix), suffix)); } @@ -327,7 +327,7 @@ public class ThrowableProxyTest { final Throwable throwable = new IllegalArgumentException("This is a test"); final ThrowableProxy proxy = new ThrowableProxy(throwable); - String suffix = "some suffix"; + final String suffix = "some suffix"; assertTrue(allLinesContain(proxy.getCauseStackTraceAsString(suffix), suffix)); } @@ -336,18 +336,18 @@ public class ThrowableProxyTest { final Throwable throwable = new RuntimeException(new IllegalArgumentException("This is a test")); final ThrowableProxy proxy = new ThrowableProxy(throwable); - String suffix = "some suffix"; + final String suffix = "some suffix"; assertTrue(allLinesContain(proxy.getCauseStackTraceAsString(suffix), suffix)); } @Test public void testSuffix_getCauseStackTraceAsStringWithSuppressedThrowable() throws Exception { - IllegalArgumentException cause = new IllegalArgumentException("This is a test"); + final IllegalArgumentException cause = new IllegalArgumentException("This is a test"); final Throwable throwable = new RuntimeException(cause); throwable.addSuppressed(new IOException("This is a test")); final ThrowableProxy proxy = new ThrowableProxy(throwable); - String suffix = "some suffix"; + final String suffix = "some suffix"; assertTrue(allLinesContain(proxy.getCauseStackTraceAsString(suffix), suffix)); }
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/078f8c87/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest2.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest2.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest2.java index 64d3118..ed0fb4c 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest2.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest2.java @@ -35,11 +35,11 @@ public class GelfLayoutTest2 { @Test public void gelfLayout() throws IOException { - Logger logger = context.getLogger(); + final Logger logger = context.getLogger(); logger.info("Message"); - String gelf = context.getListAppender("list").getMessages().get(0); - ObjectMapper mapper = new ObjectMapper(); - JsonNode json = mapper.readTree(gelf); + final String gelf = context.getListAppender("list").getMessages().get(0); + final ObjectMapper mapper = new ObjectMapper(); + final JsonNode json = mapper.readTree(gelf); assertEquals("Message", json.get("short_message").asText()); assertEquals("myhost", json.get("host").asText()); assertEquals("FOO", json.get("_foo").asText()); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/078f8c87/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java index d45cf7a..41b7979 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java @@ -74,7 +74,7 @@ public class StrSubstitutorTest { final StrSubstitutor subst = new StrSubstitutor(lookup); ThreadContext.put(TESTKEY, TESTVAL); //String value = subst.replace("${sys:TestKey1:-${ctx:TestKey}}"); - String value = subst.replace("${sys:TestKey1:-${ctx:TestKey}}"); + final String value = subst.replace("${sys:TestKey1:-${ctx:TestKey}}"); assertEquals("TestValue", value); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/078f8c87/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java index 2704d86..4931cf0 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverterTest.java @@ -39,8 +39,8 @@ public class ThrowablePatternConverterTest { } private boolean everyLineEndsWith(final String text, final String suffix) { - String[] lines = text.split(Strings.LINE_SEPARATOR); - for (String line: lines) { + final String[] lines = text.split(Strings.LINE_SEPARATOR); + for (final String line: lines) { if (!line.trim().endsWith(suffix)) { return false; } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/078f8c87/log4j-core/src/test/java/org/apache/logging/log4j/core/util/CronExpressionTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/CronExpressionTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/CronExpressionTest.java index 0212cac..bafdcfa 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/CronExpressionTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/CronExpressionTest.java @@ -76,19 +76,19 @@ public class CronExpressionTest { @Test public void testPrevFireTime1() throws Exception { - CronExpression parser = new CronExpression("0 */15,12 7-11,13-17 L * ?"); - Date date = new GregorianCalendar(2015, 10, 2).getTime(); - Date fireDate = parser.getPrevFireTime(date); - Date expected = new GregorianCalendar(2015, 9, 31, 17, 45, 0).getTime(); + final CronExpression parser = new CronExpression("0 */15,12 7-11,13-17 L * ?"); + final Date date = new GregorianCalendar(2015, 10, 2).getTime(); + final Date fireDate = parser.getPrevFireTime(date); + final Date expected = new GregorianCalendar(2015, 9, 31, 17, 45, 0).getTime(); assertEquals("Dates not equal.", expected, fireDate); } @Test public void testPrevFireTime2() throws Exception { - CronExpression parser = new CronExpression("0 0/5 14,18 * * ?"); - Date date = new GregorianCalendar(2015, 10, 2).getTime(); - Date fireDate = parser.getPrevFireTime(date); - Date expected = new GregorianCalendar(2015, 10, 1, 18, 55, 0).getTime(); + final CronExpression parser = new CronExpression("0 0/5 14,18 * * ?"); + final Date date = new GregorianCalendar(2015, 10, 2).getTime(); + final Date fireDate = parser.getPrevFireTime(date); + final Date expected = new GregorianCalendar(2015, 10, 1, 18, 55, 0).getTime(); assertEquals("Dates not equal.", expected, fireDate); } @@ -97,10 +97,10 @@ public class CronExpressionTest { */ @Test public void testPrevFireTime3() throws Exception { - CronExpression parser = new CronExpression("0 35/10 * * * ?"); - Date date = new GregorianCalendar(2015, 10, 2).getTime(); - Date fireDate = parser.getPrevFireTime(date); - Date expected = new GregorianCalendar(2015, 10, 1, 23, 55, 0).getTime(); + final CronExpression parser = new CronExpression("0 35/10 * * * ?"); + final Date date = new GregorianCalendar(2015, 10, 2).getTime(); + final Date fireDate = parser.getPrevFireTime(date); + final Date expected = new GregorianCalendar(2015, 10, 1, 23, 55, 0).getTime(); assertEquals("Dates not equal.", expected, fireDate); } @@ -110,10 +110,10 @@ public class CronExpressionTest { */ @Test public void testPrevFireTimeTenFifteen() throws Exception { - CronExpression parser = new CronExpression("0 15 10 * * ? *"); - Date date = new GregorianCalendar(2015, 10, 2).getTime(); - Date fireDate = parser.getPrevFireTime(date); - Date expected = new GregorianCalendar(2015, 10, 1, 10, 15, 0).getTime(); + final CronExpression parser = new CronExpression("0 15 10 * * ? *"); + final Date date = new GregorianCalendar(2015, 10, 2).getTime(); + final Date fireDate = parser.getPrevFireTime(date); + final Date expected = new GregorianCalendar(2015, 10, 1, 10, 15, 0).getTime(); assertEquals("Dates not equal.", expected, fireDate); } @@ -122,10 +122,10 @@ public class CronExpressionTest { */ @Test public void testPrevFireTimeTwoPM() throws Exception { - CronExpression parser = new CronExpression("0 * 14 * * ?"); - Date date = new GregorianCalendar(2015, 10, 2).getTime(); - Date fireDate = parser.getPrevFireTime(date); - Date expected = new GregorianCalendar(2015, 10, 1, 14, 59, 0).getTime(); + final CronExpression parser = new CronExpression("0 * 14 * * ?"); + final Date date = new GregorianCalendar(2015, 10, 2).getTime(); + final Date fireDate = parser.getPrevFireTime(date); + final Date expected = new GregorianCalendar(2015, 10, 1, 14, 59, 0).getTime(); assertEquals("Dates not equal.", expected, fireDate); } @@ -134,10 +134,10 @@ public class CronExpressionTest { */ @Test public void testPrevFireTimeMarch() throws Exception { - CronExpression parser = new CronExpression("0 10,44 14 ? 3 WED"); - Date date = new GregorianCalendar(2015, 10, 2).getTime(); - Date fireDate = parser.getPrevFireTime(date); - Date expected = new GregorianCalendar(2015, 2, 25, 14, 44, 0).getTime(); + final CronExpression parser = new CronExpression("0 10,44 14 ? 3 WED"); + final Date date = new GregorianCalendar(2015, 10, 2).getTime(); + final Date fireDate = parser.getPrevFireTime(date); + final Date expected = new GregorianCalendar(2015, 2, 25, 14, 44, 0).getTime(); assertEquals("Dates not equal.", expected, fireDate); } @@ -146,10 +146,10 @@ public class CronExpressionTest { */ @Test public void testPrevFireTimeThirdFriday() throws Exception { - CronExpression parser = new CronExpression("0 15 10 ? * 6#3"); - Date date = new GregorianCalendar(2015, 10, 2).getTime(); - Date fireDate = parser.getPrevFireTime(date); - Date expected = new GregorianCalendar(2015, 9, 16, 10, 15, 0).getTime(); + final CronExpression parser = new CronExpression("0 15 10 ? * 6#3"); + final Date date = new GregorianCalendar(2015, 10, 2).getTime(); + final Date fireDate = parser.getPrevFireTime(date); + final Date expected = new GregorianCalendar(2015, 9, 16, 10, 15, 0).getTime(); assertEquals("Dates not equal.", expected, fireDate); } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/078f8c87/log4j-core/src/test/java/org/apache/logging/log4j/core/util/JsonUtilsTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/JsonUtilsTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/JsonUtilsTest.java index fdfb28e..76e0f5a 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/JsonUtilsTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/JsonUtilsTest.java @@ -29,8 +29,8 @@ public class JsonUtilsTest { @Test public void testQuoteCharSequenceAsString() throws Exception { - StringBuilder output = new StringBuilder(); - StringBuilder builder = new StringBuilder(); + final StringBuilder output = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); builder.append("foobar"); JsonUtils.quoteAsString(builder, output); assertEquals("foobar", output.toString()); @@ -45,14 +45,14 @@ public class JsonUtilsTest { @Test public void testQuoteLongCharSequenceAsString() throws Exception { - StringBuilder output = new StringBuilder(); - StringBuilder input = new StringBuilder(); - StringBuilder sb2 = new StringBuilder(); + final StringBuilder output = new StringBuilder(); + final StringBuilder input = new StringBuilder(); + final StringBuilder sb2 = new StringBuilder(); for (int i = 0; i < 1111; ++i) { input.append('"'); sb2.append("\\\""); } - String exp = sb2.toString(); + final String exp = sb2.toString(); JsonUtils.quoteAsString(input, output); assertEquals(2*input.length(), output.length()); assertEquals(exp, output.toString()); @@ -63,10 +63,10 @@ public class JsonUtilsTest { @Test public void testCharSequenceWithCtrlChars() throws Exception { - char[] input = new char[] { 0, 1, 2, 3, 4 }; - StringBuilder builder = new StringBuilder(); + final char[] input = new char[] { 0, 1, 2, 3, 4 }; + final StringBuilder builder = new StringBuilder(); builder.append(input); - StringBuilder output = new StringBuilder(); + final StringBuilder output = new StringBuilder(); JsonUtils.quoteAsString(builder, output); assertEquals("\\u0000\\u0001\\u0002\\u0003\\u0004", output.toString()); } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/078f8c87/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormatTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormatTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormatTest.java index 183a10c..55e5d59 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormatTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormatTest.java @@ -1,374 +1,374 @@ -/* - * 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.util.datetime; - -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.Locale; -import java.util.TimeZone; -import java.util.concurrent.TimeUnit; - -import org.apache.logging.log4j.core.util.datetime.FixedDateFormat.FixedFormat; -import org.junit.Test; - -import static org.apache.logging.log4j.core.util.datetime.FixedDateFormat.FixedFormat.*; -import static org.junit.Assert.*; - -/** - * Tests {@link FixedDateFormat}. - */ -public class FixedDateFormatTest { - - @Test - public void testFixedFormat_getDatePatternNullIfNoDateInPattern() { - assertNull(FixedFormat.ABSOLUTE.getDatePattern()); - assertNull(FixedFormat.ABSOLUTE_PERIOD.getDatePattern()); - } - - @Test - public void testFixedFormat_getDatePatternLengthZeroIfNoDateInPattern() { - assertEquals(0, FixedFormat.ABSOLUTE.getDatePatternLength()); - assertEquals(0, FixedFormat.ABSOLUTE_PERIOD.getDatePatternLength()); - } - - @Test - public void testFixedFormat_getFastDateFormatNullIfNoDateInPattern() { - assertNull(FixedFormat.ABSOLUTE.getFastDateFormat()); - assertNull(FixedFormat.ABSOLUTE_PERIOD.getFastDateFormat()); - } - - @Test - public void testFixedFormat_getDatePatternReturnsDatePatternIfExists() { - assertEquals("yyyyMMdd", FixedFormat.COMPACT.getDatePattern()); - assertEquals("yyyy-MM-dd ", DEFAULT.getDatePattern()); - } - - @Test - public void testFixedFormat_getDatePatternLengthReturnsDatePatternLength() { - assertEquals("yyyyMMdd".length(), FixedFormat.COMPACT.getDatePatternLength()); - assertEquals("yyyy-MM-dd ".length(), DEFAULT.getDatePatternLength()); - } - - @Test - public void testFixedFormat_getFastDateFormatNonNullIfDateInPattern() { - assertNotNull(FixedFormat.COMPACT.getFastDateFormat()); - assertNotNull(DEFAULT.getFastDateFormat()); - assertEquals("yyyyMMdd", FixedFormat.COMPACT.getFastDateFormat().getPattern()); - assertEquals("yyyy-MM-dd ", DEFAULT.getFastDateFormat().getPattern()); - } - - @Test - public void testCreateIfSupported_nonNullIfNameMatches() { - for (final FixedDateFormat.FixedFormat format : FixedDateFormat.FixedFormat.values()) { - final String[] options = {format.name()}; - assertNotNull(format.name(), FixedDateFormat.createIfSupported(options)); - } - } - - @Test - public void testCreateIfSupported_nonNullIfPatternMatches() { - for (final FixedDateFormat.FixedFormat format : FixedDateFormat.FixedFormat.values()) { - final String[] options = {format.getPattern()}; - assertNotNull(format.name(), FixedDateFormat.createIfSupported(options)); - } - } - - @Test - public void testCreateIfSupported_nullIfNameDoesNotMatch() { - final String[] options = {"DEFAULT3"}; - assertNull("DEFAULT3", FixedDateFormat.createIfSupported(options)); - } - - @Test - public void testCreateIfSupported_nullIfPatternDoesNotMatch() { - final String[] options = {"y M d H m s"}; - assertNull("y M d H m s", FixedDateFormat.createIfSupported(options)); - } - - @Test - public void testCreateIfSupported_defaultIfOptionsArrayNull() { - final FixedDateFormat fmt = FixedDateFormat.createIfSupported((String[]) null); - assertEquals(DEFAULT.getPattern(), fmt.getFormat()); - } - - @Test - public void testCreateIfSupported_defaultIfOptionsArrayEmpty() { - final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new String[0]); - assertEquals(DEFAULT.getPattern(), fmt.getFormat()); - } - - @Test - public void testCreateIfSupported_defaultIfOptionsArrayWithSingleNullElement() { - final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new String[1]); - assertEquals(DEFAULT.getPattern(), fmt.getFormat()); - assertEquals(TimeZone.getDefault(), fmt.getTimeZone()); - } - - @Test - public void testCreateIfSupported_defaultTimeZoneIfOptionsArrayWithSecondNullElement() { - final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new String[] {DEFAULT.getPattern(), null, ""}); - assertEquals(DEFAULT.getPattern(), fmt.getFormat()); - assertEquals(TimeZone.getDefault(), fmt.getTimeZone()); - } - - @Test - public void testCreateIfSupported_customTimeZoneIfOptionsArrayWithTimeZoneElement() { - final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new String[] {DEFAULT.getPattern(), "+08:00", ""}); - assertEquals(DEFAULT.getPattern(), fmt.getFormat()); - assertEquals(TimeZone.getTimeZone("+08:00"), fmt.getTimeZone()); - } - - @Test(expected = NullPointerException.class) - public void testConstructorDisallowsNullFormat() { - new FixedDateFormat(null, TimeZone.getDefault()); - } - - @Test(expected = NullPointerException.class) - public void testConstructorDisallowsNullTimeZone() { - new FixedDateFormat(FixedFormat.ABSOLUTE, null); - } - - @Test - public void testGetFormatReturnsConstructorFixedFormatPattern() { - final FixedDateFormat format = new FixedDateFormat(FixedDateFormat.FixedFormat.ABSOLUTE, TimeZone.getDefault()); - assertSame(FixedDateFormat.FixedFormat.ABSOLUTE.getPattern(), format.getFormat()); - } - - @Test - public void testFormatLong() { - final long now = System.currentTimeMillis(); - final long start = now - TimeUnit.HOURS.toMillis(25); - final long end = now + TimeUnit.HOURS.toMillis(25); - for (final FixedFormat format : FixedFormat.values()) { - final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); - final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); - for (long time = start; time < end; time += 12345) { - final String actual = customTF.format(time); - final String expected = simpleDF.format(new Date(time)); - assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); - } - } - } - - @Test - public void testFormatLong_goingBackInTime() { - final long now = System.currentTimeMillis(); - final long start = now - TimeUnit.HOURS.toMillis(25); - final long end = now + TimeUnit.HOURS.toMillis(25); - for (final FixedFormat format : FixedFormat.values()) { - final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); - final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); - for (long time = end; time > start; time -= 12345) { - final String actual = customTF.format(time); - final String expected = simpleDF.format(new Date(time)); - assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); - } - } - } - - @Test - public void testFormatLongCharArrayInt() { - final long now = System.currentTimeMillis(); - final long start = now - TimeUnit.HOURS.toMillis(25); - final long end = now + TimeUnit.HOURS.toMillis(25); - final char[] buffer = new char[128]; - for (final FixedFormat format : FixedFormat.values()) { - final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); - final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); - for (long time = start; time < end; time += 12345) { - final int length = customTF.format(time, buffer, 23); - final String actual = new String(buffer, 23, length); - final String expected = simpleDF.format(new Date(time)); - assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); - } - } - } - - @Test - public void testFormatLongCharArrayInt_goingBackInTime() { - final long now = System.currentTimeMillis(); - final long start = now - TimeUnit.HOURS.toMillis(25); - final long end = now + TimeUnit.HOURS.toMillis(25); - final char[] buffer = new char[128]; - for (final FixedFormat format : FixedFormat.values()) { - final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); - final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); - for (long time = end; time > start; time -= 12345) { - final int length = customTF.format(time, buffer, 23); - final String actual = new String(buffer, 23, length); - final String expected = simpleDF.format(new Date(time)); - assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); - } - } - } - - @Test - public void testDaylightSavingToSummerTime() throws Exception { - final Calendar calendar = Calendar.getInstance(); - calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse("2017-03-12 00:00:00 UTC")); - - final SimpleDateFormat usCentral = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.US); - usCentral.setTimeZone(TimeZone.getTimeZone("US/Central")); - - final SimpleDateFormat utc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.US); - utc.setTimeZone(TimeZone.getTimeZone("UTC")); - - final FixedDateFormat fixedUsCentral = FixedDateFormat.create(DEFAULT, TimeZone.getTimeZone("US/Central")); - final FixedDateFormat fixedUtc = FixedDateFormat.create(DEFAULT, TimeZone.getTimeZone("UTC")); - - final String[][] expectedDstAndNoDst = { - // US/Central, UTC - { "2017-03-11 18:00:00,000", "2017-03-12 00:00:00,000" }, // - { "2017-03-11 19:00:00,000", "2017-03-12 01:00:00,000" }, // - { "2017-03-11 20:00:00,000", "2017-03-12 02:00:00,000" }, // - { "2017-03-11 21:00:00,000", "2017-03-12 03:00:00,000" }, // - { "2017-03-11 22:00:00,000", "2017-03-12 04:00:00,000" }, // - { "2017-03-11 23:00:00,000", "2017-03-12 05:00:00,000" }, // - { "2017-03-12 00:00:00,000", "2017-03-12 06:00:00,000" }, // - { "2017-03-12 01:00:00,000", "2017-03-12 07:00:00,000" }, // - { "2017-03-12 03:00:00,000", "2017-03-12 08:00:00,000" }, // DST jump at 2am US central time - { "2017-03-12 04:00:00,000", "2017-03-12 09:00:00,000" }, // - { "2017-03-12 05:00:00,000", "2017-03-12 10:00:00,000" }, // - { "2017-03-12 06:00:00,000", "2017-03-12 11:00:00,000" }, // - { "2017-03-12 07:00:00,000", "2017-03-12 12:00:00,000" }, // - { "2017-03-12 08:00:00,000", "2017-03-12 13:00:00,000" }, // - { "2017-03-12 09:00:00,000", "2017-03-12 14:00:00,000" }, // - { "2017-03-12 10:00:00,000", "2017-03-12 15:00:00,000" }, // - { "2017-03-12 11:00:00,000", "2017-03-12 16:00:00,000" }, // - { "2017-03-12 12:00:00,000", "2017-03-12 17:00:00,000" }, // - { "2017-03-12 13:00:00,000", "2017-03-12 18:00:00,000" }, // - { "2017-03-12 14:00:00,000", "2017-03-12 19:00:00,000" }, // - { "2017-03-12 15:00:00,000", "2017-03-12 20:00:00,000" }, // - { "2017-03-12 16:00:00,000", "2017-03-12 21:00:00,000" }, // - { "2017-03-12 17:00:00,000", "2017-03-12 22:00:00,000" }, // - { "2017-03-12 18:00:00,000", "2017-03-12 23:00:00,000" }, // 24 - { "2017-03-12 19:00:00,000", "2017-03-13 00:00:00,000" }, // - { "2017-03-12 20:00:00,000", "2017-03-13 01:00:00,000" }, // - { "2017-03-12 21:00:00,000", "2017-03-13 02:00:00,000" }, // - { "2017-03-12 22:00:00,000", "2017-03-13 03:00:00,000" }, // - { "2017-03-12 23:00:00,000", "2017-03-13 04:00:00,000" }, // - { "2017-03-13 00:00:00,000", "2017-03-13 05:00:00,000" }, // - { "2017-03-13 01:00:00,000", "2017-03-13 06:00:00,000" }, // - { "2017-03-13 02:00:00,000", "2017-03-13 07:00:00,000" }, // - { "2017-03-13 03:00:00,000", "2017-03-13 08:00:00,000" }, // - { "2017-03-13 04:00:00,000", "2017-03-13 09:00:00,000" }, // - { "2017-03-13 05:00:00,000", "2017-03-13 10:00:00,000" }, // - { "2017-03-13 06:00:00,000", "2017-03-13 11:00:00,000" }, // - }; - - TimeZone tz = TimeZone.getTimeZone("US/Central"); - for (int i = 0; i < 36; i++) { - final Date date = calendar.getTime(); - assertEquals("SimpleDateFormat TZ=US Central", expectedDstAndNoDst[i][0], usCentral.format(date)); - assertEquals("SimpleDateFormat TZ=UTC", expectedDstAndNoDst[i][1], utc.format(date)); - assertEquals("FixedDateFormat TZ=US Central", expectedDstAndNoDst[i][0], fixedUsCentral.format(date.getTime())); - assertEquals("FixedDateFormat TZ=UTC", expectedDstAndNoDst[i][1], fixedUtc.format(date.getTime())); - calendar.add(Calendar.HOUR_OF_DAY, 1); - } - } - - @Test - public void testDaylightSavingToWinterTime() throws Exception { - final Calendar calendar = Calendar.getInstance(); - calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse("2017-11-05 00:00:00 UTC")); - - final SimpleDateFormat usCentral = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.US); - usCentral.setTimeZone(TimeZone.getTimeZone("US/Central")); - - final SimpleDateFormat utc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.US); - utc.setTimeZone(TimeZone.getTimeZone("UTC")); - - final FixedDateFormat fixedUsCentral = FixedDateFormat.create(DEFAULT, TimeZone.getTimeZone("US/Central")); - final FixedDateFormat fixedUtc = FixedDateFormat.create(DEFAULT, TimeZone.getTimeZone("UTC")); - - final String[][] expectedDstAndNoDst = { - // US/Central, UTC - { "2017-11-04 19:00:00,000", "2017-11-05 00:00:00,000" }, // - { "2017-11-04 20:00:00,000", "2017-11-05 01:00:00,000" }, // - { "2017-11-04 21:00:00,000", "2017-11-05 02:00:00,000" }, // - { "2017-11-04 22:00:00,000", "2017-11-05 03:00:00,000" }, // - { "2017-11-04 23:00:00,000", "2017-11-05 04:00:00,000" }, // - { "2017-11-05 00:00:00,000", "2017-11-05 05:00:00,000" }, // - { "2017-11-05 01:00:00,000", "2017-11-05 06:00:00,000" }, // DST jump at 2am US central time - { "2017-11-05 01:00:00,000", "2017-11-05 07:00:00,000" }, // - { "2017-11-05 02:00:00,000", "2017-11-05 08:00:00,000" }, // - { "2017-11-05 03:00:00,000", "2017-11-05 09:00:00,000" }, // - { "2017-11-05 04:00:00,000", "2017-11-05 10:00:00,000" }, // - { "2017-11-05 05:00:00,000", "2017-11-05 11:00:00,000" }, // - { "2017-11-05 06:00:00,000", "2017-11-05 12:00:00,000" }, // - { "2017-11-05 07:00:00,000", "2017-11-05 13:00:00,000" }, // - { "2017-11-05 08:00:00,000", "2017-11-05 14:00:00,000" }, // - { "2017-11-05 09:00:00,000", "2017-11-05 15:00:00,000" }, // - { "2017-11-05 10:00:00,000", "2017-11-05 16:00:00,000" }, // - { "2017-11-05 11:00:00,000", "2017-11-05 17:00:00,000" }, // - { "2017-11-05 12:00:00,000", "2017-11-05 18:00:00,000" }, // - { "2017-11-05 13:00:00,000", "2017-11-05 19:00:00,000" }, // - { "2017-11-05 14:00:00,000", "2017-11-05 20:00:00,000" }, // - { "2017-11-05 15:00:00,000", "2017-11-05 21:00:00,000" }, // - { "2017-11-05 16:00:00,000", "2017-11-05 22:00:00,000" }, // - { "2017-11-05 17:00:00,000", "2017-11-05 23:00:00,000" }, // 24 - { "2017-11-05 18:00:00,000", "2017-11-06 00:00:00,000" }, // - { "2017-11-05 19:00:00,000", "2017-11-06 01:00:00,000" }, // - { "2017-11-05 20:00:00,000", "2017-11-06 02:00:00,000" }, // - { "2017-11-05 21:00:00,000", "2017-11-06 03:00:00,000" }, // - { "2017-11-05 22:00:00,000", "2017-11-06 04:00:00,000" }, // - { "2017-11-05 23:00:00,000", "2017-11-06 05:00:00,000" }, // - { "2017-11-06 00:00:00,000", "2017-11-06 06:00:00,000" }, // - { "2017-11-06 01:00:00,000", "2017-11-06 07:00:00,000" }, // - { "2017-11-06 02:00:00,000", "2017-11-06 08:00:00,000" }, // - { "2017-11-06 03:00:00,000", "2017-11-06 09:00:00,000" }, // - { "2017-11-06 04:00:00,000", "2017-11-06 10:00:00,000" }, // - { "2017-11-06 05:00:00,000", "2017-11-06 11:00:00,000" }, // - }; - - TimeZone tz = TimeZone.getTimeZone("US/Central"); - for (int i = 0; i < 36; i++) { - final Date date = calendar.getTime(); - //System.out.println(usCentral.format(date) + ", Fixed: " + fixedUsCentral.format(date.getTime()) + ", utc: " + utc.format(date)); - assertEquals("SimpleDateFormat TZ=US Central", expectedDstAndNoDst[i][0], usCentral.format(date)); - assertEquals("SimpleDateFormat TZ=UTC", expectedDstAndNoDst[i][1], utc.format(date)); - assertEquals("FixedDateFormat TZ=US Central", expectedDstAndNoDst[i][0], fixedUsCentral.format(date.getTime())); - assertEquals("FixedDateFormat TZ=UTC", expectedDstAndNoDst[i][1], fixedUtc.format(date.getTime())); - calendar.add(Calendar.HOUR_OF_DAY, 1); - } - } - /** - * This test case validates date pattern before and after DST - * Base Date : 12 Mar 2017 - * Daylight Savings started on : 02:00 AM - */ - @Test - public void testFormatLong_goingBackInTime_DST() { - final Calendar instance = Calendar.getInstance(TimeZone.getTimeZone("EST")); - instance.set(2017, 2, 12, 2, 0); - final long now = instance.getTimeInMillis(); - final long start = now - TimeUnit.HOURS.toMillis(1); - final long end = now + TimeUnit.HOURS.toMillis(1); - - for (final FixedFormat format : FixedFormat.values()) { - final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); - final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); - for (long time = end; time > start; time -= 12345) { - final String actual = customTF.format(time); - final String expected = simpleDF.format(new Date(time)); - assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); - } - } - } -} +/* + * 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.util.datetime; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; +import java.util.concurrent.TimeUnit; + +import org.apache.logging.log4j.core.util.datetime.FixedDateFormat.FixedFormat; +import org.junit.Test; + +import static org.apache.logging.log4j.core.util.datetime.FixedDateFormat.FixedFormat.*; +import static org.junit.Assert.*; + +/** + * Tests {@link FixedDateFormat}. + */ +public class FixedDateFormatTest { + + @Test + public void testFixedFormat_getDatePatternNullIfNoDateInPattern() { + assertNull(FixedFormat.ABSOLUTE.getDatePattern()); + assertNull(FixedFormat.ABSOLUTE_PERIOD.getDatePattern()); + } + + @Test + public void testFixedFormat_getDatePatternLengthZeroIfNoDateInPattern() { + assertEquals(0, FixedFormat.ABSOLUTE.getDatePatternLength()); + assertEquals(0, FixedFormat.ABSOLUTE_PERIOD.getDatePatternLength()); + } + + @Test + public void testFixedFormat_getFastDateFormatNullIfNoDateInPattern() { + assertNull(FixedFormat.ABSOLUTE.getFastDateFormat()); + assertNull(FixedFormat.ABSOLUTE_PERIOD.getFastDateFormat()); + } + + @Test + public void testFixedFormat_getDatePatternReturnsDatePatternIfExists() { + assertEquals("yyyyMMdd", FixedFormat.COMPACT.getDatePattern()); + assertEquals("yyyy-MM-dd ", DEFAULT.getDatePattern()); + } + + @Test + public void testFixedFormat_getDatePatternLengthReturnsDatePatternLength() { + assertEquals("yyyyMMdd".length(), FixedFormat.COMPACT.getDatePatternLength()); + assertEquals("yyyy-MM-dd ".length(), DEFAULT.getDatePatternLength()); + } + + @Test + public void testFixedFormat_getFastDateFormatNonNullIfDateInPattern() { + assertNotNull(FixedFormat.COMPACT.getFastDateFormat()); + assertNotNull(DEFAULT.getFastDateFormat()); + assertEquals("yyyyMMdd", FixedFormat.COMPACT.getFastDateFormat().getPattern()); + assertEquals("yyyy-MM-dd ", DEFAULT.getFastDateFormat().getPattern()); + } + + @Test + public void testCreateIfSupported_nonNullIfNameMatches() { + for (final FixedDateFormat.FixedFormat format : FixedDateFormat.FixedFormat.values()) { + final String[] options = {format.name()}; + assertNotNull(format.name(), FixedDateFormat.createIfSupported(options)); + } + } + + @Test + public void testCreateIfSupported_nonNullIfPatternMatches() { + for (final FixedDateFormat.FixedFormat format : FixedDateFormat.FixedFormat.values()) { + final String[] options = {format.getPattern()}; + assertNotNull(format.name(), FixedDateFormat.createIfSupported(options)); + } + } + + @Test + public void testCreateIfSupported_nullIfNameDoesNotMatch() { + final String[] options = {"DEFAULT3"}; + assertNull("DEFAULT3", FixedDateFormat.createIfSupported(options)); + } + + @Test + public void testCreateIfSupported_nullIfPatternDoesNotMatch() { + final String[] options = {"y M d H m s"}; + assertNull("y M d H m s", FixedDateFormat.createIfSupported(options)); + } + + @Test + public void testCreateIfSupported_defaultIfOptionsArrayNull() { + final FixedDateFormat fmt = FixedDateFormat.createIfSupported((String[]) null); + assertEquals(DEFAULT.getPattern(), fmt.getFormat()); + } + + @Test + public void testCreateIfSupported_defaultIfOptionsArrayEmpty() { + final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new String[0]); + assertEquals(DEFAULT.getPattern(), fmt.getFormat()); + } + + @Test + public void testCreateIfSupported_defaultIfOptionsArrayWithSingleNullElement() { + final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new String[1]); + assertEquals(DEFAULT.getPattern(), fmt.getFormat()); + assertEquals(TimeZone.getDefault(), fmt.getTimeZone()); + } + + @Test + public void testCreateIfSupported_defaultTimeZoneIfOptionsArrayWithSecondNullElement() { + final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new String[] {DEFAULT.getPattern(), null, ""}); + assertEquals(DEFAULT.getPattern(), fmt.getFormat()); + assertEquals(TimeZone.getDefault(), fmt.getTimeZone()); + } + + @Test + public void testCreateIfSupported_customTimeZoneIfOptionsArrayWithTimeZoneElement() { + final FixedDateFormat fmt = FixedDateFormat.createIfSupported(new String[] {DEFAULT.getPattern(), "+08:00", ""}); + assertEquals(DEFAULT.getPattern(), fmt.getFormat()); + assertEquals(TimeZone.getTimeZone("+08:00"), fmt.getTimeZone()); + } + + @Test(expected = NullPointerException.class) + public void testConstructorDisallowsNullFormat() { + new FixedDateFormat(null, TimeZone.getDefault()); + } + + @Test(expected = NullPointerException.class) + public void testConstructorDisallowsNullTimeZone() { + new FixedDateFormat(FixedFormat.ABSOLUTE, null); + } + + @Test + public void testGetFormatReturnsConstructorFixedFormatPattern() { + final FixedDateFormat format = new FixedDateFormat(FixedDateFormat.FixedFormat.ABSOLUTE, TimeZone.getDefault()); + assertSame(FixedDateFormat.FixedFormat.ABSOLUTE.getPattern(), format.getFormat()); + } + + @Test + public void testFormatLong() { + final long now = System.currentTimeMillis(); + final long start = now - TimeUnit.HOURS.toMillis(25); + final long end = now + TimeUnit.HOURS.toMillis(25); + for (final FixedFormat format : FixedFormat.values()) { + final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); + final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); + for (long time = start; time < end; time += 12345) { + final String actual = customTF.format(time); + final String expected = simpleDF.format(new Date(time)); + assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); + } + } + } + + @Test + public void testFormatLong_goingBackInTime() { + final long now = System.currentTimeMillis(); + final long start = now - TimeUnit.HOURS.toMillis(25); + final long end = now + TimeUnit.HOURS.toMillis(25); + for (final FixedFormat format : FixedFormat.values()) { + final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); + final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); + for (long time = end; time > start; time -= 12345) { + final String actual = customTF.format(time); + final String expected = simpleDF.format(new Date(time)); + assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); + } + } + } + + @Test + public void testFormatLongCharArrayInt() { + final long now = System.currentTimeMillis(); + final long start = now - TimeUnit.HOURS.toMillis(25); + final long end = now + TimeUnit.HOURS.toMillis(25); + final char[] buffer = new char[128]; + for (final FixedFormat format : FixedFormat.values()) { + final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); + final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); + for (long time = start; time < end; time += 12345) { + final int length = customTF.format(time, buffer, 23); + final String actual = new String(buffer, 23, length); + final String expected = simpleDF.format(new Date(time)); + assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); + } + } + } + + @Test + public void testFormatLongCharArrayInt_goingBackInTime() { + final long now = System.currentTimeMillis(); + final long start = now - TimeUnit.HOURS.toMillis(25); + final long end = now + TimeUnit.HOURS.toMillis(25); + final char[] buffer = new char[128]; + for (final FixedFormat format : FixedFormat.values()) { + final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); + final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); + for (long time = end; time > start; time -= 12345) { + final int length = customTF.format(time, buffer, 23); + final String actual = new String(buffer, 23, length); + final String expected = simpleDF.format(new Date(time)); + assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); + } + } + } + + @Test + public void testDaylightSavingToSummerTime() throws Exception { + final Calendar calendar = Calendar.getInstance(); + calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse("2017-03-12 00:00:00 UTC")); + + final SimpleDateFormat usCentral = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.US); + usCentral.setTimeZone(TimeZone.getTimeZone("US/Central")); + + final SimpleDateFormat utc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.US); + utc.setTimeZone(TimeZone.getTimeZone("UTC")); + + final FixedDateFormat fixedUsCentral = FixedDateFormat.create(DEFAULT, TimeZone.getTimeZone("US/Central")); + final FixedDateFormat fixedUtc = FixedDateFormat.create(DEFAULT, TimeZone.getTimeZone("UTC")); + + final String[][] expectedDstAndNoDst = { + // US/Central, UTC + { "2017-03-11 18:00:00,000", "2017-03-12 00:00:00,000" }, // + { "2017-03-11 19:00:00,000", "2017-03-12 01:00:00,000" }, // + { "2017-03-11 20:00:00,000", "2017-03-12 02:00:00,000" }, // + { "2017-03-11 21:00:00,000", "2017-03-12 03:00:00,000" }, // + { "2017-03-11 22:00:00,000", "2017-03-12 04:00:00,000" }, // + { "2017-03-11 23:00:00,000", "2017-03-12 05:00:00,000" }, // + { "2017-03-12 00:00:00,000", "2017-03-12 06:00:00,000" }, // + { "2017-03-12 01:00:00,000", "2017-03-12 07:00:00,000" }, // + { "2017-03-12 03:00:00,000", "2017-03-12 08:00:00,000" }, // DST jump at 2am US central time + { "2017-03-12 04:00:00,000", "2017-03-12 09:00:00,000" }, // + { "2017-03-12 05:00:00,000", "2017-03-12 10:00:00,000" }, // + { "2017-03-12 06:00:00,000", "2017-03-12 11:00:00,000" }, // + { "2017-03-12 07:00:00,000", "2017-03-12 12:00:00,000" }, // + { "2017-03-12 08:00:00,000", "2017-03-12 13:00:00,000" }, // + { "2017-03-12 09:00:00,000", "2017-03-12 14:00:00,000" }, // + { "2017-03-12 10:00:00,000", "2017-03-12 15:00:00,000" }, // + { "2017-03-12 11:00:00,000", "2017-03-12 16:00:00,000" }, // + { "2017-03-12 12:00:00,000", "2017-03-12 17:00:00,000" }, // + { "2017-03-12 13:00:00,000", "2017-03-12 18:00:00,000" }, // + { "2017-03-12 14:00:00,000", "2017-03-12 19:00:00,000" }, // + { "2017-03-12 15:00:00,000", "2017-03-12 20:00:00,000" }, // + { "2017-03-12 16:00:00,000", "2017-03-12 21:00:00,000" }, // + { "2017-03-12 17:00:00,000", "2017-03-12 22:00:00,000" }, // + { "2017-03-12 18:00:00,000", "2017-03-12 23:00:00,000" }, // 24 + { "2017-03-12 19:00:00,000", "2017-03-13 00:00:00,000" }, // + { "2017-03-12 20:00:00,000", "2017-03-13 01:00:00,000" }, // + { "2017-03-12 21:00:00,000", "2017-03-13 02:00:00,000" }, // + { "2017-03-12 22:00:00,000", "2017-03-13 03:00:00,000" }, // + { "2017-03-12 23:00:00,000", "2017-03-13 04:00:00,000" }, // + { "2017-03-13 00:00:00,000", "2017-03-13 05:00:00,000" }, // + { "2017-03-13 01:00:00,000", "2017-03-13 06:00:00,000" }, // + { "2017-03-13 02:00:00,000", "2017-03-13 07:00:00,000" }, // + { "2017-03-13 03:00:00,000", "2017-03-13 08:00:00,000" }, // + { "2017-03-13 04:00:00,000", "2017-03-13 09:00:00,000" }, // + { "2017-03-13 05:00:00,000", "2017-03-13 10:00:00,000" }, // + { "2017-03-13 06:00:00,000", "2017-03-13 11:00:00,000" }, // + }; + + final TimeZone tz = TimeZone.getTimeZone("US/Central"); + for (int i = 0; i < 36; i++) { + final Date date = calendar.getTime(); + assertEquals("SimpleDateFormat TZ=US Central", expectedDstAndNoDst[i][0], usCentral.format(date)); + assertEquals("SimpleDateFormat TZ=UTC", expectedDstAndNoDst[i][1], utc.format(date)); + assertEquals("FixedDateFormat TZ=US Central", expectedDstAndNoDst[i][0], fixedUsCentral.format(date.getTime())); + assertEquals("FixedDateFormat TZ=UTC", expectedDstAndNoDst[i][1], fixedUtc.format(date.getTime())); + calendar.add(Calendar.HOUR_OF_DAY, 1); + } + } + + @Test + public void testDaylightSavingToWinterTime() throws Exception { + final Calendar calendar = Calendar.getInstance(); + calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse("2017-11-05 00:00:00 UTC")); + + final SimpleDateFormat usCentral = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.US); + usCentral.setTimeZone(TimeZone.getTimeZone("US/Central")); + + final SimpleDateFormat utc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.US); + utc.setTimeZone(TimeZone.getTimeZone("UTC")); + + final FixedDateFormat fixedUsCentral = FixedDateFormat.create(DEFAULT, TimeZone.getTimeZone("US/Central")); + final FixedDateFormat fixedUtc = FixedDateFormat.create(DEFAULT, TimeZone.getTimeZone("UTC")); + + final String[][] expectedDstAndNoDst = { + // US/Central, UTC + { "2017-11-04 19:00:00,000", "2017-11-05 00:00:00,000" }, // + { "2017-11-04 20:00:00,000", "2017-11-05 01:00:00,000" }, // + { "2017-11-04 21:00:00,000", "2017-11-05 02:00:00,000" }, // + { "2017-11-04 22:00:00,000", "2017-11-05 03:00:00,000" }, // + { "2017-11-04 23:00:00,000", "2017-11-05 04:00:00,000" }, // + { "2017-11-05 00:00:00,000", "2017-11-05 05:00:00,000" }, // + { "2017-11-05 01:00:00,000", "2017-11-05 06:00:00,000" }, // DST jump at 2am US central time + { "2017-11-05 01:00:00,000", "2017-11-05 07:00:00,000" }, // + { "2017-11-05 02:00:00,000", "2017-11-05 08:00:00,000" }, // + { "2017-11-05 03:00:00,000", "2017-11-05 09:00:00,000" }, // + { "2017-11-05 04:00:00,000", "2017-11-05 10:00:00,000" }, // + { "2017-11-05 05:00:00,000", "2017-11-05 11:00:00,000" }, // + { "2017-11-05 06:00:00,000", "2017-11-05 12:00:00,000" }, // + { "2017-11-05 07:00:00,000", "2017-11-05 13:00:00,000" }, // + { "2017-11-05 08:00:00,000", "2017-11-05 14:00:00,000" }, // + { "2017-11-05 09:00:00,000", "2017-11-05 15:00:00,000" }, // + { "2017-11-05 10:00:00,000", "2017-11-05 16:00:00,000" }, // + { "2017-11-05 11:00:00,000", "2017-11-05 17:00:00,000" }, // + { "2017-11-05 12:00:00,000", "2017-11-05 18:00:00,000" }, // + { "2017-11-05 13:00:00,000", "2017-11-05 19:00:00,000" }, // + { "2017-11-05 14:00:00,000", "2017-11-05 20:00:00,000" }, // + { "2017-11-05 15:00:00,000", "2017-11-05 21:00:00,000" }, // + { "2017-11-05 16:00:00,000", "2017-11-05 22:00:00,000" }, // + { "2017-11-05 17:00:00,000", "2017-11-05 23:00:00,000" }, // 24 + { "2017-11-05 18:00:00,000", "2017-11-06 00:00:00,000" }, // + { "2017-11-05 19:00:00,000", "2017-11-06 01:00:00,000" }, // + { "2017-11-05 20:00:00,000", "2017-11-06 02:00:00,000" }, // + { "2017-11-05 21:00:00,000", "2017-11-06 03:00:00,000" }, // + { "2017-11-05 22:00:00,000", "2017-11-06 04:00:00,000" }, // + { "2017-11-05 23:00:00,000", "2017-11-06 05:00:00,000" }, // + { "2017-11-06 00:00:00,000", "2017-11-06 06:00:00,000" }, // + { "2017-11-06 01:00:00,000", "2017-11-06 07:00:00,000" }, // + { "2017-11-06 02:00:00,000", "2017-11-06 08:00:00,000" }, // + { "2017-11-06 03:00:00,000", "2017-11-06 09:00:00,000" }, // + { "2017-11-06 04:00:00,000", "2017-11-06 10:00:00,000" }, // + { "2017-11-06 05:00:00,000", "2017-11-06 11:00:00,000" }, // + }; + + final TimeZone tz = TimeZone.getTimeZone("US/Central"); + for (int i = 0; i < 36; i++) { + final Date date = calendar.getTime(); + //System.out.println(usCentral.format(date) + ", Fixed: " + fixedUsCentral.format(date.getTime()) + ", utc: " + utc.format(date)); + assertEquals("SimpleDateFormat TZ=US Central", expectedDstAndNoDst[i][0], usCentral.format(date)); + assertEquals("SimpleDateFormat TZ=UTC", expectedDstAndNoDst[i][1], utc.format(date)); + assertEquals("FixedDateFormat TZ=US Central", expectedDstAndNoDst[i][0], fixedUsCentral.format(date.getTime())); + assertEquals("FixedDateFormat TZ=UTC", expectedDstAndNoDst[i][1], fixedUtc.format(date.getTime())); + calendar.add(Calendar.HOUR_OF_DAY, 1); + } + } + /** + * This test case validates date pattern before and after DST + * Base Date : 12 Mar 2017 + * Daylight Savings started on : 02:00 AM + */ + @Test + public void testFormatLong_goingBackInTime_DST() { + final Calendar instance = Calendar.getInstance(TimeZone.getTimeZone("EST")); + instance.set(2017, 2, 12, 2, 0); + final long now = instance.getTimeInMillis(); + final long start = now - TimeUnit.HOURS.toMillis(1); + final long end = now + TimeUnit.HOURS.toMillis(1); + + for (final FixedFormat format : FixedFormat.values()) { + final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); + final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); + for (long time = end; time > start; time -= 12345) { + final String actual = customTF.format(time); + final String expected = simpleDF.format(new Date(time)); + assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); + } + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/078f8c87/log4j-nosql/src/test/java/org/apache/logging/log4j/nosql/appender/cassandra/CassandraRule.java ---------------------------------------------------------------------- diff --git a/log4j-nosql/src/test/java/org/apache/logging/log4j/nosql/appender/cassandra/CassandraRule.java b/log4j-nosql/src/test/java/org/apache/logging/log4j/nosql/appender/cassandra/CassandraRule.java index 02e2b74..36e2672 100644 --- a/log4j-nosql/src/test/java/org/apache/logging/log4j/nosql/appender/cassandra/CassandraRule.java +++ b/log4j-nosql/src/test/java/org/apache/logging/log4j/nosql/appender/cassandra/CassandraRule.java @@ -107,12 +107,12 @@ public class CassandraRule extends ExternalResource { } private void cancelOnWindows() { - SecurityManager currentSecurityManager = System.getSecurityManager(); + final SecurityManager currentSecurityManager = System.getSecurityManager(); try { final SecurityManager securityManager = new SecurityManager() { @Override public void checkPermission(final Permission permission) { - String permissionName = permission.getName(); + final String permissionName = permission.getName(); if (permissionName != null && permissionName.startsWith("exitVM")) { throw new SecurityException("test"); } @@ -120,7 +120,7 @@ public class CassandraRule extends ExternalResource { }; System.setSecurityManager(securityManager); daemon.stop(); - } catch (SecurityException ex) { + } catch (final SecurityException ex) { // ignore } finally { System.setSecurityManager(currentSecurityManager); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/078f8c87/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/Log4jLogEventBenchmark.java ---------------------------------------------------------------------- diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/Log4jLogEventBenchmark.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/Log4jLogEventBenchmark.java index 9326dbe..4c89f55 100644 --- a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/Log4jLogEventBenchmark.java +++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/Log4jLogEventBenchmark.java @@ -72,7 +72,7 @@ public class Log4jLogEventBenchmark { @Benchmark public Serializable createSerializableLogEventProxyWithoutException(final Blackhole bh) { final Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, null, null); - Serializable obj = Log4jLogEvent.serialize(event, false); + final Serializable obj = Log4jLogEvent.serialize(event, false); bh.consume(obj); return obj; } @@ -80,7 +80,7 @@ public class Log4jLogEventBenchmark { @Benchmark public Serializable createSerializableLogEventProxyWithoutExceptionWithLocation(final Blackhole bh) { final Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, null, null); - Serializable obj = Log4jLogEvent.serialize(event, true); + final Serializable obj = Log4jLogEvent.serialize(event, true); bh.consume(obj); return obj; } @@ -88,7 +88,7 @@ public class Log4jLogEventBenchmark { @Benchmark public Serializable createSerializableLogEventProxyWithException(final Blackhole bh) { final Log4jLogEvent event = new Log4jLogEvent("a.b.c", null, "a.b.c", Level.INFO, MESSAGE, null, ERROR); - Serializable obj = Log4jLogEvent.serialize(event, false); + final Serializable obj = Log4jLogEvent.serialize(event, false); bh.consume(obj); return obj; }
