1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
*
* Derby - Class org.apache.derbyTesting.functionTests.tests.lang.UrlLocaleTest
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* i18n governing permissions and limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.i18n;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.Locale;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.LocaleTestSetup;
import org.apache.derbyTesting.junit.SupportFilesSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
public class UrlLocaleTest extends BaseJDBCTestCase {
public UrlLocaleTest(String name) {
super(name);
}
/**
* Shutdown on tearDown to make sure all databases can be removed.
* @throws Exception
* @see org.apache.derbyTesting.junit.BaseJDBCTestCase#tearDown()
*/
public void tearDown() throws Exception {
super.tearDown();
TestConfiguration.getCurrent().shutdownEngine();
// Reregister driver for any subsequent tests
String driverClass =
TestConfiguration.getCurrent().getJDBCClient().getJDBCDriverName();
Class<?> clazz = Class.forName(driverClass);
clazz.getConstructor().newInstance();
}
/**
* Test valid territory settings on URL
*
*/
public void testURLLocale() throws SQLException, MalformedURLException {
// check this current database was created with the default locale
Statement s = createStatement();
s.executeUpdate("call checkRDefaultLoc()");
// create a swiss database
String url = getReadWriteJDBCURL("swissdb");
url += ";create=true;territory=fr_CH";
Connection locConn = DriverManager.getConnection(url);
createLocaleProcedures(locConn);
Statement locStatement = locConn.createStatement();
locStatement.executeUpdate("call checkDatabaseLoc('fr_CH')");
locStatement.close();
locConn.close();
//-- create a Hindi in India database (hi_IN)
url = getReadWriteJDBCURL("hindi");
url += ";create=true;territory=hi_IN";
locConn = DriverManager.getConnection(url);
createLocaleProcedures(locConn);
locStatement = locConn.createStatement();
locStatement.executeUpdate("call checkDatabaseLoc('hi_IN')");
locStatement.close();
locConn.close();
//- now try one with a variant
// -- create a English in Israel database for JavaOS en_IL_JavaOS
url = getReadWriteJDBCURL("Israel");
url += ";create=true;territory=en_IL_JavaOS";
locConn = DriverManager.getConnection(url);
createLocaleProcedures(locConn);
locStatement = locConn.createStatement();
locStatement.executeUpdate("call checkDatabaseLoc('en_IL_JavaOS')");
locStatement.close();
locConn.close();
// now try with just a language - we support this
// as some vms do.
url = getReadWriteJDBCURL("bacon");
url += ";create=true;territory=da";
locConn = DriverManager.getConnection(url);
createLocaleProcedures(locConn);
locStatement = locConn.createStatement();
locStatement.executeUpdate("call checkDatabaseLoc('da')");
locStatement.close();
locConn.close();
}
/**
* Test invalid territory settings
*/
public void testUrlLocaleNegative() throws SQLException {
//Connection without territory specified in territory attribute
String url = getReadWriteJDBCURL("fail1");
url += ";create=true;territory=";
checkInvalidTerritoryFormat(url);
//- database will not have been created so this connection will fail
url = getReadWriteJDBCURL("fail1");
try {
DriverManager.getConnection(url);
fail("Database connect " + url + " should fail because db does not exist");
} catch (SQLException se ) {
assertSQLState("XJ004", se);
}
//Invalid territory specification
checkInvalidTerritoryFormat("en_");
checkInvalidTerritoryFormat("en_d");
checkInvalidTerritoryFormat("en-US");
}
/**
* Test valid message resolution for an unknown Locale.
* converted from i18n/messageLocale.sql
*
* This test case must run in a decorator that sets the locale to one
* that is not recognized by Derby.
*/
public void messageLocale_unknown() throws SQLException {
String url = getReadWriteJDBCURL("rrTTdb");
url += ";create=true";
Connection locConn = DriverManager.getConnection(url);
Statement s = locConn.createStatement();
createLocaleProcedures(locConn);
// check this current database was created with the default locale rr_TT
s.executeUpdate("call checkDefaultLoc()");
// check database Locale
s.executeUpdate("call checkDatabaseLoc('rr_TT')");
// Expect an error in English because rr_TT has no translated messages.
// Language is determined by choosing a random word (that we hope
// won't change) in the current
try {
s.executeUpdate("create table t1 oops (i int)");
} catch (SQLException se) {
assertSQLState("42X01", se);
assertTrue("Expected English Message with \"Encountered\" " ,
(se.getMessage().indexOf("Encountered") != -1));
}
// Setup for warning
s.executeUpdate("create table t2 (i int)");
s.executeUpdate("create index i2_a on t2(i)");
// Expect WARNING to also be English. Index is a duplicate
s.executeUpdate("create index i2_b on t2(i)");
SQLWarning sqlw = s.getWarnings();
assertSQLState("01504", sqlw);
assertTrue("Expected English warning",
sqlw.getMessage().indexOf("duplicate") != -1);
s.close();
locConn.close();
}
/**
* Test valid message resolution for German Locale.
* converted from i18n/messageLocale.sql
*
* This test case must run in a decorator that sets the default locale
* to Locale.GERMANY.
*/
public void messageLocale_Germany() throws SQLException {
//create a database with a locale that has a small
// number of messages. Missing ones will default to
// the locale of the default locale: German;
String url = getReadWriteJDBCURL("qqPPdb");
url += ";create=true;territory=qq_PP_testOnly";
Connection locConn = DriverManager.getConnection(url);
Statement s = locConn.createStatement();
s.executeUpdate("create table t2 (i int)");
s.executeUpdate("create index i2_a on t2(i)");
// Error that is in qq_PP messages
try {
s.executeUpdate("create table t1 oops (i int)");
} catch (SQLException se) {
assertSQLState("42X01", se);
assertTrue("Expected qq_PP Message with \"Encountered\" " ,
(se.getMessage().indexOf("Encountered") != -1));
}
// Expect WARNING to be in German (default) because there is no
//qq_PP message. Index is a duplicate
s.executeUpdate("create index i2_b on t2(i)");
SQLWarning sqlw = s.getWarnings();
assertSQLState("01504", sqlw);
assertTrue("Expected German warning with Duplikat",
sqlw.getMessage().indexOf(" Duplikat") != -1);
// Error from default German Locale as it does not exist in qq_PP
// from default locale (German);
try {
s.executeUpdate("drop table t3");
} catch (SQLException se) {
assertSQLState("42Y55", se);
assertTrue("Expected German Message with vorhanden" ,
(se.getMessage().indexOf("vorhanden") != -1));
}
//Now all English messages
url = getReadWriteJDBCURL("enUSdb");
url += ";create=true;territory=en_US";
locConn = DriverManager.getConnection(url);
s = locConn.createStatement();
s.executeUpdate("create table t2 (i int)");
s.executeUpdate("create index i2_a on t2(i)");
try {
s.executeUpdate("create table t1 oops (i int)");
} catch (SQLException se) {
assertSQLState("42X01", se);
assertTrue("Expected English message with \"Encountered\" " ,
(se.getMessage().indexOf("Encountered") != -1));
}
// Expect WARNING to be in English because it is English db
// Even though German default Locale still
s.executeUpdate("create index i2_b on t2(i)");
sqlw = s.getWarnings();
assertSQLState("01504", sqlw);
assertTrue("Expected English warning with duplicate",
sqlw.getMessage().indexOf("duplicate") != -1);
try {
s.executeUpdate("drop table t3");
} catch (SQLException se) {
assertSQLState("42Y55", se);
assertTrue("Expected English Message with performed" ,
(se.getMessage().indexOf("performed") != -1));
}
}
private void checkInvalidTerritoryFormat(String territory) {
try {
String url = getReadWriteJDBCURL("fail3");
url += ";create=true;territory=" + territory;
DriverManager.getConnection(url);
fail("connection without territory: " + url + "should have failed");
} catch (SQLException se ) {
assertSQLState("XJ041", se);
assertSQLState("XBM0X", se.getNextException());
}
}
/**
* Get JDBC URL for database to be accessed in the read-write directory
* @param dbname short database name to be created
* @return the JDBC URL for the database
*/
private static String getReadWriteJDBCURL(String dbname)
{
return TestConfiguration.getCurrent().
getJDBCUrl(SupportFilesSetup.getReadWriteFileName(dbname));
}
/**
* Create procedures to test current territory value
*
*/
private static void createLocaleProcedures(Connection conn) throws SQLException {
Statement s = conn.createStatement();
s.executeUpdate("create procedure checkDatabaseLoc(in locale " +
"char(12)) parameter style java language java external name " +
"'org.apache.derbyTesting.functionTests.tests.i18n." +
"DefaultLocale.checkDatabaseLocale'");
s.executeUpdate("create procedure checkRDefaultLoc() parameter " +
"style java language java external name " +
"'org.apache.derbyTesting.functionTests.tests.i18n." +
"DefaultLocale.checkRDefaultLocale'");
s.executeUpdate("create procedure checkDefaultLoc() parameter " +
"style java language java external name " +
"'org.apache.derbyTesting.functionTests.tests.i18n." +
"DefaultLocale.checkDefaultLocale'");
}
public static Test suite() {
BaseTestSuite suite = new BaseTestSuite();
suite.addTestSuite(UrlLocaleTest.class);
suite.addTest(new LocaleTestSetup(
new UrlLocaleTest("messageLocale_unknown"),
new Locale("rr", "TT")));
suite.addTest(new LocaleTestSetup(
new UrlLocaleTest("messageLocale_Germany"),
Locale.GERMANY));
Test tsuite = new CleanDatabaseTestSetup(suite) {
/**
* Creates the table used in the test cases.
*
*/
protected void decorateSQL(Statement s) throws SQLException {
createLocaleProcedures(s.getConnection());
}
};
tsuite = new SupportFilesSetup(tsuite);
return tsuite;
}
}
|