Path: blob/master/test/langtools/tools/sjavac/SJavacTester.java
41144 views
/*1* Copyright (c) 2014, 2016, 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*/2223import java.util.*;24import java.io.*;25import java.nio.file.*;26import java.nio.file.attribute.*;2728import com.sun.tools.sjavac.Main;2930import toolbox.ToolBox;3132public class SJavacTester {3334final ToolBox tb = new ToolBox();35final Path TEST_ROOT = Paths.get(getClass().getSimpleName());3637// Generated sources that will test aspects of sjavac38final Path GENSRC = TEST_ROOT.resolve("gensrc");39// Gensrc dirs used to test merging of serveral source roots.40final Path GENSRC2 = TEST_ROOT.resolve("gensrc2");41final Path GENSRC3 = TEST_ROOT.resolve("gensrc3");4243// Dir for compiled classes.44final Path BIN = TEST_ROOT.resolve("bin");45// Dir for c-header files.46final Path HEADERS = TEST_ROOT.resolve("headers");4748// Remember the previous bin and headers state here.49Map<String,Long> previous_bin_state;50Map<String,Long> previous_headers_state;5152void initialCompile() throws Exception {53System.out.println("\nInitial compile of gensrc.");54tb.writeFile(GENSRC.resolve("alfa/omega/AINT.java"),55"package alfa.omega; public interface AINT { void aint(); }");56tb.writeFile(GENSRC.resolve("alfa/omega/A.java"),57"package alfa.omega; public class A implements AINT { "+58"public final static int DEFINITION = 17; public void aint() { } }");59tb.writeFile(GENSRC.resolve("alfa/omega/AA.java"),60"package alfa.omega;"+61"// A package private class, not contributing to the public api.\n"+62"class AA {"+63" // A properly nested static inner class.\n"+64" static class AAA { }\n"+65" // A properly nested inner class.\n"+66" class AAAA { }\n"+67" Runnable foo() {\n"+68" // A proper anonymous class.\n"+69" return new Runnable() { public void run() { } };\n"+70" }\n"+71" AAA aaa;\n"+72" AAAA aaaa;\n"+73" AAAAA aaaaa;\n"+74"}\n"+75"class AAAAA {\n"+76" // A bad auxiliary class, but no one is referencing it\n"+77" // from outside of this source file, therefore it is ok.\n"+78"}\n");79tb.writeFile(GENSRC.resolve("beta/BINT.java"),80"package beta;public interface BINT { void foo(); }");81tb.writeFile(GENSRC.resolve("beta/B.java"),82"package beta; import alfa.omega.A; public class B {"+83"private int b() { return A.DEFINITION; } native void foo(); }");8485compile(GENSRC.toString(),86"-d", BIN.toString(),87"--state-dir=" + BIN,88"-h", HEADERS.toString(),89"-j", "1",90"--log=debug");91}9293void removeFrom(Path dir, String... args) throws IOException {94for (String filename : args) {95Path p = dir.resolve(filename);96Files.delete(p);97}98}99100void compile(String... args) throws Exception {101int rc = Main.go(args);102if (rc != 0) throw new Exception("Error during compile!");103104// Wait a second, to get around the (temporary) problem with105// second resolution in the Java file api. But do not do this106// on windows where the timestamps work.107long in_a_sec = System.currentTimeMillis()+1000;108while (in_a_sec > System.currentTimeMillis()) {109try {110Thread.sleep(1000);111} catch (InterruptedException e) {112}113}114}115116void compileExpectFailure(String... args) throws Exception {117int rc = Main.go(args);118if (rc == 0) throw new Exception("Expected error during compile! Did not fail!");119}120121Map<String,Long> collectState(Path dir) throws IOException {122final Map<String,Long> files = new HashMap<>();123Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {124@Override125public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)126throws IOException127{128files.put(file.toString(),new Long(Files.getLastModifiedTime(file).toMillis()));129return FileVisitResult.CONTINUE;130}131});132return files;133}134135void verifyThatFilesHaveBeenRemoved(Map<String,Long> from,136Map<String,Long> to,137String... args) throws Exception {138139Set<String> froms = from.keySet();140Set<String> tos = to.keySet();141142if (froms.equals(tos)) {143throw new Exception("Expected new state to have fewer files than previous state!");144}145146for (String t : tos) {147if (!froms.contains(t)) {148throw new Exception("Expected "+t+" to exist in previous state!");149}150}151152for (String f : args) {153f = f.replace("/", File.separator);154if (!froms.contains(f)) {155throw new Exception("Expected "+f+" to exist in previous state!");156}157if (tos.contains(f)) {158throw new Exception("Expected "+f+" to have been removed from the new state!");159}160}161162if (froms.size() - args.length != tos.size()) {163throw new Exception("There are more removed files than the expected list!");164}165}166167void verifyThatFilesHaveBeenAdded(Map<String,Long> from,168Map<String,Long> to,169String... args) throws Exception {170171Set<String> froms = from.keySet();172Set<String> tos = to.keySet();173174if (froms.equals(tos)) {175throw new Exception("Expected new state to have more files than previous state!");176}177178for (String t : froms) {179if (!tos.contains(t)) {180throw new Exception("Expected "+t+" to exist in new state!");181}182}183184for (String f : args) {185f = f.replace("/", File.separator);186if (!tos.contains(f)) {187throw new Exception("Expected "+f+" to have been added to new state!");188}189if (froms.contains(f)) {190throw new Exception("Expected "+f+" to not exist in previous state!");191}192}193194if (froms.size() + args.length != tos.size()) {195throw new Exception("There are more added files than the expected list!");196}197}198199void verifyNewerFiles(Map<String,Long> from,200Map<String,Long> to,201String... args) throws Exception {202if (!from.keySet().equals(to.keySet())) {203throw new Exception("Expected the set of files to be identical!");204}205Set<String> files = new HashSet<String>();206for (String s : args) {207files.add(s.replace("/", File.separator));208}209for (String fn : from.keySet()) {210long f = from.get(fn);211long t = to.get(fn);212if (files.contains(fn)) {213if (t <= f) {214throw new Exception("Expected "+fn+" to have a more recent timestamp!");215}216} else {217if (t != f) {218throw new Exception("Expected "+fn+" to have the same timestamp!");219}220}221}222}223224String print(Map<String,Long> m) {225StringBuilder b = new StringBuilder();226Set<String> keys = m.keySet();227for (String k : keys) {228b.append(k+" "+m.get(k)+"\n");229}230return b.toString();231}232233void verifyEqual(Map<String,Long> from, Map<String,Long> to) throws Exception {234if (!from.equals(to)) {235System.out.println("FROM---"+print(from));236System.out.println("TO-----"+print(to));237throw new Exception("The dir should not differ! But it does!");238}239}240}241242243