1 /*
   2  * Copyright (c) 2016, 2017, 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 8170772
  27  * @library /test/lib
  28  * @modules jdk.compiler
  29  * @build CacheTest jdk.test.lib.compiler.CompilerUtils
  30  * @run testng CacheTest
  31  */
  32 
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import java.nio.file.Paths;
  36 
  37 import static jdk.test.lib.process.ProcessTools.*;
  38 import jdk.test.lib.compiler.CompilerUtils;
  39 
  40 import org.testng.annotations.BeforeTest;
  41 import org.testng.annotations.Test;
  42 import static org.testng.Assert.*;
  43 
  44 @Test
  45 public class CacheTest {
  46 
  47     private static final String TEST_SRC = System.getProperty("test.src");
  48 
  49     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  50     private static final Path MODS_DIR = Paths.get("mods");
  51 
  52     private static final String TEST_MODULE = "test";
  53     private static final String MAIN_BUNDLES_MODULE = "mainbundles";
  54 
  55     private static final String MAIN = "test/jdk.test.Main";
  56     private static final String MAIN_CLASS = "jdk.test.Main";
  57 
  58     @BeforeTest
  59     public void compileTestModules() throws Exception {
  60 
  61         for (String mn : new String[] {MAIN_BUNDLES_MODULE, TEST_MODULE}) {
  62             boolean compiled =
  63                 CompilerUtils.compile(SRC_DIR.resolve(mn),
  64                                       MODS_DIR.resolve(mn),
  65                             "--module-path", MODS_DIR.toString());
  66             assertTrue(compiled, "module " + mn + " did not compile");
  67         }
  68 
  69         Path res = Paths.get("jdk", "test", "resources", "MyResources.properties");
  70         Path dest = MODS_DIR.resolve(MAIN_BUNDLES_MODULE).resolve(res);
  71         Files.createDirectories(dest.getParent());
  72         Files.copy(SRC_DIR.resolve(MAIN_BUNDLES_MODULE).resolve(res), dest);
  73     }
  74 
  75     /**
  76      * Load resource bundle in cache first.  Verify that the subsequent
  77      * loading of ResourceBundle from another module will not get it from cache.
  78      */
  79     @Test
  80     public void loadCacheFirst() throws Exception {
  81         assertTrue(executeTestJava("--module-path", MODS_DIR.toString(),
  82                                    "-m", MAIN, "cache")
  83                         .outputTo(System.out)
  84                         .errorTo(System.out)
  85                         .getExitValue() == 0);
  86 
  87         assertTrue(executeTestJava("--class-path", MODS_DIR.resolve(TEST_MODULE).toString(),
  88                                    "--module-path", MODS_DIR.resolve(MAIN_BUNDLES_MODULE).toString(),
  89                                    "--add-modules", MAIN_BUNDLES_MODULE,
  90                                    "--illegal-access=deny",
  91                                    MAIN_CLASS, "cache")
  92                         .outputTo(System.out)
  93                         .errorTo(System.out)
  94                         .getExitValue() == 0);
  95     }
  96 
  97     /**
  98      * Load non-existent resource bundle in cache first.  Verify that
  99      * the subsequent loading of ResourceBundle from another module
 100      * can still successfully load the bundle.
 101      */
 102     @Test
 103     public void loadNonExistentBundleInCache() throws Exception {
 104         assertTrue(executeTestJava("--module-path", MODS_DIR.toString(),
 105                                    "-m", MAIN)
 106                         .outputTo(System.out)
 107                         .errorTo(System.out)
 108                         .getExitValue() == 0);
 109 
 110         assertTrue(executeTestJava("--class-path", MODS_DIR.resolve(TEST_MODULE).toString(),
 111                                    "--module-path", MODS_DIR.resolve(MAIN_BUNDLES_MODULE).toString(),
 112                                    "--add-modules", MAIN_BUNDLES_MODULE,
 113                                    "--illegal-access=deny",
 114                                    MAIN_CLASS)
 115                         .outputTo(System.out)
 116                         .errorTo(System.out)
 117                         .getExitValue() == 0);
 118     }
 119 }