Skip to content

Commit 656f92a

Browse files
author
Suszyński Krzysztof
committed
Quality fixes for Eid
1 parent 45f79a5 commit 656f92a

File tree

3 files changed

+97
-13
lines changed

3 files changed

+97
-13
lines changed

src/main/java/pl/wavesoftware/eid/exceptions/Eid.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ public class Eid implements Serializable {
7171
* @param ref an optional reference
7272
*/
7373
public Eid(String id, @Nullable String ref) {
74-
futureUniqueId = new StdFuture<String>() {
75-
@Override
76-
protected String produce() {
77-
return uniqIdGenerator.generateUniqId();
78-
}
79-
};
74+
futureUniqueId = new UniqFuture();
8075
this.id = id;
8176
this.ref = ref == null ? "" : ref;
8277
}
@@ -246,17 +241,18 @@ public interface UniqIdGenerator {
246241
String generateUniqId();
247242
}
248243

249-
private interface Future<T> {
244+
private interface Future<T extends Serializable> extends Serializable {
250245
T get();
251246
}
252247

253-
private static abstract class StdFuture<T> implements Future<T> {
254-
private T future;
255-
protected abstract T produce();
248+
private static final class UniqFuture implements Future<String> {
249+
private static final long serialVersionUID = 20160325113314L;
250+
private String future;
251+
private UniqFuture() {}
256252
@Override
257-
public T get() {
253+
public String get() {
258254
if (future == null) {
259-
future = produce();
255+
future = uniqIdGenerator.generateUniqId();
260256
}
261257
return future;
262258
}

src/test/java/pl/wavesoftware/eid/exceptions/EidIT.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pl.wavesoftware.eid.exceptions;
22

3+
import org.junit.ClassRule;
34
import org.junit.Test;
45
import org.openjdk.jmh.annotations.Benchmark;
56
import org.openjdk.jmh.annotations.Mode;
@@ -12,6 +13,7 @@
1213
import org.openjdk.jmh.runner.options.TimeValue;
1314
import org.slf4j.Logger;
1415
import org.slf4j.LoggerFactory;
16+
import pl.wavesoftware.testing.JmhCleaner;
1517

1618
import java.util.Collection;
1719
import java.util.Date;
@@ -25,10 +27,13 @@
2527
*/
2628
public class EidIT {
2729

30+
private static final int PERCENT = 100;
2831
private static final int OPERATIONS = 1000;
2932
private static final Logger LOG = LoggerFactory.getLogger(EidIT.class);
3033
private static final double SPEED_THRESHOLD = 0.75d;
31-
public static final int PERCENT = 100;
34+
35+
@ClassRule
36+
public static JmhCleaner cleaner = new JmhCleaner(EidIT.class);
3237

3338
@Test
3439
public void doBenckmarking() throws Exception {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package pl.wavesoftware.testing;
2+
3+
import org.junit.Test;
4+
import org.junit.rules.TestRule;
5+
import org.junit.runner.Description;
6+
import org.junit.runners.model.Statement;
7+
8+
import java.io.IOException;
9+
import java.lang.reflect.Method;
10+
import java.lang.reflect.Modifier;
11+
import java.net.URISyntaxException;
12+
import java.nio.file.FileVisitResult;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
import java.nio.file.SimpleFileVisitor;
17+
import java.nio.file.attribute.BasicFileAttributes;
18+
19+
/**
20+
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
21+
* @since 25.03.16
22+
*/
23+
public class JmhCleaner implements TestRule {
24+
private static final String GENERATED_TEST_SOURCES = "generated-test-sources";
25+
private static final String TEST_ANNOTATIONS = "test-annotations";
26+
private final Class<?> testClass;
27+
28+
public JmhCleaner(Class<?> testClass) {
29+
this.testClass = validateTestClass(testClass);
30+
}
31+
32+
private Class<?> validateTestClass(Class<?> testClass) {
33+
boolean hasTests = false;
34+
for (Method method : testClass.getDeclaredMethods()) {
35+
Test annot = method.getAnnotation(Test.class);
36+
if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()) && annot != null) {
37+
hasTests = true;
38+
break;
39+
}
40+
}
41+
if (!hasTests) {
42+
throw new IllegalArgumentException("You need to pass a test class to constructor of JmhCleaner!!");
43+
}
44+
return testClass;
45+
}
46+
47+
48+
@Override
49+
public Statement apply(final Statement base, Description description) {
50+
51+
return new Statement() {
52+
@Override
53+
public void evaluate() throws Throwable {
54+
try {
55+
base.evaluate();
56+
} finally {
57+
cleanup();
58+
}
59+
}
60+
};
61+
}
62+
63+
private void cleanup() throws IOException, URISyntaxException {
64+
String location = testClass.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
65+
Path testAnnotationsPath = Paths.get(location).getParent().resolve(GENERATED_TEST_SOURCES).resolve(TEST_ANNOTATIONS);
66+
if (!testAnnotationsPath.toFile().isDirectory()) {
67+
return;
68+
}
69+
Files.walkFileTree(testAnnotationsPath, new SimpleFileVisitor<Path>() {
70+
@Override
71+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
72+
Files.delete(file);
73+
return FileVisitResult.CONTINUE;
74+
}
75+
76+
@Override
77+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
78+
Files.delete(dir);
79+
return FileVisitResult.CONTINUE;
80+
}
81+
});
82+
}
83+
}

0 commit comments

Comments
 (0)