001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.system.serverinfo;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.util.Properties;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * @version $Rev: 520273 $ $Date: 2007-03-19 21:51:15 -0700 (Mon, 19 Mar 2007) $
026: */
027: public class ServerInfoTest extends TestCase {
028: private static final File basedir = new File(System.getProperty(
029: "basedir", System.getProperty("user.dir")));
030:
031: protected void setUp() throws Exception {
032: // Ensure we are in a known state before each test
033: System.getProperties()
034: .remove(BasicServerInfo.HOME_DIR_SYS_PROP);
035: }
036:
037: public final void testResolvePath() {
038: ServerInfo si = null;
039:
040: String pathArg;
041: {
042: si = new BasicServerInfo();
043: pathArg = "/";
044: assertEquals(new File(pathArg).getAbsolutePath(), si
045: .resolvePath(pathArg));
046: pathArg = "/x";
047: assertEquals(new File(pathArg).getAbsolutePath(), si
048: .resolvePath(pathArg));
049: pathArg = "/x/y";
050: assertEquals(new File(pathArg).getAbsolutePath(), si
051: .resolvePath(pathArg));
052: pathArg = "C:/Documents and Settings/Administrator/Application Data/geronimo";
053: assertEquals(new File(pathArg).getAbsolutePath(), si
054: .resolvePath(pathArg));
055:
056: pathArg = ".";
057: assertEquals(new File(pathArg).getAbsolutePath(), si
058: .resolvePath(pathArg));
059: pathArg = "x";
060: assertEquals(new File(pathArg).getAbsolutePath(), si
061: .resolvePath(pathArg));
062: pathArg = "x/y";
063: assertEquals(new File(pathArg).getAbsolutePath(), si
064: .resolvePath(pathArg));
065: pathArg = "Documents and Settings/Administrator/Application Data/geronimo";
066: assertEquals(new File(pathArg).getAbsolutePath(), si
067: .resolvePath(pathArg));
068: }
069:
070: try {
071: String basedir = "/";
072: si = new BasicServerInfo(basedir);
073: pathArg = "Documents and Settings/Administrator/Application Data/geronimo";
074: assertEquals(new File(basedir, pathArg).getAbsolutePath(),
075: si.resolvePath(pathArg));
076: } catch (Exception e) {
077: fail("ServerInfo ctor threw exception " + e);
078: }
079:
080: //try {
081: // String basedir = File.listRoots()[0].getAbsolutePath();
082: // si = new ServerInfo(basedir);
083: // pathArg = "Documents and Settings/Administrator/Application Data/geronimo";
084: // assertEquals(new File(basedir, pathArg).getAbsolutePath(), si.resolvePath(pathArg));
085: //} catch (Exception e) {
086: // fail("ServerInfo ctor threw exception " + e);
087: //}
088: }
089:
090: public final void testServerInfo() throws Exception {
091: try {
092: File file;
093: try {
094: file = File.createTempFile("geronimo", null);
095: // a workaround - ServerInfo sets system-wide property
096: System.setProperty(BasicServerInfo.HOME_DIR_SYS_PROP,
097: file.getName());
098: new BasicServerInfo(file.getName());
099: fail("ServerInfo should throw exception when given non-directory path");
100: } catch (IOException ioe) {
101: fail(ioe.getMessage());
102: } catch (Exception expected) {
103: }
104:
105: String basedir = ".";
106: // a workaround - ServerInfo sets system-wide property
107: System.setProperty(BasicServerInfo.HOME_DIR_SYS_PROP,
108: basedir);
109: ServerInfo si = new BasicServerInfo(basedir);
110: assertNotNull(System
111: .getProperty(BasicServerInfo.HOME_DIR_SYS_PROP));
112: assertEquals("base directory is incorrect", basedir, si
113: .getBaseDirectory());
114: } finally {
115: resetSysProperties();
116: }
117: }
118:
119: public void testWithServerName() throws Exception {
120: String serverName = "target/serverName";
121: File serverDir = new File(basedir, serverName);
122: serverDir.mkdirs();
123: try {
124: System.setProperty(BasicServerInfo.SERVER_NAME_SYS_PROP,
125: serverName);
126: new BasicServerInfo(basedir.getAbsolutePath());
127: assertEquals(serverDir.getAbsolutePath(), System
128: .getProperty(BasicServerInfo.SERVER_DIR_SYS_PROP));
129: } finally {
130: resetSysProperties();
131: serverDir.delete();
132: }
133: }
134:
135: public void testWithServerDirAbsolute() throws Exception {
136: String serverDirName = "./target/serverDir";
137: File serverDir = new File(basedir, serverDirName);
138: serverDir.mkdirs();
139: try {
140: System.setProperty(BasicServerInfo.SERVER_DIR_SYS_PROP,
141: serverDir.getAbsolutePath());
142: new BasicServerInfo(basedir.getAbsolutePath());
143: assertEquals(serverDir.getAbsolutePath(), System
144: .getProperty(BasicServerInfo.SERVER_DIR_SYS_PROP));
145: } finally {
146: resetSysProperties();
147: serverDir.delete();
148: }
149: }
150:
151: public void testWithServerDirRelative() throws Exception {
152: String serverDirName = "./target/serverDir";
153: File serverDir = new File(basedir, serverDirName);
154: serverDir.mkdirs();
155: try {
156: System.setProperty(BasicServerInfo.SERVER_DIR_SYS_PROP,
157: serverDirName);
158: new BasicServerInfo(basedir.getAbsolutePath());
159: assertEquals(serverDir.getAbsolutePath(), System
160: .getProperty(BasicServerInfo.SERVER_DIR_SYS_PROP));
161: } finally {
162: resetSysProperties();
163: }
164: }
165:
166: private void resetSysProperties() {
167: Properties sysProps = System.getProperties();
168: sysProps.remove(BasicServerInfo.HOME_DIR_SYS_PROP);
169: sysProps.remove(BasicServerInfo.SERVER_DIR_SYS_PROP);
170: sysProps.remove(BasicServerInfo.SERVER_NAME_SYS_PROP);
171: }
172: }
|