KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > derby > DerbyOptionsTest


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;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.netbeans.modules.derby.test.TestBase;
25 import org.openide.modules.InstalledFileLocator;
26
27 /**
28  *
29  * @author abadea
30  */

31 public class DerbyOptionsTest extends TestBase {
32
33     File JavaDoc userdir;
34     File JavaDoc externalDerby;
35     
36     public DerbyOptionsTest(String JavaDoc testName) {
37         super(testName);
38     }
39
40     public void setUp() throws Exception JavaDoc {
41         clearWorkDir();
42
43         userdir = new File JavaDoc(getWorkDir(), ".netbeans");
44         userdir.mkdirs();
45         
46         // create a fake installation of an external derby database
47
externalDerby = new File JavaDoc(userdir, "derby");
48         createFakeDerbyInstallation(externalDerby);
49     }
50
51     public void testDerbyLocationIsNullWhenBundledDerbyNotInstalled() {
52         // assert the bundled derby is not installed
53
assertNull(DerbyOptions.getDefaultInstallLocation());
54         
55         DerbyOptions.getDefault().setLocation(externalDerby.getAbsolutePath());
56         assertFalse(DerbyOptions.getDefault().isLocationNull());
57         
58         DerbyOptions.getDefault().setLocation("");
59         assertTrue(DerbyOptions.getDefault().isLocationNull());
60     }
61     
62     public void testDerbyLocationIsNotNullWhenBundledDerbyInstalled() throws Exception JavaDoc {
63         // create a fake bundled derby database installation
64
File JavaDoc bundledDerby = new File JavaDoc(userdir, DerbyOptions.INST_DIR);
65         createFakeDerbyInstallation(bundledDerby);
66         
67         // create a IFL which will find the bundled derby
68
setLookup(new Object JavaDoc[] { new InstalledFileLocatorImpl(userdir) });
69         
70         // assert the bundled derby is installed
71
String JavaDoc derbyLocation = DerbyOptions.getDefaultInstallLocation();
72         assertNotNull(derbyLocation);
73         
74         DerbyOptions.getDefault().setLocation(externalDerby.getAbsolutePath());
75         assertFalse(DerbyOptions.getDefault().isLocationNull());
76         
77         DerbyOptions.getDefault().setLocation(""); // this should set the location to the one of the bundled derby
78
assertFalse(DerbyOptions.getDefault().isLocationNull());
79         assertEquals(DerbyOptions.getDefault().getLocation(), derbyLocation);
80     }
81     
82     public void testLocationWhenNDSHPropertySetIssue76908() throws IOException JavaDoc {
83         assertEquals("", DerbyOptions.getDefault().getSystemHome());
84         
85         File JavaDoc ndshSystemHome = new File JavaDoc(getWorkDir(), ".netbeans-derby-ndsh");
86         if (!ndshSystemHome.mkdirs()) {
87             throw new IOException JavaDoc("Could not create " + ndshSystemHome);
88         }
89         File JavaDoc systemHome = new File JavaDoc(getWorkDir(), ".netbeans-derby");
90         if (!systemHome.mkdirs()) {
91             throw new IOException JavaDoc("Could not create " + systemHome);
92         }
93         
94         // returning the value of the netbeans.derby.system.home property when systemHome is not set...
95
System.setProperty(DerbyOptions.NETBEANS_DERBY_SYSTEM_HOME, ndshSystemHome.getAbsolutePath());
96         assertEquals(ndshSystemHome.getAbsolutePath(), DerbyOptions.getDefault().getSystemHome());
97         
98         // ... but returning systemHome when it is set
99
DerbyOptions.getDefault().setSystemHome(systemHome.getAbsolutePath());
100         assertEquals(systemHome.getAbsolutePath(), DerbyOptions.getDefault().getSystemHome());
101     }
102     
103     private static final class InstalledFileLocatorImpl extends InstalledFileLocator {
104         
105         private File JavaDoc userdir;
106         
107         public InstalledFileLocatorImpl(File JavaDoc userdir) {
108             this.userdir = userdir;
109         }
110         
111         public File JavaDoc locate(String JavaDoc relativePath, String JavaDoc codeNameBase, boolean localized) {
112             File JavaDoc f = new File JavaDoc(userdir, relativePath);
113             return f.exists() ? f : null;
114         }
115     }
116 }
117
Popular Tags