1 17 package org.apache.geronimo.system.serverinfo; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Properties ; 22 23 import junit.framework.TestCase; 24 25 28 public class ServerInfoTest extends TestCase { 29 private static final File basedir = new File (System.getProperty("basedir", System.getProperty("user.dir"))); 30 31 protected void setUp() throws Exception { 32 System.getProperties().remove(BasicServerInfo.HOME_DIR_SYS_PROP); 34 } 35 36 public final void testResolvePath() { 37 ServerInfo si = null; 38 39 String pathArg; 40 { 41 si = new BasicServerInfo(); 42 pathArg = "/"; 43 assertEquals(new File (pathArg).getAbsolutePath(), si.resolvePath(pathArg)); 44 pathArg = "/x"; 45 assertEquals(new File (pathArg).getAbsolutePath(), si.resolvePath(pathArg)); 46 pathArg = "/x/y"; 47 assertEquals(new File (pathArg).getAbsolutePath(), si.resolvePath(pathArg)); 48 pathArg = "C:/Documents and Settings/Administrator/Application Data/geronimo"; 49 assertEquals(new File (pathArg).getAbsolutePath(), si.resolvePath(pathArg)); 50 51 pathArg = "."; 52 assertEquals(new File (pathArg).getAbsolutePath(), si.resolvePath(pathArg)); 53 pathArg = "x"; 54 assertEquals(new File (pathArg).getAbsolutePath(), si.resolvePath(pathArg)); 55 pathArg = "x/y"; 56 assertEquals(new File (pathArg).getAbsolutePath(), si.resolvePath(pathArg)); 57 pathArg = "Documents and Settings/Administrator/Application Data/geronimo"; 58 assertEquals(new File (pathArg).getAbsolutePath(), si.resolvePath(pathArg)); 59 } 60 61 try { 62 String basedir = "/"; 63 si = new BasicServerInfo(basedir); 64 pathArg = "Documents and Settings/Administrator/Application Data/geronimo"; 65 assertEquals(new File (basedir, pathArg).getAbsolutePath(), si.resolvePath(pathArg)); 66 } catch (Exception e) { 67 fail("ServerInfo ctor threw exception " + e); 68 } 69 70 } 79 80 public final void testServerInfo() throws Exception { 81 try { 82 File file; 83 try { 84 file = File.createTempFile("geronimo", null); 85 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 ioe) { 90 fail(ioe.getMessage()); 91 } catch (Exception expected) { 92 } 93 94 String basedir = "."; 95 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 { 106 String serverName = "target/serverName"; 107 File serverDir = new File (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 { 120 String serverDirName = "./target/serverDir"; 121 File serverDir = new File (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 { 134 String serverDirName = "./target/serverDir"; 135 File serverDir = new File (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 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 |