1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
/*
Derby - Class org.apache.derbyTesting.unitTests.junit.AssertFailureTest
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.derbyTesting.unitTests.junit;
import junit.framework.Test;
import org.apache.derby.shared.common.sanity.AssertFailure;
import org.apache.derbyTesting.junit.BaseTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.SecurityManagerSetup;
/**
* Testcase that test that AssertFailure's message string is correct when we
* have permisssion to do thread dumps and when we don't. Depends on the
* policyfiles AssertFailureTest.policy and AssertFailureTest1.policy where only
* the former grants this permission.
*/
public class AssertFailureTest extends BaseTestCase {
/**
* Security policy file that allows thread dumps.
*/
private static String POLICY_FILENAME =
"org/apache/derbyTesting/unitTests/junit/AssertFailureTest.policy";
/**
* Security policy file that DOESN'T allow thread dumps.
*/
private static String NO_DUMP_POLICY_FILENAME =
"org/apache/derbyTesting/unitTests/junit/AssertFailureTest1.policy";
public AssertFailureTest(String name) {
super(name);
}
public static Test suite() {
BaseTestSuite suite = new BaseTestSuite("AssertFailureTest");
try {
//Only add the tests if this is a sane build.
Class.forName("org.apache.derby.shared.common.sanity." +
"AssertFailure");
// Run with thread dump permissions
suite.addTest(new SecurityManagerSetup(new AssertFailureTest(
"testAssertFailureThreadDump"), POLICY_FILENAME));
// Run WITHOUT thread dump permissions
suite.addTest(new SecurityManagerSetup(new AssertFailureTest(
"testAssertFailureNoThreadDump"), NO_DUMP_POLICY_FILENAME));
} catch (ClassNotFoundException e) {
//Ignore. Just return an empty suite.
}
return suite;
}
/**
* Test that AssertFailure's message string is correct when we have
* permission to do thread dumps. Must be run with correct permissions, ie.
* with java.lang.RuntimePermission "getStackTrace" and
* java.lang.RuntimePermission "modifyThreadGroup".
*/
public void testAssertFailureThreadDump() {
String s = new AssertFailure("AssertFailureTest").getThreadDump();
//System.out.println(s); //Debug failures
// Assert that the string is correct, by checking that
// it starts the right way.
String expected = "---------------\n" +
"Stack traces for all live threads:\nThread name=";
assertTrue("String not correct. Expected to start with:\n<"
+ expected + ">...\nWas:\n<" + s + ">.\n" ,
s.startsWith(expected));
}
/**
* Test that AssertFailure's log entry is correct when we DON'T have
* permission to to thread dumps. Must be run with correct permissions, ie.
* WITHOUT java.lang.RuntimePermission "getStackTrace" and
* java.lang.RuntimePermission "modifyThreadGroup";
*/
public void testAssertFailureNoThreadDump() {
String s = new AssertFailure("AssertFailureTest").getThreadDump();
//System.out.println(s); //Debug failures.
// Assert that the string is correct, by checking that is starts
// the right way.
String expected = "(Skipping thread dump because of insufficient " +
"permissions:\njava.security.AccessControlException:";
assertTrue("String not correct. Expected: <" + expected +
">\nWas:\n<" + s + ">", s.startsWith(expected));
}
}
|