KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > serverinfo > ServerInfoTest


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 package org.apache.geronimo.system.serverinfo;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import junit.framework.TestCase;
24
25 /**
26  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
27  */

28 public class ServerInfoTest extends TestCase {
29     private static final File JavaDoc basedir = new File JavaDoc(System.getProperty("basedir", System.getProperty("user.dir")));
30
31     protected void setUp() throws Exception JavaDoc {
32         // Ensure we are in a known state before each test
33
System.getProperties().remove(BasicServerInfo.HOME_DIR_SYS_PROP);
34     }
35     
36     public final void testResolvePath() {
37         ServerInfo si = null;
38
39         String JavaDoc pathArg;
40         {
41             si = new BasicServerInfo();
42             pathArg = "/";
43             assertEquals(new File JavaDoc(pathArg).getAbsolutePath(), si.resolvePath(pathArg));
44             pathArg = "/x";
45             assertEquals(new File JavaDoc(pathArg).getAbsolutePath(), si.resolvePath(pathArg));
46             pathArg = "/x/y";
47             assertEquals(new File JavaDoc(pathArg).getAbsolutePath(), si.resolvePath(pathArg));
48             pathArg = "C:/Documents and Settings/Administrator/Application Data/geronimo";
49             assertEquals(new File JavaDoc(pathArg).getAbsolutePath(), si.resolvePath(pathArg));
50
51             pathArg = ".";
52             assertEquals(new File JavaDoc(pathArg).getAbsolutePath(), si.resolvePath(pathArg));
53             pathArg = "x";
54             assertEquals(new File JavaDoc(pathArg).getAbsolutePath(), si.resolvePath(pathArg));
55             pathArg = "x/y";
56             assertEquals(new File JavaDoc(pathArg).getAbsolutePath(), si.resolvePath(pathArg));
57             pathArg = "Documents and Settings/Administrator/Application Data/geronimo";
58             assertEquals(new File JavaDoc(pathArg).getAbsolutePath(), si.resolvePath(pathArg));
59         }
60
61         try {
62             String JavaDoc basedir = "/";
63             si = new BasicServerInfo(basedir);
64             pathArg = "Documents and Settings/Administrator/Application Data/geronimo";
65             assertEquals(new File JavaDoc(basedir, pathArg).getAbsolutePath(), si.resolvePath(pathArg));
66         } catch (Exception JavaDoc e) {
67             fail("ServerInfo ctor threw exception " + e);
68         }
69
70         //try {
71
// String basedir = File.listRoots()[0].getAbsolutePath();
72
// si = new ServerInfo(basedir);
73
// pathArg = "Documents and Settings/Administrator/Application Data/geronimo";
74
// assertEquals(new File(basedir, pathArg).getAbsolutePath(), si.resolvePath(pathArg));
75
//} catch (Exception e) {
76
// fail("ServerInfo ctor threw exception " + e);
77
//}
78
}
79
80     public final void testServerInfo() throws Exception JavaDoc {
81         try {
82             File JavaDoc file;
83             try {
84                 file = File.createTempFile("geronimo", null);
85                 // a workaround - ServerInfo sets system-wide property
86
System.setProperty(BasicServerInfo.HOME_DIR_SYS_PROP, file.getName());
87                 new BasicServerInfo(file.getName());
88                 fail("ServerInfo should throw exception when given non-directory path");
89             } catch (IOException JavaDoc ioe) {
90                 fail(ioe.getMessage());
91             } catch (Exception JavaDoc expected) {
92             }
93             
94             String JavaDoc basedir = ".";
95             // a workaround - ServerInfo sets system-wide property
96
System.setProperty(BasicServerInfo.HOME_DIR_SYS_PROP, basedir);
97             ServerInfo si = new BasicServerInfo(basedir);
98             assertNotNull(System.getProperty(BasicServerInfo.HOME_DIR_SYS_PROP));
99             assertEquals("base directory is incorrect", basedir, si.getBaseDirectory());
100         } finally {
101             resetSysProperties();
102         }
103     }
104
105     public void testWithServerName() throws Exception JavaDoc {
106         String JavaDoc serverName = "target/serverName";
107         File JavaDoc serverDir = new File JavaDoc(basedir, serverName);
108         serverDir.mkdirs();
109         try {
110             System.setProperty(BasicServerInfo.SERVER_NAME_SYS_PROP, serverName);
111             new BasicServerInfo(basedir.getAbsolutePath());
112             assertEquals(serverDir.getAbsolutePath(), System.getProperty(BasicServerInfo.SERVER_DIR_SYS_PROP));
113         } finally {
114             resetSysProperties();
115             serverDir.delete();
116         }
117     }
118
119     public void testWithServerDirAbsolute() throws Exception JavaDoc {
120         String JavaDoc serverDirName = "./target/serverDir";
121         File JavaDoc serverDir = new File JavaDoc(basedir, serverDirName);
122         serverDir.mkdirs();
123         try {
124             System.setProperty(BasicServerInfo.SERVER_DIR_SYS_PROP, serverDir.getAbsolutePath());
125             new BasicServerInfo(basedir.getAbsolutePath());
126             assertEquals(serverDir.getAbsolutePath(), System.getProperty(BasicServerInfo.SERVER_DIR_SYS_PROP));
127         } finally {
128             resetSysProperties();
129             serverDir.delete();
130         }
131     }
132
133     public void testWithServerDirRelative() throws Exception JavaDoc {
134         String JavaDoc serverDirName = "./target/serverDir";
135         File JavaDoc serverDir = new File JavaDoc(basedir, serverDirName);
136         serverDir.mkdirs();
137         try {
138             System.setProperty(BasicServerInfo.SERVER_DIR_SYS_PROP, serverDirName);
139             new BasicServerInfo(basedir.getAbsolutePath());
140             assertEquals(serverDir.getAbsolutePath(), System.getProperty(BasicServerInfo.SERVER_DIR_SYS_PROP));
141         } finally {
142             resetSysProperties();
143         }
144     }
145     
146     private void resetSysProperties() {
147         Properties JavaDoc sysProps = System.getProperties();
148         sysProps.remove(BasicServerInfo.HOME_DIR_SYS_PROP);
149         sysProps.remove(BasicServerInfo.SERVER_DIR_SYS_PROP);
150         sysProps.remove(BasicServerInfo.SERVER_NAME_SYS_PROP);
151     }
152 }
153
Popular Tags