Path: blob/master/test/jdk/java/util/Properties/SaveComments.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 508744826* @summary Verify that property.save saves comments correctly27*/2829import java.io.ByteArrayInputStream;30import java.io.ByteArrayOutputStream;31import java.io.IOException;32import java.util.Properties;3334public class SaveComments {3536public static void main(String[] argv) throws IOException {37String ls = System.getProperty("line.separator");38String[] input = new String[] {39"Comments with \u4e2d\u6587\u6c49\u5b57 included",40"Comments with \n Second comments line",41"Comments with \n# Second comments line",42"Comments with \n! Second comments line",43"Comments with last character is \n",44"Comments with last character is \r\n",45"Comments with last two characters are \n\n",46"Comments with last four characters are \r\n\r\n",47"Comments with \nkey4=value4",48"Comments with \n#key4=value4"};4950String[] output = new String[] {51"#Comments with \\u4E2D\\u6587\\u6C49\\u5B57 included" + ls,52"#Comments with " + ls + "# Second comments line" + ls,53"#Comments with " + ls + "# Second comments line" + ls,54"#Comments with " + ls + "! Second comments line" + ls,55"#Comments with last character is " + ls+"#"+ls,56"#Comments with last character is " + ls+"#"+ls,57"#Comments with last two characters are " + ls+"#"+ls+"#"+ls,58"#Comments with last four characters are " + ls+"#"+ls+"#"+ls};5960Properties props = new Properties();61boolean failed = false;62int i = 0;63for (i = 0; i < output.length; i++) {64ByteArrayOutputStream baos = new ByteArrayOutputStream(200);65props.store(baos, input[i]);66String result = baos.toString("iso8859-1");67if (result.indexOf(output[i]) == -1) {68System.out.println("Wrong \"store()\" output:");69System.out.println(result);70failed = true;71}72}7374props.put("key1", "value1");75props.put("key2", "value2");76props.put("key3", "value3");77for (; i < input.length; i++) {78ByteArrayOutputStream baos = new ByteArrayOutputStream(200);79props.store(baos, input[i]);80Properties propsNew = new Properties();81propsNew.load(new ByteArrayInputStream(baos.toByteArray()));82/*83Set<Map.Entry<Object, Object>> kvsetNew = propsNew.entrySet();84Set<Map.Entry<Object, Object>> kvset = props.entrySet();85if (!kvsetNew.containsAll(kvset) || !kvset.containsAll(kvsetNew)) {86*/87if (!props.equals (propsNew)) {88System.out.println("Wrong output:");89System.out.println(baos.toString("iso8859-1"));90failed = true;91}92}93if (failed)94throw new RuntimeException("Incorrect Properties Comment Output.");95}96}979899