Skip to content

Commit ba28b5c

Browse files
committed
Use map instead of array to publish parameters.
1 parent 59af7f6 commit ba28b5c

File tree

5 files changed

+80
-15
lines changed

5 files changed

+80
-15
lines changed

src/main/java/com/nordstrom/automation/junit/ArtifactCollector.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.nio.file.Files;
55
import java.nio.file.Path;
66
import java.util.ArrayList;
7-
import java.util.Arrays;
87
import java.util.List;
98
import java.util.Map;
109
import java.util.Optional;
@@ -123,12 +122,12 @@ private Path getCollectionPath() {
123122
* @return artifact file base name
124123
*/
125124
private String getArtifactBaseName() {
126-
if (getParameters().length == 0) {
127-
return getDescription().getMethodName();
128-
} else {
129-
int hashcode = Arrays.deepHashCode(getParameters());
125+
int hashcode = getParameters().hashCode();
126+
if (hashcode != 0) {
130127
String hashStr = String.format("%08X", hashcode);
131128
return getDescription().getMethodName() + "-" + hashStr;
129+
} else {
130+
return getDescription().getMethodName();
132131
}
133132
}
134133

src/main/java/com/nordstrom/automation/junit/ArtifactParams.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.nordstrom.automation.junit;
22

3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.Optional;
7+
38
import org.junit.runner.Description;
49

510
/**
@@ -17,10 +22,61 @@ public interface ArtifactParams {
1722
/**
1823
* Get the parameters associated with this test class instance.
1924
*
20-
* @return array of test class parameters
25+
* @return optional map of named test class parameters
26+
*/
27+
default Optional<Map<String, Object>> getParameters() {
28+
return Optional.empty();
29+
}
30+
31+
/**
32+
* Assemble a map of test class instance parameters.
33+
*
34+
* @param params array of {@link Param} objects; may be {@code null} or empty
35+
* @return optional map of parameters (may be empty)
2136
*/
22-
default Object[] getParameters() {
23-
return new Object[0];
37+
public static Optional<Map<String, Object>> mapOf(Param... params) {
38+
if ((params == null) || (params.length == 0)) {
39+
return Optional.empty();
40+
}
41+
Map<String, Object> paramMap = new HashMap<>();
42+
for (Param param : params) {
43+
paramMap.put(param.key, param.val);
44+
}
45+
return Optional.of(Collections.unmodifiableMap(paramMap));
2446
}
2547

48+
/**
49+
* Create a test parameter object for the specified key/value pair.
50+
*
51+
* @param key test parameter key (name)
52+
* @param val test parameter value
53+
* @return test parameter object
54+
*/
55+
public static Param param(String key, Object val) {
56+
return new Param(key, val);
57+
}
58+
59+
/**
60+
* This class defines a test parameter object.
61+
*/
62+
static class Param {
63+
64+
private final String key;
65+
private final Object val;
66+
67+
/**
68+
* Constructor for test parameter object.
69+
*
70+
* @param key test parameter key
71+
* @param val test parameter value
72+
*/
73+
public Param(String key, Object val) {
74+
if ((key == null) || key.isEmpty()) {
75+
throw new IllegalArgumentException("[key] must be a non-empty string");
76+
}
77+
this.key = key;
78+
this.val = val;
79+
}
80+
}
81+
2682
}

src/main/java/com/nordstrom/automation/junit/AtomIdentity.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.nordstrom.automation.junit;
22

3+
import java.util.Map;
4+
import java.util.Optional;
5+
36
import org.junit.rules.TestWatcher;
47
import org.junit.runner.Description;
58

@@ -47,11 +50,11 @@ public Description getDescription() {
4750
* {@inheritDoc}
4851
*/
4952
@Override
50-
public Object[] getParameters() {
53+
public Optional<Map<String, Object>> getParameters() {
5154
if (instance instanceof ArtifactParams) {
5255
return ((ArtifactParams) instance).getParameters();
5356
}
54-
return new Object[0];
57+
return Optional.empty();
5558
}
5659

5760
}

src/test/java/com/nordstrom/automation/junit/ArtifactCollectorParameterized.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.nordstrom.automation.junit;
22

33
import static org.junit.Assert.assertEquals;
4+
import static com.nordstrom.automation.junit.ArtifactParams.param;
5+
6+
import java.util.Map;
7+
import java.util.Optional;
48

59
import org.junit.Test;
610
import org.junit.runner.RunWith;
@@ -22,12 +26,13 @@ public static Object[] data() {
2226
}
2327

2428
@Override
25-
public Object[] getParameters() {
26-
return new Object[] { input };
29+
public Optional<Map<String, Object>> getParameters() {
30+
return ArtifactParams.mapOf(param("input", input));
2731
}
2832

2933
@Test
3034
public void parameterized() {
35+
System.out.println("parameterized: input = [" + input + "]");
3136
assertEquals("first test", input);
3237
}
3338
}

src/test/java/com/nordstrom/automation/junit/UnitTestArtifact.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.nordstrom.automation.junit;
22

3+
import java.util.Map.Entry;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56

@@ -39,9 +40,10 @@ public byte[] getArtifact(Object instance, Throwable reason) {
3940
ArtifactParams publisher = (ArtifactParams) instance;
4041
StringBuilder artifact = new StringBuilder("method: ")
4142
.append(publisher.getDescription().getMethodName()).append("\n");
42-
int i = 0;
43-
for (Object param : publisher.getParameters()) {
44-
artifact.append("param" + i++ + ": [" + param + "]\n");
43+
if (publisher.getParameters().isPresent()) {
44+
for (Entry<String, Object> param : publisher.getParameters().get().entrySet()) {
45+
artifact.append(param.getKey() + ": [" + param.getValue() + "]\n");
46+
}
4547
}
4648
return artifact.toString().getBytes().clone();
4749
} else {

0 commit comments

Comments
 (0)