1 19 20 package org.netbeans.modules.derby.api; 21 22 import java.io.File ; 23 import java.net.URI ; 24 import java.util.Arrays ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 import org.netbeans.modules.derby.DerbyOptions; 28 import org.netbeans.modules.derby.test.TestBase; 29 import org.openide.modules.InstalledFileLocator; 30 31 35 public class DerbyDatabasesTest extends TestBase { 36 37 private File systemHome; 38 39 public DerbyDatabasesTest(String testName) { 40 super(testName); 41 } 42 43 public void setUp() throws Exception { 44 clearWorkDir(); 45 46 systemHome = new File (getWorkDir(), ".netbeans-derby"); 47 systemHome.mkdirs(); 48 49 DerbyOptions.getDefault().setSystemHome(systemHome.getAbsolutePath()); 50 } 51 52 public void testGetFirstFreeDatabaseName() throws Exception { 53 assertEquals("testdb", DerbyDatabases.getFirstFreeDatabaseName("testdb")); 54 55 new File (systemHome, "testdb").createNewFile(); 56 57 assertEquals("testdb1", DerbyDatabases.getFirstFreeDatabaseName("testdb")); 58 59 new File (systemHome, "testdb1").createNewFile(); 60 61 assertEquals("testdb2", DerbyDatabases.getFirstFreeDatabaseName("testdb")); 62 } 63 64 public void testDatabaseExists() throws Exception { 65 assertFalse(DerbyDatabases.databaseExists("")); 66 assertFalse(DerbyDatabases.databaseExists("testdb")); 67 68 new File (systemHome, "testdb").createNewFile(); 69 70 assertTrue(DerbyDatabases.databaseExists("testdb")); 71 } 72 73 public void testGetFirstIllegalCharacter() throws Exception { 74 assertEquals((int)File.separatorChar, DerbyDatabases.getFirstIllegalCharacter("a" + File.separatorChar + "b")); 75 assertEquals((int)'/', DerbyDatabases.getFirstIllegalCharacter("a/b")); 76 } 77 78 public void testExtractSampleDatabase() throws Exception { 79 setLookup(new Object [] { new SampleDatabaseLocator() }); 80 81 DerbyDatabases.extractSampleDatabase("newdb"); 82 File newDBDir = new File (systemHome, "newdb"); 83 Set sampleDBFiles = new HashSet (Arrays.asList(newDBDir.list())); 84 85 assertEquals(3, sampleDBFiles.size()); 86 assertTrue(sampleDBFiles.contains("log")); 87 assertTrue(sampleDBFiles.contains("seg0")); 88 assertTrue(sampleDBFiles.contains("service.properties")); 89 } 90 91 public void testDatabaseNotExtractedToExistingDirectoryIssue80122() throws Exception { 92 setLookup(new Object [] { new SampleDatabaseLocator() }); 93 94 File sampleDir = new File (systemHome, "sample"); 95 sampleDir.mkdirs(); 96 97 assertEquals("There should be no files in the sample directory", 0, sampleDir.listFiles().length); 98 99 DerbyDatabases.extractSampleDatabase("sample"); 100 101 assertEquals("Should not have extracted the sample database to an existing directory", 0, sampleDir.listFiles().length); 102 } 103 104 private static final class SampleDatabaseLocator extends InstalledFileLocator { 105 106 public File directory; 107 108 public SampleDatabaseLocator() { 109 File derbyModule = new File (URI.create(DerbyOptions.class.getProtectionDomain().getCodeSource().getLocation().toExternalForm())); 110 directory = derbyModule.getParentFile().getParentFile(); 111 } 112 113 public File locate(String relativePath, String codeNameBase, boolean localized) { 114 if ("modules/ext/derbysampledb.zip".equals(relativePath)) { 115 return new File (directory, relativePath); 116 } 117 return null; 118 } 119 } 120 } 121 | Popular Tags |