Skip to content

Commit 414faea

Browse files
committed
support multiple time formats
1 parent feed6c7 commit 414faea

File tree

2 files changed

+168
-3
lines changed

2 files changed

+168
-3
lines changed

diagnostic/server/services/src/main/java/org/terracotta/diagnostic/server/extensions/DiagnosticExtensionsMBeanImpl.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javax.management.StandardMBean;
2929
import java.time.Instant;
3030
import java.time.format.DateTimeFormatter;
31+
import java.time.format.DateTimeParseException;
3132
import java.util.Set;
3233
import java.util.regex.Matcher;
3334
import java.util.regex.Pattern;
@@ -71,9 +72,18 @@ public KitInformation getKitInformation() {
7172

7273
Matcher matcher = Pattern.compile("^(?<date>.*) \\(Revision (?<revision>.*) from (?<branch>.*)\\)$").matcher(b);
7374
if (matcher.matches()) {
74-
// DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd 'at' HH:mm:ss z"); // from core
75-
DateTimeFormatter isoInstant = DateTimeFormatter.ISO_INSTANT;
76-
Instant timestamp = isoInstant.parse(matcher.group("date"), Instant::from);
75+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd 'at' HH:mm:ss z");
76+
String date = matcher.group("date");
77+
Instant timestamp;
78+
try {
79+
timestamp = dtf.parse(date, Instant::from);
80+
} catch (DateTimeParseException parsing) {
81+
try {
82+
timestamp = DateTimeFormatter.ISO_INSTANT.parse(matcher.group("date"), Instant::from);
83+
} catch (DateTimeParseException parsing2) {
84+
timestamp = Instant.ofEpochMilli(0L);
85+
}
86+
}
7787
return new KitInformation(Version.valueOf(version), matcher.group("revision"), matcher.group("branch"), timestamp);
7888
} else {
7989
return new KitInformation(Version.valueOf(version), "UNKNOWN", "UNKNOWN", Instant.ofEpochMilli(0L));
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright Terracotta, Inc.
3+
* Copyright IBM Corp. 2024, 2025
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.terracotta.diagnostic.server.extensions;
18+
19+
import java.time.Instant;
20+
21+
import static org.hamcrest.CoreMatchers.equalTo;
22+
import static org.hamcrest.CoreMatchers.is;
23+
import static org.hamcrest.MatcherAssert.assertThat;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.when;
28+
import org.terracotta.common.struct.Version;
29+
import static org.terracotta.diagnostic.common.DiagnosticConstants.MBEAN_SERVER;
30+
import org.terracotta.diagnostic.model.KitInformation;
31+
import org.terracotta.server.ServerJMX;
32+
33+
public class DiagnosticExtensionsMBeanImplTest {
34+
35+
private ServerJMX serverJMX;
36+
private DiagnosticExtensionsMBeanImpl diagnosticExtensionsMBean;
37+
38+
@Before
39+
public void setUp() {
40+
serverJMX = mock(ServerJMX.class);
41+
diagnosticExtensionsMBean = new DiagnosticExtensionsMBeanImpl(serverJMX);
42+
}
43+
44+
@Test
45+
public void testGetKitInformation_StandardDateFormat() {
46+
// Setup
47+
String version = "Terracotta 5.8.2";
48+
String buildId = "2021-06-29 at 20:54:46 UTC (Revision abc123 from main-branch)";
49+
50+
when(serverJMX.call(MBEAN_SERVER, "getVersion", null)).thenReturn(version);
51+
when(serverJMX.call(MBEAN_SERVER, "getBuildID", null)).thenReturn(buildId);
52+
53+
// Execute
54+
KitInformation kitInfo = diagnosticExtensionsMBean.getKitInformation();
55+
56+
// Verify
57+
assertThat(kitInfo.getVersion(), is(equalTo(Version.valueOf("5.8.2"))));
58+
assertThat(kitInfo.getRevision(), is(equalTo("abc123")));
59+
assertThat(kitInfo.getBranch(), is(equalTo("main-branch")));
60+
61+
// Verify timestamp - 2021-06-29 at 20:54:46 UTC
62+
Instant expectedTimestamp = Instant.parse("2021-06-29T20:54:46Z");
63+
assertThat(kitInfo.getTimestamp(), is(equalTo(expectedTimestamp)));
64+
}
65+
66+
@Test
67+
public void testGetKitInformation_ISODateFormat() {
68+
// Setup
69+
String version = "Terracotta 5.8.3";
70+
String buildId = "2021-07-15T14:30:00Z (Revision def456 from feature-branch)";
71+
72+
when(serverJMX.call(MBEAN_SERVER, "getVersion", null)).thenReturn(version);
73+
when(serverJMX.call(MBEAN_SERVER, "getBuildID", null)).thenReturn(buildId);
74+
75+
// Execute
76+
KitInformation kitInfo = diagnosticExtensionsMBean.getKitInformation();
77+
78+
// Verify
79+
assertThat(kitInfo.getVersion(), is(equalTo(Version.valueOf("5.8.3"))));
80+
assertThat(kitInfo.getRevision(), is(equalTo("def456")));
81+
assertThat(kitInfo.getBranch(), is(equalTo("feature-branch")));
82+
83+
// Verify timestamp - ISO format
84+
Instant expectedTimestamp = Instant.parse("2021-07-15T14:30:00Z");
85+
assertThat(kitInfo.getTimestamp(), is(equalTo(expectedTimestamp)));
86+
}
87+
88+
@Test
89+
public void testGetKitInformation_InvalidDateFormat() {
90+
// Setup
91+
String version = "Terracotta 5.8.4";
92+
String buildId = "Invalid-Date-Format (Revision ghi789 from bugfix-branch)";
93+
94+
when(serverJMX.call(MBEAN_SERVER, "getVersion", null)).thenReturn(version);
95+
when(serverJMX.call(MBEAN_SERVER, "getBuildID", null)).thenReturn(buildId);
96+
97+
// Execute
98+
KitInformation kitInfo = diagnosticExtensionsMBean.getKitInformation();
99+
100+
// Verify
101+
assertThat(kitInfo.getVersion(), is(equalTo(Version.valueOf("5.8.4"))));
102+
assertThat(kitInfo.getRevision(), is(equalTo("ghi789")));
103+
assertThat(kitInfo.getBranch(), is(equalTo("bugfix-branch")));
104+
105+
// Verify timestamp - should default to epoch 0 for invalid format
106+
Instant expectedTimestamp = Instant.ofEpochMilli(0L);
107+
assertThat(kitInfo.getTimestamp(), is(equalTo(expectedTimestamp)));
108+
}
109+
110+
@Test
111+
public void testGetKitInformation_NoSpaceInVersion() {
112+
// Setup
113+
String version = "5.8.5-SNAPSHOT";
114+
String buildId = "2021-08-01 at 10:00:00 UTC (Revision jkl012 from dev-branch)";
115+
116+
when(serverJMX.call(MBEAN_SERVER, "getVersion", null)).thenReturn(version);
117+
when(serverJMX.call(MBEAN_SERVER, "getBuildID", null)).thenReturn(buildId);
118+
119+
// Execute
120+
KitInformation kitInfo = diagnosticExtensionsMBean.getKitInformation();
121+
122+
// Verify
123+
assertThat(kitInfo.getVersion(), is(equalTo(Version.valueOf("5.8.5-SNAPSHOT"))));
124+
assertThat(kitInfo.getRevision(), is(equalTo("jkl012")));
125+
assertThat(kitInfo.getBranch(), is(equalTo("dev-branch")));
126+
127+
// Verify timestamp
128+
Instant expectedTimestamp = Instant.parse("2021-08-01T10:00:00Z");
129+
assertThat(kitInfo.getTimestamp(), is(equalTo(expectedTimestamp)));
130+
}
131+
132+
@Test
133+
public void testGetKitInformation_UnknownBuildInfo() {
134+
// Setup
135+
String version = "Terracotta 5.9.0";
136+
String buildId = "Something completely different";
137+
138+
when(serverJMX.call(MBEAN_SERVER, "getVersion", null)).thenReturn(version);
139+
when(serverJMX.call(MBEAN_SERVER, "getBuildID", null)).thenReturn(buildId);
140+
141+
// Execute
142+
KitInformation kitInfo = diagnosticExtensionsMBean.getKitInformation();
143+
144+
// Verify
145+
assertThat(kitInfo.getVersion(), is(equalTo(Version.valueOf("5.9.0"))));
146+
assertThat(kitInfo.getRevision(), is(equalTo("UNKNOWN")));
147+
assertThat(kitInfo.getBranch(), is(equalTo("UNKNOWN")));
148+
149+
// Verify timestamp - should default to epoch 0 for unmatched pattern
150+
Instant expectedTimestamp = Instant.ofEpochMilli(0L);
151+
assertThat(kitInfo.getTimestamp(), is(equalTo(expectedTimestamp)));
152+
}
153+
}
154+
155+
// Made with Bob

0 commit comments

Comments
 (0)