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 that javac can handle -release when invoked using the Compiler API
  28  */
  29 
  30 import java.io.IOException;
  31 import java.io.PrintWriter;
  32 import java.io.StringWriter;
  33 import java.util.Arrays;
  34 import java.util.List;
  35 import javax.tools.JavaCompiler;
  36 import javax.tools.JavaFileObject;
  37 import javax.tools.StandardJavaFileManager;
  38 import javax.tools.ToolProvider;
  39 
  40 public class ReleaseOptionThroughAPI {
  41     public static void main(String... args) throws IOException {
  42         new ReleaseOptionThroughAPI().run();
  43     }
  44 
  45     void run() throws IOException {
  46         String lineSep = System.getProperty("line.separator");
  47         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  48         try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
  49              StringWriter out = new StringWriter();
  50              PrintWriter outWriter = new PrintWriter(out)) {
  51             Iterable<? extends JavaFileObject> input =
  52                     fm.getJavaFileObjects(System.getProperty("test.src") + "/ReleaseOption.java");
  53             List<String> options = Arrays.asList("-release", "7", "-XDrawDiagnostics");
  54 
  55             compiler.getTask(outWriter, fm, null, options, null, input).call();
  56             String expected =
  57                     "ReleaseOption.java:9:49: compiler.err.doesnt.exist: java.util.stream" + lineSep +
  58                     "1 error" + lineSep;
  59             if (!expected.equals(out.toString())) {
  60                 throw new AssertionError("Unexpected output: " + out.toString());
  61             }
  62         }
  63     }
  64 }