1/*
2 * Copyright (c) 2012, 2013, 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 4894330 4810347 6277269 8029388
27 * @compile -XDignore.symbol.file ChangeDataModel.java
28 * @run main ChangeDataModel
29 * @summary Verify -d32 and -d64 options are accepted(rejected) on all platforms
30 * @author Joseph D. Darcy, ksrini
31 */
32import java.io.File;
33import java.util.ArrayList;
34import java.util.List;
35
36public 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
42    static void createTestJar() throws Exception {
43        String[] code = {
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    }
49    public static void main(String... args) throws Exception {
50        createTestJar();
51        createOptionsJar();
52
53        // verify if data model flag for default data model is accepted, also
54        // verify if the complimentary data model is rejected.
55        if (is32Bit) {
56            checkAcceptance(javaCmd, "-d32");
57            checkRejection(javaCmd, "-d64");
58            checkOption(javaCmd, "-d64");
59        } else if (is64Bit) {
60            checkAcceptance(javaCmd, "-d64");
61            checkRejection(javaCmd, "-d32");
62            checkOption(javaCmd, "-d32");
63        } else {
64            throw new Error("unsupported data model");
65        }
66    }
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}
127