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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.AbortTest
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.functionTests.tests.jdbc4;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.J2EEDataSource;
import org.apache.derbyTesting.junit.SecurityManagerSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Tests for the new JDBC 4.1 Connection.abort(Executor) method. This
* class tests the affect of SecurityManagers on the method. A related
* test case can be found in ConnectionMethodsTest.
*/
public class AbortTest extends Wrapper41Test
{
///////////////////////////////////////////////////////////////////////
//
// CONSTANTS
//
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//
// STATE
//
///////////////////////////////////////////////////////////////////////
private boolean _hasSecurityManager;
///////////////////////////////////////////////////////////////////////
//
// CONSTRUCTOR
//
///////////////////////////////////////////////////////////////////////
public AbortTest(String name, boolean hasSecurityManager)
{
super(name);
_hasSecurityManager = hasSecurityManager;
}
///////////////////////////////////////////////////////////////////////
//
// JUnit BEHAVIOR
//
///////////////////////////////////////////////////////////////////////
public static Test suite()
{
BaseTestSuite suite = new BaseTestSuite( "AbortTest" );
suite.addTest( baseSuite( true ) );
suite.addTest( baseSuite( false ) );
suite.addTest
(
TestConfiguration.clientServerDecorator
( baseSuite( true ) )
);
suite.addTest
(
TestConfiguration.clientServerDecorator
( baseSuite( false ) )
);
return suite;
}
public static Test baseSuite( boolean hasSecurityManager )
{
AbortTest abortTest = new AbortTest( "test_basic", hasSecurityManager );
Test test = new CleanDatabaseTestSetup( abortTest )
{
protected void decorateSQL( Statement s ) throws SQLException
{
s.execute("create table abort_table( a int )");
}
};
if ( hasSecurityManager )
{
return new SecurityManagerSetup( test, "org/apache/derbyTesting/functionTests/tests/jdbc4/noAbortPermission.policy" );
}
else
{
return SecurityManagerSetup.noSecurityManager( test );
}
}
///////////////////////////////////////////////////////////////////////
//
// TESTS
//
///////////////////////////////////////////////////////////////////////
/**
* <p>
* Test Connection.abort(Executor) with and without a security manager.
* </p>
*/
public void test_basic() throws Exception
{
//
// Only run if we can grant permissions to the jar files.
//
if ( !TestConfiguration.loadingFromJars() ) { return; }
println( "AbortTest( " + _hasSecurityManager + " )" );
assertEquals( _hasSecurityManager, (System.getSecurityManager() != null) );
physical();
pooled();
xa();
}
private void physical() throws Exception
{
Connection conn0 = openUserConnection( "user0");
Connection conn1 = openUserConnection( "user1");
Connection conn2 = openUserConnection( "user2");
vet( conn0, conn1, conn2 );
}
private void pooled() throws Exception
{
ConnectionPoolDataSource cpDs =
J2EEDataSource.getConnectionPoolDataSource();
PooledConnection conn0 = getPooledConnection( cpDs, "user3");
PooledConnection conn1 = getPooledConnection( cpDs, "user4");
PooledConnection conn2 = getPooledConnection( cpDs, "user5");
vet( conn0.getConnection(), conn1.getConnection(), conn2.getConnection() );
}
private PooledConnection getPooledConnection
( ConnectionPoolDataSource cpDs, String userName ) throws Exception
{
return cpDs.getPooledConnection( userName, getTestConfiguration().getPassword( userName ) );
}
private void xa() throws Exception
{
XADataSource xads = J2EEDataSource.getXADataSource();
XAConnection conn0 = getXAConnection( xads, "user6");
XAConnection conn1 = getXAConnection( xads, "user7");
XAConnection conn2 = getXAConnection( xads, "user8");
vet( conn0.getConnection(), conn1.getConnection(), conn2.getConnection() );
}
private XAConnection getXAConnection
( XADataSource xads, String userName ) throws Exception
{
return xads.getXAConnection( userName, getTestConfiguration().getPassword( userName ) );
}
/**
* <p>
* Test Connection.abort(Executor) with and without a security manager.
* </p>
*/
public void vet( Connection conn0, Connection conn1, Connection conn2 ) throws Exception
{
assertNotNull( conn0 );
assertNotNull( conn1 );
assertNotNull( conn2 );
// NOP if called on a closed connection
conn0.close();
Wrapper41Conn wrapper0 = new Wrapper41Conn( conn0 );
wrapper0.abort( new ConnectionMethodsTest.DirectExecutor() );
conn1.setAutoCommit( false );
final Wrapper41Conn wrapper1 = new Wrapper41Conn( conn1 );
// the Executor may not be null
try {
wrapper1.abort( null );
}
catch (SQLException se)
{
assertSQLState( "XCZ02", se );
}
if ( _hasSecurityManager ) { missingPermission( wrapper1 ); }
else { noSecurityManager( wrapper1, conn2 ); }
}
// Run if we have a security manager. This tests that abort() fails
// if the caller has not been granted the correct permission.
private void missingPermission( final Wrapper41Conn wrapper1 ) throws Exception
{
// should not be able to abort the connection because this code
// lacks the permission
try {
//
// This doPrivileged block absolves outer code blocks (like JUnit)
// of the need to be granted SQLPermission( "callAbort" ). However,
// derbyTesting.jar still needs that permission.
//
AccessController.doPrivileged
(
new PrivilegedExceptionAction<Object>()
{
public Object run() throws Exception
{
ConnectionMethodsTest.DirectExecutor executor = new ConnectionMethodsTest.DirectExecutor();
wrapper1.abort( executor );
return null;
}
}
);
fail( "The call to Connection.abort(Executor) should have failed." );
}
catch (Exception e)
{
assertTrue( e instanceof AccessControlException );
}
}
// Run if we don't have a security manager. Verifies that abort() is uncontrolled
// in that situation.
private void noSecurityManager( final Wrapper41Conn wrapper1, Connection conn2 ) throws Exception
{
PreparedStatement ps = prepareStatement
( wrapper1.getWrappedObject(), "insert into app.abort_table( a ) values ( 1 )" );
ps.execute();
ps.close();
// abort the connection
ConnectionMethodsTest.DirectExecutor executor = new ConnectionMethodsTest.DirectExecutor();
wrapper1.abort( executor );
// verify that the connection is closed
try {
prepareStatement( wrapper1.getWrappedObject(), "select * from sys.systables" );
fail( "Connection should be dead!" );
}
catch (SQLException se)
{
assertSQLState( "08003", se );
}
// verify that the changes were rolled back
ps = prepareStatement( conn2, "select * from app.abort_table" );
ResultSet rs = ps.executeQuery();
assertFalse( rs.next() );
rs.close();
ps.close();
conn2.close();
}
}
|