KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > AggregateClassLoading


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.AggregateClassLoading
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.lang;
23
24 import java.net.URL JavaDoc;
25 import java.net.URLClassLoader JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Statement JavaDoc;
30
31 import org.apache.derby.tools.ij;
32
33 /**
34  * Test for ensuring the aggregate implementation classes are loaded
35  * correctly, even when the context class loader loads Derby engine
36  * classes as well. This is a typical situation we have seen with
37  * J2EE servers where Derby may be in the application WAR and provided
38  * as a system service by the container.
39  * <BR>
40  * Jira issue DERBY-997
41  * <BR>
42  * Assumes embedded and only needs to be run in embedded, since
43  * all class loading happens on the engine side.
44  *
45  */

46 public class AggregateClassLoading {
47     
48     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
49
50         System.out.println("Test AggregateClassLoading starting");
51
52         // use the ij utility to read the property file and
53
// make the initial connection.
54
ij.getPropertyArg(args);
55         Connection JavaDoc conn = ij.startJBMS();
56         
57         // Find the location of the code for the Derby connection.
58
// The rest of the engine will be at the same location!
59
URL JavaDoc derbyURL = conn.getClass().getProtectionDomain().getCodeSource().getLocation();
60         
61         // Create a new loader that loads from the same location as the engine.
62
// Create it without a parent, otherwise the parent
63
// will be the class loader of this class which is most likely
64
// the same as the engine. Since the class loader delegates to
65
// its parent first the bug would not show, as all the derby
66
// engine classes would be from a single loader.
67
URLClassLoader JavaDoc cl = new URLClassLoader JavaDoc(new URL JavaDoc[] {derbyURL}, null);
68         Thread.currentThread().setContextClassLoader(cl);
69         
70         Statement JavaDoc s = conn.createStatement();
71         
72         s.execute("create table t (i int)");
73         s.execute("insert into t values 1,2,3,4,5,6,null,4,5,456,2,4,6,7,2144,44,2,-2,4");
74         System.out.println(s.getUpdateCount() + " rows inserted");
75         
76         // Test some aggregates, their generated class will attempt
77
// to load the internal aggregate through the context loader
78
// first, and then any remaining loader.
79
testAggregate(s, "select MAX(i) from t");
80         testAggregate(s, "select MIN(i) from t");
81         testAggregate(s, "select AVG(i) from t");
82         testAggregate(s, "select COUNT(i) from t");
83         testAggregate(s, "select COUNT(*) from t");
84         
85         s.execute("drop table t");
86         s.close();
87         conn.close();
88         
89         Thread.currentThread().setContextClassLoader(null);
90     }
91     
92     /**
93      * Just run and display the aggregates result.
94      */

95     private static void testAggregate(Statement JavaDoc s, String JavaDoc query) {
96         try {
97             ResultSet JavaDoc rs = s.executeQuery(query);
98             rs.next();
99             System.out.println("query = " + rs.getInt(1));
100             rs.close();
101         } catch (SQLException JavaDoc e) {
102             System.out.println("FAIL " + e.getSQLState() + " " + e.getMessage());
103             // TODO Auto-generated catch block
104
e.printStackTrace(System.out);
105         }
106    }
107 }
108
Popular Tags