|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 2019, 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 8259530 |
| 27 | + * @summary Generated docs contain MIT/GPL-licenced works without reproducing the licence |
| 28 | + * @library /tools/lib ../lib |
| 29 | + * @modules jdk.javadoc/jdk.javadoc.internal.tool |
| 30 | + * @build toolbox.ToolBox JavadocTester |
| 31 | + * @run main TestLegalNotices |
| 32 | + */ |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.nio.file.DirectoryStream; |
| 36 | +import java.nio.file.Files; |
| 37 | +import java.nio.file.Path; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Set; |
| 42 | +import java.util.TreeSet; |
| 43 | +import java.util.function.Predicate; |
| 44 | + |
| 45 | +// import javadoc.tester.JavadocTester; |
| 46 | +import toolbox.ToolBox; |
| 47 | + |
| 48 | +public class TestLegalNotices extends JavadocTester { |
| 49 | + public static void main(String... args) throws Exception { |
| 50 | + TestLegalNotices tester = new TestLegalNotices(); |
| 51 | + tester.runTests(m -> new Object[]{Path.of(m.getName())}); |
| 52 | + } |
| 53 | + |
| 54 | + private final ToolBox tb = new ToolBox(); |
| 55 | + |
| 56 | + enum OptionKind { |
| 57 | + UNSET, DEFAULT, NONE, DIR |
| 58 | + } |
| 59 | + |
| 60 | + enum IndexKind { |
| 61 | + INDEX, NO_INDEX |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testCombo(Path base) throws IOException { |
| 67 | + Path src = base.resolve("src"); |
| 68 | + tb.writeJavaFiles(src, "package p; public class C { }"); |
| 69 | + Path legal = base.resolve("toy-legal"); |
| 70 | + tb.writeFile(legal.resolve("TOY-LICENSE"), "This is a demo license."); |
| 71 | + |
| 72 | + for (var optionKind : OptionKind.values()) { |
| 73 | + for (var indexKind : IndexKind.values()) { |
| 74 | + test(base, src, legal, optionKind, indexKind); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + void test(Path base, Path src, Path legal, OptionKind optionKind, IndexKind indexKind) throws IOException { |
| 80 | + System.out.println("testing " + optionKind + " " + indexKind); |
| 81 | + Path out = base.resolve(optionKind + "-" + indexKind).resolve("out"); |
| 82 | + List<String> args = new ArrayList<>(); |
| 83 | + args.addAll(List.of( |
| 84 | + "-d", out.toString())); |
| 85 | + |
| 86 | + if (indexKind == IndexKind.NO_INDEX) { |
| 87 | + args.add("-noindex"); |
| 88 | + } |
| 89 | + |
| 90 | + args.addAll(List.of( |
| 91 | + "-Xdoclint:-missing", |
| 92 | + "--source-path", src.toString(), |
| 93 | + "p")); |
| 94 | + |
| 95 | + String value = null; |
| 96 | + switch (optionKind) { |
| 97 | + case UNSET: |
| 98 | + value = null; |
| 99 | + break; |
| 100 | + case DEFAULT: |
| 101 | + value = "default"; |
| 102 | + break; |
| 103 | + case NONE: |
| 104 | + value = "none"; |
| 105 | + break; |
| 106 | + case DIR: |
| 107 | + value= legal.toString(); |
| 108 | + break; |
| 109 | + } |
| 110 | + if (value != null) { |
| 111 | + args.addAll(List.of("--legal-notices", value)); |
| 112 | + } |
| 113 | + javadoc(args.toArray(new String[0])); |
| 114 | + |
| 115 | + Set<Path> expectFiles = getExpectFiles(optionKind, indexKind, legal); |
| 116 | + Set<Path> foundFiles = listFiles(out.resolve("legal")); |
| 117 | + |
| 118 | + checking("Checking legal notice files"); |
| 119 | + super.out.println("Expected: " + expectFiles); |
| 120 | + super.out.println(" Found: " + foundFiles); |
| 121 | + if (foundFiles.equals(expectFiles)) { |
| 122 | + passed("Found all expected files"); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + Set<Path> getExpectFiles(OptionKind optionKind, IndexKind indexKind, Path legal) throws IOException { |
| 127 | + switch (optionKind) { |
| 128 | + case UNSET: |
| 129 | + case DEFAULT: |
| 130 | + Path javaHome = Path.of(System.getProperty("java.home")); |
| 131 | + Path legal_javadoc = javaHome.resolve("legal").resolve("jdk.javadoc"); |
| 132 | + return listFiles(legal_javadoc, p -> { |
| 133 | + switch (indexKind) { |
| 134 | + case INDEX: |
| 135 | + return true; |
| 136 | + case NO_INDEX: |
| 137 | + return !p.getFileName().toString().contains("jquery"); |
| 138 | + default: |
| 139 | + throw new AssertionError("Should not reach"); |
| 140 | + } } ); |
| 141 | + |
| 142 | +/* |
| 143 | + return listFiles(legal_javadoc, p -> { |
| 144 | + if (indexKind==IndexKind.INDEX) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + else if (indexKind==IndexKind.NO_INDEX) { |
| 148 | + return !p.getFileName().toString().contains("jquery"); |
| 149 | + } |
| 150 | + else { |
| 151 | + return false; |
| 152 | + } } ); |
| 153 | +*/ |
| 154 | + |
| 155 | +/* |
| 156 | + return listFiles(legal_javadoc, p -> |
| 157 | + (indexKind==IndexKind.INDEX) ? true |
| 158 | + : (indexKind==IndexKind.NO_INDEX) ? !p.getFileName().toString().contains("jquery") : false); |
| 159 | +*/ |
| 160 | + |
| 161 | + case NONE: |
| 162 | + return Collections.emptySet(); |
| 163 | + case DIR: |
| 164 | + return listFiles(legal); |
| 165 | + } |
| 166 | + throw new IllegalStateException(); |
| 167 | + } |
| 168 | + |
| 169 | + Set<Path> listFiles(Path dir) throws IOException { |
| 170 | + return listFiles(dir, p -> true); |
| 171 | + } |
| 172 | + |
| 173 | + Set<Path> listFiles(Path dir, Predicate<Path> filter) throws IOException { |
| 174 | + if (!Files.exists(dir)) { |
| 175 | + return Collections.emptySet(); |
| 176 | + } |
| 177 | + |
| 178 | + try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) { |
| 179 | + Set<Path> files = new TreeSet<>(); |
| 180 | + for (Path p : ds) { |
| 181 | + if (!Files.isDirectory(p) && filter.test(p)) { |
| 182 | + files.add(p.getFileName()); |
| 183 | + } |
| 184 | + } |
| 185 | + return files; |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments