StringFoldingTest.java revision 3061:777c5a760a84
1/*
2 * Copyright (c) 2011, 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 7068902 8139751
27 * @summary verify that string folding can be enabled or disabled
28 * @modules jdk.compiler
29 */
30
31import com.sun.source.tree.CompilationUnitTree;
32import com.sun.source.util.JavacTask;
33import java.io.File;
34import java.io.IOException;
35import java.net.URI;
36import java.util.ArrayList;
37import java.util.Arrays;
38import java.util.List;
39import javax.tools.JavaCompiler;
40import javax.tools.JavaFileObject;
41import javax.tools.SimpleJavaFileObject;
42import javax.tools.ToolProvider;
43
44public class StringFoldingTest {
45    final JavaCompiler tool;
46    final JavaSource source;
47
48    public StringFoldingTest() {
49        tool = ToolProvider.getSystemJavaCompiler();
50        source = new JavaSource();
51    }
52
53    static class JavaSource extends SimpleJavaFileObject {
54
55        final static String source =
56                "class C {String X=\"F\" + \"O\" + \"L\" + \"D\" + \"E\" + \"D\";}";
57
58        JavaSource() {
59            super(URI.create("myfo:/C.java"), JavaFileObject.Kind.SOURCE);
60        }
61
62        @Override
63        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
64            return source;
65        }
66    }
67
68    public static void main(String... args) throws IOException {
69        StringFoldingTest t = new StringFoldingTest();
70        t.run(false);
71        t.run(true);
72    }
73
74    void run(boolean disableStringFolding) throws IOException {
75        List<String> argsList = new ArrayList<String>();
76        if (disableStringFolding) {
77            argsList.add("-XDallowStringFolding=false");
78        }
79        JavacTask ct = (JavacTask)tool.getTask(null, null, null,
80                argsList,
81                null,
82                Arrays.asList(source));
83        Iterable<? extends CompilationUnitTree> trees = ct.parse();
84        String text = trees.toString();
85        System.out.println(text);
86
87        if (disableStringFolding) {
88            if (text.contains("FOLDED")) {
89                throw new AssertionError("Expected no string folding");
90            }
91            if (!text.contains("\"F\"")) {
92                throw new AssertionError("Expected content not found");
93            }
94        } else {
95            if (!text.contains("FOLDED")) {
96                throw new AssertionError("Expected string folding");
97            }
98        }
99    }
100}
101