jdk/test/tools/launcher/ChangeDataModel.java
changeset 45062 37ed4313c8c1
parent 22066 f2133b498789
equal deleted inserted replaced
45061:74b09ee3cd55 45062:37ed4313c8c1
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 4894330 4810347 6277269 8029388
    26  * @bug 4894330 4810347 6277269 8029388 8169646
    27  * @compile -XDignore.symbol.file ChangeDataModel.java
    27  * @compile -XDignore.symbol.file ChangeDataModel.java
    28  * @run main ChangeDataModel
    28  * @run main ChangeDataModel
    29  * @summary Verify -d32 and -d64 options are accepted(rejected) on all platforms
    29  * @summary Verify -d32, -d64 and -J prefixed data-model options are rejected on all platforms
    30  * @author Joseph D. Darcy, ksrini
    30  * @author Joseph D. Darcy, ksrini
    31  */
    31  */
    32 import java.io.File;
    32 
    33 import java.util.ArrayList;
    33 import java.util.Arrays;
    34 import java.util.List;
       
    35 
    34 
    36 public class ChangeDataModel extends TestHelper {
    35 public class ChangeDataModel extends TestHelper {
    37     private static final File TestJar      = new File("test" + JAR_FILE_EXT);
       
    38     private static final String OptionName = "Args";
       
    39     private static final File TestOptionJar  = new File(OptionName + JAR_FILE_EXT);
       
    40     private static final String OPT_PREFIX = "ARCH_OPT:";
       
    41 
    36 
    42     static void createTestJar() throws Exception {
    37     public static void main(String... args) throws Exception {
    43         String[] code = {
    38         new ChangeDataModel().run(args);
    44             "   public static void main(String argv[]) {",
       
    45             "      System.out.println(\"" + OPT_PREFIX + "-d\" + System.getProperty(\"sun.arch.data.model\", \"none\"));",
       
    46             "   }",};
       
    47         createJar(TestJar, code);
       
    48     }
    39     }
    49     public static void main(String... args) throws Exception {
       
    50         createTestJar();
       
    51         createOptionsJar();
       
    52 
    40 
    53         // verify if data model flag for default data model is accepted, also
    41     @Test
    54         // verify if the complimentary data model is rejected.
    42     public void check32bitRejection() throws Exception {
    55         if (is32Bit) {
    43         checkRejection("-d32");
    56             checkAcceptance(javaCmd, "-d32");
    44     }
    57             checkRejection(javaCmd, "-d64");
    45 
    58             checkOption(javaCmd, "-d64");
    46     @Test
    59         } else if (is64Bit) {
    47     public void check64bitRejection() throws Exception {
    60             checkAcceptance(javaCmd, "-d64");
    48         checkRejection("-d64");
    61             checkRejection(javaCmd, "-d32");
    49     }
    62             checkOption(javaCmd, "-d32");
    50 
    63         } else {
    51     void checkRejection(String dmodel) throws Exception {
    64             throw new Error("unsupported data model");
    52         String expect = "Unrecognized option: " + dmodel;
       
    53         String[] cmds1 = {
       
    54             javaCmd,
       
    55             dmodel,
       
    56             "-version"
       
    57         };
       
    58         checkRejection(expect, cmds1);
       
    59 
       
    60         String[] cmds2 = {
       
    61             javacCmd,
       
    62             "-J" + dmodel,
       
    63             "-version"
       
    64         };
       
    65         checkRejection(expect, cmds2);
       
    66     }
       
    67 
       
    68 
       
    69     void checkRejection(String expect, String... cmds) throws Exception {
       
    70         TestResult tr = doExec(cmds);
       
    71         tr.checkNegative();
       
    72         if (!tr.contains(expect)) {
       
    73             System.out.println(tr);
       
    74             String error = "did not get " + "\'" + expect + "\'" +
       
    75                            "with options " + Arrays.asList(cmds);
       
    76             throw new Exception(error);
    65         }
    77         }
    66     }
    78     }
    67 
       
    68     static void checkAcceptance(String cmd, String dmodel) {
       
    69         TestResult tr = doExec(cmd, dmodel, "-jar", TestJar.getAbsolutePath());
       
    70         if (!tr.contains(OPT_PREFIX + dmodel)) {
       
    71             System.out.println(tr);
       
    72             String message = "Data model flag " + dmodel +
       
    73                     " not accepted or had improper effect.";
       
    74             throw new RuntimeException(message);
       
    75         }
       
    76     }
       
    77 
       
    78     static void checkRejection(String cmd, String dmodel) {
       
    79         TestResult tr = doExec(cmd, dmodel, "-jar", TestJar.getAbsolutePath());
       
    80         if (tr.contains(OPT_PREFIX + dmodel)) {
       
    81             System.out.println(tr);
       
    82             String message = "Data model flag " + dmodel + " was accepted.";
       
    83             throw new RuntimeException(message);
       
    84         }
       
    85     }
       
    86 
       
    87     static void checkOption(String cmd, String dmodel) throws Exception {
       
    88         TestResult tr = doExec(cmd, "-jar", TestOptionJar.getAbsolutePath(), dmodel);
       
    89         verifyOption(tr, dmodel);
       
    90 
       
    91         tr = doExec(cmd, "-cp", ".", OptionName, dmodel);
       
    92         verifyOption(tr, dmodel);
       
    93     }
       
    94 
       
    95     static void verifyOption(TestResult tr, String dmodel) {
       
    96         if (!tr.contains(OPT_PREFIX + dmodel)) {
       
    97             System.out.println(tr);
       
    98             String message = "app argument: " + dmodel + " not found.";
       
    99             throw new RuntimeException(message);
       
   100         }
       
   101         if (!tr.isOK()) {
       
   102             System.out.println(tr);
       
   103             String message = "app argument: " + dmodel + " interpreted ?";
       
   104             throw new RuntimeException(message);
       
   105         }
       
   106     }
       
   107 
       
   108     static void createOptionsJar() throws Exception {
       
   109         List<String> code = new ArrayList<>();
       
   110         code.add("public class Args {");
       
   111         code.add("   public static void main(String argv[]) {");
       
   112         code.add("       for (String x : argv)");
       
   113         code.add("           System.out.println(\"" + OPT_PREFIX + "\" + x);");
       
   114         code.add("   }");
       
   115         code.add("}");
       
   116         File optionsJava  = new File(OptionName + JAVA_FILE_EXT);
       
   117         createFile(optionsJava, code);
       
   118         File optionsClass = new File(OptionName + CLASS_FILE_EXT);
       
   119 
       
   120         compile(optionsJava.getName());
       
   121         createJar("cvfe",
       
   122                   TestOptionJar.getName(),
       
   123                   OptionName,
       
   124                   optionsClass.getName());
       
   125     }
       
   126 }
    79 }