test/jdk/jdk/jfr/api/modules/TestModularizedEvent.java
changeset 50113 caf115bb98ad
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 2018, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.jfr.api.modules;
       
    27 
       
    28 import java.nio.file.Paths;
       
    29 
       
    30 import static jdk.test.lib.Asserts.assertTrue;
       
    31 
       
    32 import java.io.IOException;
       
    33 import java.nio.file.Files;
       
    34 import java.nio.file.Path;
       
    35 import java.util.Arrays;
       
    36 import java.util.Collections;
       
    37 import java.util.List;
       
    38 import java.util.stream.Collectors;
       
    39 import javax.tools.JavaCompiler;
       
    40 import javax.tools.StandardJavaFileManager;
       
    41 import javax.tools.StandardLocation;
       
    42 import javax.tools.ToolProvider;
       
    43 
       
    44 import jdk.test.lib.process.OutputAnalyzer;
       
    45 import jdk.test.lib.process.ProcessTools;
       
    46 
       
    47 public class TestModularizedEvent {
       
    48 
       
    49     private static final String TEST_SRC = System.getProperty("test.src");
       
    50 
       
    51     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src_mods");
       
    52     private static final Path MODS_DIR = Paths.get("mods");
       
    53 
       
    54     private static final String ANNO_MODULE = "test.jfr.annotation";
       
    55     private static final String SETTING_MODULE = "test.jfr.setting";
       
    56     private static final String EVENT_MODULE = "test.jfr.event";
       
    57     private static final String TEST_MODULE = "test.jfr.main";
       
    58 
       
    59     public static void main(String... args) throws Exception {
       
    60         compileModule(ANNO_MODULE);
       
    61         compileModule(SETTING_MODULE);
       
    62         compileModule(EVENT_MODULE, "--module-path", MODS_DIR.toString());
       
    63         compileModule(TEST_MODULE, "--module-path", MODS_DIR.toString());
       
    64 
       
    65         OutputAnalyzer oa = ProcessTools.executeTestJava("--module-path", "mods", "-m", "test.jfr.main/test.jfr.main.MainTest");
       
    66         oa.stdoutShouldContain("Test passed.");
       
    67     }
       
    68 
       
    69     private static void compileModule(String modDir, String... opts) throws Exception {
       
    70         boolean compiled = compile(SRC_DIR.resolve(modDir),
       
    71                 MODS_DIR.resolve(modDir),
       
    72                 opts);
       
    73         assertTrue(compiled, "module " + modDir + " did not compile");
       
    74     }
       
    75 
       
    76     private static boolean compile(Path source, Path destination, String... options)
       
    77             throws IOException {
       
    78         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       
    79         if (compiler == null) {
       
    80             // no compiler available
       
    81             throw new UnsupportedOperationException("Unable to get system java compiler. "
       
    82                     + "Perhaps, jdk.compiler module is not available.");
       
    83         }
       
    84         StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);
       
    85 
       
    86         List<Path> sources
       
    87                 = Files.find(source, Integer.MAX_VALUE,
       
    88                         (file, attrs) -> (file.toString().endsWith(".java")))
       
    89                 .collect(Collectors.toList());
       
    90 
       
    91         Files.createDirectories(destination);
       
    92         jfm.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList());
       
    93         jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
       
    94                 Arrays.asList(destination));
       
    95 
       
    96         List<String> opts = Arrays.asList(options);
       
    97         JavaCompiler.CompilationTask task
       
    98                 = compiler.getTask(null, jfm, null, opts, null,
       
    99                         jfm.getJavaFileObjectsFromPaths(sources));
       
   100 
       
   101         return task.call();
       
   102     }
       
   103 
       
   104 }