Skip to content

Commit 0605ff1

Browse files
committed
test case
1 parent 8a39fd3 commit 0605ff1

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

test/org/opensolaris/opengrok/configuration/ConfigurationTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,94 @@
2424

2525
import java.beans.XMLDecoder;
2626
import java.beans.XMLEncoder;
27+
import java.io.BufferedInputStream;
2728
import java.io.ByteArrayInputStream;
2829
import java.io.ByteArrayOutputStream;
2930
import java.io.IOException;
31+
import java.lang.reflect.Field;
32+
import java.lang.reflect.Modifier;
3033
import java.util.LinkedList;
34+
import javax.xml.parsers.SAXParser;
35+
import javax.xml.parsers.SAXParserFactory;
3136
import junit.framework.AssertionFailedError;
37+
import org.junit.Assert;
3238
import org.junit.Test;
3339
import org.opensolaris.opengrok.util.ClassUtil;
3440

3541
import static org.junit.Assert.assertEquals;
3642
import static org.junit.Assert.assertNotNull;
43+
import org.xml.sax.Attributes;
44+
import org.xml.sax.ext.DefaultHandler2;
3745

3846
/**
3947
*
4048
* @author Krystof Tulinger
4149
*/
4250
public class ConfigurationTest {
4351

52+
private static class Handler extends DefaultHandler2 {
53+
54+
StringBuilder sb;
55+
56+
Handler() {
57+
sb = new StringBuilder();
58+
}
59+
60+
@Override
61+
public void startElement(String uri, String localName, String qname, Attributes attr) {
62+
if ("void".equals(qname)) {
63+
String prop = null;
64+
if ((prop = attr.getValue("property")) != null) {
65+
for (Field f : Group.class.getDeclaredFields()) {
66+
if (Modifier.isTransient(f.getModifiers())) {
67+
Assert.assertFalse("'" + f.getName() +
68+
"' field is transient and yet is present in XML " +
69+
"encoding of Group object",
70+
prop.equals(f.getName()));
71+
}
72+
}
73+
}
74+
}
75+
sb.setLength(0);
76+
}
77+
78+
@Override
79+
public void characters(char[] arg0, int arg1, int arg2) {
80+
sb.append(arg0, arg1, arg2);
81+
}
82+
}
83+
84+
/**
85+
* Verify that encoding of Group class does not contain transient members.
86+
* @throws Exception
87+
*/
88+
@Test
89+
public void testTransientKeywordGroups() throws Exception {
90+
Group foo = new Group("foo", "foo.*");
91+
Group bar = new Group("bar", "bar.*");
92+
93+
Configuration cfg = new Configuration();
94+
cfg.addGroup(foo);
95+
foo.addGroup(bar);
96+
cfg.addGroup(bar);
97+
98+
ByteArrayOutputStream out = new ByteArrayOutputStream();
99+
100+
try (XMLEncoder enc = new XMLEncoder(out)) {
101+
enc.writeObject(cfg);
102+
}
103+
104+
// In this test we are no so much interested in exceptions during the
105+
// XML decoding as that is covered by the {@code serializationOrderTest}
106+
// test.
107+
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
108+
SAXParserFactory factory = SAXParserFactory.newInstance();
109+
SAXParser saxParser = factory.newSAXParser();
110+
Handler handler = new Handler();
111+
saxParser.parse(new BufferedInputStream(in), handler);
112+
}
113+
}
114+
44115
/**
45116
* Test for a serialization bug in configuration. The problem is that with
46117
* this scenario the output is written in a way that when deserializing the

0 commit comments

Comments
 (0)