Skip to content

Commit

Permalink
Support multiple method recordings.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkotsbak committed Jun 12, 2010
1 parent 462bffe commit 53dab01
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/main/java/no/muda/jackbox/DependencyRecording.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package no.muda.jackbox;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/no/muda/jackbox/JackboxRecorder.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package no.muda.jackbox;

import java.util.LinkedList;
import java.util.List;

public class JackboxRecorder {

private static List<MethodRecording> methodRecordings = new LinkedList<MethodRecording>();
private static MethodRecording lastCompletedRecording;

public static MethodRecording getLastCompletedRecording() {
return lastCompletedRecording;
}

public static void addRecording(MethodRecording methodRecording) {
methodRecordings.add(methodRecording);
lastCompletedRecording = methodRecording;
}

public static MethodRecording[] getAllRecordings() {
return methodRecordings.toArray(new MethodRecording[]{});
}


public static void startRecording() {
clearRecordings();
}

private static void clearRecordings() {
methodRecordings.clear();
lastCompletedRecording = null;
}

}
32 changes: 26 additions & 6 deletions src/test/java/no/muda/jackbox/JackboxRecordingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import no.muda.jackbox.example.ExampleRecordedObject;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class JackboxRecordingTest {

private static ClassLoader originalClassLoader;

private ExampleRecordedObject recordedObject = new ExampleRecordedObject();

@BeforeClass
public static void setupClassloader() {
originalClassLoader = Thread.currentThread().getContextClassLoader();
Expand All @@ -27,35 +30,52 @@ public static void resetClassloader() {
Thread.currentThread().setContextClassLoader(originalClassLoader);
}

@Before
public void startRecording() {
JackboxRecorder.startRecording();
}

@Test
public void shouldRecordMethodCall() throws Exception {
ExampleRecordedObject recordedObject = new ExampleRecordedObject();
int actualReturnedValue = recordedObject.exampleMethod(2, 3);

MethodRecording recording = JackboxRecorder.getLastCompletedRecording();

assertThat(recording.getMethod().getName()).isEqualTo("exampleMethod");
assertThat(recording.getArguments()).containsOnly(2, 3);
assertThat(recording.getRecordedResult()).isEqualTo(actualReturnedValue);
assertExampleMethodCall(2, 3, actualReturnedValue, recording);
}

@Test
public void shouldRecordDependency() throws Exception {
ExampleDependency exampleDependency = new ExampleDependency();
ExampleRecordedObject recordedObject = new ExampleRecordedObject();
recordedObject.setDependency(exampleDependency);

String delegatedArgument = "abcd";
recordedObject.exampleMethodThatDelegatesToDependency(delegatedArgument);

MethodRecording recording = JackboxRecorder.getLastCompletedRecording();


Method invokedMethodOnDependency = ExampleDependency.class.getMethod("invokedMethodOnDependency", String.class);
MethodRecording dependentRecording =
recording.getDependencyMethodRecordings(invokedMethodOnDependency)[0];

assertThat(dependentRecording.getArguments()).containsOnly(delegatedArgument);
assertThat(dependentRecording.getRecordedResult()).isEqualTo("ABCD");
}

@Test
public void shouldRecordMultipleMethodCalls() throws Exception {
int actualReturnedValue = recordedObject.exampleMethod(2, 3);
int actualReturnedValue2 = recordedObject.exampleMethod(4, 5);
MethodRecording[] recordings = JackboxRecorder.getAllRecordings();

assertExampleMethodCall(2, 3, actualReturnedValue, recordings[0]);
assertExampleMethodCall(4, 5, actualReturnedValue2, recordings[1]);
}

private void assertExampleMethodCall(int param1, int param2, int actualReturnedValue,
MethodRecording recording) {
assertThat(recording.getMethod().getName()).isEqualTo("exampleMethod");
assertThat(recording.getArguments()).containsOnly(param1, param2);
assertThat(recording.getRecordedResult()).isEqualTo(actualReturnedValue);
}
}

0 comments on commit 53dab01

Please sign in to comment.