38255
|
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 |
|
|
25 |
#include <stdio.h>
|
|
26 |
#include <string.h>
|
|
27 |
#include <stdlib.h>
|
|
28 |
#ifdef __APPLE__
|
|
29 |
# include <dlfcn.h>
|
|
30 |
#endif
|
|
31 |
|
|
32 |
#include "prims/jni.h"
|
|
33 |
#include "unittest.hpp"
|
|
34 |
|
|
35 |
extern "C" {
|
|
36 |
|
|
37 |
static int init_jvm(int argc, char **argv, bool is_executing_death_test) {
|
|
38 |
// don't care about the program name
|
|
39 |
argc--;
|
|
40 |
argv++;
|
|
41 |
|
|
42 |
int extra_jvm_args = is_executing_death_test ? 4 : 2;
|
|
43 |
int num_jvm_options = argc + extra_jvm_args;
|
|
44 |
|
|
45 |
JavaVMOption* options = new JavaVMOption[num_jvm_options];
|
|
46 |
options[0].optionString = (char*) "-Dsun.java.launcher.is_altjvm=true";
|
|
47 |
options[1].optionString = (char*) "-XX:+ExecutingUnitTests";
|
|
48 |
|
|
49 |
if (is_executing_death_test) {
|
|
50 |
// don't create core files or hs_err files when executing death tests
|
|
51 |
options[2].optionString = (char*) "-XX:+SuppressFatalErrorMessage";
|
|
52 |
options[3].optionString = (char*) "-XX:-CreateCoredumpOnCrash";
|
|
53 |
}
|
|
54 |
|
|
55 |
for (int i = 0; i < argc; i++) {
|
|
56 |
options[extra_jvm_args + i].optionString = argv[i];
|
|
57 |
}
|
|
58 |
|
|
59 |
JavaVMInitArgs args;
|
|
60 |
args.version = JNI_VERSION_1_8;
|
|
61 |
args.nOptions = num_jvm_options;
|
|
62 |
args.options = options;
|
|
63 |
|
|
64 |
JavaVM* jvm;
|
|
65 |
JNIEnv* env;
|
|
66 |
|
|
67 |
return JNI_CreateJavaVM(&jvm, (void**)&env, &args);
|
|
68 |
}
|
|
69 |
|
|
70 |
class JVMInitializerListener : public ::testing::EmptyTestEventListener {
|
|
71 |
private:
|
|
72 |
int _argc;
|
|
73 |
char** _argv;
|
|
74 |
bool _is_initialized;
|
|
75 |
|
|
76 |
void initialize_jvm() {
|
|
77 |
}
|
|
78 |
|
|
79 |
public:
|
|
80 |
JVMInitializerListener(int argc, char** argv) :
|
|
81 |
_argc(argc), _argv(argv), _is_initialized(false) {
|
|
82 |
}
|
|
83 |
|
|
84 |
virtual void OnTestStart(const ::testing::TestInfo& test_info) {
|
|
85 |
const char* name = test_info.name();
|
|
86 |
if (strstr(name, "_test_vm") != NULL && !_is_initialized) {
|
|
87 |
ASSERT_EQ(init_jvm(_argc, _argv, false), 0) << "Could not initialize the JVM";
|
|
88 |
_is_initialized = true;
|
|
89 |
}
|
|
90 |
}
|
|
91 |
};
|
|
92 |
|
|
93 |
static bool is_prefix(const char* prefix, const char* str) {
|
|
94 |
return strncmp(str, prefix, strlen(prefix)) == 0;
|
|
95 |
}
|
|
96 |
|
|
97 |
static char* get_java_home_arg(int argc, char** argv) {
|
|
98 |
for (int i = 0; i < argc; i++) {
|
|
99 |
if (strncmp(argv[i], "-jdk", strlen(argv[i])) == 0) {
|
|
100 |
return argv[i+1];
|
|
101 |
}
|
|
102 |
if (is_prefix("--jdk=", argv[i])) {
|
|
103 |
return argv[i] + strlen("--jdk=");
|
|
104 |
}
|
|
105 |
if (is_prefix("-jdk:", argv[i])) {
|
|
106 |
return argv[i] + strlen("-jdk:");
|
|
107 |
}
|
|
108 |
}
|
|
109 |
return NULL;
|
|
110 |
}
|
|
111 |
|
|
112 |
static int num_args_to_skip(char* arg) {
|
|
113 |
if (strcmp(arg, "-jdk") == 0) {
|
|
114 |
return 2; // skip the argument after -jdk as well
|
|
115 |
}
|
|
116 |
if (is_prefix("--jdk=", arg)) {
|
|
117 |
return 1;
|
|
118 |
}
|
|
119 |
if (is_prefix("-jdk:", arg)) {
|
|
120 |
return 1;
|
|
121 |
}
|
|
122 |
return 0;
|
|
123 |
}
|
|
124 |
|
|
125 |
static char** remove_test_runner_arguments(int* argcp, char **argv) {
|
|
126 |
int argc = *argcp;
|
|
127 |
char** new_argv = (char**) malloc(sizeof(char*) * argc);
|
|
128 |
int new_argc = 0;
|
|
129 |
|
|
130 |
int i = 0;
|
|
131 |
while (i < argc) {
|
|
132 |
int args_to_skip = num_args_to_skip(argv[i]);
|
|
133 |
if (args_to_skip == 0) {
|
|
134 |
new_argv[new_argc] = argv[i];
|
|
135 |
i++;
|
|
136 |
new_argc++;
|
|
137 |
} else {
|
|
138 |
i += num_args_to_skip(argv[i]);
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
*argcp = new_argc;
|
|
143 |
return new_argv;
|
|
144 |
}
|
|
145 |
|
|
146 |
JNIEXPORT void JNICALL runUnitTests(int argc, char** argv) {
|
|
147 |
// Must look at googletest options before initializing googletest, since
|
|
148 |
// InitGoogleTest removes googletest options from argv.
|
|
149 |
bool is_executing_death_test = true;
|
|
150 |
for (int i = 0; i < argc; i++) {
|
|
151 |
const char* death_test_flag = "--gtest_internal_run_death_test";
|
|
152 |
if (is_prefix(death_test_flag, argv[i])) {
|
|
153 |
is_executing_death_test = true;
|
|
154 |
}
|
|
155 |
}
|
|
156 |
|
|
157 |
::testing::InitGoogleTest(&argc, argv);
|
|
158 |
::testing::GTEST_FLAG(death_test_style) = "threadsafe";
|
|
159 |
// ::testing::GTEST_FLAG(death_test_output_prefix) = "Other VM";
|
|
160 |
|
|
161 |
char* java_home = get_java_home_arg(argc, argv);
|
|
162 |
if (java_home == NULL) {
|
|
163 |
fprintf(stderr, "ERROR: You must specify a JDK to use for running the unit tests.\n");
|
|
164 |
exit(1);
|
|
165 |
}
|
|
166 |
#ifndef _WIN32
|
|
167 |
int overwrite = 1; // overwrite an eventual existing value for JAVA_HOME
|
|
168 |
setenv("JAVA_HOME", java_home, overwrite);
|
|
169 |
|
|
170 |
// workaround for JDK-7131356
|
|
171 |
#ifdef __APPLE__
|
|
172 |
size_t len = strlen(java_home) + strlen("/lib/jli/libjli.dylib") + 1;
|
|
173 |
char* path = new char[len];
|
|
174 |
snprintf(path, len, "%s/lib/jli/libjli.dylib", java_home);
|
|
175 |
dlopen(path, RTLD_NOW | RTLD_GLOBAL);
|
|
176 |
#endif // __APPLE__
|
|
177 |
|
|
178 |
#else // _WIN32
|
|
179 |
char* java_home_var = "_ALT_JAVA_HOME_DIR";
|
|
180 |
size_t len = strlen(java_home) + strlen(java_home_var) + 2;
|
|
181 |
char * envString = new char[len];
|
|
182 |
sprintf_s(envString, len, "%s=%s", java_home_var, java_home);
|
|
183 |
_putenv(envString);
|
|
184 |
#endif // _WIN32
|
|
185 |
argv = remove_test_runner_arguments(&argc, argv);
|
|
186 |
|
|
187 |
if (is_executing_death_test) {
|
|
188 |
if (init_jvm(argc, argv, true) != 0) {
|
|
189 |
abort();
|
|
190 |
}
|
|
191 |
} else {
|
|
192 |
::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();
|
|
193 |
listeners.Append(new JVMInitializerListener(argc, argv));
|
|
194 |
}
|
|
195 |
|
|
196 |
int result = RUN_ALL_TESTS();
|
|
197 |
if (result != 0) {
|
|
198 |
fprintf(stderr, "ERROR: RUN_ALL_TESTS() failed. Error %d\n", result);
|
|
199 |
exit(2);
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
} // extern "C"
|