Author: jlaskowski Date: Sun Jan 2 14:33:45 2005 New Revision: 123908 URL: http://svn.apache.org/viewcvs?view=rev&rev=123908 Log: http://issues.apache.org/jira/browse/GERONIMO-535
Handle fully-qualified paths in ServerInfo.resolvePath() Thanks Toby Cabot! Added: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/serverinfo/ geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/serverinfo/ServerInfoTest.java (contents, props changed) Modified: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/serverinfo/ServerInfo.java Modified: geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/serverinfo/ServerInfo.java Url: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/serverinfo/ServerInfo.java?view=diff&rev=123908&p1=geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/serverinfo/ServerInfo.java&r1=123907&p2=geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/serverinfo/ServerInfo.java&r2=123908 ============================================================================== --- geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/serverinfo/ServerInfo.java (original) +++ geronimo/trunk/modules/system/src/java/org/apache/geronimo/system/serverinfo/ServerInfo.java Sun Jan 2 14:33:45 2005 @@ -27,6 +27,9 @@ import org.apache.geronimo.gbean.GBeanInfoBuilder; /** + * Contains information about the server and functions for resolving + * pathnames. + * * @version $Rev$ $Date$ */ public class ServerInfo { @@ -74,12 +77,38 @@ System.setProperty("geronimo.base.dir", base.getAbsolutePath()); } + /** + * Resolves an abstract pathname to an absolute one. + * + * @param filename a pathname that can either be + * fully-qualified (i.e. starts with a "/") or + * relative (i.e. starts with any character but "/"). If it's + * fully-qualified it will be resolved to an absolute pathname + * using system-dependent rules (@link java.io.File). If it's relative + * it will be resolved relative to the base directory. + * + * @return an absolute pathname + * + * @see java.io.File#File(String pathname) + * @see java.io.File#getAbsolutePath() + */ public String resolvePath(final String filename) { - File file = new File(base, filename); - return file.getAbsolutePath(); + return resolve(filename).getAbsolutePath(); } + /** + * Resolves an abstract pathname to a File. + * + * @param filename a <code>String</code> containing a pathname, + * which will be resolved by [EMAIL PROTECTED] #resolvePath(String + * filename)}. + * @return a <code>File</code> value + */ public File resolve(final String filename) { + File file = new File(filename); + if (file.isAbsolute()) { + return file; + } return new File(base, filename); } Added: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/serverinfo/ServerInfoTest.java Url: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/serverinfo/ServerInfoTest.java?view=auto&rev=123908 ============================================================================== --- (empty file) +++ geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/serverinfo/ServerInfoTest.java Sun Jan 2 14:33:45 2005 @@ -0,0 +1,93 @@ +/** + * + * Copyright 2003-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.geronimo.system.serverinfo; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +/** + * @version $Rev: 57150 $ $Date: 2004-11-10 09:15:53 +0100 (Wed, 10 Nov 2004) $ + */ +public class ServerInfoTest extends TestCase { + + public final void testResolvePath() { + ServerInfo si = null; + + String pathArg; + { + si = new ServerInfo(); + pathArg = "/"; + assertEquals(new File(pathArg).getAbsolutePath(), si.resolvePath(pathArg)); + pathArg = "/x"; + assertEquals(new File(pathArg).getAbsolutePath(), si.resolvePath(pathArg)); + pathArg = "/x/y"; + assertEquals(new File(pathArg).getAbsolutePath(), si.resolvePath(pathArg)); + pathArg = "C:/Documents and Settings/Administrator/Application Data/geronimo"; + assertEquals(new File(pathArg).getAbsolutePath(), si.resolvePath(pathArg)); + + pathArg = "."; + assertEquals(new File(pathArg).getAbsolutePath(), si.resolvePath(pathArg)); + pathArg = "x"; + assertEquals(new File(pathArg).getAbsolutePath(), si.resolvePath(pathArg)); + pathArg = "x/y"; + assertEquals(new File(pathArg).getAbsolutePath(), si.resolvePath(pathArg)); + pathArg = "Documents and Settings/Administrator/Application Data/geronimo"; + assertEquals(new File(pathArg).getAbsolutePath(), si.resolvePath(pathArg)); + } + + try { + String basedir = "/"; + si = new ServerInfo(basedir); + pathArg = "Documents and Settings/Administrator/Application Data/geronimo"; + assertEquals(new File(basedir, pathArg).getAbsolutePath(), si.resolvePath(pathArg)); + } catch (Exception e) { + fail("ServerInfo ctor threw exception " + e); + } + + try { + String basedir = "c:/"; + si = new ServerInfo(basedir); + pathArg = "Documents and Settings/Administrator/Application Data/geronimo"; + assertEquals(new File(basedir, pathArg).getAbsolutePath(), si.resolvePath(pathArg)); + } catch (Exception e) { + fail("ServerInfo ctor threw exception " + e); + } + } + + public final void testServerInfo() throws Exception { + File file; + try { + file = File.createTempFile("geronimo", null); + // a workaround - ServerInfo sets system-wide property + System.setProperty("geronimo.base.dir", file.getName()); + new ServerInfo(file.getName()); + fail("ServerInfo should throw exception when given non-directory path"); + } catch (IOException ioe) { + fail(ioe.getMessage()); + } catch (Exception expected) { + } + + String basedir = "."; + // a workaround - ServerInfo sets system-wide property + System.setProperty("geronimo.base.dir", basedir); + ServerInfo si = new ServerInfo(basedir); + assertNotNull(System.getProperty("geronimo.base.dir")); + assertEquals("base directory is incorrect", basedir, si.getBaseDirectory()); + } +}