test/jdk/java/util/jar/Attributes/TestAttrsNL.java
changeset 50413 1234ff7199c7
equal deleted inserted replaced
50412:3d658c910e83 50413:1234ff7199c7
       
     1 /*
       
     2  * Copyright (c) 2018, 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 /* @test
       
    25  * @bug 8200530
       
    26  * @summary Test Attributes newline
       
    27  */
       
    28 
       
    29 import java.util.jar.Manifest;
       
    30 import java.util.jar.Attributes;
       
    31 import java.util.jar.Attributes.Name;
       
    32 import java.io.ByteArrayInputStream;
       
    33 import java.util.Map;
       
    34 
       
    35 import static java.nio.charset.StandardCharsets.UTF_8;
       
    36 
       
    37 public class TestAttrsNL {
       
    38 
       
    39     public static void main(String[] args) throws Throwable {
       
    40 
       
    41         String manifestStr =
       
    42             "Manifest-Version: 1.0\r\n" +
       
    43             "Created-By: 11 (Oracle Corporation)\r\n" +
       
    44             "key1: value1\r\n" +
       
    45             "key2: value2\r\n END\r\n" +
       
    46             "key3: value3\r\n \r\n" +
       
    47             "key4: value4\r\n" +
       
    48             "\r\n\r\n" +
       
    49             "Name: Hello\r\n" +
       
    50             "key11: value11\r\n" +
       
    51             "key22: value22\r\n END\r\n" +
       
    52             "key33: value33\r\n \r\n" +
       
    53             "key44: value44\r\n";
       
    54 
       
    55         Map<Name, String> mainAttrsExped = Map.of(
       
    56                 new Name("Manifest-Version"), "1.0",
       
    57                 new Name("Created-By"), "11 (Oracle Corporation)",
       
    58                 new Name("key1"), "value1",
       
    59                 new Name("key2"), "value2END",
       
    60                 new Name("key3"), "value3",
       
    61                 new Name("key4"), "value4"
       
    62         );
       
    63 
       
    64         Map<Name, String> attrsExped = Map.of(
       
    65                 new Name("key11"), "value11",
       
    66                 new Name("key22"), "value22END",
       
    67                 new Name("key33"), "value33",
       
    68                 new Name("key44"), "value44"
       
    69         );
       
    70 
       
    71         test(new Manifest(new ByteArrayInputStream(manifestStr.getBytes(UTF_8))),
       
    72              mainAttrsExped, attrsExped);
       
    73 
       
    74         test(new Manifest(new ByteArrayInputStream(
       
    75                     manifestStr.replaceAll("\r\n", "\r").getBytes(UTF_8))),
       
    76              mainAttrsExped, attrsExped);
       
    77 
       
    78         test(new Manifest(new ByteArrayInputStream(
       
    79                     manifestStr.replaceAll("\r\n", "\n").getBytes(UTF_8))),
       
    80              mainAttrsExped, attrsExped);
       
    81 
       
    82         // mixed
       
    83         manifestStr =
       
    84             "Manifest-Version: 1.0\r\n" +
       
    85             "Created-By: 11 (Oracle Corporation)\n" +
       
    86             "key1: value1\r" +
       
    87             "key2: value2\r\n END\r" +
       
    88             "key3: value3\n \r\n" +
       
    89             "key4: value4\r" +
       
    90             "\r\n\n" +
       
    91             "Name: Hello\r\n" +
       
    92             "key11: value11\r" +
       
    93             "key22: value22\n END\r\n" +
       
    94             "key33: value33\r \n" +
       
    95             "key44: value44\n";
       
    96         test(new Manifest(new ByteArrayInputStream(manifestStr.getBytes(UTF_8))),
       
    97              mainAttrsExped, attrsExped);
       
    98 
       
    99 
       
   100     }
       
   101 
       
   102     private static void test(Manifest m,
       
   103                              Map<Name, String> mainAttrsExped,
       
   104                              Map<Name, String> attrsExped) {
       
   105         Attributes mainAttrs = m.getMainAttributes();
       
   106         mainAttrsExped.forEach( (k, v) -> {
       
   107             if (!mainAttrs.containsKey(k) || !mainAttrs.get(k).equals(v)) {
       
   108                 System.out.printf(" containsKey(%s) : %b%n", k, mainAttrs.containsKey(k));
       
   109                 System.out.printf("         get(%s) : %s%n", k, mainAttrs.get(k));
       
   110                 throw new RuntimeException("expected attr: k=<" + k + ">, v=<" + v + ">");
       
   111             }
       
   112         });
       
   113 
       
   114         Attributes attrs = m.getAttributes("Hello");
       
   115         attrs.forEach( (k, v) -> {
       
   116             if (!attrs.containsKey(k) || !attrs.get(k).equals(v)) {
       
   117                 System.out.printf(" containsKey(%s) : %b%n", k, attrs.containsKey(k));
       
   118                 System.out.printf("         get(%s) : %s%n", k, attrs.get(k));
       
   119                 throw new RuntimeException("expected attr: k=<" + k + ">, v=<" + v + ">");
       
   120             }
       
   121         });
       
   122     }
       
   123 }