1 /*
   2  * Copyright (c) 2010, 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 6987827
  27  * @modules java.base/sun.security.util
  28  *          java.base/sun.security.tools.keytool
  29  *          jdk.jartool/sun.security.tools.jarsigner
  30  *          jdk.policytool/sun.security.tools.policytool
  31  * @summary security/util/Resources.java needs improvement
  32  * @run main/othervm NewNamesFormat
  33  */
  34 
  35 
  36 import java.lang.reflect.Method;
  37 import java.util.HashSet;
  38 import java.util.Set;
  39 
  40 /**
  41  * This test makes sure that the keys in resources files are using the new
  42  * format and there is no duplication.
  43  */
  44 public class NewNamesFormat {
  45     public static void main(String[] args) throws Exception {
  46         checkRes("sun.security.util.Resources");
  47         checkRes("sun.security.util.AuthResources");
  48         checkRes("sun.security.tools.jarsigner.Resources");
  49         checkRes("sun.security.tools.keytool.Resources");
  50         checkRes("sun.security.tools.policytool.Resources");
  51     }
  52 
  53     private static void checkRes(String resName) throws Exception {
  54         System.out.println("Checking " + resName + "...");
  55         Class clazz = Class.forName(resName);
  56         Method m = clazz.getMethod("getContents");
  57         Object[][] contents = (Object[][])m.invoke(clazz.newInstance());
  58         Set<String> keys = new HashSet<String>();
  59         for (Object[] pair: contents) {
  60             String key = (String)pair[0];
  61             if (keys.contains(key)) {
  62                 System.out.println("Found dup: " + key);
  63                 throw new Exception();
  64             }
  65             checkKey(key);
  66             keys.add(key);
  67         }
  68     }
  69 
  70     private static void checkKey(String key) throws Exception {
  71         for (char c: key.toCharArray()) {
  72             if (Character.isLetter(c) || Character.isDigit(c) ||
  73                     c == '{' || c == '}' || c == '.') {
  74                 // OK
  75             } else {
  76                 System.out.println("Illegal char [" + c + "] in key: " + key);
  77                 throw new Exception();
  78             }
  79         }
  80     }
  81 }