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 import jdk.internal.util.SystemProps.Raw;
25 
26 import org.testng.Assert;
27 import org.testng.annotations.Test;
28 
29 import java.lang.reflect.Field;
30 import java.lang.reflect.Modifier;
31 
32 
33 /*
34  * @test
35  * @summary Test to verify that the SystemProps.Raw _xxx_NDX indices are unique and without gaps.
36  * @modules java.base/jdk.internal.util:+open
37  * @run testng VerifyRawIndexesTest
38  */
39 
40 @Test
41 public class VerifyRawIndexesTest {
42 
43     /**
44      * Check that the Raw._*_NDX indexes are sequential and followed by
45      * the FIXED_LENGTH value.
46      * It verifies there there are no gaps or duplication in the declarations.
47      */
48     @Test
verifyIndexes()49     void verifyIndexes() {
50         Field[] fields = Raw.class.getDeclaredFields();
51         int expectedModifiers = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
52 
53         int next = 0;       // indexes start at zero
54         int fixedLength = -1;
55         for (Field f : fields) {
56             try {
57                 int mods = f.getModifiers();
58                 String name = f.getName();
59                 if (((mods & expectedModifiers) == expectedModifiers) &&
60                     (name.endsWith("_NDX") || name.equals("FIXED_LENGTH"))) {
61                     f.setAccessible(true);
62                     int ndx = f.getInt(null);
63                     System.out.printf("%s: %s%n", name, ndx);
64                     Assert.assertEquals(ndx, next, "index value wrong");
65                     if (name.equals("FIXED_LENGTH")) {
66                         fixedLength = next;     // remember for final check
67                     }
68                     next++;
69                 } else {
70                     System.out.printf("Ignoring field: " + f);
71                 }
72             } catch (IllegalAccessException iae) {
73                 Assert.fail("unexpected exception", iae);
74             }
75         }
76         Assert.assertEquals(next - 1, fixedLength,
77                 "FIXED_LENGTH should be 1 greater than max of _NDX indexes");
78     }
79 }
80