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