1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
37
38
39
40
41
42
43
44 public class JDBCResourcesTestCase
45 extends CollectionResourcesBaseTestCase {
46
47
48
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
67
68
69 public JDBCResourcesTestCase(String name) {
70 super(name);
71 }
72
73
74
75
76
77
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
88
89
90
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
116 public static Test suite() {
117 return (new TestSuite(JDBCResourcesTestCase.class));
118 }
119
120
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
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
174
175
176
177
178
179
180
181
182
183
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
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
215
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 }