Path: blob/master/test/langtools/jdk/javadoc/tool/EnablePreviewOption.java
40957 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 819919626* @summary Test --enable-preview option in javadoc27* @modules jdk.javadoc/jdk.javadoc.internal.tool28* @library /tools/lib29* @build toolbox.ToolBox toolbox.TestRunner30* @run main EnablePreviewOption31*/3233import java.io.IOException;34import java.io.PrintWriter;35import java.io.StringWriter;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.util.ArrayList;39import java.util.Arrays;40import java.util.List;41import java.util.function.Predicate;4243import jdk.javadoc.internal.tool.Main;44import jdk.javadoc.internal.tool.Main.Result;4546import static jdk.javadoc.internal.tool.Main.Result.*;4748import toolbox.TestRunner;49import toolbox.ToolBox;5051public class EnablePreviewOption extends TestRunner {52public static void main(String... args) throws Exception {53new EnablePreviewOption().runTests();54}5556ToolBox tb = new ToolBox();5758Path file = Paths.get("C.java");59String thisVersion = System.getProperty("java.specification.version");60String prevVersion = String.valueOf(Integer.valueOf(thisVersion) - 1);6162EnablePreviewOption() throws IOException {63super(System.err);64tb.writeFile(file, "public class C { }");65}6667@Test68public void testSource() {69runTest(List.of("--enable-preview", "-source", thisVersion),70OK,71out -> !out.contains("error")72&& out.contains("Building tree for all the packages and classes..."));73}7475@Test76public void testRelease() {77runTest(List.of("--enable-preview", "--release", thisVersion),78OK,79out -> !out.contains("error")80&& out.contains("Building tree for all the packages and classes..."));81}8283@Test84public void testNoVersion() {85runTest(List.of("--enable-preview"),86CMDERR,87out -> out.contains("error: --enable-preview must be used with either -source or --release"));88}8990@Test91public void testBadSource() {92runTest(List.of("--enable-preview", "-source", "BAD"),93ERROR,94out -> out.contains("error: invalid source release: BAD"));95}9697@Test98public void testOldSource() {99runTest(List.of("--enable-preview", "-source", prevVersion),100CMDERR,101out -> out.matches("(?s)error: invalid source release .* with --enable-preview.*"));102}103104private void runTest(List<String> options, Result expectedResult, Predicate<String> validate) {105System.err.println("running with options: " + options);106List<String> args = new ArrayList<>();107args.addAll(options);108args.add("-XDrawDiagnostics");109args.add(file.toString());110StringWriter out = new StringWriter();111PrintWriter pw = new PrintWriter(out);112int actualResult = Main.execute(args.toArray(new String[0]), pw);113System.err.println("actual result=" + actualResult);114System.err.println("actual output=" + out.toString());115if (actualResult != expectedResult.exitCode)116error("Exit code not as expected");117if (!validate.test(out.toString())) {118error("Output not as expected");119}120}121}122123124