Skip to content

Commit 3ee95b2

Browse files
committed
B ImageApprovalWriter can handle version strings like "19-ea"
1 parent fa1d860 commit 3ee95b2

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

approvaltests-util-tests/src/test/java/com/spun/util/NumberUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.spun.util;
22

3+
import org.approvaltests.Approvals;
34
import org.junit.jupiter.api.Test;
45

56
import java.util.Arrays;
@@ -62,4 +63,11 @@ private void assertDoubleStream(DoubleStream stream)
6263
{
6364
assertTrue(stream instanceof DoubleStream);
6465
}
66+
@Test
67+
void testStripNonNumeric()
68+
{
69+
String[] inputs = {"11.0", "19-ea", "", null, "19", "1.8.0"};
70+
Approvals.verifyAll("Strip non numeric", inputs,
71+
s -> String.format("%s -> %s", s, NumberUtils.stripNonNumeric(s)));
72+
}
6573
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Strip non numeric
2+
3+
4+
11.0 -> 11.0
5+
19-ea -> 19
6+
->
7+
null -> null
8+
19 -> 19
9+
1.8.0 -> 1.80

approvaltests-util/src/main/java/com/spun/util/NumberUtils.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,41 @@ public static double convertPercentageToDouble(double percent)
208208
{
209209
return percent / 100;
210210
}
211+
public static String stripNonNumeric(String input)
212+
{
213+
if (input == null)
214+
{ return null; }
215+
StringBuffer result = new StringBuffer();
216+
int periods = 0;
217+
for (int i = 0; i < input.length(); i++)
218+
{
219+
char c = input.charAt(i);
220+
switch (c)
221+
{
222+
case '0' :
223+
case '1' :
224+
case '2' :
225+
case '3' :
226+
case '4' :
227+
case '5' :
228+
case '6' :
229+
case '7' :
230+
case '8' :
231+
case '9' :
232+
result.append(c);
233+
break;
234+
case '.' :
235+
if (periods < 1)
236+
{
237+
result.append(c);
238+
periods++;
239+
}
240+
default :
241+
break;
242+
}
243+
}
244+
return result.toString();
245+
}
211246
/* INNER CLASS */
212247
public static class Shuffler implements java.util.Comparator<Shuffler>, Serializable
213248
{

approvaltests/src/main/java/org/approvaltests/writers/ImageApprovalWriter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.approvaltests.writers;
22

3+
import com.spun.util.NumberUtils;
34
import com.spun.util.ObjectUtils;
45
import org.approvaltests.core.ApprovalWriter;
56
import org.approvaltests.namer.NamedEnvironment;
@@ -24,7 +25,9 @@ public static NamedEnvironment asJreAware()
2425
private static String getJreInformation()
2526
{
2627
String javaVersion = System.getProperty("java.version");
27-
int major = Integer.parseInt(javaVersion.split("\\.")[0]);
28+
String majorVersion = javaVersion.split("\\.")[0];
29+
majorVersion = NumberUtils.stripNonNumeric(majorVersion);
30+
int major = Integer.parseInt(majorVersion);
2831
return major < 11 ? "jdkPre11" : "jdkPost11";
2932
}
3033
@Override

0 commit comments

Comments
 (0)