test/hotspot/gtest/runtime/test_classLoader.cpp
author chegar
Fri, 17 Nov 2017 10:42:01 +0000
branchhttp-client-branch
changeset 55822 28dcbbc4148b
parent 47216 71c04702a3d5
permissions -rw-r--r--
http-client-branch: minor doc change
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39216
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
     1
/*
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
     4
 *
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
     7
 * published by the Free Software Foundation.
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
     8
 *
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    13
 * accompanied this code).
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    14
 *
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    18
 *
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    21
 * questions.
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    22
 */
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    23
39394
044a0eb96215 8159364: Gtest unit tests does not support PCH
ehelin
parents: 39216
diff changeset
    24
#include "precompiled.hpp"
39216
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    25
#include "classfile/classLoader.hpp"
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    26
#include "memory/resourceArea.hpp"
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    27
#include "unittest.hpp"
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    28
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    29
// Tests ClassLoader::package_from_name()
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    30
TEST_VM(classLoader, null_class_name) {
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    31
  ResourceMark rm;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    32
  bool bad_class_name = false;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    33
  const char* retval= ClassLoader::package_from_name(NULL, &bad_class_name);
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    34
  ASSERT_TRUE(bad_class_name) << "Function did not set bad_class_name with NULL class name";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    35
  ASSERT_STREQ(retval, NULL) << "Wrong package for NULL class name pointer";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    36
}
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    37
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    38
TEST_VM(classLoader, empty_class_name) {
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    39
  ResourceMark rm;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    40
  const char* retval = ClassLoader::package_from_name("");
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    41
  ASSERT_STREQ(retval, NULL) << "Wrong package for empty string";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    42
}
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    43
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    44
TEST_VM(classLoader, no_slash) {
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    45
  ResourceMark rm;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    46
  const char* retval = ClassLoader::package_from_name("L");
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    47
  ASSERT_STREQ(retval, NULL) << "Wrong package for class with no slashes";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    48
}
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    49
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    50
TEST_VM(classLoader, just_slash) {
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    51
  ResourceMark rm;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    52
  bool bad_class_name = false;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    53
  const char* retval = ClassLoader::package_from_name("/", &bad_class_name);
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    54
  ASSERT_TRUE(bad_class_name) << "Function did not set bad_class_name with package of length 0";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    55
  ASSERT_STREQ(retval, NULL) << "Wrong package for class with just slash";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    56
}
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    57
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    58
TEST_VM(classLoader, multiple_slashes) {
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    59
  ResourceMark rm;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    60
  const char* retval = ClassLoader::package_from_name("///");
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    61
  ASSERT_STREQ(retval, "//") << "Wrong package for class with just slashes";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    62
}
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    63
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    64
TEST_VM(classLoader, standard_case_1) {
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    65
  ResourceMark rm;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    66
  bool bad_class_name = true;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    67
  const char* retval = ClassLoader::package_from_name("package/class", &bad_class_name);
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    68
  ASSERT_FALSE(bad_class_name) << "Function did not reset bad_class_name";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    69
  ASSERT_STREQ(retval, "package") << "Wrong package for class with one slash";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    70
}
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    71
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    72
TEST_VM(classLoader, standard_case_2) {
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    73
  ResourceMark rm;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    74
  const char* retval = ClassLoader::package_from_name("package/folder/class");
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    75
  ASSERT_STREQ(retval, "package/folder") << "Wrong package for class with multiple slashes";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    76
}
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    77
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    78
TEST_VM(classLoader, class_array) {
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    79
  ResourceMark rm;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    80
  bool bad_class_name = false;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    81
  const char* retval = ClassLoader::package_from_name("[package/class", &bad_class_name);
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    82
  ASSERT_FALSE(bad_class_name) << "Function set bad_class_name with class array";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    83
  ASSERT_STREQ(retval, "package") << "Wrong package for class with leading bracket";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    84
}
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    85
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    86
TEST_VM(classLoader, class_object_array) {
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    87
  ResourceMark rm;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    88
  bool bad_class_name = false;
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    89
  const char* retval = ClassLoader::package_from_name("[Lpackage/class", &bad_class_name);
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    90
  ASSERT_TRUE(bad_class_name) << "Function did not set bad_class_name with array of class objects";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    91
  ASSERT_STREQ(retval, NULL) << "Wrong package for class with leading '[L'";
40c3d66352ae 8153858: Clean up needed when obtaining the package name from a fully qualified class name
rprotacio
parents:
diff changeset
    92
}