001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.luni.tests.java.io;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023:
024: import tests.support.resource.Support_Resources;
025:
026: import junit.framework.TestCase;
027:
028: public class WinFileTest extends TestCase {
029:
030: /**
031: * @tests java.io.File#mkdir()
032: * @throws IOException
033: */
034: public void test_mkdir() throws IOException {
035: // Test for method boolean java.io.File.mkdir() in Windows Platform
036:
037: String base = System.getProperty("user.dir");
038: // Old test left behind "garbage files" so this time it creates a
039: // directory
040: // that is guaranteed not to already exist (and deletes it afterward.)
041: int dirNumber = 1;
042: boolean dirExists = true;
043: File dir = new File(base, String.valueOf(dirNumber));
044: while (dirExists) {
045: if (dir.exists()) {
046: dirNumber++;
047: dir = new File(base, String.valueOf(dirNumber));
048: } else {
049: dirExists = false;
050: }
051: }
052: assertTrue("mkdir failed", dir.mkdir() && dir.exists());
053: dir.deleteOnExit();
054: String newbase = new String(dir + File.separator);
055:
056: dir = new File(newbase, ".abcd");
057: assertTrue("mkdir " + dir.getCanonicalPath() + " failed", dir
058: .mkdir()
059: && dir.exists()
060: && !(new File(newbase, "abcd")).exists());
061: dir.deleteOnExit();
062:
063: String[] ss1 = {
064: ".abcd" + File.separator + "." + File.separator
065: + "dir1",
066: ".abcd" + File.separator + ".." + File.separator
067: + "dir2",
068: ".abcd" + File.separator + "." + File.separator + "."
069: + File.separator + "dir3",
070: "12" + File.separator + "34" + File.separator + ".."
071: + File.separator + ".." + File.separator
072: + "dir4",
073: "12" + File.separator + ".." + File.separator + "34"
074: + File.separator + ".." + File.separator
075: + "dir5",
076: ".abcd." + File.separator + ".." + File.separator
077: + "dir6.",
078: ".abcd.." + File.separator + "dir7",
079: ".abcd.." + File.separator + ".." + File.separator
080: + "dir8" };
081: String[] ss2 = { ".abcd" + File.separator + "dir1", "dir2",
082: ".abcd" + File.separator + "dir3", "dir4", "dir5",
083: "dir6", ".abcd" + File.separator + "dir7", "dir8" };
084: for (int i = 0; i < ss1.length; i++) {
085: dir = new File(newbase, ss1[i]);
086: assertTrue("mkdir " + dir.getCanonicalPath() + " failed",
087: dir.mkdir() && dir.exists());
088: dir = new File(newbase, ss2[i]);
089: assertTrue("mkdir " + dir.getCanonicalPath() + " failed",
090: dir.exists());
091: dir.deleteOnExit();
092: }
093: }
094:
095: /**
096: * Regression test for HARMONY-4794
097: */
098: public void testNonASCIIFileName_4794() throws IOException {
099: final String FILENAME = "\u30d5\u30a1\u30a4\u30eb1.txt";
100: final String CONTENT = "A pretty predicament";
101: final String CNTNT_CHARSET = "ISO-8859-1";
102:
103: File folder = Support_Resources.createTempFolder();
104: File f = new File(folder, FILENAME);
105: FileOutputStream fos = new FileOutputStream(f);
106: FileInputStream fis;
107:
108: f.createNewFile();
109: f.deleteOnExit();
110: fos.write(CONTENT.getBytes(CNTNT_CHARSET));
111: fos.close();
112:
113: f = new File(folder, FILENAME);
114: assertEquals("Invalid file name", FILENAME, f.getName());
115: assertTrue("File does not exist", f.exists());
116: byte tmp[] = new byte[256];
117: String wasRed;
118: int n;
119:
120: fis = new FileInputStream(f);
121: n = fis.read(tmp);
122: fis.close();
123: wasRed = new String(tmp, 0, n, CNTNT_CHARSET);
124: assertEquals("Invalid content was red", CONTENT, wasRed);
125: }
126:
127: }
|