-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HBASE-24185: Junit tests do not behave well with System.exit or Runti…
…me.halt or JVM exits in general.
- Loading branch information
1 parent
55f65dc
commit 9c04300
Showing
3 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
hbase-common/src/test/java/org/apache/hadoop/hbase/SystemExitRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.hbase; | ||
|
||
import java.io.FileDescriptor; | ||
import java.net.InetAddress; | ||
import java.security.Permission; | ||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
|
||
public class SystemExitRule implements TestRule { | ||
|
||
public SystemExitRule() { | ||
|
||
} | ||
|
||
@Override public Statement apply(final Statement s, Description d) { | ||
return new Statement() { | ||
@Override public void evaluate() throws Throwable { | ||
|
||
try { | ||
forbidSystemExitCall(); | ||
s.evaluate(); | ||
} finally { | ||
System.setSecurityManager(null); | ||
} | ||
} | ||
|
||
}; | ||
}; | ||
|
||
// Exiting the JVM is not allowed in tests and this exception is thrown instead | ||
// when it is done | ||
public static class ExitTrappedException extends SecurityException { | ||
} | ||
|
||
private static void forbidSystemExitCall() { | ||
final SecurityManager securityManager = new SecurityManager() { | ||
@Override | ||
public void checkExit(int status) { | ||
super.checkExit(status); | ||
throw new ExitTrappedException(); | ||
} | ||
|
||
@Override | ||
public void checkPermission(Permission permission) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkPermission(Permission var1, Object var2) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkSecurityAccess(String var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkConnect(String var1, int var2, Object var3) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkWrite(String var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkDelete(String var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkConnect(String var1, int var2) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkLink(String var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkRead(FileDescriptor var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkAccess(Thread var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkAccess(ThreadGroup var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkCreateClassLoader() { | ||
|
||
} | ||
|
||
@Override | ||
public void checkListen(int var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkAccept(String var1, int var2) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkMulticast(InetAddress var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkMulticast(InetAddress var1, byte var2) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkPropertiesAccess() { | ||
|
||
} | ||
|
||
@Override | ||
public void checkPropertyAccess(String var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkPackageAccess(String var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkPackageDefinition(String var1) { | ||
|
||
} | ||
|
||
@Override | ||
public void checkSetFactory() { | ||
|
||
|
||
} | ||
|
||
}; | ||
System.setSecurityManager(securityManager); | ||
} | ||
} | ||
|
||
|
||
|
46 changes: 46 additions & 0 deletions
46
hbase-common/src/test/java/org/apache/hadoop/hbase/TestSystemExitInTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.hbase; | ||
|
||
import static org.junit.Assert.fail; | ||
import org.apache.hadoop.hbase.testclassification.MiscTests; | ||
import org.apache.hadoop.hbase.testclassification.SmallTests; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
@Category({MiscTests.class, SmallTests.class}) | ||
public class TestSystemExitInTest { | ||
|
||
@ClassRule | ||
public static final HBaseClassTestRule CLASS_RULE = | ||
HBaseClassTestRule.forClass(TestSystemExitInTest.class); | ||
|
||
|
||
@Test | ||
public void testSystemExit() { | ||
try { | ||
System.exit(1); | ||
fail("Expected JVM exit is blocked and exception is thrown instead " + | ||
SystemExitRule.ExitTrappedException.class.getName()); | ||
} catch (SystemExitRule.ExitTrappedException e) { | ||
// expected | ||
} | ||
} | ||
|
||
} |