-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PHOENIX-6275 : Handle JVM exit gracefully in tests
- Loading branch information
1 parent
827067d
commit c1f16d4
Showing
7 changed files
with
254 additions
and
3 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
phoenix-core/src/test/java/org/apache/phoenix/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,55 @@ | ||
/* | ||
* 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.phoenix; | ||
|
||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
|
||
/** | ||
* Test rule that prevents System.exit / JVM exit to error out the test runner, which manages | ||
* JVM and providing test output files, and instead throw valid Exception to handle JVM exit | ||
* gracefully | ||
*/ | ||
public class SystemExitRule implements TestRule { | ||
|
||
private static final SecurityManager SECURITY_MANAGER = new TestSecurityManager(); | ||
|
||
@Override | ||
public Statement apply(final Statement s, Description d) { | ||
return new Statement() { | ||
@Override | ||
public void evaluate() throws Throwable { | ||
try { | ||
System.setSecurityManager(SECURITY_MANAGER); | ||
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 SystemExitInTestException extends SecurityException { | ||
// empty | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
phoenix-core/src/test/java/org/apache/phoenix/TestJVMExit.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,37 @@ | ||
/* | ||
* 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.phoenix; | ||
|
||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
|
||
public class TestJVMExit { | ||
|
||
@ClassRule | ||
public static final SystemExitRule SYSTEM_EXIT_RULE = new SystemExitRule(); | ||
|
||
@Test(expected = SystemExitRule.SystemExitInTestException.class) | ||
public void testSystemExit() { | ||
System.exit(10); | ||
} | ||
|
||
@Test(expected = SystemExitRule.SystemExitInTestException.class) | ||
public void testRuntimeHalt() { | ||
Runtime.getRuntime().halt(10); | ||
} | ||
|
||
} |
139 changes: 139 additions & 0 deletions
139
phoenix-core/src/test/java/org/apache/phoenix/TestSecurityManager.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,139 @@ | ||
/* | ||
* 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.phoenix; | ||
|
||
import java.io.FileDescriptor; | ||
import java.net.InetAddress; | ||
import java.security.Permission; | ||
|
||
/** | ||
* Custom SecurityManager impl used by SystemExitRule | ||
*/ | ||
class TestSecurityManager extends SecurityManager { | ||
|
||
@Override | ||
public void checkExit(int status) { | ||
throw new SystemExitRule.SystemExitInTestException(); | ||
} | ||
|
||
@Override | ||
public void checkPermission(Permission permission) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkPermission(Permission var1, Object var2) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkSecurityAccess(String var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkConnect(String var1, int var2, Object var3) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkWrite(String var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkDelete(String var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkConnect(String var1, int var2) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkLink(String var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkRead(FileDescriptor var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkAccess(Thread var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkAccess(ThreadGroup var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkCreateClassLoader() { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkListen(int var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkAccept(String var1, int var2) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkMulticast(InetAddress var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkMulticast(InetAddress var1, byte var2) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkPropertiesAccess() { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkPropertyAccess(String var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkPackageAccess(String var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkPackageDefinition(String var1) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void checkSetFactory() { | ||
// no-op | ||
} | ||
|
||
} |
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
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
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
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