harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r360077 - in /incubator/harmony/enhanced/classlib/trunk/java-src/luni/src: main/java/java/io/ test/java/org/ test/java/org/apache/ test/java/org/apache/harmony/ test/java/org/apache/harmony/test/ test/java/org/apache/harmony/test/java/ test...
Date Fri, 30 Dec 2005 12:51:51 GMT
Author: tellison
Date: Fri Dec 30 04:51:44 2005
New Revision: 360077

URL: http://svn.apache.org/viewcvs?rev=360077&view=rev
Log:
Fix and regression test for HARMONY-21 (File constructor produces wrong path to absolute path
specified relative to root)

Added:
    incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/
    incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/apache/
    incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/apache/harmony/
    incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/apache/harmony/test/
    incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/apache/harmony/test/java/
    incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/apache/harmony/test/java/io/
    incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/apache/harmony/test/java/io/FileTest.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/main/java/java/io/File.java

Modified: incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/main/java/java/io/File.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/main/java/java/io/File.java?rev=360077&r1=360076&r2=360077&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/main/java/java/io/File.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/main/java/java/io/File.java
Fri Dec 30 04:51:44 2005
@@ -99,15 +99,8 @@
 			if (dir == null) {
 				this.path = name;
 			} else {
-				String dirPath = dir.getPath();
-				if (dirPath.length() > 0
-						&& dirPath.charAt(dirPath.length() - 1) == separatorChar) {
-					this.path = dirPath + name;
-				} else {
-					this.path = dirPath + separator + name;
-				}
+				this.path = calculatePath(dir.getPath(),name);
 			}
-			fixSlashes();
 		} else
 			throw new NullPointerException();
 	}
@@ -120,8 +113,7 @@
 	 */
 	public File(String path) {
 		// NullPointerException thrown by fixSlashes
-		this.path = path;
-		fixSlashes();
+		this.path = fixSlashes(path);
 	}
 
 	/**
@@ -138,14 +130,8 @@
 			if (dirPath == null) {
 				this.path = name;
 			} else {
-				if (dirPath.length() > 0
-						&& dirPath.charAt(dirPath.length() - 1) == separatorChar) {
-					this.path = dirPath + name;
-				} else {
-					this.path = dirPath + separator + name;
-				}
+				this.path = calculatePath(dirPath, name);
 			}
-			fixSlashes();
 		} else
 			throw new NullPointerException();
 	}
@@ -170,8 +156,22 @@
 	public File(URI uri) {
 		// check pre-conditions
 		checkURI(uri);
-		this.path = uri.getPath();
-		fixSlashes();
+		this.path = fixSlashes(uri.getPath());
+	}
+
+	private String calculatePath(String dirPath, String name) {
+		// Remove all the proceeding separator chars from name
+		name = fixSlashes(name);
+		while (name.length() > 0 && (name.charAt(0) == separatorChar))
+			name = name.substring(1, name.length());
+
+		// Ensure there is a separator char between dirPath and name
+		dirPath = fixSlashes(dirPath);
+		if (dirPath.length() > 0 && (dirPath.charAt(dirPath.length() - 1) == separatorChar))
{
+			return dirPath + name;
+		}
+
+		return dirPath + separatorChar + name;
 	}
 
 	private void checkURI(URI uri) {
@@ -237,16 +237,16 @@
 	 * includes changing them all to the current platforms fileSeparator and
 	 * removing duplicates.
 	 */
-	private void fixSlashes() {
+	private String fixSlashes(String origPath) {
 		int uncIndex = 1;
-		int length = path.length(), newLength = 0;
+		int length = origPath.length(), newLength = 0;
 		if (separatorChar == '/') // disable UNC names
 			uncIndex = 0;
-		else if (length > 2 && path.charAt(1) == ':')
+		else if (length > 2 && origPath.charAt(1) == ':')
 			uncIndex = 2;
 
 		boolean foundSlash = false;
-		char newPath[] = path.toCharArray();
+		char newPath[] = origPath.toCharArray();
 		for (int i = 0; i < length; i++) {
 			char pathChar = newPath[i];
 			if (pathChar == '\\' || pathChar == '/') {
@@ -273,8 +273,10 @@
 			newLength--;
 		String tempPath = new String(newPath, 0, newLength);
 		// If it's the same keep it identical for SecurityManager purposes
-		if (!tempPath.equals(path))
-			path = tempPath;
+		if (!tempPath.equals(origPath)) {
+			return tempPath;
+		}
+		return origPath;
 	}
 
 	/**

Added: incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/apache/harmony/test/java/io/FileTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/apache/harmony/test/java/io/FileTest.java?rev=360077&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/apache/harmony/test/java/io/FileTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/java-src/luni/src/test/java/org/apache/harmony/test/java/io/FileTest.java
Fri Dec 30 04:51:44 2005
@@ -0,0 +1,62 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * 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.harmony.test.java.io;
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+public class FileTest extends TestCase {
+
+	/**
+	 * @tests java.io.File#File(java.io.File, java.lang.String)
+	 */
+	public void test_File_ConstructorLjava_io_FileLjava_lang_String() {
+		// Regression test for HARMONY-21
+		File path = new File("/dir/file");
+		File root = new File("/");
+		File file = new File(root, "/dir/file");
+		assertEquals("Assert 1: wrong path result ", path.getPath(), file
+				.getPath());
+
+		// Test data used in a few places below
+		String dirName = System.getProperty("user.dir");
+		String fileName = "input.tst";
+
+		// Check filename is preserved correctly
+		File d = new File(dirName);
+		File f = new File(d, fileName);
+		if (!dirName
+				.regionMatches((dirName.length() - 1), File.separator, 0, 1)) {
+			dirName += File.separator;
+		}
+		dirName += fileName;
+		assertTrue("Assert 2: Created incorrect file " + f.getPath(), f
+				.getPath().equals(dirName));
+
+		// Check null argument is handled
+		try {
+			f = new File(d, null);
+			fail("Assert 3: NullPointerException not thrown.");
+		} catch (NullPointerException e) {
+			// Expected.
+		}
+
+		f = new File((File) null, fileName);
+		assertTrue("Assert 4: Created incorrect file " + f.getPath(), f
+				.getAbsolutePath().equals(dirName));
+	}
+}



Mime
View raw message