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