File: ClientSideSystemPropertiesTest.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (155 lines) | stat: -rw-r--r-- 5,774 bytes parent folder | download | duplicates (4)
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
/*
 
   Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.ClientSideSystemProperties
 
   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.derbynet;

import java.io.File;
import java.security.AccessController;
import java.sql.Connection;
import java.util.Properties;

import javax.sql.PooledConnection;
import javax.sql.XAConnection;

import junit.framework.Test;

import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.J2EEDataSource;
import org.apache.derbyTesting.junit.JDBCDataSource;
import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
import org.apache.derbyTesting.junit.TestConfiguration;

/** The test of the jvm properties for enabling client side tracing(DERBY-1275)
  */
public class ClientSideSystemPropertiesTest extends BaseJDBCTestCase { 
	/** Establish a connection and that should start client side tracing
     *  because we have set the system properties to enable client side
     *  tracing. */
    public void testConnection() throws Exception {
        Connection conn = openDefaultConnection();
        conn.setAutoCommit(false);
        checkTraceFileIsPresent();
        conn.rollback();
        conn.close();
    }

    public void testClientDataSourceConnection() throws Exception {
       Connection conn = JDBCDataSource.getDataSource().getConnection();
       conn.setAutoCommit(false);
       checkTraceFileIsPresent();
       conn.rollback();
       conn.close();
    }
    
    public void testClientCPDataSourceConnection() throws Exception {
        PooledConnection pconn = J2EEDataSource.getConnectionPoolDataSource().
                getPooledConnection(); 
        Connection conn = pconn.getConnection();
        conn.setAutoCommit(false);
        checkTraceFileIsPresent();
        conn.rollback();
        conn.close();
        pconn.close();
     }

    public void testClientXADataSourceConnection() throws Exception {
        XAConnection xaconn = J2EEDataSource.getXADataSource().
                getXAConnection();
        Connection conn = xaconn.getConnection();
        conn.setAutoCommit(false);
        checkTraceFileIsPresent();
        conn.close();
        xaconn.close();
     }

    
    private void checkTraceFileIsPresent() {
        //Make sure the connection above created a trace file. This check is 
        //made in the privilege block below by looking inside the 
        //trace Directory and making sure the file count is greater than 0.
        AccessController.doPrivileged
		    (new java.security.PrivilegedAction<Void>(){
		    	public Void run(){
		    		File dir = new File(getSystemProperty("derby.client.traceDirectory"));
		    		int fileCounter = 0;
    	            File[] list = dir.listFiles();
    	            File tempFile;
    	            for (;fileCounter<list.length; fileCounter++)
    	            	tempFile = list[fileCounter];
    	            junit.framework.Assert.assertTrue(fileCounter>0);
    	            return null;
    		    }
    		}	 
    	    );
    }
    
    /** If the trace Directory doesn't exist then create one. If there is one
     *  already there, then delete everything under it. */
    protected void setUp() throws Exception
    {
    	AccessController.doPrivileged(
    			new java.security.PrivilegedAction<Void>(){
    				public Void run(){
    					File dir = new File(getSystemProperty("derby.client.traceDirectory"));
    					if (dir.exists() == false) //create the trace Directory
    						junit.framework.Assert.assertTrue(dir.mkdir() || dir.mkdirs());
    					else {//cleanup the trace Directory which already exists
    						int fileCounter = 0;
    						File[] list = dir.listFiles();
    						File tempFile;
    						for (;fileCounter<list.length; fileCounter++) {
    							tempFile = list[fileCounter];
    							assertTrue(tempFile.delete());
        					}
		        }
	            return null;
		    }
		}	 
	    );
    }
    
    /** Delete the trace Directory so that the test environment is clean for the
     *  next test run. */
    protected void tearDown() throws Exception
    {
        super.tearDown();
        
        removeDirectory(getSystemProperty("derby.client.traceDirectory"));
    }
    
    /* ------------------- end helper methods  -------------------------- */
    public ClientSideSystemPropertiesTest(String name) {
        super(name);
    }

    /*
     * Set the system properties related to client side tracing.
     */
    public static Test suite() {
        //Create the traceDirectory required by the tests in this class
    	Properties traceRelatedProperties = new Properties();
        traceRelatedProperties.setProperty("derby.client.traceLevel", "64");
        traceRelatedProperties.setProperty("derby.client.traceDirectory", "TraceDir");
        Test suite = TestConfiguration.clientServerSuite(ClientSideSystemPropertiesTest.class);
        return new SystemPropertyTestSetup(suite, traceRelatedProperties); 
    }
    
}