Path: blob/master/test/jdk/java/lang/StackTraceElement/ModuleFrames.java
41149 views
/*1* Copyright (c) 2015, 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*/2223/*24* @test25* @summary Stack trace should have module information26* @run testng ModuleFrames27*/2829import java.nio.file.Files;30import java.nio.file.Path;31import java.nio.file.Paths;32import java.util.Arrays;3334import org.testng.annotations.Test;35import static org.testng.Assert.*;3637public class ModuleFrames {3839@Test40public void testModuleName() {41try {42Integer.parseInt("a");43} catch (NumberFormatException e) {4445StackTraceElement[] stack = e.getStackTrace();46StackTraceElement topFrame = stack[0];47StackTraceElement thisFrame = findFrame(this.getClass().getName(), stack);4849assertEquals(topFrame.getModuleName(), "java.base",50"Expected top frame to be in module java.base");5152assertTrue(topFrame.toString().startsWith("java.base"),53"Expected toString of top frame to omit loader name, start with module name: java.base");5455assertTrue(!topFrame.toString().contains("@"),56"Expected toString of top frame not to include module version ('@')");5758Path home = Paths.get(System.getProperty("java.home"));59boolean isImage = Files.exists(home.resolve("lib").resolve("modules"));60if (isImage) {61assertTrue(!topFrame.getModuleVersion().isEmpty(),62"Expected non-empty STE.getModuleVersion() for top frame");63}6465assertNull(thisFrame.getModuleName(),66"Expected frame for test not to have a module name");6768assertTrue(thisFrame.toString().startsWith(this.getClass().getName()),69"Expected toString to start with class name (no loader or module name");7071ClassLoader testCL = this.getClass().getClassLoader();72if (ClassLoader.getSystemClassLoader().getClass().isInstance(testCL)) {73assertEquals(thisFrame.getClassLoaderName(), "app",74"Expect STE to have loader name of \"app\"");75}76}77}7879private static StackTraceElement findFrame(String cn, StackTraceElement[] stack) {80return Arrays.stream(stack)81.filter(s -> s.getClassName().equals(cn))82.findFirst()83.get();84}85}868788