|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 8263043 |
| 27 | + * @summary Add test to verify order of tag output |
| 28 | + * @library /tools/lib ../../lib |
| 29 | + * @modules jdk.javadoc/jdk.javadoc.internal.tool |
| 30 | + * @build toolbox.ToolBox javadoc.tester.* |
| 31 | + * @run main TestTagOrder |
| 32 | + */ |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Comparator; |
| 38 | +import java.util.LinkedHashMap; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | +import java.util.SortedSet; |
| 42 | +import java.util.TreeMap; |
| 43 | +import java.util.TreeSet; |
| 44 | + |
| 45 | +import javadoc.tester.JavadocTester; |
| 46 | +import toolbox.ToolBox; |
| 47 | + |
| 48 | +/** |
| 49 | + * Tests the order of the output of block tags in the generated output. |
| 50 | + * There is a default order, embodied in the order of declaration of tags in |
| 51 | + * {@code TagletManager}, but this can be overridden on the command line by |
| 52 | + * specifying {@code -tag} options in the desired order. |
| 53 | + */ |
| 54 | +public class TestTagOrder extends JavadocTester { |
| 55 | + public static void main(String... args) throws Exception { |
| 56 | + TestTagOrder tester = new TestTagOrder(); |
| 57 | + tester.runTests(m -> new Object[] { Path.of(m.getName()) }); |
| 58 | + } |
| 59 | + |
| 60 | + ToolBox tb = new ToolBox(); |
| 61 | + Path src = Path.of("src"); |
| 62 | + Map<String, String> expectMethod = new LinkedHashMap<>(); |
| 63 | + Map<String, String> expectClass = new LinkedHashMap<>(); |
| 64 | + |
| 65 | + TestTagOrder() throws IOException { |
| 66 | + tb.writeJavaFiles(src, |
| 67 | + """ |
| 68 | + package p; |
| 69 | + /** Class C1. */ |
| 70 | + public class C1 { |
| 71 | + /** |
| 72 | + * This is method m. |
| 73 | + * @param p1 first parameter |
| 74 | + * @param p2 second parameter |
| 75 | + * @return zero |
| 76 | + * @throws IllegalArgumentException well, never |
| 77 | + * @since 1.0 |
| 78 | + * @see <a href="http://example.com">example</a> |
| 79 | + */ |
| 80 | + public int m(int p1, int p2) throws IllegalArgumentException { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + } |
| 84 | + """, """ |
| 85 | + package p; |
| 86 | + /** |
| 87 | + * Class C2. |
| 88 | + * @since 1.0 |
| 89 | + * @author J. Duke. |
| 90 | + * @version 2.0 |
| 91 | + * @see <a href="http://example.com">example</a> |
| 92 | + */ |
| 93 | + public class C2 { } |
| 94 | + """); |
| 95 | + |
| 96 | + // The following add map entries in the default order of appearance in the output. |
| 97 | + // Note that the list is not otherwise ordered, such as alphabetically. |
| 98 | + |
| 99 | + expectMethod.put("@param", """ |
| 100 | + <dt>Parameters:</dt> |
| 101 | + <dd><code>p1</code> - first parameter</dd> |
| 102 | + <dd><code>p2</code> - second parameter</dd> |
| 103 | + """); |
| 104 | + |
| 105 | + expectMethod.put("@return", """ |
| 106 | + <dt>Returns:</dt> |
| 107 | + <dd>zero</dd> |
| 108 | + """); |
| 109 | + |
| 110 | + expectMethod.put("@throws", """ |
| 111 | + <dt>Throws:</dt> |
| 112 | + <dd><code>java.lang.IllegalArgumentException</code> - well, never</dd> |
| 113 | + """); |
| 114 | + |
| 115 | + expectMethod.put("@since", """ |
| 116 | + <dt>Since:</dt> |
| 117 | + <dd>1.0</dd> |
| 118 | + """); |
| 119 | + |
| 120 | + expectMethod.put("@see", """ |
| 121 | + <dt>See Also:</dt> |
| 122 | + <dd><a href="http://example.com">example</a></dd> |
| 123 | + """); |
| 124 | + |
| 125 | + expectClass.put("@since", """ |
| 126 | + <dt>Since:</dt> |
| 127 | + <dd>1.0</dd> |
| 128 | + """); |
| 129 | + |
| 130 | + expectClass.put("@version", """ |
| 131 | + <dt>Version:</dt> |
| 132 | + <dd>2.0</dd> |
| 133 | + """); |
| 134 | + |
| 135 | + expectClass.put("@author", """ |
| 136 | + <dt>Author:</dt> |
| 137 | + <dd>J. Duke.</dd> |
| 138 | + """); |
| 139 | + |
| 140 | + expectClass.put("@see", """ |
| 141 | + <dt>See Also:</dt> |
| 142 | + <dd><a href="http://example.com">example</a></dd> |
| 143 | + """); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testDefault(Path base) { |
| 148 | + test(base, null); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testAlpha(Path base) { |
| 153 | + test(base, Comparator.naturalOrder()); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testReverse(Path base) { |
| 158 | + test(base, Comparator.reverseOrder()); |
| 159 | + } |
| 160 | + |
| 161 | + private void test(Path base, Comparator<String> c) { |
| 162 | + List<String> args = new ArrayList<>(); |
| 163 | + args.addAll(List.of( |
| 164 | + "-d", base.resolve("out").toString(), |
| 165 | + "--source-path", src.toString(), |
| 166 | + "--no-platform-links", |
| 167 | + "-author", |
| 168 | + "-version")); |
| 169 | + args.addAll(getTagArgs(c, expectMethod, expectClass)); |
| 170 | + args.add("p"); |
| 171 | + |
| 172 | + javadoc(args.toArray(new String[0])); |
| 173 | + checkExit(Exit.OK); |
| 174 | + |
| 175 | + checkOutput("p/C1.html", true, |
| 176 | + "<dl class=\"notes\">\n" |
| 177 | + + getExpectString(c, expectMethod) |
| 178 | + + "</dl>"); |
| 179 | + |
| 180 | + checkOutput("p/C2.html", true, |
| 181 | + "<dl class=\"notes\">\n" |
| 182 | + + getExpectString(c, expectClass) |
| 183 | + + "</dl>"); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Returns a series of {@code -tag} options derived from the keys of a series of maps, |
| 188 | + * sorted according to the given comparator, or an empty list if the comparator is {@code null}. |
| 189 | + * |
| 190 | + * @param c the comparator, or {@code null} |
| 191 | + * @param expect the maps from which to infer the options |
| 192 | + * |
| 193 | + * @return the list of options |
| 194 | + */ |
| 195 | + @SafeVarargs |
| 196 | + private List<String> getTagArgs(Comparator<String> c, Map<String, String>... expect) { |
| 197 | + if (c == null) { |
| 198 | + return List.of(); |
| 199 | + } |
| 200 | + |
| 201 | + SortedSet<String> allTags = new TreeSet<>(c); |
| 202 | + for (Map<String, String> e : expect) { |
| 203 | + allTags.addAll(e.keySet()); |
| 204 | + } |
| 205 | + List<String> args = new ArrayList<>(); |
| 206 | + allTags.forEach(t -> { args.add("-tag"); args.add(t.substring(1)); }); |
| 207 | + return args; |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Returns the "expected string" derived from the values of a map, sorted according |
| 212 | + * to the keys of the map with a given comparator if the comparator is not {@code null}. |
| 213 | + * |
| 214 | + * @param c the comparator, or {@code null} |
| 215 | + * @param expect the map |
| 216 | + * |
| 217 | + * @return the "expected string" |
| 218 | + */ |
| 219 | + private String getExpectString(Comparator<String> c, Map<String, String> expect) { |
| 220 | + Map<String, String> e; |
| 221 | + if (c == null) { |
| 222 | + e = expect; |
| 223 | + } else { |
| 224 | + e = new TreeMap<>(c); |
| 225 | + e.putAll(expect); |
| 226 | + } |
| 227 | + return String.join("", e.values()); |
| 228 | + } |
| 229 | +} |
0 commit comments