1 /*
   2  * Copyright (c) 2015, 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 8072480
  27  * @summary Verify option clash between -platform and -source is reported correctly.
  28  */
  29 
  30 import java.io.ByteArrayOutputStream;
  31 import java.io.File;
  32 import java.lang.reflect.Field;
  33 import java.util.ArrayList;
  34 import java.util.Arrays;
  35 import java.util.List;
  36 
  37 import javax.tools.Tool;
  38 import javax.tools.ToolProvider;
  39 
  40 public class PlatformOptionClashes {
  41     public static void main(String... args) throws Exception {
  42         new PlatformOptionClashes().run();
  43     }
  44 
  45     void run() throws Exception {
  46         doRunTest("-bootclasspath", "any");
  47         doRunTest("-Xbootclasspath:any");
  48         doRunTest("-Xbootclasspath/a:any");
  49         doRunTest("-Xbootclasspath/p:any");
  50         doRunTest("-endorseddirs", "any");
  51         doRunTest("-extdirs", "any");
  52         doRunTest("-source", "8");
  53         doRunTest("-target", "8");
  54     }
  55 
  56     void doRunTest(String... args) throws Exception {
  57         System.out.println("Testing clashes for arguments: " + Arrays.asList(args));
  58         Class<?> log = Class.forName("com.sun.tools.javac.util.Log", true, cl);
  59         Field useRawMessages = log.getDeclaredField("useRawMessages");
  60         useRawMessages.setAccessible(true);
  61         useRawMessages.setBoolean(null, true);
  62         ByteArrayOutputStream out = new ByteArrayOutputStream();
  63         List<String> options = new ArrayList<>();
  64         options.addAll(Arrays.asList("-platform", "7"));
  65         options.addAll(Arrays.asList(args));
  66         options.add(System.getProperty("test.src") + File.separator + "PlatformOptionClashes.java");
  67         compiler.run(null, null, out, options.toArray(new String[0]));
  68         useRawMessages.setBoolean(null, false);
  69         if (!out.toString().equals(String.format("%s%n%s%n",
  70                                                  "javac: javac.err.platform.bootclasspath.conflict",
  71                                                  "javac.msg.usage")) &&
  72             //-Xbootclasspath:any produces two warnings: one for -bootclasspath and one for -Xbootclasspath:
  73             !out.toString().equals(String.format("%s%n%s%n%s%n%s%n",
  74                                                  "javac: javac.err.platform.bootclasspath.conflict",
  75                                                  "javac.msg.usage",
  76                                                  "javac: javac.err.platform.bootclasspath.conflict",
  77                                                  "javac.msg.usage"))) {
  78             throw new AssertionError(out);
  79         }
  80         System.out.println("Test PASSED.  Running javac again to see localized output:");
  81         compiler.run(null, null, System.out, options.toArray(new String[0]));
  82     }
  83 
  84     ClassLoader cl = ToolProvider.getSystemToolClassLoader();
  85     Tool compiler = ToolProvider.getSystemJavaCompiler();
  86 }