|
24 | 24 |
|
25 | 25 | import java.beans.XMLDecoder;
|
26 | 26 | import java.beans.XMLEncoder;
|
| 27 | +import java.io.BufferedInputStream; |
27 | 28 | import java.io.ByteArrayInputStream;
|
28 | 29 | import java.io.ByteArrayOutputStream;
|
29 | 30 | import java.io.IOException;
|
| 31 | +import java.lang.reflect.Field; |
| 32 | +import java.lang.reflect.Modifier; |
30 | 33 | import java.util.LinkedList;
|
| 34 | +import javax.xml.parsers.SAXParser; |
| 35 | +import javax.xml.parsers.SAXParserFactory; |
31 | 36 | import junit.framework.AssertionFailedError;
|
| 37 | +import org.junit.Assert; |
32 | 38 | import org.junit.Test;
|
33 | 39 | import org.opensolaris.opengrok.util.ClassUtil;
|
34 | 40 |
|
35 | 41 | import static org.junit.Assert.assertEquals;
|
36 | 42 | import static org.junit.Assert.assertNotNull;
|
| 43 | +import org.xml.sax.Attributes; |
| 44 | +import org.xml.sax.ext.DefaultHandler2; |
37 | 45 |
|
38 | 46 | /**
|
39 | 47 | *
|
40 | 48 | * @author Krystof Tulinger
|
41 | 49 | */
|
42 | 50 | public class ConfigurationTest {
|
43 | 51 |
|
| 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 | + |
44 | 106 | /**
|
45 | 107 | * Test for a serialization bug in configuration. The problem is that with
|
46 | 108 | * this scenario the output is written in a way that when deserializing the
|
|
0 commit comments