1   /*
2    * $Id: JDBCResourcesTestCase.java 354761 2005-12-07 15:11:58Z niallp $
3    * $Revision: 354761 $
4    * $Date: 2005-12-07 15:11:58 +0000 (Wed, 07 Dec 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2003-2005 The Apache Software Foundation
9    * 
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   *
22   */
23  package org.apache.commons.resources.impl;
24  
25  import java.net.URL;
26  import java.sql.Connection;
27  import java.sql.DriverManager;
28  import java.sql.SQLException;
29  import java.sql.Statement;
30  import java.util.StringTokenizer;
31  
32  import junit.framework.Test;
33  import junit.framework.TestSuite;
34  
35  /**
36   * <p>Unit tests for
37   * <code>org.apache.commons.resources.impl.JDBCResources</code>.
38   * </p>
39   *
40   * @author James Mitchell
41   * @version $Revision: 354761 $ $Date: 2005-12-07 15:11:58 +0000 (Wed, 07 Dec 2005) $
42   */
43  
44  public class JDBCResourcesTestCase
45      extends CollectionResourcesBaseTestCase {
46  
47  
48      // ----------------------------------------------------- Instance Variables
49      private Connection con;
50  
51  
52      protected String getBase() throws Exception
53      {
54          URL url = 
55              JDBCResourcesTestCase.class.getResource
56              ("/org/apache/commons/resources/impl/jdbc.test.config.properties");
57      
58          if (url == null) {
59              fail("URL NOT FOUND");
60          }
61          String string = url.toExternalForm();
62          String base = string.substring(0, string.length() - 11);
63          return base;
64      }
65  
66      // ----------------------------------------------------------- Constructors
67  
68  
69      public JDBCResourcesTestCase(String name) {
70          super(name);
71      }
72  
73  
74      // --------------------------------------------------- Overall Test Methods
75  
76  
77      // Set up instance variables required by this test case
78      public void setUp() throws Exception {
79          if (isPre14JVM()) {
80              System.out.println("WARNING: CANNOT TEST with HSQLDB 1.7.2 ON PRE1.4 JVM");
81              return;
82          }
83  
84          factory = new JDBCResourcesFactory();
85          resources = factory.getResources(NAME, getBase());
86          
87  //        InputStream in = this.getClass().getResourceAsStream("init.sql");
88  //        File file = new File("init.sql");
89          
90          // change this to load from init.sql
91          String[] sql = {""
92          + "create table resources (" 
93          + "  locale            varchar(10)      not null,"
94          + "  msgKey            varchar(255)     not null,"
95          + "  val               varchar(255),"
96          + "  Primary Key("
97          + "    locale,"
98          + "    msgKey"
99          + "  )"
100         + " );"
101         , "Insert into resources (locale, msgKey, val) Values ('', 'test.base', '[Base] ONLY');"
102         , "Insert into resources (locale, msgKey, val) Values ('', 'test.specific', '[Base] SPECIFIC');"
103         , "Insert into resources (locale, msgKey, val) Values ('', 'test.inherit', '[Base] INHERIT');"
104         , "Insert into resources (locale, msgKey, val) Values ('', 'test.message.single', '[Base] REPLACE {0}');"
105         , "Insert into resources (locale, msgKey, val) Values ('', 'test.message', '[Base] REPLACE {0} WITH {1}');"
106         , "Insert into resources (locale, msgKey, val) Values ('en', 'test.specific', '[en] SPECIFIC');"
107         , "Insert into resources (locale, msgKey, val) Values ('en', 'test.inherit', '[en] INHERIT');"
108         , "Insert into resources (locale, msgKey, val) Values ('en_US', 'test.specific', '[en_US] SPECIFIC');"
109         , "Insert into resources (locale, msgKey, val) Values ('fr', 'test.specific', '[fr] SPECIFIC');"
110         , "Insert into resources (locale, msgKey, val) Values ('fr', 'test.inherit', '[fr] INHERIT');"
111         };
112         runSql(sql);
113     }
114 
115     // Return the tests included in this test suite
116     public static Test suite() {
117         return (new TestSuite(JDBCResourcesTestCase.class));
118     }
119 
120     // Tear down the instance variables required by this test case
121     public void tearDown() {
122         resources = null;
123         factory = null;
124         if (isPre14JVM()) {
125             return;
126         }
127         try{
128             runSql(new String[]{"drop table resources"});
129             con.close();
130         }catch(Exception e){
131             // not really necessary to fail if creation also fails
132             e.printStackTrace();
133         }
134 
135     }
136     public void testInherit() throws Exception {
137         if (!isPre14JVM()) {
138             super.testInherit();
139         }
140     }
141     public void testLocaleList() {
142         if (!isPre14JVM()) {
143             super.testLocaleList();
144         }
145     }
146     public void testMissing() throws Exception {
147         if (!isPre14JVM()) {
148             super.testMissing();
149         }
150     }
151     public void testSpecific() throws Exception {
152         if (!isPre14JVM()) {
153             super.testSpecific();
154         }
155     }
156     public void testKeys() throws Exception {
157         if (!isPre14JVM()) {
158             super.testKeys();
159         }
160     }
161     public void testPristine() {
162         if (!isPre14JVM()) {
163             super.testPristine();
164         }
165     }
166     public void testRelease() throws Exception {
167         if (!isPre14JVM()) {
168             super.testRelease();
169         }
170     }
171 
172 
173     // ------------------------------------------------ Individual Test Methods
174 
175 
176     // ------------------------------------------------------ Protected Methods
177 
178 
179     // ------------------------------------------------------ Private Methods
180 
181     /**
182      * @param sql
183      * @throws SQLException
184      */
185     private void runSql(String[] sql) throws SQLException {
186 
187         String url = "jdbc:hsqldb:.";
188         
189         Statement stmt;
190         try {
191             Class.forName("org.hsqldb.jdbcDriver");
192 
193         } catch(java.lang.ClassNotFoundException e) {
194             fail("ClassNotFoundException:" + e.getMessage());
195         }
196 
197         try {
198             // TODO change this to pull from jdbc.test.config.properties
199             if (con == null || con.isClosed())
200                 con = DriverManager.getConnection(url, "sa", "");
201             stmt = con.createStatement();
202             
203             for (int i = 0; i < sql.length; i++) {
204                    stmt.execute(sql[i]);
205             }
206             stmt.close();
207     
208         } catch(SQLException ex) {
209             fail("SQLException: " + ex.getMessage());
210         }
211     }
212 
213     private boolean isPre14JVM() {
214         // some pre 1.4 JVM have buggy WeakHashMap implementations 
215         // this is used to test for those JVM
216         String version = System.getProperty("java.specification.version");
217         StringTokenizer tokenizer = new StringTokenizer(version,".");
218         if (tokenizer.nextToken().equals("1")) {
219             String minorVersion = tokenizer.nextToken();
220             if (minorVersion.equals("0")) return true;
221             if (minorVersion.equals("1")) return true;
222             if (minorVersion.equals("2")) return true;
223             if (minorVersion.equals("3")) return true;
224         }
225         return false;
226     }
227 }