Permalink
Browse files

Added tests using PowerMock.

  • Loading branch information...
1 parent b0ae8cc commit dadab590ee1959239d3b11f263bd1d14ad8b4fab @schuchert committed Mar 26, 2013
View
34 pom.xml
@@ -237,7 +237,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
- <version>1.9.0</version>
+ <version>1.9.5</version>
<scope>test</scope>
</dependency>
@@ -277,26 +277,28 @@
<dependency>
<groupId>info.cukes</groupId>
- <artifactId>cucumber-core</artifactId>
- <version>1.1.2</version>
+ <artifactId>cucumber-jvm</artifactId>
+ <version>1.1.1</version>
<scope>test</scope>
</dependency>
+
<dependency>
<groupId>info.cukes</groupId>
- <artifactId>cucumber-jvm</artifactId>
- <version>1.1.2</version>
+ <artifactId>cucumber-junit</artifactId>
+ <version>1.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
- <artifactId>cucumber-junit</artifactId>
- <version>1.1.2</version>
+ <artifactId>cucumber-java</artifactId>
+ <version>1.1.1</version>
<scope>test</scope>
</dependency>
+
<dependency>
<groupId>info.cukes</groupId>
- <artifactId>cucumber-java</artifactId>
- <version>1.1.2</version>
+ <artifactId>cucumber-html</artifactId>
+ <version>0.2.2</version>
<scope>test</scope>
</dependency>
@@ -306,6 +308,20 @@
<version>2.31.0</version>
</dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-module-junit4</artifactId>
+ <version>1.5</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-api-mockito</artifactId>
+ <version>1.5</version>
+ <scope>test</scope>
+ </dependency>
+
</dependencies>
<build>
@@ -7,23 +7,23 @@
private static ThreadLocal<Integer> id = new ThreadLocal<Integer>();
private static ThreadLocal<Integer> level = new ThreadLocal<Integer>();
- public static void enter() {
+ public void enter() {
if (id.get() == null) {
id.set(uniqueId.getAndIncrement());
level.set(0);
}
level.set(level.get() + 1);
}
- public static String get() {
+ public String get() {
if (id.get() == null) {
throw new RuntimeException("get called outside of enter/exit sequence");
}
return String.format("%d.%d", id.get(), level.get());
}
- public static void exit() {
+ public void exit() {
if (id.get() != null) {
level.set(level.get() - 1);
if (level.get() < 1) {
@@ -37,7 +37,7 @@ private Object executeShell(ProceedingJoinPoint jp) throws Throwable {
String className = jp.getSignature().getDeclaringTypeName();
String methodName = jp.getSignature().getName();
- MetricRecorder metricRecorder = new MetricRecorder(trackMetrics, className, methodName);
+ MetricsRecorder metricRecorder = new MetricsRecorder(trackMetrics, className, methodName);
boolean success = false;
try {
@@ -1,74 +0,0 @@
-package shoe.example.metrics;
-
-import com.yammer.metrics.Metrics;
-import com.yammer.metrics.core.MetricName;
-import com.yammer.metrics.core.Timer;
-import com.yammer.metrics.core.TimerContext;
-import shoe.example.log.SystemLoggerFactory;
-import shoe.example.toggles.TrackMetrics;
-
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-
-import static java.util.concurrent.TimeUnit.MILLISECONDS;
-import static java.util.concurrent.TimeUnit.SECONDS;
-
-public class MetricRecorder {
- public static final String APP_NAME_PROPERTY = "APPLICATION_NAME";
- public static final String PORT_NAME_PROPERTY = "PORT";
- private final String className;
- private final String methodName;
- private final TrackMetrics trackMetrics;
- private TimerContext context;
- private long startTime;
- private long stopTime;
-
- public MetricRecorder(TrackMetrics trackMetrics, String className, String methodName) {
- this.className = className;
- this.methodName = methodName;
- this.trackMetrics = trackMetrics;
- }
-
- public void enter() {
- CorrelationId.enter();
- startTime = System.currentTimeMillis();
-
- if (trackMetrics.isEnabled()) {
- MetricName name = new MetricName(group(), className, methodName);
- Timer responses = Metrics.newTimer(name, MILLISECONDS, SECONDS);
- context = responses.time();
- }
-
- SystemLoggerFactory.get(className).info("start : %s-%s", methodName, CorrelationId.get());
- }
-
- public void exit(boolean success) {
- if (context != null) {
- context.stop();
- }
-
- stopTime = System.currentTimeMillis();
-
- String result = success ? "finish" : "failure";
- SystemLoggerFactory.get(className).info("%7s: %s-%s(%dms)", result, methodName, CorrelationId.get(), duration());
- CorrelationId.exit();
- }
-
- private long duration() {
- return stopTime - startTime;
- }
-
- private String hostName() {
- try {
- return InetAddress.getLocalHost().getHostName();
- } catch (UnknownHostException e) {
- return "UnknownHost";
- }
- }
-
- private String group() {
- String app = System.getProperty(APP_NAME_PROPERTY);
- String port = System.getProperty(PORT_NAME_PROPERTY);
- return String.format("%s.%s.%s", app, hostName(), port);
- }
-}
@@ -23,6 +23,7 @@
private final String className;
private final String methodName;
private final TrackMetrics trackMetrics;
+ private CorrelationId correlationId;
public MetricsRecorder(TrackMetrics trackMetrics, String className, String methodName) {
this.className = className;
@@ -31,7 +32,8 @@ public MetricsRecorder(TrackMetrics trackMetrics, String className, String metho
}
public void enter() {
- CorrelationId.enter();
+ correlationId = new CorrelationId();
+ correlationId.enter();
startTime = System.currentTimeMillis();
if (trackMetrics.isEnabled()) {
@@ -40,7 +42,7 @@ public void enter() {
context = responses.time();
}
- SystemLoggerFactory.get(className).info("start : %s-%s", methodName, CorrelationId.get());
+ SystemLoggerFactory.get(className).info("start : %s-%s", methodName, correlationId.get());
}
public void exit(boolean success) {
@@ -51,8 +53,8 @@ public void exit(boolean success) {
stopTime = System.currentTimeMillis();
String result = success ? "finish" : "failure";
- SystemLoggerFactory.get(className).info("%7s: %s-%s(%dms)", result, methodName, CorrelationId.get(), duration());
- CorrelationId.exit();
+ SystemLoggerFactory.get(className).info("%7s: %s-%s(%dms)", result, methodName, correlationId.get(), duration());
+ correlationId.exit();
}
private long duration() {
@@ -3,29 +3,14 @@
import cucumber.api.junit.Cucumber;
import org.junit.AfterClass;
import org.junit.BeforeClass;
+import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import shoe.example.context.SystemApplicationContext;
import shoe.example.server.EmbeddedJetty;
+@Ignore
@RunWith(Cucumber.class)
-@Cucumber.Options(format = {"pretty", "html:target/cucumber"}, features = "src/test/resources", glue = "shoe/example/features/step_definitions")
+@Cucumber.Options(format = {"pretty", "html:target/cucumber-html-report"}, features = "src/test/resources", glue = "shoe/example/features/step_definitions")
public class LightweightCucumberExecutor {
- public static final String SPRING_PROFILE = "spring.profiles.active";
- private static String originalProfile;
-
- @BeforeClass
- public static void initJetty() throws Exception {
- originalProfile = System.getProperty(SPRING_PROFILE);
- if (originalProfile == null) {
- originalProfile = "";
- }
- System.setProperty(SPRING_PROFILE, "test");
-// ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("/Users/schuchert/src/spring_aop/src/main/webapp/applicationContext.xml");
- }
-
- @AfterClass
- public static void undoItNow() throws Exception {
- System.setProperty(SPRING_PROFILE, originalProfile);
- }
}
@@ -7,6 +7,8 @@
import static org.hamcrest.MatcherAssert.assertThat;
public class CorrelationIdTest {
+ CorrelationId id = new CorrelationId();
+
int id(String correlationId) {
return Integer.parseInt(correlationId.split("[.]")[0]);
}
@@ -17,74 +19,74 @@ int level(String correlationId) {
@Test
public void initialLevelIs1() {
- CorrelationId.enter();
+ id.enter();
try {
- assertThat(level(CorrelationId.get()), is(1));
+ assertThat(level(id.get()), is(1));
} finally {
- CorrelationId.exit();
+ id.exit();
}
}
@Test
public void enterIncrements() {
- CorrelationId.enter();
- CorrelationId.enter();
+ id.enter();
+ id.enter();
try {
- assertThat(level(CorrelationId.get()), is(2));
+ assertThat(level(id.get()), is(2));
} finally {
- CorrelationId.exit();
- CorrelationId.exit();
+ id.exit();
+ id.exit();
}
}
@Test(expected = RuntimeException.class)
public void cannotGetAfterLastExit() {
- CorrelationId.enter();
- CorrelationId.exit();
- CorrelationId.get();
+ id.enter();
+ id.exit();
+ id.get();
}
@Test(expected = RuntimeException.class)
public void exitBeforeEnterFails() {
- CorrelationId.exit();
+ id.exit();
}
@Test
public void enterInAnotherThreadDoesNotChangeThisThread() throws Exception {
- CorrelationId.enter();
- String original = CorrelationId.get();
+ id.enter();
+ String original = id.get();
Thread thread = new Thread(new Runnable() {
public void run() {
- CorrelationId.enter();
- CorrelationId.exit();
+ id.enter();
+ id.exit();
}
});
thread.start();
thread.join();
try {
- assertThat(CorrelationId.get(), is(original));
+ assertThat(id.get(), is(original));
} finally {
- CorrelationId.exit();
+ id.exit();
}
}
@Test
public void idsInDifferentThreadsAreNotEqual() throws Exception {
- CorrelationId.enter();
- final int firstThreadId = id(CorrelationId.get());
+ id.enter();
+ final int firstThreadId = id(id.get());
final boolean[] sameId = {true};
Thread thread = new Thread(new Runnable() {
public void run() {
- CorrelationId.enter();
- sameId[0] = firstThreadId == id(CorrelationId.get());
- CorrelationId.exit();
+ id.enter();
+ sameId[0] = firstThreadId == id(id.get());
+ id.exit();
}
});
@@ -94,7 +96,7 @@ public void run() {
try {
assertFalse(sameId[0]);
} finally {
- CorrelationId.exit();
+ id.exit();
}
}
}
Oops, something went wrong.

0 comments on commit dadab59

Please sign in to comment.