Skip to content

Commit f7ed9f9

Browse files
authored
Merge pull request #1708 from vladak/ClassUtilTake2
Class util take2
2 parents a493a97 + b9ce6ba commit f7ed9f9

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/org/opensolaris/opengrok/util/ClassUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static void remarkTransientFields(Class targetClass) {
5656
BeanInfo info;
5757
info = Introspector.getBeanInfo(targetClass);
5858
PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors();
59-
for (Field f : Project.class.getDeclaredFields()) {
59+
for (Field f : targetClass.getDeclaredFields()) {
6060
if (Modifier.isTransient(f.getModifiers())) {
6161
for (int i = 0; i < propertyDescriptors.length; ++i) {
6262
if (propertyDescriptors[i].getName().equals(f.getName())) {

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,85 @@
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+
Handler() {
55+
}
56+
57+
@Override
58+
public void startElement(String uri, String localName, String qname, Attributes attr) {
59+
if ("void".equals(qname)) {
60+
String prop = null;
61+
if ((prop = attr.getValue("property")) != null) {
62+
for (Field f : Group.class.getDeclaredFields()) {
63+
if (Modifier.isTransient(f.getModifiers())) {
64+
Assert.assertFalse("'" + f.getName() +
65+
"' field is transient and yet is present in XML " +
66+
"encoding of Group object",
67+
prop.equals(f.getName()));
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}
74+
75+
/**
76+
* Verify that encoding of Group class does not contain transient members.
77+
* @throws Exception
78+
*/
79+
@Test
80+
public void testTransientKeywordGroups() throws Exception {
81+
Group foo = new Group("foo", "foo.*");
82+
Group bar = new Group("bar", "bar.*");
83+
84+
Configuration cfg = new Configuration();
85+
cfg.addGroup(foo);
86+
foo.addGroup(bar);
87+
cfg.addGroup(bar);
88+
89+
ByteArrayOutputStream out = new ByteArrayOutputStream();
90+
91+
try (XMLEncoder enc = new XMLEncoder(out)) {
92+
enc.writeObject(cfg);
93+
}
94+
95+
// In this test we are no so much interested in exceptions during the
96+
// XML decoding as that is covered by the {@code serializationOrderTest}
97+
// test.
98+
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
99+
SAXParserFactory factory = SAXParserFactory.newInstance();
100+
SAXParser saxParser = factory.newSAXParser();
101+
Handler handler = new Handler();
102+
saxParser.parse(new BufferedInputStream(in), handler);
103+
}
104+
}
105+
44106
/**
45107
* Test for a serialization bug in configuration. The problem is that with
46108
* this scenario the output is written in a way that when deserializing the

0 commit comments

Comments
 (0)