Skip to content

Commit 792bf8a

Browse files
committed
Fixes #1: multiple calls to vardump() now just prints dumps from this call
1 parent 5cbcb4e commit 792bf8a

12 files changed

+123
-22
lines changed

build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
group 'net.workingdeveloper.java'
2-
version '0.0.1-SNAPSHOT'
2+
version '0.2.0'
33

44
apply plugin: 'java'
55

66
sourceCompatibility = 1.8
77

8+
89
repositories {
910
mavenCentral()
1011
}
@@ -13,6 +14,10 @@ dependencies {
1314
compile 'org.apache.commons:commons-lang3:3+'
1415
compile 'org.slf4j:slf4j-api:1.7+'
1516
testCompile group: 'junit', name: 'junit', version: '4.+'
17+
testCompile "org.hamcrest:hamcrest-core:1.+"
18+
testCompile "org.hamcrest:hamcrest-library:1.+"
19+
testCompile group: 'com.tngtech.java', name: 'junit-dataprovider', version: '1+'
20+
testCompile "org.mockito:mockito-core:1.+"
1621
}
1722

1823
task wrapper(type: Wrapper) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.workingdeveloper.java.vardump;
2+
3+
/**
4+
* Created by Christoph Graupner on 2016-12-04.
5+
*
6+
* @author Christoph Graupner <ch.graupner@workingdeveloper.net>
7+
*/
8+
public interface AppendableFactory {
9+
/**
10+
* Every time it is called it gives a <b>new</b> instance of an Appendable implementation
11+
*
12+
* @return a new instance of an implementation of Appendable
13+
*/
14+
Appendable createAppendable();
15+
}

src/main/java/net/workingdeveloper/java/vardump/IVarDumperFormatter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public interface IVarDumperFormatter {
3939

4040
IVarDumperFormatter openObject(Object aName);
4141

42-
IVarDumperFormatter setStringBuffer(Appendable aBuffer);
42+
IVarDumperFormatter reset();
43+
44+
IVarDumperFormatter setAppendableFactory(AppendableFactory aAppendableFactory);
4345

4446
String toString();
4547
}

src/main/java/net/workingdeveloper/java/vardump/VarDumpFactory.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Predicate<Field> createFieldAcceptPredicate(String[] aExcludeFieldNames,
2727
return new FieldAcceptorPredicate(aExcludeFieldNames, aAppendStatics, aAppendTransients);
2828
}
2929

30-
public IVarDumperFormatter createIndentFormatter(Appendable aAppendable, int aIndent, boolean aShortClassNames) {
30+
public IVarDumperFormatter createIndentFormatter(AppendableFactory aAppendable, int aIndent, boolean aShortClassNames) {
3131
return new VarDumperIndentFormatterImpl(aAppendable, aIndent, aShortClassNames);
3232
}
3333

@@ -48,6 +48,10 @@ public IVarDumper createRecursiveDumper() {
4848
getDefaultFormatter(), getDefaultFieldPredicate(), getDefaultCyclicRegistry());
4949
}
5050

51+
public AppendableFactory getDefaultAppendableFactory() {
52+
return StringBuilder::new;
53+
}
54+
5155
public IVarDumperCyclicRegistry getDefaultCyclicRegistry() {
5256
return new VarDumperCyclicRegistryImpl();
5357
}
@@ -57,6 +61,10 @@ public Predicate<Field> getDefaultFieldPredicate() {
5761
}
5862

5963
public IVarDumperFormatter getDefaultFormatter() {
60-
return new VarDumperIndentFormatterImpl(new StringBuilder(), 2, false);
64+
return new VarDumperIndentFormatterImpl(
65+
getDefaultAppendableFactory(),
66+
2,
67+
false
68+
);
6169
}
6270
}

src/main/java/net/workingdeveloper/java/vardump/impl/AbstractVarDumperFormatter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.workingdeveloper.java.vardump.impl;
22

3+
import net.workingdeveloper.java.vardump.AppendableFactory;
34
import net.workingdeveloper.java.vardump.IVarDumperFormatter;
45

