Skip to content

Commit

Permalink
Finish unit tests; fix instance access bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbabcoc committed Oct 16, 2017
1 parent bff8f0b commit 17be86f
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 161 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ runner
test-output
*.md.html
*.log
*.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
Expand All @@ -16,13 +18,8 @@

public class JUnitArtifactCollector<T extends JUnitArtifactType> extends TestWatcher {

private static final ThreadLocal<List<JUnitArtifactCollector<? extends JUnitArtifactType>>> watchers =
new InheritableThreadLocal<List<JUnitArtifactCollector<? extends JUnitArtifactType>>>() {
@Override
public List<JUnitArtifactCollector<? extends JUnitArtifactType>> initialValue() {
return new ArrayList<>();
}
};
private static final Map<Description, List<JUnitArtifactCollector<? extends JUnitArtifactType>>> watcherMap =
new ConcurrentHashMap<>();

private final T provider;
private final Object instance;
Expand All @@ -32,14 +29,25 @@ public List<JUnitArtifactCollector<? extends JUnitArtifactType>> initialValue()
public JUnitArtifactCollector(Object instance, T provider) {
this.instance = instance;
this.provider = provider;
watchers.get().add(this);
}

/**
* {@inheritDoc}
*/
@Override
public void starting(Description description) {
this.description = description;
List<JUnitArtifactCollector<? extends JUnitArtifactType>> watcherList = watcherMap.get(description);
if (watcherList == null) {
watcherList = new ArrayList<>();
watcherMap.put(description, watcherList);
}
watcherList.add(this);
}

/**
* {@inheritDoc}
*/
@Override
public void failed(Throwable e, Description description) {
captureArtifact();
Expand Down Expand Up @@ -97,11 +105,10 @@ public Optional<Path> captureArtifact() {
/**
* Get path of directory at which to store artifacts.
*
* @param description JUnit method description
* @return path of artifact storage directory
*/
private Path getCollectionPath() {
Path collectionPath = Paths.get("");
Path collectionPath = Paths.get(System.getProperty("user.dir"));
return collectionPath.resolve(provider.getArtifactPath());
}

Expand Down Expand Up @@ -169,16 +176,21 @@ public Description getDescription() {
}

/**
* Get reference to an instance of the specified watcher type associated with the described method.
*
* @param watcherClass
* @return
* @param description JUnit method description object
* @param watcherType watcher type
* @return optional watcher instance
*/
@SuppressWarnings("unchecked")
public static <S extends JUnitArtifactCollector<? extends JUnitArtifactType>> Optional<S>
getWatcher(Class<S> watcherClass) {
for (JUnitArtifactCollector<? extends JUnitArtifactType> watcher : watchers.get()) {
if (watcher.getClass() == watcherClass) {
return Optional.of((S) watcher);
getWatcher(Description description, Class<S> watcherType) {
List<JUnitArtifactCollector<? extends JUnitArtifactType>> watcherList = watcherMap.get(description);
if (watcherList != null) {
for (JUnitArtifactCollector<? extends JUnitArtifactType> watcher : watcherList) {
if (watcher.getClass() == watcherType) {
return Optional.of((S) watcher);
}
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.nordstrom.automation.junit;

import static org.junit.Assert.fail;

import org.junit.Rule;
import org.junit.Test;

public class ArtifactCollectorCrippled {

@Rule
public final UnitTestCapture watcher = new UnitTestCapture(this);

@Test// (groups = {"willNotCapture"})
public void testWillNotCapture() {
System.out.println("willNotCapture");
watcher.getArtifactProvider().crippleCapture();
fail("willNotCapture");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.nordstrom.automation.junit;

import static org.junit.Assert.fail;

import org.junit.Rule;
import org.junit.Test;

public class ArtifactCollectorDisabled {

@Rule
public final UnitTestCapture watcher = new UnitTestCapture(this);

@Test// (groups = {"canNotCapture"})
public void testCanNotCapture() {
System.out.println("canNotCapture");
watcher.getArtifactProvider().disableCapture();
fail("canNotCapture");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.nordstrom.automation.junit;

import static org.junit.Assert.fail;

import org.junit.Rule;
import org.junit.Test;

public class ArtifactCollectorFailing {

@Rule
public final UnitTestCapture watcher = new UnitTestCapture(this);

@Test// (groups = {"testFailed"})
public void testFailed() {
System.out.println("testFailed");
fail("testFailed");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.nordstrom.automation.junit;

import org.junit.Rule;
import org.junit.Test;

public class ArtifactCollectorOnDemand {

@Rule
public final UnitTestCapture watcher = new UnitTestCapture(this);

@Test// (groups = {"onDemandCapture"})
public void testOnDemandCapture() {
System.out.println("onDemandCapture");
watcher.captureArtifact();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.nordstrom.automation.junit;

import static org.junit.Assert.assertTrue;
import org.junit.Rule;
import org.junit.Test;

public class ArtifactCollectorPassing {

@Rule
public final UnitTestCapture watcher = new UnitTestCapture(this);

@Test// (groups = {"testPassed"})
public void testPassed() {
System.out.println("testPassed");
assertTrue(true);
}

}
Loading

0 comments on commit 17be86f

Please sign in to comment.