Skip to content

Commit e44d334

Browse files
Fix imestamps in unit tests
1 parent 17d7dd0 commit e44d334

File tree

2 files changed

+23
-9
lines changed
  • cf-java-logging-support-log4j2/src/test/java/com/sap/hcp/cf/logging/common
  • cf-java-logging-support-logback/src/test/java/com/sap/hcp/cf/logging/common

2 files changed

+23
-9
lines changed

cf-java-logging-support-log4j2/src/test/java/com/sap/hcp/cf/logging/common/TestAppLog.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.sap.hcp.cf.logging.common;
22

33
import static com.sap.hcp.cf.logging.common.converter.CustomFieldMatchers.hasCustomField;
4+
import static org.hamcrest.MatcherAssert.assertThat;
45
import static org.hamcrest.Matchers.contains;
56
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
67
import static org.hamcrest.Matchers.lessThanOrEqualTo;
78
import static org.hamcrest.core.Is.is;
89
import static org.hamcrest.core.IsNull.notNullValue;
9-
import static org.junit.Assert.assertThat;
10+
11+
import java.time.Instant;
1012

1113
import org.junit.Test;
1214
import org.slf4j.Logger;
@@ -63,9 +65,9 @@ public void testMDC() {
6365
MDC.put(SOME_KEY, SOME_VALUE);
6466
MDC.put("testNumeric", "200");
6567
logMsg = "Running testMDC()";
66-
long beforeTS = System.currentTimeMillis() * 1000000;
68+
long beforeTS = now();
6769
LOGGER.info(logMsg);
68-
long afterTS = System.currentTimeMillis() * 1000000;
70+
long afterTS = now();
6971
assertThat(getMessage(), is(logMsg));
7072
assertThat(getField(Fields.COMPONENT_ID), is("-"));
7173
assertThat(getField(Fields.COMPONENT_NAME), is("-"));
@@ -78,7 +80,7 @@ public void testMDC() {
7880
@Test
7981
public void testUnregisteredCustomField() {
8082
logMsg = "Running testUnregisteredCustomField()";
81-
long beforeTS = System.currentTimeMillis() * 1000000;
83+
long beforeTS = now();
8284
LOGGER.info(logMsg, CustomField.customField(SOME_KEY, SOME_VALUE));
8385
assertThat(getMessage(), is(logMsg));
8486
assertThat(getField(SOME_KEY), is(SOME_VALUE));
@@ -96,7 +98,7 @@ public void testCustomFieldOverwritesMdc() throws Exception {
9698
MDC.put(RETAINED_FIELD_KEY, SOME_VALUE);
9799
MDC.put(SOME_KEY, SOME_VALUE);
98100
logMsg = "Running testCustomFieldOverwritesMdc()";
99-
long beforeTS = System.currentTimeMillis() * 1000000;
101+
long beforeTS = now();
100102
LOGGER.info(logMsg, CustomField.customField(CUSTOM_FIELD_KEY, SOME_OTHER_VALUE),
101103
CustomField.customField(RETAINED_FIELD_KEY, SOME_OTHER_VALUE),
102104
CustomField.customField(SOME_KEY, SOME_OTHER_VALUE));
@@ -137,4 +139,9 @@ public void testJSONMsg() {
137139
LOGGER.info(jsonMsg);
138140
assertThat(getMessage(), is(jsonMsg));
139141
}
142+
143+
private static long now() {
144+
Instant now = Instant.now();
145+
return now.getEpochSecond() * 1_000_000_000L + now.getNano();
146+
}
140147
}

cf-java-logging-support-logback/src/test/java/com/sap/hcp/cf/logging/common/TestAppLog.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.sap.hcp.cf.logging.common;
22

33
import static com.sap.hcp.cf.logging.common.converter.CustomFieldMatchers.hasCustomField;
4+
import static org.hamcrest.MatcherAssert.assertThat;
45
import static org.hamcrest.Matchers.contains;
56
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
67
import static org.hamcrest.Matchers.lessThanOrEqualTo;
78
import static org.hamcrest.core.Is.is;
89
import static org.hamcrest.core.IsNull.notNullValue;
9-
import static org.junit.Assert.assertThat;
10+
11+
import java.time.Instant;
1012

1113
import org.junit.Test;
1214
import org.slf4j.Logger;
@@ -64,7 +66,7 @@ public void testMDC() {
6466
MDC.put(SOME_KEY, SOME_VALUE);
6567
MDC.put("testNumeric", "200");
6668
logMsg = "Running testMDC()";
67-
long beforeTS = System.currentTimeMillis() * 1000000;
69+
long beforeTS = now();
6870
LOGGER.info(logMsg);
6971
assertThat(getMessage(), is(logMsg));
7072
assertThat(getField(Fields.COMPONENT_ID), is("-"));
@@ -80,7 +82,7 @@ public void testMDC() {
8082
@Test
8183
public void testUnregisteredCustomField() {
8284
logMsg = "Running testUnregisteredCustomField()";
83-
long beforeTS = System.currentTimeMillis() * 1000000;
85+
long beforeTS = now();
8486
LOGGER.info(logMsg, CustomField.customField(SOME_KEY, SOME_VALUE));
8587
assertThat(getMessage(), is(logMsg));
8688
assertThat(getField(SOME_KEY), is(SOME_VALUE));
@@ -98,7 +100,7 @@ public void testCustomFieldOverwritesMdc() throws Exception {
98100
MDC.put(RETAINED_FIELD_KEY, SOME_VALUE);
99101
MDC.put(SOME_KEY, SOME_VALUE);
100102
logMsg = "Running testCustomFieldOverwritesMdc()";
101-
long beforeTS = System.currentTimeMillis() * 1000000;
103+
long beforeTS = now();
102104
LOGGER.info(logMsg, CustomField.customField(CUSTOM_FIELD_KEY, SOME_OTHER_VALUE),
103105
CustomField.customField(RETAINED_FIELD_KEY, SOME_OTHER_VALUE),
104106
CustomField.customField(SOME_KEY, SOME_OTHER_VALUE));
@@ -139,4 +141,9 @@ public void testJSONMsg() {
139141
LOGGER.info(jsonMsg);
140142
assertThat(getMessage(), is(jsonMsg));
141143
}
144+
145+
private static long now() {
146+
Instant now = Instant.now();
147+
return now.getEpochSecond() * 1_000_000_000L + now.getNano();
148+
}
142149
}

0 commit comments

Comments
 (0)