KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > derby > DerbySystemGBeanTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.derby;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.sql.Connection JavaDoc;
23 import java.sql.DriverManager JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import junit.framework.TestCase;
28 import org.apache.geronimo.system.serverinfo.ServerInfo;
29 import org.apache.geronimo.system.serverinfo.BasicServerInfo;
30
31 /**
32  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
33  */

34 public class DerbySystemGBeanTest extends TestCase {
35     private File JavaDoc systemDir;
36
37     public void testCreateSystemUsingServerInfo() throws Exception JavaDoc {
38         ServerInfo serverInfo = new BasicServerInfo(systemDir.toString());
39         String JavaDoc derbyDir = "var/dbderby";
40         DerbySystemGBean gbean = new DerbySystemGBean(serverInfo, derbyDir);
41         try {
42             gbean.doStart();
43             new org.apache.derby.jdbc.EmbeddedDriver();
44             connect();
45             gbean.doStop();
46             assertTrue(new File JavaDoc(systemDir, derbyDir+"/derby.log").exists());
47         } catch (Exception JavaDoc e) {
48             fail(e.getMessage());
49         }
50     }
51
52     private void connect() throws SQLException JavaDoc {
53         Connection JavaDoc c = DriverManager.getConnection("jdbc:derby:testdb;create=true");
54         c.close();
55     }
56
57     protected void setUp() throws Exception JavaDoc {
58         try {
59             super.setUp();
60             systemDir = File.createTempFile("derbyTest", ".tmp");
61             systemDir.delete();
62             systemDir.mkdir();
63
64             Properties JavaDoc props = System.getProperties();
65             props.remove("derby.system.home");
66         } catch (Exception JavaDoc e) {
67             delete(systemDir);
68             throw e;
69         }
70     }
71
72     protected void tearDown() throws Exception JavaDoc {
73         delete(systemDir);
74         super.tearDown();
75     }
76
77     private void delete(File JavaDoc file) throws IOException JavaDoc {
78         if (file == null) {
79             return;
80         }
81
82         File JavaDoc[] files = file.listFiles();
83         if (files != null) {
84             for (int i = 0; i < files.length; i++) {
85                 delete(files[i]);
86             }
87         }
88         file.delete();
89     }
90 }
91
Popular Tags