001 /* 002 * $Id: JDBCResourcesTestCase.java 354761 2005-12-07 15:11:58Z niallp $ 003 * $Revision: 354761 $ 004 * $Date: 2005-12-07 15:11:58 +0000 (Wed, 07 Dec 2005) $ 005 * 006 * ==================================================================== 007 * 008 * Copyright 2003-2005 The Apache Software Foundation 009 * 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 * 022 */ 023 package org.apache.commons.resources.impl; 024 025 import java.net.URL; 026 import java.sql.Connection; 027 import java.sql.DriverManager; 028 import java.sql.SQLException; 029 import java.sql.Statement; 030 import java.util.StringTokenizer; 031 032 import junit.framework.Test; 033 import junit.framework.TestSuite; 034 035 /** 036 * <p>Unit tests for 037 * <code>org.apache.commons.resources.impl.JDBCResources</code>. 038 * </p> 039 * 040 * @author James Mitchell 041 * @version $Revision: 354761 $ $Date: 2005-12-07 15:11:58 +0000 (Wed, 07 Dec 2005) $ 042 */ 043 044 public class JDBCResourcesTestCase 045 extends CollectionResourcesBaseTestCase { 046 047 048 // ----------------------------------------------------- Instance Variables 049 private Connection con; 050 051 052 protected String getBase() throws Exception 053 { 054 URL url = 055 JDBCResourcesTestCase.class.getResource 056 ("/org/apache/commons/resources/impl/jdbc.test.config.properties"); 057 058 if (url == null) { 059 fail("URL NOT FOUND"); 060 } 061 String string = url.toExternalForm(); 062 String base = string.substring(0, string.length() - 11); 063 return base; 064 } 065 066 // ----------------------------------------------------------- Constructors 067 068 069 public JDBCResourcesTestCase(String name) { 070 super(name); 071 } 072 073 074 // --------------------------------------------------- Overall Test Methods 075 076 077 // Set up instance variables required by this test case 078 public void setUp() throws Exception { 079 if (isPre14JVM()) { 080 System.out.println("WARNING: CANNOT TEST with HSQLDB 1.7.2 ON PRE1.4 JVM"); 081 return; 082 } 083 084 factory = new JDBCResourcesFactory(); 085 resources = factory.getResources(NAME, getBase()); 086 087 // InputStream in = this.getClass().getResourceAsStream("init.sql"); 088 // File file = new File("init.sql"); 089 090 // change this to load from init.sql 091 String[] sql = {"" 092 + "create table resources (" 093 + " locale varchar(10) not null," 094 + " msgKey varchar(255) not null," 095 + " val varchar(255)," 096 + " Primary Key(" 097 + " locale," 098 + " msgKey" 099 + " )" 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 }