]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.arrow.vector; | |
19 | ||
20 | import java.lang.reflect.Field; | |
21 | import java.net.URLClassLoader; | |
22 | ||
23 | import org.junit.Assert; | |
24 | import org.junit.Test; | |
25 | ||
26 | /** | |
27 | * Test cases for {@link NullCheckingForGet}. | |
28 | */ | |
29 | public class TestNullCheckingForGet { | |
30 | ||
31 | /** | |
32 | * Get a copy of the current class loader. | |
33 | * @return the newly created class loader. | |
34 | */ | |
35 | private ClassLoader copyClassLoader() { | |
36 | ClassLoader curClassLoader = this.getClass().getClassLoader(); | |
37 | if (curClassLoader instanceof URLClassLoader) { | |
38 | // for Java 1.8 | |
39 | return new URLClassLoader(((URLClassLoader) curClassLoader).getURLs(), null); | |
40 | } | |
41 | ||
42 | // for Java 1.9 and Java 11. | |
43 | return null; | |
44 | } | |
45 | ||
46 | /** | |
47 | * Get the value of flag {@link NullCheckingForGet#NULL_CHECKING_ENABLED}. | |
48 | * @param classLoader the class loader from which to get the flag value. | |
49 | * @return value of the flag. | |
50 | */ | |
51 | private boolean getFlagValue(ClassLoader classLoader) throws Exception { | |
52 | Class<?> clazz = classLoader.loadClass("org.apache.arrow.vector.NullCheckingForGet"); | |
53 | Field field = clazz.getField("NULL_CHECKING_ENABLED"); | |
54 | return (Boolean) field.get(null); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Ensure the flag for null checking is enabled by default. | |
59 | * This will protect users from JVM crashes. | |
60 | */ | |
61 | @Test | |
62 | public void testDefaultValue() throws Exception { | |
63 | ClassLoader classLoader = copyClassLoader(); | |
64 | if (classLoader != null) { | |
65 | boolean nullCheckingEnabled = getFlagValue(classLoader); | |
66 | Assert.assertTrue(nullCheckingEnabled); | |
67 | } | |
68 | } | |
69 | ||
70 | /** | |
71 | * Test setting the null checking flag by the system property. | |
72 | * @throws Exception if loading class {@link NullCheckingForGet#NULL_CHECKING_ENABLED} fails. | |
73 | */ | |
74 | @Test | |
75 | public void testEnableSysProperty() throws Exception { | |
76 | String sysProperty = System.getProperty("arrow.enable_null_check_for_get"); | |
77 | System.setProperty("arrow.enable_null_check_for_get", "false"); | |
78 | ||
79 | ClassLoader classLoader = copyClassLoader(); | |
80 | if (classLoader != null) { | |
81 | boolean nullCheckingEnabled = getFlagValue(classLoader); | |
82 | Assert.assertFalse(nullCheckingEnabled); | |
83 | } | |
84 | ||
85 | // restore system property | |
86 | if (sysProperty != null) { | |
87 | System.setProperty("arrow.enable_null_check_for_get", sysProperty); | |
88 | } else { | |
89 | System.clearProperty("arrow.enable_null_check_for_get"); | |
90 | } | |
91 | } | |
92 | } |