1/*
2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25/* @test
26 * @bug 6203576 4700020 7197662
27 * @summary checks if the output of exportSubtree() is identical to
28 *          the output from previous release.
29 * @run main/othervm -Djava.util.prefs.userRoot=. ExportSubtree
30 */
31
32import java.io.*;
33import java.util.prefs.*;
34
35public class ExportSubtree {
36   public static void main(String[] args) throws Exception {
37      try
38      {
39          //File f = new File(System.getProperty("test.src", "."), "TestPrefs.xml");
40          ByteArrayInputStream bais = new ByteArrayInputStream(importPrefs.getBytes("utf-8"));
41          Preferences.importPreferences(bais);
42          ByteArrayOutputStream baos = new ByteArrayOutputStream();
43          Preferences.userRoot().node("testExportSubtree").exportSubtree(baos);
44          Preferences.userRoot().node("testExportSubtree").removeNode();
45          if (!expectedResult.equals(baos.toString())) {
46              //System.out.print(baos.toString());
47              //System.out.print(expectedResult);
48              throw new IOException("exportSubtree does not output expected result");
49          }
50      }
51      catch( Exception e ) {
52         e.printStackTrace();
53      }
54   }
55
56   static String ls = System.getProperty("line.separator");
57   static String importPrefs =
58       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
59        + "<!DOCTYPE preferences SYSTEM \"http://java.sun.com/dtd/preferences.dtd\">"
60        + "<preferences EXTERNAL_XML_VERSION=\"1.0\">"
61        + "  <root type=\"user\">"
62        + "    <map>"
63        + "      <entry key=\"key1\" value=\"value1\"/>"
64        + "    </map>"
65        + "    <node name=\"testExportSubtree\">"
66        + "      <map>"
67        + "        <entry key=\"key2\" value=\"value2\"/>"
68        + "      </map>"
69        + "      <node name=\"test\">"
70        + "        <map>"
71        + "          <entry key=\"key3\" value=\"value3\"/>"
72        + "        </map>"
73        + "      </node>"
74        + "    </node>"
75        + "  </root>"
76        + "</preferences>";
77
78   static String expectedResult =
79       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
80        + ls    +  "<!DOCTYPE preferences SYSTEM \"http://java.sun.com/dtd/preferences.dtd\">"
81        + ls    +  "<preferences EXTERNAL_XML_VERSION=\"1.0\">"
82        + ls    +  "  <root type=\"user\">"
83        + ls    +  "    <map/>"
84        + ls    +  "    <node name=\"testExportSubtree\">"
85        + ls    +  "      <map>"
86        + ls    +  "        <entry key=\"key2\" value=\"value2\"/>"
87        + ls    +  "      </map>"
88        + ls    +  "      <node name=\"test\">"
89        + ls    +  "        <map>"
90        + ls    +  "          <entry key=\"key3\" value=\"value3\"/>"
91        + ls    +  "        </map>"
92        + ls    +  "      </node>"
93        + ls    +  "    </node>"
94        + ls    +  "  </root>"
95        + ls    +  "</preferences>"     + ls;
96}
97