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 
  32 import java.io.*;
  33 import java.util.prefs.*;
  34 
  35 public class ExportSubtree {
  36    public static void main(String[] args) throws Exception {
  37       ByteArrayInputStream bais = new ByteArrayInputStream(importPrefs.getBytes("utf-8"));
  38       Preferences.importPreferences(bais);
  39       ByteArrayOutputStream baos = new ByteArrayOutputStream();
  40       Preferences.userRoot().node("testExportSubtree").exportSubtree(baos);
  41       Preferences.userRoot().node("testExportSubtree").removeNode();
  42       if (!expectedResult.equals(baos.toString())) {
  43           //System.out.print(baos.toString());
  44           //System.out.print(expectedResult);
  45           throw new IOException("exportSubtree does not output expected result");
  46       }
  47    }
  48 
  49    static String ls = System.getProperty("line.separator");
  50    static String importPrefs =
  51        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  52         + "<!DOCTYPE preferences SYSTEM \"http://java.sun.com/dtd/preferences.dtd\">"
  53         + "<preferences EXTERNAL_XML_VERSION=\"1.0\">"
  54         + "  <root type=\"user\">"
  55         + "    <map>"
  56         + "      <entry key=\"key1\" value=\"value1\"/>"
  57         + "    </map>"
  58         + "    <node name=\"testExportSubtree\">"
  59         + "      <map>"
  60         + "        <entry key=\"key2\" value=\"value2\"/>"
  61         + "      </map>"
  62         + "      <node name=\"test\">"
  63         + "        <map>"
  64         + "          <entry key=\"key3\" value=\"value3\"/>"
  65         + "        </map>"
  66         + "      </node>"
  67         + "    </node>"
  68         + "  </root>"
  69         + "</preferences>";
  70 
  71    static String expectedResult =
  72        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
  73         + ls    +  "<!DOCTYPE preferences SYSTEM \"http://java.sun.com/dtd/preferences.dtd\">"
  74         + ls    +  "<preferences EXTERNAL_XML_VERSION=\"1.0\">"
  75         + ls    +  "  <root type=\"user\">"
  76         + ls    +  "    <map/>"
  77         + ls    +  "    <node name=\"testExportSubtree\">"
  78         + ls    +  "      <map>"
  79         + ls    +  "        <entry key=\"key2\" value=\"value2\"/>"
  80         + ls    +  "      </map>"
  81         + ls    +  "      <node name=\"test\">"
  82         + ls    +  "        <map>"
  83         + ls    +  "          <entry key=\"key3\" value=\"value3\"/>"
  84         + ls    +  "        </map>"
  85         + ls    +  "      </node>"
  86         + ls    +  "    </node>"
  87         + ls    +  "  </root>"
  88         + ls    +  "</preferences>"     + ls;
  89 }