TestSourceTab.java revision 3294:9adfb22ff08f
1321936Shselasky/*
2321936Shselasky * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
3321936Shselasky * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4321936Shselasky *
5321936Shselasky * This code is free software; you can redistribute it and/or modify it
6321936Shselasky * under the terms of the GNU General Public License version 2 only, as
7321936Shselasky * published by the Free Software Foundation.
8321936Shselasky *
9321936Shselasky * This code is distributed in the hope that it will be useful, but WITHOUT
10321936Shselasky * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11321936Shselasky * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12321936Shselasky * version 2 for more details (a copy is included in the LICENSE file that
13321936Shselasky * accompanied this code).
14321936Shselasky *
15321936Shselasky * You should have received a copy of the GNU General Public License version
16321936Shselasky * 2 along with this work; if not, write to the Free Software Foundation,
17321936Shselasky * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18321936Shselasky *
19321936Shselasky * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20321936Shselasky * or visit www.oracle.com if you need additional information or have any
21321936Shselasky * questions.
22321936Shselasky */
23321936Shselasky
24321936Shselasky/*
25321936Shselasky * @test
26321936Shselasky * @bug 4510979
27321936Shselasky * @summary Test to make sure that the source documentation is indented properly
28321936Shselasky * when -linksourcetab is used.
29321936Shselasky * @author jamieh
30321936Shselasky * @library ../lib
31321936Shselasky * @modules jdk.javadoc/jdk.javadoc.internal.tool
32321936Shselasky * @build JavadocTester
33321936Shselasky * @run main TestSourceTab
34321936Shselasky */
35321936Shselasky
36321936Shselaskyimport java.io.*;
37321936Shselasky
38321936Shselaskypublic class TestSourceTab extends JavadocTester {
39321936Shselasky
40321936Shselasky    public static void main(String... args) throws Exception {
41321936Shselasky        TestSourceTab tester = new TestSourceTab();
42321936Shselasky        tester.runTests();
43321936Shselasky    }
44321936Shselasky
45321936Shselasky    @Test
46321936Shselasky    void test() throws Exception {
47321936Shselasky        String tmpSrcDir = "tmpSrc";
48321936Shselasky        String outdir1 = "out-tabLengthEight";
49321936Shselasky        String outdir2 = "out-tabLengthFour";
50321936Shselasky        initTabs(new File(testSrc), new File(tmpSrcDir));
51321936Shselasky
52321936Shselasky        // Run Javadoc on a source file with that is indented with a single tab per line
53321936Shselasky        javadoc("-d", outdir1,
54321936Shselasky                "-sourcepath", tmpSrcDir,
55321936Shselasky                "-notimestamp",
56321936Shselasky                "-linksource",
57321936Shselasky                tmpSrcDir + "/SingleTab/C.java");
58321936Shselasky        checkExit(Exit.OK);
59321936Shselasky
60321936Shselasky        // Run Javadoc on a source file with that is indented with a two tab per line
61321936Shselasky        // If we double the tabs and decrease the tab length by a half, the output should
62321936Shselasky        // be the same as the one generated above.
63321936Shselasky        javadoc("-d", outdir2,
64321936Shselasky                "-sourcepath", tmpSrcDir,
65321936Shselasky                "-notimestamp",
66321936Shselasky                "-sourcetab", "4",
67321936Shselasky                tmpSrcDir + "/DoubleTab/C.java");
68321936Shselasky        checkExit(Exit.OK);
69321936Shselasky
70321936Shselasky        diff(outdir1, outdir2,
71321936Shselasky                "src-html/C.html",
72321936Shselasky                "C.html");
73321936Shselasky    }
74321936Shselasky
75321936Shselasky    void initTabs(File from, File to) throws IOException {
76321936Shselasky        for (File f: from.listFiles()) {
77321936Shselasky            File t = new File(to, f.getName());
78321936Shselasky            if (f.isDirectory()) {
79321936Shselasky                initTabs(f, t);
80321936Shselasky            } else if (f.getName().endsWith(".java")) {
81321936Shselasky                write(t, read(f).replace("\\t", "\t"));
82321936Shselasky            }
83321936Shselasky        }
84321936Shselasky    }
85321936Shselasky
86321936Shselasky    String read(File f) throws IOException {
87321936Shselasky        StringBuilder sb = new StringBuilder();
88321936Shselasky        try (BufferedReader in = new BufferedReader(new FileReader(f))) {
89321936Shselasky            String line;
90321936Shselasky            while ((line = in.readLine()) != null) {
91321936Shselasky                sb.append(line);
92321936Shselasky                sb.append(NL);
93321936Shselasky            }
94321936Shselasky        }
95321936Shselasky        return sb.toString();
96321936Shselasky    }
97321936Shselasky
98321936Shselasky    void write(File f, String s) throws IOException {
99321936Shselasky        f.getParentFile().mkdirs();
100321936Shselasky        try (Writer out = new FileWriter(f)) {
101321936Shselasky            out.write(s);
102321936Shselasky        }
103321936Shselasky    }
104321936Shselasky}
105321936Shselasky