KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > derby > api > DerbyDatabasesTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.derby.api;
21
22 import java.io.File JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.modules.derby.DerbyOptions;
28 import org.netbeans.modules.derby.test.TestBase;
29 import org.openide.modules.InstalledFileLocator;
30
31 /**
32  *
33  * @author Andrei Badea
34  */

35 public class DerbyDatabasesTest extends TestBase {
36
37     private File JavaDoc systemHome;
38
39     public DerbyDatabasesTest(String JavaDoc testName) {
40         super(testName);
41     }
42
43     public void setUp() throws Exception JavaDoc {
44         clearWorkDir();
45
46         systemHome = new File JavaDoc(getWorkDir(), ".netbeans-derby");
47         systemHome.mkdirs();
48
49         DerbyOptions.getDefault().setSystemHome(systemHome.getAbsolutePath());
50     }
51
52     public void testGetFirstFreeDatabaseName() throws Exception JavaDoc {
53         assertEquals("testdb", DerbyDatabases.getFirstFreeDatabaseName("testdb"));
54
55         new File JavaDoc(systemHome, "testdb").createNewFile();
56
57         assertEquals("testdb1", DerbyDatabases.getFirstFreeDatabaseName("testdb"));
58
59         new File JavaDoc(systemHome, "testdb1").createNewFile();
60
61         assertEquals("testdb2", DerbyDatabases.getFirstFreeDatabaseName("testdb"));
62     }
63
64     public void testDatabaseExists() throws Exception JavaDoc {
65         assertFalse(DerbyDatabases.databaseExists(""));
66         assertFalse(DerbyDatabases.databaseExists("testdb"));
67
68         new File JavaDoc(systemHome, "testdb").createNewFile();
69
70         assertTrue(DerbyDatabases.databaseExists("testdb"));
71     }
72
73     public void testGetFirstIllegalCharacter() throws Exception JavaDoc {
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 JavaDoc {
79         setLookup(new Object JavaDoc[] { new SampleDatabaseLocator() });
80
81         DerbyDatabases.extractSampleDatabase("newdb");
82         File JavaDoc newDBDir = new File JavaDoc(systemHome, "newdb");
83         Set JavaDoc sampleDBFiles = new HashSet JavaDoc(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 JavaDoc {
92         setLookup(new Object JavaDoc[] { new SampleDatabaseLocator() });
93
94         File JavaDoc sampleDir = new File JavaDoc(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 JavaDoc directory;
107
108         public SampleDatabaseLocator() {
109             File JavaDoc derbyModule = new File JavaDoc(URI.create(DerbyOptions.class.getProtectionDomain().getCodeSource().getLocation().toExternalForm()));
110             directory = derbyModule.getParentFile().getParentFile();
111         }
112
113         public File JavaDoc locate(String JavaDoc relativePath, String JavaDoc codeNameBase, boolean localized) {
114             if ("modules/ext/derbysampledb.zip".equals(relativePath)) {
115                 return new File JavaDoc(directory, relativePath);
116             }
117             return null;
118         }
119     }
120 }
121
Popular Tags