test/java/util/Properties/XMLReadAndWriteTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 24 Jun 2009 16:38:24 +0200
branchxml-sax-and-dom-2
changeset 1262 52864f10883d
parent 1261 29f4f9b451b8
permissions -rw-r--r--
By default (when no XML SAX and DOM 2 present) parsing the properties stream with simple nanoXML parser
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 
     6 import java.io.ByteArrayInputStream;
     7 import sun.util.xml.PropertiesXMLUtils;
     8 
     9 import java.io.ByteArrayOutputStream;
    10 import java.io.IOException;
    11 import java.util.Properties;
    12 
    13 /** Checks whether reading and writing via standard DOM and simplified API
    14  * results in same results.
    15  *
    16  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    17  */
    18 public class XMLReadAndWriteTest {
    19     private static PropertiesXMLUtils FULL = new com.sun.xml.internal.PropertiesXMLUtilsImpl();
    20     private static PropertiesXMLUtils SIMPLE = new sun.util.xml.DefaultPropertiesXMLUtils();
    21 
    22 
    23     public static void main(String[] args) throws Exception {
    24         XMLReadAndWriteTest test = new XMLReadAndWriteTest();
    25         test.testCompareOutput();
    26         test.testCompareInput();
    27     }
    28 
    29 
    30     public XMLReadAndWriteTest() {
    31     }
    32 
    33     public void testCompareOutput() throws IOException {
    34         Properties p = new Properties();
    35         p.setProperty("ahoj", "simple");
    36         p.setProperty("kuk", "buk");
    37         p.setProperty("multi", "one\ntwo\nthree\nfour");
    38 
    39         ByteArrayOutputStream full = new ByteArrayOutputStream();
    40         FULL.save(p, full, "my commment on beginging\nand on the second line\n", "UTF-8");
    41 
    42         ByteArrayOutputStream simple = new ByteArrayOutputStream();
    43         SIMPLE.save(p, simple, "my commment on beginging\nand on the second line\n", "UTF-8");
    44         if (full.toString().equals(simple.toString())) {
    45             // OK
    46             System.err.println("OK: testCompareOutput");
    47         } else {
    48             assert false :
    49                 "Full version differs from simplified. Full:\n" + full + "\nSimple:\n" + simple;
    50         }
    51     }
    52 
    53     public void testCompareInput() throws IOException {
    54         String text = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>" +
    55             "  <!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'>" +
    56             "<properties>\n" +
    57             "<comment>my commment on beginging\n" +
    58             "and on the second line" +
    59             "</comment>" +
    60             "<entry key=\"ahoj\">simple</entry>\n" +
    61             "         <entry key=\"kuk\">buk</entry>" +
    62             "   <entry key='multi'>one\n" +
    63             "two\n" +
    64             "three\n" +
    65             "four</entry>\n</properties>";
    66 
    67         Properties p1 = new Properties();
    68         {
    69             ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
    70             FULL.load(p1, is);
    71         }
    72 
    73         Properties p2 = new Properties();
    74         {
    75             ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
    76             SIMPLE.load(p2, is);
    77         }
    78 
    79         assert p1.equals(p2) : "P1: " + p1 + "\nP2: " + p2;
    80         System.err.println("OK: testCompareInput");
    81     }
    82 
    83 }