Skip to content

Commit ba67231

Browse files
committed
Added PropertlyDebug as a helper class for debugging.
1 parent 5d9e74f commit ba67231

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package de.adito.propertly.core.common;
2+
3+
import de.adito.propertly.core.spi.*;
4+
5+
/**
6+
* Little helper class for debugging.
7+
*
8+
* @author j.boesl, 14.01.15
9+
*/
10+
public class PropertlyDebug
11+
{
12+
13+
/**
14+
* Transforms given IHierarchy to a readable string.
15+
*
16+
* @param pHierarchy the starting point.
17+
* @return the whole tree as a string.
18+
*/
19+
public static String toTreeString(IHierarchy<?> pHierarchy)
20+
{
21+
return toTreeString(pHierarchy.getProperty());
22+
}
23+
24+
/**
25+
* Transforms given IPropertyPitProvider to a readable string.
26+
*
27+
* @param pPitProvider the starting point.
28+
* @return the subtree starting at the supplied IPropertyPitProvider as a string.
29+
*/
30+
public static String toTreeString(IPropertyPitProvider<?, ?, ?> pPitProvider)
31+
{
32+
return toTreeString(pPitProvider.getPit().getOwnProperty());
33+
}
34+
35+
/**
36+
* Transforms given IProperty to a readable string.
37+
*
38+
* @param pProperty the starting point.
39+
* @return the subtree starting at the supplied IProperty as a string.
40+
*/
41+
public static String toTreeString(IProperty<?, ?> pProperty)
42+
{
43+
StringBuilder stringBuilder = new StringBuilder();
44+
_buildTree(stringBuilder, pProperty, "");
45+
return stringBuilder.toString();
46+
}
47+
48+
private static void _buildTree(StringBuilder pStr, IProperty<?, ?> pProperty, String pLevel)
49+
{
50+
if (pProperty == null)
51+
return;
52+
boolean isPitProvider = IPropertyPitProvider.class.isAssignableFrom(pProperty.getType());
53+
Object value = pProperty.getValue();
54+
55+
pStr.append(pLevel);
56+
if (isPitProvider)
57+
pStr.append("/");
58+
else
59+
pStr.append(" ");
60+
pStr.append(pProperty.getName());
61+
if (!isPitProvider)
62+
pStr.append(" : ").append(value);
63+
pStr.append("\n");
64+
if (isPitProvider)
65+
{
66+
assert value != null;
67+
for (IProperty<?, ?> childProperty : ((IPropertyPitProvider<?, ?, ?>) value).getPit().getProperties())
68+
_buildTree(pStr, childProperty, pLevel + "\t");
69+
}
70+
}
71+
72+
}

propertly.core/src/test/java/de/adito/propertly/test/core/PropertyTest.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package de.adito.propertly.test.core;
22

33
import de.adito.propertly.core.api.Hierarchy;
4-
import de.adito.propertly.core.common.PropertyPitEventAdapter;
4+
import de.adito.propertly.core.common.*;
55
import de.adito.propertly.core.common.exception.InaccessibleException;
66
import de.adito.propertly.core.spi.*;
77
import de.adito.propertly.test.core.impl.ColoredPitProvider;
@@ -26,8 +26,8 @@ public void simpleTest()
2626
{
2727
final StringBuilder resultStringBuild = new StringBuilder();
2828

29-
IHierarchy<TProperty> IHierarchy = new VerifyingHierarchy<TProperty>(new Hierarchy<TProperty>("root", new TProperty()));
30-
IHierarchy.addStrongListener(new IPropertyPitEventListener()
29+
IHierarchy<TProperty> hierarchy = new VerifyingHierarchy<TProperty>(new Hierarchy<TProperty>("root", new TProperty()));
30+
hierarchy.addStrongListener(new IPropertyPitEventListener()
3131
{
3232
@Override
3333
public void propertyChanged(IProperty pProperty, Object pOldValue, Object pNewValue)
@@ -53,7 +53,7 @@ public void propertyRemoved(IPropertyPitProvider pSource, IPropertyDescription p
5353
_append(resultStringBuild, "hierarchy propertyRemoved", pSource, pPropertyDescription);
5454
}
5555
});
56-
TProperty tProperty = IHierarchy.getValue();
56+
TProperty tProperty = hierarchy.getValue();
5757
//GetterSetterGen.run(tProperty);
5858
tProperty.getPit().addStrongListener(new PropertyPitEventAdapter()
5959
{
@@ -133,6 +133,20 @@ public void propertyAdded(IPropertyPitProvider pSource, IPropertyDescription pPr
133133

134134
Assert.assertEquals(expected,
135135
resultStringBuild.toString());
136+
137+
expected = "/root\n" +
138+
"\t X : 123\n" +
139+
"\t Y : null\n" +
140+
"\t FF : java.awt.Dimension[width=123,height=456]\n" +
141+
"\t MAP : null\n" +
142+
"\t/CHILD\n" +
143+
"\t\t color1 : java.awt.Color[r=0,g=0,b=0]\n" +
144+
"\t\t color2 : java.awt.Color[r=255,g=0,b=0]\n" +
145+
"\t WIDTH : null\n" +
146+
"\t HEIGHT : null\n";
147+
148+
Assert.assertEquals(expected,
149+
PropertlyDebug.toTreeString(hierarchy));
136150
}
137151

138152
private static void _append(StringBuilder pStrBuilder, String pEvent, Object... pAdd)
@@ -174,8 +188,8 @@ else if (o instanceof IPropertyDescription)
174188
@Test
175189
public void readWriteTest()
176190
{
177-
IHierarchy<ColoredPitProvider> IHierarchy = new VerifyingHierarchy<ColoredPitProvider>(new Hierarchy<ColoredPitProvider>("root", new ColoredPitProvider()));
178-
IPropertyPit<IPropertyPitProvider, ColoredPitProvider, Color> pit = IHierarchy.getValue().getPit();
191+
IHierarchy<ColoredPitProvider> hierarchy = new VerifyingHierarchy<ColoredPitProvider>(new Hierarchy<ColoredPitProvider>("root", new ColoredPitProvider()));
192+
IPropertyPit<IPropertyPitProvider, ColoredPitProvider, Color> pit = hierarchy.getValue().getPit();
179193

180194

181195
pit.setValue(ColoredPitProvider.DEFAULT_COLOR, Color.MAGENTA);

0 commit comments

Comments
 (0)