langtools/test/com/sun/javadoc/testOrdering/TestOrdering.java
changeset 25004 b33effe4f252
parent 24399 af1a0220d0fa
child 25300 3b8a5067fe29
equal deleted inserted replaced
24905:bd3cf6f243be 25004:b33effe4f252
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8039410 8042601
    26  * @bug 8039410 8042601 8042829
    27  * @summary test to determine if members are ordered correctly
    27  * @summary test to determine if members are ordered correctly
    28  * @author ksrini
    28  * @author ksrini
    29  * @library ../lib/
    29  * @library ../lib/
    30  * @build JavadocTester
    30  * @build JavadocTester
    31  * @run main TestOrdering
    31  * @run main TestOrdering
    32  */
    32  */
    33 
    33 
       
    34 import java.io.File;
       
    35 import java.io.IOException;
       
    36 import java.nio.file.Files;
       
    37 import java.util.ArrayList;
       
    38 import java.util.Arrays;
       
    39 import java.util.Collections;
       
    40 import java.util.List;
       
    41 
       
    42 import static java.nio.file.StandardOpenOption.*;
       
    43 
    34 public class TestOrdering extends JavadocTester {
    44 public class TestOrdering extends JavadocTester {
    35 
    45 
    36     public static void main(String[] args) throws Exception {
    46     public static void main(String[] args) throws Exception {
    37         TestOrdering tester = new TestOrdering();
    47         TestOrdering tester = new TestOrdering();
    38         tester.runTests();
    48         tester.runTests();
    39     }
    49     }
    40 
    50 
    41     @Test
    51     @Test
    42     void testUnnamedPackages() {
    52     void testUnnamedPackagesForClassUse() {
    43         javadoc("-d", "out",
    53         javadoc("-d", "out",
    44                 "-sourcepath", testSrc,
    54                 "-sourcepath", testSrc,
    45                 "-use",
    55                 "-use",
    46                 testSrc("C.java"), testSrc("UsedInC.java"));
    56                 testSrc("C.java"), testSrc("UsedInC.java"));
    47         checkExit(Exit.OK);
    57         checkExit(Exit.OK);
    48         checkExecutableMemberOrdering("class-use/UsedInC.html");
    58         checkExecutableMemberOrdering("class-use/UsedInC.html");
    49     }
    59     }
    50 
    60 
    51     @Test
    61     @Test
    52     void testNamedPackages() {
    62     void testNamedPackagesForClassUse() {
    53         javadoc("-d", "out-1",
    63         javadoc("-d", "out-1",
    54                 "-sourcepath", testSrc,
    64                 "-sourcepath", testSrc,
    55                 "-use",
    65                 "-use",
    56                 "pkg1");
    66                 "pkg1");
    57         checkExit(Exit.OK);
    67         checkExit(Exit.OK);
    58         checkClassUseOrdering("pkg1/class-use/UsedClass.html");
    68         checkClassUseOrdering("pkg1/class-use/UsedClass.html");
    59         checkIndexPathOrdering("index-all.html");
    69     }
    60     }
    70 
    61 
    71     enum ListOrder { NONE, REVERSE, SHUFFLE };
       
    72     /*
       
    73      * By default we do not shuffle the input list, in order to keep the list deterministic,
       
    74      * and the test predictable. However, we can turn on the stress mode, by setting the following
       
    75      * property if required.
       
    76      */
       
    77     static final ListOrder STRESS_MODE = Boolean.getBoolean("TestOrder.STRESS")
       
    78             ? ListOrder.SHUFFLE
       
    79             : ListOrder.REVERSE;
       
    80 
       
    81     /*
       
    82      * Controls the number of sibling packages,  pkg0, pkg1, pkg2, .....
       
    83      */
       
    84     static final int MAX_PACKAGES = 4;
       
    85 
       
    86     /*
       
    87      * Controls the number of children packages, pkg0, pkg0.pkg, pkg0.pkg.pkg, .....
       
    88      * Note: having too long a depth (> 256 chars on Windows), will likely lead to
       
    89      * cause problems with automated build and test systems.
       
    90      */
       
    91     static final int MAX_SUBPACKAGES_DEPTH = 4;
       
    92     @Test
       
    93     void testIndexOrdering() throws IOException {
       
    94         final String clsname = "Add";
       
    95         List<String> cmdArgs = new ArrayList();
       
    96         cmdArgs.add("-d");
       
    97         cmdArgs.add("out-2");
       
    98         cmdArgs.add("-sourcepath");
       
    99         cmdArgs.add("src");
       
   100         cmdArgs.add("-package");
       
   101         System.out.println("STRESS_MODE: " + STRESS_MODE);
       
   102         emitFile(null, clsname, STRESS_MODE);
       
   103         for (int width = 0 ; width < MAX_PACKAGES ; width++) {
       
   104             String wpkgname = "add" + width;
       
   105             String dpkgname = wpkgname;
       
   106             emitFile(wpkgname, clsname, ListOrder.NONE); // list as-is
       
   107             cmdArgs.add(wpkgname);
       
   108             for (int depth = 1 ; depth < MAX_SUBPACKAGES_DEPTH ; depth++) {
       
   109                 dpkgname = dpkgname + ".add";
       
   110                 emitFile(dpkgname, clsname, STRESS_MODE);
       
   111                 cmdArgs.add(dpkgname);
       
   112             }
       
   113         }
       
   114         File srcDir = new File(new File("."), "src");
       
   115         cmdArgs.add(new File(srcDir, clsname + ".java").getPath());
       
   116         javadoc(cmdArgs.toArray(new String[cmdArgs.size()]));
       
   117         checkExit(Exit.OK);
       
   118         checkOrder("index-all.html", composeTestVectors());
       
   119     }
       
   120     String[] composeTestVectors() {
       
   121         List<String> testList = new ArrayList<>();
       
   122 
       
   123         for (String x : expectedMethodOrdering) {
       
   124             testList.add(x);
       
   125             for (int i = 0; i < MAX_PACKAGES; i++) {
       
   126                 String wpkg = "add" + i;
       
   127                 testList.add(wpkg + "/" + x);
       
   128                 String dpkg = wpkg;
       
   129                 for (int j = 1; j < MAX_SUBPACKAGES_DEPTH; j++) {
       
   130                     dpkg = dpkg + "/" + "add";
       
   131                     testList.add(dpkg + "/" + x);
       
   132                 }
       
   133             }
       
   134         }
       
   135         for (String x : expectedEnumOrdering) {
       
   136             testList.add(x.replace("REPLACE_ME", "&lt;Unnamed&gt;"));
       
   137             for (int i = 0; i < MAX_PACKAGES; i++) {
       
   138                 String wpkg = "add" + i;
       
   139                 testList.add(wpkg + "/" + x.replace("REPLACE_ME", wpkg));
       
   140                 String dpkg = wpkg;
       
   141                 for (int j = 1; j < MAX_SUBPACKAGES_DEPTH; j++) {
       
   142                     dpkg = dpkg + "/" + "add";
       
   143                     testList.add(dpkg + "/" + x.replace("REPLACE_ME", pathToPackage(dpkg)));
       
   144                 }
       
   145             }
       
   146         }
       
   147         testList.addAll(Arrays.asList(expectedFieldOrdering));
       
   148         return testList.toArray(new String[testList.size()]);
       
   149     }
    62     void checkExecutableMemberOrdering(String usePage) {
   150     void checkExecutableMemberOrdering(String usePage) {
    63         String contents = readFile(usePage);
   151         String contents = readFile(usePage);
    64         // check constructors
   152         // check constructors
    65         checking("constructors");
   153         checking("constructors");
    66         int idx1 = contents.indexOf("C.html#C-UsedInC");
   154         int idx1 = contents.indexOf("C.html#C-UsedInC");
   107             }
   195             }
   108             lastidx = idx;
   196             lastidx = idx;
   109         }
   197         }
   110     }
   198     }
   111 
   199 
   112     void checkIndexPathOrdering(String indexPage) {
   200     static String[] contents = {
   113         checkOrder(indexPage,
   201         "public add ADDADD;",
   114             "pkg1/UsedClass.html#add--",
   202         "public add AddAdd;",
   115             "pkg1/ZZTop.html#add--",
   203         "public add addadd;",
   116             "pkg1/UsedClass.html#add-double-",
   204         "public enum add {add, ADD, addd, ADDD};",
   117             "pkg1/UsedClass.html#add-java.lang.Double-",
   205         "public enum ADD {ADD, add, addd, ADDD};",
   118             "pkg1/ZZTop.html#add-double-",
   206         "public void   add(){}",
   119             "pkg1/ZZTop.html#add-java.lang.Double-",
   207         "public void   add(double d){}",
   120             "pkg1/UsedClass.html#add-double-byte-",
   208         "public void   add(int i, float f){}",
   121             "pkg1/ZZTop.html#add-double-byte-",
   209         "public void   add(float f, int i){}",
   122             "pkg1/UsedClass.html#add-double-double-",
   210         "public void   add(double d, byte b){}",
   123             "pkg1/UsedClass.html#add-double-java.lang.Double-",
   211         "public Double add(Double d) {return (double) 22/7;}",
   124             "pkg1/ZZTop.html#add-double-double-",
   212         "public double add(double d1, double d2) {return d1 + d2;}",
   125             "pkg1/ZZTop.html#add-double-java.lang.Double-",
   213         "public double add(double d1, Double  d2) {return d1 + d2;}",
   126             "pkg1/UsedClass.html#add-float-",
   214         "public Float  add(float f) {return (float) 22/7;}",
   127             "pkg1/ZZTop.html#add-float-",
   215         "public void   add(int i){}",
   128             "pkg1/UsedClass.html#add-float-int-",
   216         "public int    add(Integer i) {return 0;}"
   129             "pkg1/ZZTop.html#add-float-int-",
   217     };
   130             "pkg1/UsedClass.html#add-int-",
   218 
   131             "pkg1/ZZTop.html#add-int-",
   219     void emitFile(String pkgname, String clsname, ListOrder order) throws IOException {
   132             "pkg1/UsedClass.html#add-int-float-",
   220         File srcDir = new File("src");
   133             "pkg1/ZZTop.html#add-int-float-",
   221         File outDir = pkgname == null
   134             "pkg1/UsedClass.html#add-java.lang.Integer-",
   222             ? srcDir
   135             "pkg1/ZZTop.html#add-java.lang.Integer-");
   223             : new File(srcDir, pkgname.replace(".", File.separator));
   136     }
   224         File outFile = new File(outDir, clsname + ".java");
       
   225         outDir.mkdirs();
       
   226         List<String> scratch = new ArrayList<>(Arrays.asList(contents));
       
   227         switch (order) {
       
   228             case SHUFFLE:
       
   229                 Collections.shuffle(scratch);
       
   230                 break;
       
   231             case REVERSE:
       
   232                 Collections.reverse(scratch);
       
   233                 break;
       
   234             default:
       
   235                 // leave list as-is
       
   236         }
       
   237         // insert the header
       
   238         scratch.add(0, "public class " + clsname + " {");
       
   239         if (pkgname != null) {
       
   240             scratch.add(0, "package " + pkgname + ";");
       
   241         }
       
   242         // append the footer
       
   243         scratch.add("}");
       
   244         Files.write(outFile.toPath(), scratch, CREATE, TRUNCATE_EXISTING);
       
   245     }
       
   246 
       
   247     String pathToPackage(String in) {
       
   248         return in.replace("/", ".");
       
   249     }
       
   250 
       
   251     final String expectedMethodOrdering[] = {
       
   252         "Add.html#add--",
       
   253         "Add.html#add-double-",
       
   254         "Add.html#add-java.lang.Double-",
       
   255         "Add.html#add-double-byte-",
       
   256         "Add.html#add-double-double-",
       
   257         "Add.html#add-double-java.lang.Double-",
       
   258         "Add.html#add-float-",
       
   259         "Add.html#add-float-int-",
       
   260         "Add.html#add-int-",
       
   261         "Add.html#add-int-float-",
       
   262         "Add.html#add-java.lang.Integer-"
       
   263     };
       
   264     final String expectedEnumOrdering[] = {
       
   265         "Add.add.html\" title=\"enum in REPLACE_ME\"",
       
   266         "Add.ADD.html\" title=\"enum in REPLACE_ME\""
       
   267     };
       
   268     final String expectedFieldOrdering[] = {
       
   269         "Add.html#addadd\"",
       
   270         "add0/add/add/add/Add.html#addadd\"",
       
   271         "add0/add/add/Add.html#addadd\"",
       
   272         "add0/add/Add.html#addadd\"",
       
   273         "add0/Add.html#addadd\"",
       
   274         "add1/add/add/add/Add.html#addadd\"",
       
   275         "add1/add/add/Add.html#addadd\"",
       
   276         "add1/add/Add.html#addadd\"",
       
   277         "add1/Add.html#addadd\"",
       
   278         "add2/add/add/add/Add.html#addadd\"",
       
   279         "add2/add/add/Add.html#addadd\"",
       
   280         "add2/add/Add.html#addadd\"",
       
   281         "add2/Add.html#addadd\"",
       
   282         "add3/add/add/add/Add.html#addadd\"",
       
   283         "add3/add/add/Add.html#addadd\"",
       
   284         "add3/add/Add.html#addadd\"",
       
   285         "add3/Add.html#addadd\"",
       
   286         "Add.html#AddAdd\"",
       
   287         "add0/add/add/add/Add.html#AddAdd\"",
       
   288         "add0/add/add/Add.html#AddAdd\"",
       
   289         "add0/add/Add.html#AddAdd\"",
       
   290         "add0/Add.html#AddAdd\"",
       
   291         "add1/add/add/add/Add.html#AddAdd\"",
       
   292         "add1/add/add/Add.html#AddAdd\"",
       
   293         "add1/add/Add.html#AddAdd\"",
       
   294         "add1/Add.html#AddAdd\"",
       
   295         "add2/add/add/add/Add.html#AddAdd\"",
       
   296         "add2/add/add/Add.html#AddAdd\"",
       
   297         "add2/add/Add.html#AddAdd\"",
       
   298         "add2/Add.html#AddAdd\"",
       
   299         "add3/add/add/add/Add.html#AddAdd\"",
       
   300         "add3/add/add/Add.html#AddAdd\"",
       
   301         "add3/add/Add.html#AddAdd\"",
       
   302         "add3/Add.html#AddAdd\"",
       
   303         "Add.html#ADDADD\"",
       
   304         "add0/add/add/add/Add.html#ADDADD\"",
       
   305         "add0/add/add/Add.html#ADDADD\"",
       
   306         "add0/add/Add.html#ADDADD\"",
       
   307         "add0/Add.html#ADDADD\"",
       
   308         "add1/add/add/add/Add.html#ADDADD\"",
       
   309         "add1/add/add/Add.html#ADDADD\"",
       
   310         "add1/add/Add.html#ADDADD\"",
       
   311         "add1/Add.html#ADDADD\"",
       
   312         "add2/add/add/add/Add.html#ADDADD\"",
       
   313         "add2/add/add/Add.html#ADDADD\"",
       
   314         "add2/add/Add.html#ADDADD\"",
       
   315         "add2/Add.html#ADDADD\"",
       
   316         "add3/add/add/add/Add.html#ADDADD\"",
       
   317         "add3/add/add/Add.html#ADDADD\"",
       
   318         "add3/add/Add.html#ADDADD\"",
       
   319         "add3/Add.html#ADDADD\""
       
   320     };
   137 }
   321 }