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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.store.ServicePropertiesFileTest
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 language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.store;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.JDBCDataSource;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Tests Derby's ability to recover from various conditions related to the
* service properties file.
* <p>
* The basic pattern of the tests is to start with a pristine database, modify
* 'service.properties' and/or 'service.propertiesold', and then finally boot
* the database and assert what happened with the two aforementioned files.
*/
public class ServicePropertiesFileTest
extends BaseJDBCTestCase {
//DERBY-5816
//Service Properties File is always encoded in ISO-8859-1
// because it is written with Properties.store
private static final String SPF_ENCODING = "ISO-8859-1";
private static final String LOG_A_MODE =
"derby.storage.logArchiveMode";
/**
* End-of-file token used by Derby in 'service.properties'.
*/
private static final String END_TOKEN =
"#--- last line, don't put anything after this line ---";
/** Logical name of the pristine database. */
private static final String DB_NAME = "spfTestDb";
/** Where the databases are living. */
private static File databasesDir;
/** Path to the pristine database. */
private static File pristineDb;
/** Whether the pristine database has been initialized or not. */
private static boolean dbInitialized;
/** Database that will be deleted during {@code shutDown}. */
private File dbToDelete;
/**
* Path to 'service.properties' of the current database.
* @see #copyDbAs
*/
private File spf;
/**
* Path to 'service.propertiesold' of the current database.
* @see #copyDbAs
*/
private File spfOld;
public ServicePropertiesFileTest(String name) {
super(name);
}
/**
* Initializes the pristine database if required.
*/
public void setUp()
throws SQLException {
if (!dbInitialized) {
DataSource ds = JDBCDataSource.getDataSourceLogical(DB_NAME);
JDBCDataSource.setBeanProperty(ds, "createDatabase", "create");
ds.getConnection();
JDBCDataSource.shutdownDatabase(ds);
File systemHome = new File(getSystemProperty("derby.system.home"));
databasesDir = new File(systemHome, "singleUse");
pristineDb = new File(
systemHome,
TestConfiguration.getCurrent().getPhysicalDatabaseName(
DB_NAME));
dbInitialized = true;
}
}
/**
* Deletes the last database copy (if one exists).
*/
public void tearDown()
throws Exception {
if (dbToDelete != null) {
assertDirectoryDeleted(dbToDelete);
}
super.tearDown();
}
/**
* Tests what happens when the service properties file is missing and there
* is no backup available.
*/
public void testMissingServicePropertiesFileNoBackup()
throws IOException, SQLException {
// Prepare
String db = "spfTestMissingSPFNB";
copyDbAs(db);
PrivilegedFileOpsForTests.delete(spf);
assertPresence(false, false);
// This will currently fail with a message saying the database wasn't
// found, even though everything is there except for the service
// properties file.
try {
connectThenShutdown(db);
fail("booted database without a service.properties file");
} catch (SQLException sqle) {
assertSQLState("error message has changed", "XJ004", sqle);
}
}
/**
* Tests handling of the situation where the service properties file is
* missing, but a backup is available.
* <p>
* The expected behavior is to restore (by renaming) the service properties
* file from the backup.
*/
public void testMissingServicePropertiesFileWithBackup()
throws IOException, SQLException {
// Prepare
String db = "spfTestMissingSPFWB";
copyDbAs(db);
createSPFBackup(false);
assertPresence(false, true);
// Recover and assert
connectThenShutdown(db);
assertNormalPresence();
}
/**
* Tests handling of the situation where both the service properties file
* and the backup are available.
* <p>
* Expected behavior here is to delete the backup (given that the original
* service properties file contains the end-of-file token).
*/
public void testSevicePropertiesFileWithBackup()
throws IOException, SQLException {
// Prepare
String db = "spfTestSPFWB";
copyDbAs(db);
createSPFBackup(true);
assertPresence(true, true);
// Recover and assert
connectThenShutdown(db);
assertNormalPresence();
assertEOFToken(spf,SPF_ENCODING);
}
/**
* Tests the situation where both the service properties file and a backup
* are available, but the service properties file is corrupted (see note
* below).
* <p>
* The expected behavior is to delete the original service properties file
* and then restore it from the backup (i.e. by renaming).
* <p>
* In this regard, a corrupt service properties file is one where the
* end-of-file token is missing. No other error conditions are detected,
* i.e. if properties are removed manually or the values are modified.
*/
public void testSevicePropertiesFileCorruptedWithBackup()
throws IOException, SQLException {
// Prepare
String db = "spfTestSPFCWB";
copyDbAs(db);
createSPFBackup(true);
removeEOFToken(spf, SPF_ENCODING);
assertPresence(true, true);
// Recover and assert
connectThenShutdown(db);
assertNormalPresence();
assertEOFToken(spf,SPF_ENCODING);
}
/**
* Ensures that Derby can handle the case where the backup file already
* exists when editing the service properties.
*/
public void testBackupWithBackupExisting()
throws IOException, SQLException {
// Prepare
String db = "spfTestBWBE";
copyDbAs(db);
assertPresence(true, false);
// Make sure 'derby.storage.logArchiveMode' isn't present already.
assertEquals(0, grepForToken(LOG_A_MODE, spf));
// Connect, then enable log archive mode to trigger edit.
DataSource ds = JDBCDataSource.getDataSource();
JDBCDataSource.setBeanProperty(ds, "databaseName", "singleUse/" + db);
Connection con = ds.getConnection();
// Create the service properties file backup.
createSPFBackup(true);
// Trigger service properties file edit.
Statement stmt = con.createStatement();
stmt.execute("CALL SYSCS_UTIL.SYSCS_DISABLE_LOG_ARCHIVE_MODE(0)");
con.close();
// Shut down the database.
JDBCDataSource.shutdownDatabase(ds);
assertNormalPresence();
assertEquals(1, grepForToken(LOG_A_MODE + "=false", spf));
}
/**
* Asserts that the presence of the service properties file and the backup
* is normal, that is that the former is present and the latter isn't.
*/
private void assertNormalPresence() {
assertPresence(true, false);
}
/**
* Asserts the specified presence of the original and the backup service
* properties files.
*
* @param spfPresence presence of the original file
* @param spfOldPresence presence of the backup file
*/
private void assertPresence(boolean spfPresence, boolean spfOldPresence) {
assertEquals("incorrect '" + spf.getAbsolutePath() + "' presence,",
spfPresence, PrivilegedFileOpsForTests.exists(spf));
assertEquals("incorrect '" + spfOld.getPath() + "' presence,",
spfOldPresence, PrivilegedFileOpsForTests.exists(spfOld));
}
/**
* Asserts that the specified file ends with the end-of-file token.
*/
private void assertEOFToken(File file, String encoding)
throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(
PrivilegedFileOpsForTests.getFileInputStream(file), encoding));
String prev = null;
String cur;
while ((cur = in.readLine()) != null) {
prev = cur;
}
in.close();
assertNotNull("last line is null - empty file?", prev);
assertTrue("prev:" + prev +": does not equal " + END_TOKEN,
prev.startsWith(END_TOKEN));
}
/**
* Removes the end-of-file token from the specified file.
*/
private void removeEOFToken(File original, String encoding)
throws IOException {
// Move file, then rewrite by removing last line (the token).
File renamed = new File(original.getAbsolutePath() + "-renamed");
PrivilegedFileOpsForTests.copy(original, renamed);
PrivilegedFileOpsForTests.delete(original);
BufferedReader in = new BufferedReader(new InputStreamReader(
PrivilegedFileOpsForTests.getFileInputStream(renamed),
encoding));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
PrivilegedFileOpsForTests.getFileOutputStream(original),
encoding));
String prev = null;
String line;
while ((line = in.readLine()) != null) {
if (prev != null) {
out.write(prev);
out.newLine();
}
prev = line;
}
assertEquals(END_TOKEN, prev);
in.close();
out.close();
PrivilegedFileOpsForTests.delete(renamed);
}
/**
* Looks for the specified token in the given file.
*
* @param token the search token
* @param file the file to search
* @return The number of matching lines.
*
* @throws IOException if accessing the specified file fails
*/
private int grepForToken(String token, File file)
throws IOException {
int matchingLines = 0;
BufferedReader in = new BufferedReader(new InputStreamReader(
PrivilegedFileOpsForTests.getFileInputStream(file),
SPF_ENCODING));
String line;
while ((line = in.readLine()) != null) {
if (line.indexOf(token) != -1) {
matchingLines++;
}
}
in.close();
return matchingLines;
}
/**
* Copies the master/pristine database to a new database.
*
* @param name name of the database to copy to
*/
private void copyDbAs(String name)
throws IOException {
File newDb = new File(databasesDir, name);
dbToDelete = newDb;
PrivilegedFileOpsForTests.copy(pristineDb, newDb);
spf = new File(newDb, "service.properties");
spfOld = new File(newDb, "service.propertiesold");
}
/** Dependent on state set by {@linkplain #copyDbAs}. */
private void createSPFBackup(boolean keepOriginal)
throws IOException {
PrivilegedFileOpsForTests.copy(spf, spfOld);
if (!keepOriginal) {
PrivilegedFileOpsForTests.delete(spf);
}
}
/**
* Connects to the specified database, then shuts it down.
* <p>
* This method is used to trigger the recovery logic for the service
* properties file.
*
* @param db database to connect to (expected to live in 'system/singleUse')
*/
private void connectThenShutdown(String db)
throws SQLException {
DataSource ds = JDBCDataSource.getDataSource();
JDBCDataSource.setBeanProperty(ds, "databaseName", "singleUse/" + db);
ds.getConnection().close();
JDBCDataSource.shutdownDatabase(ds);
}
public static Test suite() {
return TestConfiguration.additionalDatabaseDecoratorNoShutdown(
TestConfiguration.embeddedSuite(ServicePropertiesFileTest.class),
DB_NAME);
}
}
|