rev 58452 : imported patch pkg_name_from_class

   1 /*
   2  * Copyright (c) 2016, 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 #include "precompiled.hpp"
  25 #include "classfile/symbolTable.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "memory/resourceArea.hpp"
  28 #include "oops/instanceKlass.hpp"
  29 #include "unittest.hpp"
  30 
  31 // Tests InstanceKlass::package_from_name()
  32 TEST_VM(InstanceKlass, null_class_name) {
  33   bool bad_class_name = false;
  34   TempNewSymbol retval = InstanceKlass::package_from_name(NULL, &bad_class_name);
  35   ASSERT_TRUE(bad_class_name) << "Function did not set bad_class_name with NULL class name";
  36   ASSERT_TRUE(retval == NULL) << "Wrong package for NULL class name pointer";
  37 }
  38 
  39 TEST_VM(InstanceKlass, empty_class_name) {
  40   bool bad_class_name = false;
  41   TempNewSymbol name = SymbolTable::new_symbol("");
  42   TempNewSymbol retval = InstanceKlass::package_from_name(name, &bad_class_name);
  43   ASSERT_TRUE(retval == NULL) << "Wrong package for empty string";
  44 }
  45 
  46 TEST_VM(InstanceKlass, no_slash) {
  47   bool bad_class_name = false;
  48   TempNewSymbol name = SymbolTable::new_symbol("L");
  49   TempNewSymbol retval = InstanceKlass::package_from_name(name, &bad_class_name);
  50   ASSERT_FALSE(bad_class_name) << "Function set bad_class_name with empty package";
  51   ASSERT_TRUE(retval == NULL) << "Wrong package for class with no slashes";
  52 }
  53 
  54 TEST_VM(InstanceKlass, just_slash) {
  55   bool bad_class_name = false;
  56   TempNewSymbol name = SymbolTable::new_symbol("/");
  57   TempNewSymbol retval = InstanceKlass::package_from_name(name, &bad_class_name);
  58   ASSERT_TRUE(bad_class_name) << "Function did not set bad_class_name with package of length 0";
  59   ASSERT_TRUE(retval == NULL) << "Wrong package for class with just slash";
  60 }
  61 
  62 TEST_VM(InstanceKlass, multiple_slashes) {
  63   bool bad_class_name = false;
  64   TempNewSymbol name = SymbolTable::new_symbol("///");
  65   TempNewSymbol retval = InstanceKlass::package_from_name(name, &bad_class_name);
  66   ASSERT_FALSE(bad_class_name) << "Function set bad_class_name with slashes package";
  67   ASSERT_TRUE(retval->equals("//")) << "Wrong package for class with just slashes";
  68 }
  69 
  70 TEST_VM(InstanceKlass, standard_case_1) {
  71   bool bad_class_name = false;
  72   TempNewSymbol name = SymbolTable::new_symbol("package/class");
  73   TempNewSymbol retval = InstanceKlass::package_from_name(name, &bad_class_name);
  74   ASSERT_FALSE(bad_class_name) << "Function set bad_class_name for valid package";
  75   ASSERT_TRUE(retval->equals("package")) << "Wrong package for class with one slash";
  76 }
  77 
  78 TEST_VM(InstanceKlass, standard_case_2) {
  79   bool bad_class_name = false;
  80   TempNewSymbol name = SymbolTable::new_symbol("package/folder/class");
  81   TempNewSymbol retval = InstanceKlass::package_from_name(name, &bad_class_name);
  82   ASSERT_FALSE(bad_class_name) << "Function set bad_class_name for valid package";
  83   ASSERT_TRUE(retval->equals("package/folder")) << "Wrong package for class with multiple slashes";
  84 }
  85 
  86 TEST_VM(InstanceKlass, class_array) {
  87   bool bad_class_name = false;
  88   TempNewSymbol name = SymbolTable::new_symbol("[package/class");
  89   TempNewSymbol retval = InstanceKlass::package_from_name(name, &bad_class_name);
  90   ASSERT_FALSE(bad_class_name) << "Function set bad_class_name with class array";
  91   ASSERT_TRUE(retval->equals("package")) << "Wrong package for class with leading bracket";
  92 }
  93 
  94 TEST_VM(InstanceKlass, class_object_array) {
  95   bool bad_class_name = false;
  96   TempNewSymbol name = SymbolTable::new_symbol("[Lpackage/class");
  97   TempNewSymbol retval = InstanceKlass::package_from_name(name, &bad_class_name);
  98   ASSERT_TRUE(bad_class_name) << "Function did not set bad_class_name with array of class objects";
  99   ASSERT_TRUE(retval == NULL) << "Wrong package for class with leading '[L'";
 100 }
 101 
 102 // Tests for InstanceKlass::is_class_loader_instance_klass() function
 103 TEST_VM(InstanceKlass, class_loader_class) {
 104   InstanceKlass* klass = SystemDictionary::ClassLoader_klass();
 105   ASSERT_TRUE(klass->is_class_loader_instance_klass());
 106 }
 107 
 108 TEST_VM(InstanceKlass, string_klass) {
 109   InstanceKlass* klass = SystemDictionary::String_klass();
 110   ASSERT_TRUE(!klass->is_class_loader_instance_klass());
 111 }
--- EOF ---