Skip to content

Commit 6c7c462

Browse files
committed
Add unit tests; fix identified issues
1 parent ffdd5da commit 6c7c462

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

src/main/java/com/nordstrom/common/file/OSUtils.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ public T getType() {
5858
return null;
5959
}
6060

61+
/**
62+
* Add the specified mapping to the collection.<br>
63+
* <b>NOTE</b>: If a mapping for the specified constant already exists, this mapping will be replaced.
64+
*
65+
* @param typeConst OS type constant
66+
* @return value of previous mapping; 'null' if no mapping existed
67+
*/
68+
@SuppressWarnings("unchecked")
69+
public <U extends Enum<U> & OSProps> String put(U typeConst) {
70+
return typeMap.put((T) typeConst, typeConst.pattern());
71+
}
72+
6173
/**
6274
* Add the specified mapping to the collection.<br>
6375
* <b>NOTE</b>: If a mapping for the specified constant already exists, this mapping will be replaced.
@@ -66,8 +78,9 @@ public T getType() {
6678
* @param pattern OS name match pattern
6779
* @return value of previous mapping; 'null' if no mapping existed
6880
*/
69-
public String put(T typeConst, String pattern) {
70-
return typeMap.put(typeConst, pattern);
81+
@SuppressWarnings("unchecked")
82+
public <U extends Enum<U> & OSProps> String put(U typeConst, String pattern) {
83+
return typeMap.put((T) typeConst, pattern);
7184
}
7285

7386
/**
@@ -76,9 +89,9 @@ public String put(T typeConst, String pattern) {
7689
*
7790
* @param enumClass operating system mapping enumeration
7891
*/
79-
public void putAll(Class<T> enumClass) {
80-
for (T typeConst : enumClass.getEnumConstants()) {
81-
typeMap.put(typeConst, typeConst.pattern());
92+
public <U extends Enum<U> & OSProps> void putAll(Class<U> enumClass) {
93+
for (U typeConst : enumClass.getEnumConstants()) {
94+
put(typeConst);
8295
}
8396
}
8497

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,65 @@
11
package com.nordstrom.common.file;
22

3+
import static org.testng.Assert.assertEquals;
4+
import static org.testng.Assert.assertNull;
5+
36
import org.testng.annotations.Test;
47

8+
import com.nordstrom.common.file.OSUtils.OSProps;
59
import com.nordstrom.common.file.OSUtils.OSType;
610

711
public class OSUtilsTest {
812

13+
private static String osName = System.getProperty("os.name").toLowerCase();
14+
915
@Test
10-
public void testDefault() {
16+
public void testDefaultMapping() {
1117
OSUtils<OSType> osUtils = OSUtils.getDefault();
12-
System.out.println(osUtils.getType());
13-
System.out.println(OSUtils.osName());
14-
System.out.println(OSUtils.version());
15-
System.out.println(OSUtils.arch());
18+
OSType expected = (osName.startsWith("windows")) ? OSType.WINDOWS : OSType.UNIX;
19+
assertEquals(osUtils.getType(), expected, "Reported OS type doesn't match expected type");
20+
}
21+
22+
@Test
23+
public void testCustomMapping() {
24+
OSUtils<TestEnum> osUtils = new OSUtils<>(TestEnum.class);
25+
assertEquals(osUtils.getType(), TestEnum.TEST, "Reported OS type doesn't match expected type");
26+
}
27+
28+
@Test
29+
public void testAddOneMapping() {
30+
OSUtils<OSType> osUtils = OSUtils.getDefault();
31+
osUtils.put(TestEnum.TEST);
32+
assertEquals(osUtils.getType(), TestEnum.TEST, "Reported OS type doesn't match expected type");
33+
}
34+
35+
@Test
36+
public void testAddMappingSet() {
37+
OSUtils<OSType> osUtils = OSUtils.getDefault();
38+
osUtils.putAll(TestEnum.class);
39+
assertEquals(osUtils.getType(), TestEnum.TEST, "Reported OS type doesn't match expected type");
40+
}
41+
42+
@Test
43+
public void testOverrideMapping() {
44+
OSUtils<OSType> osUtils = OSUtils.getDefault();
45+
osUtils.put(OSType.SOLARIS, "(?i)" + osName);
46+
assertEquals(osUtils.getType(), OSType.SOLARIS, "Reported OS type doesn't match expected type");
47+
}
48+
49+
@Test
50+
public void testUnsupportedType() {
51+
OSUtils<OSType> osUtils = OSUtils.getDefault();
52+
osUtils.put(osUtils.getType(), "");
53+
assertNull(osUtils.getType(), "Reported OS type doesn't match expected type");
54+
}
55+
56+
public enum TestEnum implements OSProps {
57+
TEST;
58+
59+
@Override
60+
public String pattern() {
61+
return "(?i)" + osName;
62+
}
63+
1664
}
17-
1865
}

0 commit comments

Comments
 (0)