forked from nomuda/jackbox
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/test/java/no/muda/jackbox/example/classannotation/ClassAnnotatedDemoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package no.muda.jackbox.example.classannotation; | ||
|
||
import no.muda.jackbox.JackboxRecorder; | ||
import no.muda.jackbox.MethodRecording; | ||
import no.muda.jackbox.persistance.json.JSONPersister; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
public class ClassAnnotatedDemoTest { | ||
|
||
private ByteArrayOutputStream sysout; | ||
|
||
@Test | ||
public void shouldCallDaoDuringRecording() throws Exception { | ||
ClassAnnotationDemoApp.main(new String[0]); | ||
assertThat(getSysout()).contains("ClassAnnotationDemoDao"); | ||
} | ||
|
||
@Test | ||
public void shouldNotCallDaoDuringPlayback() throws Exception { | ||
ClassAnnotationDemoApp.main(new String[0]); | ||
MethodRecording recording = JackboxRecorder.getLastCompletedRecording(); | ||
|
||
JSONPersister persister = new JSONPersister(); | ||
StringWriter serializedRecording = new StringWriter(); | ||
persister.persistToWriter(recording, serializedRecording); | ||
|
||
captureSysout(); | ||
recording = persister.readFromReader(new StringReader(serializedRecording.toString())); | ||
|
||
recording.replay(); | ||
assertThat(getSysout()) | ||
.contains("ClassAnnotationDemoService") | ||
.excludes("ClassAnnotationDemoDao"); | ||
} | ||
|
||
private String getSysout() { | ||
return new String(sysout.toByteArray()); | ||
} | ||
|
||
@Before | ||
public void captureSysout() { | ||
sysout = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(sysout)); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/no/muda/jackbox/example/classannotation/ClassAnnotationDemoApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package no.muda.jackbox.example.classannotation; | ||
|
||
public class ClassAnnotationDemoApp { | ||
|
||
public static void main(String[] args) { | ||
ClassAnnotationDemoService service = new ClassAnnotationDemoService(); | ||
service.doSomething(); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/no/muda/jackbox/example/classannotation/ClassAnnotationDemoDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package no.muda.jackbox.example.classannotation; | ||
|
||
import no.muda.jackbox.annotations.Dependency; | ||
import no.muda.jackbox.example.Entity; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Dependency | ||
public class ClassAnnotationDemoDao { | ||
|
||
public List<Entity> findAll() { | ||
System.out.println("In " + getClass()); | ||
return Arrays.asList(new Entity("A"), new Entity("B")); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/test/java/no/muda/jackbox/example/classannotation/ClassAnnotationDemoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package no.muda.jackbox.example.classannotation; | ||
|
||
import no.muda.jackbox.annotations.Recording; | ||
import no.muda.jackbox.example.Entity; | ||
|
||
import java.util.List; | ||
|
||
@Recording | ||
public class ClassAnnotationDemoService { | ||
|
||
private ClassAnnotationDemoDao dao = new ClassAnnotationDemoDao(); | ||
|
||
public int doSomething() { | ||
System.out.println("In " + getClass()); | ||
List<Entity> entities = dao.findAll(); | ||
for (Entity entity : entities) { | ||
System.out.println(entity); | ||
} | ||
return entities.size(); | ||
} | ||
|
||
} |