56
import java.io.IOException;
@@ -12,8 +13,12 @@
1213
*/
1314
abstract public class AbstractVarDumperFormatter extends BasicVarDumperFormatter implements IVarDumperFormatter {
1415

15-
public AbstractVarDumperFormatter(Appendable aBuffer, boolean aShortClassName) {
16-
super(aBuffer, aShortClassName);
16+
/**
17+
* @param aAppendableFactory Could be a lamba like <code>StringBuilder::new</code>.
18+
* @param aShortClassName <em>true</em> if the outputted class name should be just the class itself without the package name.
19+
*/
20+
public AbstractVarDumperFormatter(AppendableFactory aAppendableFactory, boolean aShortClassName) {
21+
super(aAppendableFactory, aShortClassName);
1722
}
1823

1924
@Override

src/main/java/net/workingdeveloper/java/vardump/impl/BasicVarDumperFormatter.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.workingdeveloper.java.vardump.impl;
22

3+
import net.workingdeveloper.java.vardump.AppendableFactory;
34
import net.workingdeveloper.java.vardump.IVarDumperFormatter;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
@@ -21,12 +22,17 @@ enum State {MAP, OBJECT, ARRAY, FIELD}
2122
protected Appendable fBuffer;
2223
Stack<State> fContextStack = new Stack<>();
2324
boolean fShortClassName = false;
25+
private AppendableFactory fBufferFactory;
2426
private String fRefString = "REF>>";
2527
private Logger logger = LoggerFactory.getLogger(VarDumperFormatterImpl.class);
2628

27-
public BasicVarDumperFormatter(Appendable aBuffer, boolean aShortClassName) {
28-
fBuffer = aBuffer;
29+
/**
30+
* @param aAppendableFactory Could be a lamba like <code>StringBuilder::new</code>.
31+
* @param aShortClassName <em>true</em> if the outputted class name should be just the class itself without the package name.
32+
*/
33+
public BasicVarDumperFormatter(AppendableFactory aAppendableFactory, boolean aShortClassName) {
2934
fShortClassName = aShortClassName;
35+
setAppendableFactory(aAppendableFactory);
3036
}
3137

3238
@Override
@@ -48,8 +54,15 @@ public IVarDumperFormatter appendString(String aName) {
4854
}
4955

5056
@Override
51-
public IVarDumperFormatter setStringBuffer(Appendable aBuffer) {
52-
fBuffer = aBuffer;
57+
public IVarDumperFormatter reset() {
58+
fBuffer = fBufferFactory.createAppendable();
59+
return this;
60+
}
61+
62+
@Override
63+
public IVarDumperFormatter setAppendableFactory(AppendableFactory aAppendableFactory) {
64+
fBufferFactory = aAppendableFactory;
65+
fBuffer = aAppendableFactory.createAppendable();
5366
return this;
5467
}
5568

src/main/java/net/workingdeveloper/java/vardump/impl/RecursiveVarDumperImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public boolean isAppendTransients() {
6565

6666
@Override
6767
public String vardump(final Object aObject, final IVarDumperFormatter aFormatter) {
68+
reset();
6869
setFormatter(aFormatter);
6970
reflectionSwitch(aObject);
7071
return getFormatter().toString();
@@ -120,6 +121,10 @@ protected void reflectionSwitch(final Object aObject) {
120121
}
121122
}
122123

124+
private void reset() {
125+
getFormatter().reset();
126+
}
127+
123128
private void reflectMap(Object aObject) {
124129
Map<?, ?> lMapObject = (Map<?, ?>) aObject;
125130
Set<? extends Map.Entry<?, ?>> lEntrySet = lMapObject.entrySet();

src/main/java/net/workingdeveloper/java/vardump/impl/VarDumperFormatterImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.workingdeveloper.java.vardump.impl;
22

3+
import net.workingdeveloper.java.vardump.AppendableFactory;
34
import net.workingdeveloper.java.vardump.IVarDumperFormatter;
45

56
/**
@@ -11,7 +12,11 @@ public class VarDumperFormatterImpl
1112
extends AbstractVarDumperFormatter
1213
implements IVarDumperFormatter {
1314

14-
public VarDumperFormatterImpl(Appendable aBuffer, boolean aShortClassName) {
15-
super(aBuffer, aShortClassName);
15+
/**
16+
* @param aAppendableFactory Could be a lamba like <code>StringBuilder::new</code>.
17+
* @param aShortClassName <em>true</em> if the outputted class name should be just the class itself without the package name.
18+
*/
19+
public VarDumperFormatterImpl(AppendableFactory aAppendableFactory, boolean aShortClassName) {
20+
super(aAppendableFactory, aShortClassName);
1621
}
1722
}

src/main/java/net/workingdeveloper/java/vardump/impl/VarDumperFormatterSimpleImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.workingdeveloper.java.vardump.impl;
22

3+
import net.workingdeveloper.java.vardump.AppendableFactory;
34
import net.workingdeveloper.java.vardump.IVarDumperFormatter;
45

56
/**
@@ -10,7 +11,7 @@
1011
public class VarDumperFormatterSimpleImpl
1112
extends AbstractVarDumperFormatter
1213
implements IVarDumperFormatter {
13-
public VarDumperFormatterSimpleImpl(Appendable aBuffer, boolean aShortClassName) {
14-
super(aBuffer, aShortClassName);
14+
public VarDumperFormatterSimpleImpl(AppendableFactory aAppendableFactory, boolean aShortClassName) {
15+
super(aAppendableFactory, aShortClassName);
1516
}
1617
}

src/main/java/net/workingdeveloper/java/vardump/impl/VarDumperIndentFormatterImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.workingdeveloper.java.vardump.impl;
22

3+
import net.workingdeveloper.java.vardump.AppendableFactory;
34
import net.workingdeveloper.java.vardump.IVarDumperFormatter;
45

56
/**
@@ -11,7 +12,7 @@ public class VarDumperIndentFormatterImpl extends AbstractVarDumperFormatter imp
1112
private final int fIndentLevel;
1213
private int fCurrentIndention;
1314

14-
public VarDumperIndentFormatterImpl(Appendable aBuffer, int aIndentLevel, boolean aShortClassName) {
15+
public VarDumperIndentFormatterImpl(AppendableFactory aBuffer, int aIndentLevel, boolean aShortClassName) {
1516
super(aBuffer, aShortClassName);
1617
fIndentLevel = aIndentLevel;
1718
}

src/test/java/net/workingdeveloper/java/vardump/impl/RecursiveVarDumperImplTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8+
import static net.workingdeveloper.java.vardump.test.VarDumpMatcher.matchesVarDump;
9+
import static org.hamcrest.Matchers.*;
10+
import static org.junit.Assert.assertThat;
11+
812
/**
913
* Created by Christoph Graupner on 2016-10-17.
1014
*
@@ -46,7 +50,7 @@ public void testCyclic() throws Exception {
4650
TestCyclic lParent1 = new TestCyclic(lParent0);
4751
TestCyclic lChild = new TestCyclic(lParent1);
4852
RecursiveVarDumperImpl lSut = new RecursiveVarDumperImpl(
49-
new VarDumperFormatterImpl(new StringBuilder(), true),
53+
new VarDumperFormatterImpl(StringBuilder::new, true),
5054
field -> true,
5155
new VarDumperCyclicRegistryImpl()
5256
);
@@ -58,7 +62,7 @@ public void testCyclic() throws Exception {
5862
@Test
5963
public void vardumpInheritedClasses() throws Exception {
6064
RecursiveVarDumperImpl lSut = new RecursiveVarDumperImpl(
61-
new VarDumperFormatterImpl(new StringBuilder(), true),
65+
new VarDumperFormatterImpl(StringBuilder::new, true),
6266
field -> true,
6367
new VarDumperCyclicRegistryImpl()
6468
);
@@ -71,7 +75,7 @@ public void vardumpInheritedClasses() throws Exception {
7175
@Test
7276
public void vardumpLists() throws Exception {
7377
RecursiveVarDumperImpl lSut = new RecursiveVarDumperImpl(
74-
new VarDumperFormatterImpl(new StringBuilder(), true),
78+
new VarDumperFormatterImpl(StringBuilder::new, true),
7579
aField -> true,
7680
new VarDumperCyclicRegistryImpl()
7781
);
@@ -84,22 +88,26 @@ public void vardumpLists() throws Exception {
8488
@Test
8589
public void vardumpPrimitives() throws Exception {
8690
RecursiveVarDumperImpl lSut = new RecursiveVarDumperImpl(
87-
new VarDumperFormatterImpl(new StringBuilder(), true),
91+
new VarDumperFormatterImpl(StringBuilder::new, true),
8892
aField -> true,
8993
new VarDumperCyclicRegistryImpl()
9094
);
91-
System.out.println(lSut.vardump("hallo"));
92-
System.out.println(lSut.vardump(1));
95+
assertThat(lSut.vardump("hallo"), equalTo("(String)\"hallo\""));
96+
assertThat(lSut.vardump(1), equalTo("(Integer)1"));
9397
}
9498

9599
@Test
96100
public void vardumpSimpleClasses() throws Exception {
97101
RecursiveVarDumperImpl lSut = new RecursiveVarDumperImpl(
98-
new VarDumperFormatterImpl(new StringBuilder(), true),
102+
new VarDumperFormatterImpl(StringBuilder::new, true),
99103
aField -> true,
100104
new VarDumperCyclicRegistryImpl()
101105
);
102106
Object d1 = new TestEmpty();
107+
assertThat(
108+
lSut.vardump(d1),
109+
matchesVarDump("TestEmpty@00000000 {this$0 = RecursiveVarDumperImplTest@00000000 {};}")
110+
);
103111
System.out.println(lSut.vardump(d1));
104112
d1 = new TestPrimitives();
105113
System.out.println(lSut.vardump(d1));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.workingdeveloper.java.vardump.test;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.TypeSafeMatcher;
5+
6+
/**
7+
* Created by Christoph Graupner on 2016-12-26.
8+
*
9+
* @author Christoph Graupner <ch.graupner@workingdeveloper.net>
10+
*/
11+
public class VarDumpMatcher extends TypeSafeMatcher<String> {
12+
private static final String REPLACEMENT = "@00000000";
13+
private static final String UNIFIER = "@[A-Fa-f0-9]{4,}\\b";
14+
final String fMatch;
15+
16+
public VarDumpMatcher(final String aMatch) {
17+
fMatch = aMatch.replaceAll(UNIFIER, REPLACEMENT);
18+
}
19+
20+
public static VarDumpMatcher matchesVarDump(final String aVarDump) {
21+
return new VarDumpMatcher(aVarDump);
22+
}
23+
24+
@Override
25+
public void describeTo(Description description) {
26+
description.appendText("matches regular expression=`" + fMatch + "`");
27+
}
28+
29+
@Override
30+
protected boolean matchesSafely(String item) {
31+
return item.replaceAll(UNIFIER, REPLACEMENT).equals(fMatch);
32+
}
33+
}

0 commit comments

Comments
 (0)