~ubuntu-branches/ubuntu/precise/tomcat7/precise-proposed

« back to all changes in this revision

Viewing changes to modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestStatementCache.java

  • Committer: Bazaar Package Importer
  • Author(s): tony mancill
  • Date: 2011-07-25 22:58:33 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110725225833-1t773ak3y3g9utm2
Tags: 7.0.19-1
* Team upload.
* New upstream release.
  - Includes fix for CVE-2011-2526 (Closes: #634992)
* Remove patch for CVE-2011-2204 (included upstream).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 * See the License for the specific language governing permissions and
 
15
 * limitations under the License.
 
16
 */
 
17
package org.apache.tomcat.jdbc.test;
 
18
 
 
19
import java.sql.Connection;
 
20
import java.sql.PreparedStatement;
 
21
 
 
22
import org.apache.tomcat.jdbc.pool.interceptor.StatementCache;
 
23
 
 
24
public class TestStatementCache extends DefaultTestCase {
 
25
 
 
26
    
 
27
    public TestStatementCache(String name) {
 
28
        super(name);
 
29
    }
 
30
    
 
31
    private static volatile TestStatementCacheInterceptor interceptor = null;
 
32
    
 
33
    
 
34
    @Override
 
35
    protected void tearDown() throws Exception {
 
36
        // TODO Auto-generated method stub
 
37
        this.interceptor = null;
 
38
        super.tearDown();
 
39
    }
 
40
 
 
41
 
 
42
    private void config(boolean cachePrepared, boolean cacheCallable, int max) {
 
43
        datasource.getPoolProperties().setJdbcInterceptors(TestStatementCacheInterceptor.class.getName()+
 
44
                "(prepared="+cachePrepared+",callable="+cacheCallable+",max="+max+")");
 
45
    }
 
46
    
 
47
    public void testIsCacheEnabled() throws Exception {
 
48
        init();
 
49
        config(true,true,50);
 
50
        datasource.getConnection().close();
 
51
        assertNotNull("Interceptor was not created.", interceptor);
 
52
    }
 
53
    
 
54
    public void testCacheProperties() throws Exception {
 
55
        init();
 
56
        config(true,true,50);
 
57
        datasource.getConnection().close();
 
58
        assertEquals(true, interceptor.isCacheCallable());
 
59
        assertEquals(true, interceptor.isCachePrepared());
 
60
        assertEquals(50,interceptor.getMaxCacheSize());
 
61
    }
 
62
    
 
63
    public void testCacheProperties2() throws Exception {
 
64
        init();
 
65
        config(false,false,100);
 
66
        datasource.getConnection().close();
 
67
        assertEquals(false, interceptor.isCacheCallable());
 
68
        assertEquals(false, interceptor.isCachePrepared());
 
69
        assertEquals(100,interceptor.getMaxCacheSize());
 
70
    }
 
71
 
 
72
    public void testPreparedStatementCache() throws Exception {
 
73
        init();
 
74
        config(true,false,100);
 
75
        Connection con = datasource.getConnection();
 
76
        PreparedStatement ps1 = con.prepareStatement("select 1");
 
77
        PreparedStatement ps2 = con.prepareStatement("select 1");
 
78
        assertEquals(0,interceptor.getCacheSize().get());
 
79
        ps1.close();
 
80
        assertTrue(ps1.isClosed());
 
81
        assertEquals(1,interceptor.getCacheSize().get());
 
82
        PreparedStatement ps3 = con.prepareStatement("select 1");
 
83
        assertEquals(0,interceptor.getCacheSize().get());
 
84
        ps2.close();
 
85
        assertTrue(ps2.isClosed());
 
86
        ps3.close();
 
87
        assertTrue(ps3.isClosed());
 
88
        assertEquals(1,interceptor.getCacheSize().get());
 
89
    }
 
90
 
 
91
    public void testPreparedStatementCache2() throws Exception {
 
92
        init();
 
93
        config(false,false,100);
 
94
        Connection con = datasource.getConnection();
 
95
        PreparedStatement ps1 = con.prepareStatement("select 1");
 
96
        PreparedStatement ps2 = con.prepareStatement("select 1");
 
97
        assertEquals(0,interceptor.getCacheSize().get());
 
98
        ps1.close();
 
99
        assertTrue(ps1.isClosed());
 
100
        assertEquals(0,interceptor.getCacheSize().get());
 
101
        PreparedStatement ps3 = con.prepareStatement("select 1");
 
102
        assertEquals(0,interceptor.getCacheSize().get());
 
103
        ps2.close();
 
104
        assertTrue(ps2.isClosed());
 
105
        ps3.close();
 
106
        assertTrue(ps3.isClosed());
 
107
        assertEquals(0,interceptor.getCacheSize().get());
 
108
    }
 
109
 
 
110
    public void testCallableStatementCache() throws Exception {
 
111
    }
 
112
 
 
113
    public void testMaxCacheSize() throws Exception {
 
114
        init();
 
115
        config(true,false,100);
 
116
        Connection con1 = datasource.getConnection();
 
117
        Connection con2 = datasource.getConnection();
 
118
        for (int i=0; i<120; i++) {
 
119
            Connection con = (i%2==0)?con1:con2;
 
120
            PreparedStatement ps = con.prepareStatement("select "+i);
 
121
            ps.close();
 
122
        }
 
123
        assertEquals(100,interceptor.getCacheSize().get());
 
124
    }
 
125
 
 
126
    
 
127
    public static class TestStatementCacheInterceptor extends StatementCache {
 
128
        public TestStatementCacheInterceptor() {
 
129
            TestStatementCache.interceptor = this;
 
130
        }
 
131
    }
 
132
 
 
133
}