IncCompileChangeNative.java revision 3034:c8206f440046
119370Spst/*
298944Sobrien * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
398944Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
419370Spst *
598944Sobrien * This code is free software; you can redistribute it and/or modify it
619370Spst * under the terms of the GNU General Public License version 2 only, as
798944Sobrien * published by the Free Software Foundation.
898944Sobrien *
998944Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1098944Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1119370Spst * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1298944Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1398944Sobrien * accompanied this code).
1498944Sobrien *
1598944Sobrien * You should have received a copy of the GNU General Public License version
1619370Spst * 2 along with this work; if not, write to the Free Software Foundation,
1798944Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1898944Sobrien *
1998944Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2098944Sobrien * or visit www.oracle.com if you need additional information or have any
2119370Spst * questions.
2219370Spst */
2319370Spst
2498944Sobrien/*
2598944Sobrien * @test
2619370Spst * @summary Verify native files are removed when native method is removed
2798944Sobrien * @bug 8054689
2898944Sobrien * @author Fredrik O
2998944Sobrien * @author sogoel (rewrite)
3098944Sobrien * @library /tools/lib
3198944Sobrien * @modules jdk.compiler/com.sun.tools.javac.api
3298944Sobrien *          jdk.compiler/com.sun.tools.javac.file
3398944Sobrien *          jdk.compiler/com.sun.tools.javac.main
3498944Sobrien *          jdk.compiler/com.sun.tools.sjavac
3519370Spst * @build Wrapper ToolBox
3619370Spst * @run main Wrapper IncCompileChangeNative
3719370Spst */
3819370Spst
3919370Spstimport java.util.*;
4019370Spstimport java.nio.file.*;
4146283Sdfr
4298944Sobrienpublic class IncCompileChangeNative extends SJavacTester {
4319370Spst    public static void main(String... args) throws Exception {
4419370Spst        IncCompileChangeNative cn = new IncCompileChangeNative();
45130803Smarcel        cn.test();
46130803Smarcel    }
4798944Sobrien
4898944Sobrien    // Remember the previous bin and headers state here.
4919370Spst    Map<String,Long> previous_bin_state;
5098944Sobrien    Map<String,Long> previous_headers_state;
5198944Sobrien    ToolBox tb = new ToolBox();
5298944Sobrien
5398944Sobrien    void test() throws Exception {
5419370Spst        clean(TEST_ROOT);
5598944Sobrien        Files.createDirectories(GENSRC);
5646283Sdfr        Files.createDirectories(BIN);
5719370Spst        Files.createDirectories(HEADERS);
58130803Smarcel
5919370Spst        initialCompile();
6098944Sobrien        incrementalCompileDropAllNatives();
6198944Sobrien        incrementalCompileAddNative();
62130803Smarcel
6319370Spst        clean(GENSRC, BIN, HEADERS);
6419370Spst    }
6519370Spst
66130803Smarcel    // Update B.java with one less native method i.e. it has no longer any methods
67130803Smarcel    // Verify that beta_B.h is removed
68130803Smarcel    void incrementalCompileDropAllNatives() throws Exception {
69130803Smarcel        previous_bin_state = collectState(BIN);
70130803Smarcel        previous_headers_state = collectState(HEADERS);
71130803Smarcel        System.out.println("\nIn incrementalCompileDropAllNatives() ");
72130803Smarcel        System.out.println("Verify that beta_B.h is removed");
73130803Smarcel        tb.writeFile(GENSRC.resolve("beta/B.java"),
74130803Smarcel                     "package beta; import alfa.omega.A; " +
7519370Spst                     "public class B { private int b() { return A.DEFINITION; } }");
7619370Spst
7719370Spst        compile(GENSRC.toString(),
78130803Smarcel                "-d", BIN.toString(),
7919370Spst                "--state-dir=" + BIN,
8019370Spst                "-h", HEADERS.toString(),
8198944Sobrien                "-j", "1",
8219370Spst                SERVER_ARG,
8319370Spst                "--log=debug");
8419370Spst        Map<String,Long> new_bin_state = collectState(BIN);
8598944Sobrien        verifyNewerFiles(previous_bin_state, new_bin_state,
8698944Sobrien                         BIN + "/beta/B.class",
8798944Sobrien                         BIN + "/beta/BINT.class",
8898944Sobrien                         BIN + "/javac_state");
8919370Spst        previous_bin_state = new_bin_state;
9019370Spst
9119370Spst        Map<String,Long> new_headers_state = collectState(HEADERS);
9219370Spst        verifyThatFilesHaveBeenRemoved(previous_headers_state, new_headers_state,
9319370Spst                                       HEADERS + "/beta_B.h");
9419370Spst        previous_headers_state = new_headers_state;
9519370Spst    }
9619370Spst
97130803Smarcel    // Update the B.java with a final static annotated with @Native
98130803Smarcel    // Verify that beta_B.h is added again
99130803Smarcel    void incrementalCompileAddNative() throws Exception {
100130803Smarcel        System.out.println("\nIn incrementalCompileAddNative() ");
101130803Smarcel        System.out.println("Verify that beta_B.h is added again");
10298944Sobrien        tb.writeFile(GENSRC.resolve("beta/B.java"),
10319370Spst                     "package beta; import alfa.omega.A; public class B {"+
10498944Sobrien                     "private int b() { return A.DEFINITION; } "+
10598944Sobrien                     "@java.lang.annotation.Native final static int alfa = 42; }");
10698944Sobrien
10798944Sobrien        compile(GENSRC.toString(),
10898944Sobrien                "-d", BIN.toString(),
10919370Spst                "--state-dir=" + BIN,
11098944Sobrien                "-h", HEADERS.toString(),
11119370Spst                "-j", "1",
11298944Sobrien                SERVER_ARG,
11319370Spst                "--log=debug");
11498944Sobrien        Map<String,Long> new_bin_state = collectState(BIN);
11598944Sobrien        verifyNewerFiles(previous_bin_state, new_bin_state,
11619370Spst                         BIN + "/beta/B.class",
11798944Sobrien                         BIN + "/beta/BINT.class",
11819370Spst                         BIN + "/javac_state");
119130803Smarcel        previous_bin_state = new_bin_state;
12098944Sobrien
12119370Spst        Map<String,Long> new_headers_state = collectState(HEADERS);
12219370Spst        verifyThatFilesHaveBeenAdded(previous_headers_state, new_headers_state,
12398944Sobrien                                     HEADERS + "/beta_B.h");
12419370Spst        previous_headers_state = new_headers_state;
12598944Sobrien    }
12698944Sobrien}
12798944Sobrien