UnixFileTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.io;
19
20import java.io.BufferedReader;
21import java.io.File;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25
26import junit.framework.TestCase;
27
28/**
29 * Please note that this case can only be passed on Linux due to some file
30 * system dependent reason.
31 *
32 */
33public class UnixFileTest extends TestCase {
34	private boolean root = false;
35
36	private File testFile;
37
38	private File testDir;
39
40	private static final int TOTAL_SPACE_NUM = 0;
41
42	private static final int FREE_SPACE_NUM = 1;
43
44	private static final int USABLE_SPACE_NUM = 2;
45
46	private static class ConsoleResulter extends Thread {
47		Process proc;
48
49		InputStream is;
50
51		String resStr;
52
53		ConsoleResulter(Process p, InputStream in) {
54			proc = p;
55			is = in;
56		}
57
58		@Override
59		public void run() {
60			StringBuffer result = new StringBuffer();
61			synchronized (result) {
62				try {
63					BufferedReader br = new BufferedReader(
64							new InputStreamReader(is));
65					String line;
66					while ((line = br.readLine()) != null) {
67						result.append(line);
68					}
69					if (result.length() != 0) {
70						resStr = result.toString();
71					}
72
73					br.close();
74				} catch (IOException ioe) {
75					result = null;
76				}
77				synchronized (proc) {
78					proc.notifyAll();
79				}
80			}
81		}
82	}
83
84	private static long getLinuxSpace(int index, File file) throws Exception {
85		long[] result = new long[3];
86		String par = file.getAbsolutePath();
87		String osName = System.getProperty("os.name");
88		// in case the test case will run under other OS.
89		if (osName.toLowerCase().indexOf("linux") != -1) {
90			String[] cmd = new String[2];
91			cmd[0] = "df";
92			cmd[1] = par; // get the total space of file
93			Runtime rt = Runtime.getRuntime();
94
95			Process proc = rt.exec(cmd);
96			// get output from the command
97			ConsoleResulter outputResult = new ConsoleResulter(proc, proc
98					.getInputStream());
99
100			synchronized (proc) {
101				outputResult.start();
102				proc.wait();
103			}
104			// If there is no error, obtain the result
105			if (outputResult.resStr != null) {
106				// exit the subprocess safely
107				proc.waitFor();
108
109				// filter unnecessary information
110				String[] txtResult = outputResult.resStr
111						.split("\\D|\\p{javaLowerCase}|\\p{javaUpperCase}");
112				for (int i = 0, j = 0; i < txtResult.length; i++) {
113					if (txtResult[i].length() > 3) {
114						result[j++] = Long.parseLong(txtResult[i]) * 1024L;
115					}
116				}
117			}
118		}
119
120		// calculate free spaces according to df command
121		result[1] = result[0] - result[1];
122		return result[index];
123	}
124
125	/**
126	 * @tests java.io.File#canExecute()
127	 *
128	 * @since 1.6
129	 */
130	public void test_canExecute() {
131		assertFalse(testFile.canExecute());
132		assertTrue(testFile.setExecutable(true, false));
133		assertTrue(testFile.canExecute());
134		assertTrue(testFile.setExecutable(true, true));
135		assertTrue(testFile.canExecute());
136
137		assertTrue(testFile.setExecutable(false, false));
138		assertFalse(testFile.canExecute());
139		assertTrue(testFile.setExecutable(false, true));
140		assertFalse(testFile.canExecute());
141
142		assertTrue(testFile.setExecutable(true, false));
143		assertTrue(testFile.canExecute());
144
145		// tests directory
146		assertTrue(testDir.canExecute());
147		assertTrue(testDir.setExecutable(false, true));
148		if (root) {
149			assertTrue(testDir.canExecute());
150		} else {
151			assertFalse(testDir.canExecute());
152		}
153		assertTrue(testDir.setExecutable(true, false));
154		assertTrue(testDir.canExecute());
155	}
156
157	/**
158	 * @tests java.io.File#getFreeSpace()
159	 *
160	 * @since 1.6
161	 */
162	public void test_getFreeSpace() throws Exception {
163		long fileSpace = getLinuxSpace(FREE_SPACE_NUM, testFile);
164		long dirSpace = getLinuxSpace(FREE_SPACE_NUM, testDir);
165		// in case we cannot fetch the value from command line
166		if (fileSpace > 0) {
167			assertEquals(fileSpace, testFile.getFreeSpace());
168		}
169
170		if (dirSpace > 0) {
171			assertEquals(dirSpace, testDir.getFreeSpace());
172		}
173	}
174
175	/**
176	 * @tests java.io.File#getTotalSpace()
177	 *
178	 * @since 1.6
179	 */
180	public void test_getTotalSpace() throws Exception {
181		long fileSpace = getLinuxSpace(TOTAL_SPACE_NUM, testFile);
182		long dirSpace = getLinuxSpace(TOTAL_SPACE_NUM, testDir);
183		if (fileSpace > 0) {
184			assertEquals(fileSpace, testFile.getTotalSpace());
185		}
186		if (dirSpace > 0) {
187			assertEquals(dirSpace, testDir.getTotalSpace());
188		}
189	}
190
191	/**
192	 * @tests java.io.File#getUsableSpace()
193	 *
194	 * @since 1.6
195	 */
196	public void test_getUsableSpace() throws Exception {
197		long fileSpace = getLinuxSpace(USABLE_SPACE_NUM, testFile);
198		long dirSpace = getLinuxSpace(USABLE_SPACE_NUM, testDir);
199		if (fileSpace > 0) {
200			assertEquals(fileSpace, testFile.getUsableSpace());
201		}
202		if (dirSpace > 0) {
203			assertEquals(dirSpace, testDir.getUsableSpace());
204		}
205	}
206
207	/**
208	 * @tests java.io.File#setExecutable(boolean, boolean)
209	 *
210	 * @since 1.6
211	 */
212	public void test_setExecutableZZ() {
213		// setExecutable(true, true/false)
214		assertFalse(testFile.canExecute());
215		assertTrue(testFile.setExecutable(true, false));
216		assertTrue(testFile.canExecute());
217		assertTrue(testFile.setExecutable(true, true));
218		assertTrue(testFile.canExecute());
219
220		// setExecutable(false, true/false)
221		assertTrue(testFile.setExecutable(false, true));
222		if (root) {
223			assertTrue(testFile.canExecute());
224		} else {
225			assertFalse(testFile.canExecute());
226		}
227		assertTrue(testFile.setExecutable(false, false));
228		assertFalse(testFile.canExecute());
229
230		// tests directory
231		assertTrue(testDir.canExecute());
232		assertTrue(testDir.setExecutable(false, true));
233		if (root) {
234			assertTrue(testDir.canExecute());
235		} else {
236			assertFalse(testDir.canExecute());
237		}
238		assertTrue(testDir.setExecutable(false, false));
239		if (root) {
240			assertTrue(testDir.canExecute());
241		} else {
242			assertFalse(testDir.canExecute());
243		}
244
245		assertTrue(testDir.setExecutable(true, true));
246		assertTrue(testDir.canExecute());
247		assertTrue(testDir.setExecutable(true, false));
248		assertTrue(testDir.canExecute());
249	}
250
251	/**
252	 * @tests java.io.File#setExecutable(boolean)
253	 *
254	 * @since 1.6
255	 */
256	public void test_setExecutableZ() {
257		// So far this method only deals with the situation that the user is the
258		// owner of the file
259		assertTrue(testFile.setExecutable(true));
260		assertTrue(testFile.canExecute());
261		assertTrue(testFile.setExecutable(false));
262		assertFalse(testFile.canExecute());
263		assertTrue(testFile.setExecutable(true));
264
265		// tests directory
266		assertTrue(testDir.canExecute());
267		assertTrue(testDir.setExecutable(false));
268		if (root) {
269			assertTrue(testDir.canExecute());
270		} else {
271			assertFalse(testDir.canExecute());
272		}
273		assertTrue(testDir.setExecutable(true));
274		assertTrue(testDir.canExecute());
275	}
276
277	/**
278	 * @tests java.io.File#setReadable(boolean, boolean)
279	 *
280	 * @since 1.6
281	 */
282	public void test_setReadableZZ() throws Exception {
283		// setReadable(false, false/true) succeeds on Linux
284		// However, canRead() always returns true when the user is 'root'.
285		assertTrue(testFile.canRead());
286		assertTrue(testFile.setReadable(false, false));
287		if (root) {
288			assertTrue(testFile.canRead());
289		} else {
290			assertFalse(testFile.canRead());
291		}
292		assertTrue(testFile.setReadable(false, true));
293		if (root) {
294			assertTrue(testFile.canRead());
295		} else {
296			assertFalse(testFile.canRead());
297		}
298
299		// tests directory, setReadable(false, true/false)
300		assertTrue(testDir.canRead());
301		assertTrue(testDir.setReadable(false, true));
302		if (root) {
303			assertTrue(testDir.canRead());
304		} else {
305			assertFalse(testDir.canRead());
306		}
307		assertTrue(testDir.setReadable(false, false));
308		if (root) {
309			assertTrue(testDir.canRead());
310		} else {
311			assertFalse(testDir.canRead());
312		}
313
314		// setReadable(true, false/true) and set them in turn
315		assertTrue(testFile.setReadable(true, false));
316		assertTrue(testFile.canRead());
317		assertTrue(testFile.setReadable(false, true));
318		if (root) {
319			assertTrue(testFile.canRead());
320		} else {
321			assertFalse(testFile.canRead());
322		}
323		assertTrue(testFile.setReadable(true, true));
324		assertTrue(testFile.canRead());
325		assertTrue(testFile.setReadable(false, true));
326		if (root) {
327			assertTrue(testFile.canRead());
328		} else {
329			assertFalse(testFile.canRead());
330		}
331
332		// tests directory, setReadable(true, true/false)
333		assertTrue(testDir.setReadable(true, false));
334		assertTrue(testDir.canRead());
335		assertTrue(testDir.setReadable(true, true));
336		assertTrue(testDir.canRead());
337	}
338
339	/**
340	 * @tests java.io.File#setReadable(boolean)
341	 *
342	 * @since 1.6
343	 */
344	public void test_setReadableZ() {
345		// So far this method only deals with the situation that the user is the
346		// owner of the file. setReadable(false) succeeds on Linux
347		// However, canRead() always returns true when the user is 'root'.
348		assertTrue(testFile.canRead());
349		assertTrue(testFile.setReadable(false));
350		if (root) {
351			assertTrue(testFile.canRead());
352		} else {
353			assertFalse(testFile.canRead());
354		}
355		assertTrue(testFile.setReadable(true));
356		assertTrue(testFile.canRead());
357
358		assertTrue(testDir.canRead());
359		assertTrue(testDir.setReadable(false));
360		if (root) {
361			assertTrue(testDir.canRead());
362		} else {
363			assertFalse(testDir.canRead());
364		}
365	}
366
367	@Override
368	protected void setUp() throws Exception {
369		super.setUp();
370		testFile = File.createTempFile("testfile", null);
371		testDir = new File(System.getProperty("java.io.tmpdir") + "/temp");
372		if (!testDir.exists()) {
373			testDir.mkdir();
374		}
375		root = System.getProperty("user.name").equals("root");
376	}
377
378	@Override
379	protected void tearDown() throws Exception {
380		testFile.delete();
381		testDir.delete();
382		testFile = null;
383		testDir = null;
384		root = false;
385		super.tearDown();
386	}
387
388    public void test_getCanonicalPath() throws IOException,
389                                               InterruptedException {
390        File tmpFolder1 = new File("folder1");
391        tmpFolder1.mkdirs();
392        tmpFolder1.deleteOnExit();
393
394        File tmpFolder2 = new File(tmpFolder1.toString() + "/folder2");
395        tmpFolder2.mkdirs();
396        tmpFolder2.deleteOnExit();
397
398        File tmpFolder3 = new File(tmpFolder2.toString() + "/folder3");
399        tmpFolder3.mkdirs();
400        tmpFolder3.deleteOnExit();
401
402        File tmpFolder4 = new File(tmpFolder3.toString() + "/folder4");
403        tmpFolder4.mkdirs();
404        tmpFolder4.deleteOnExit();
405
406        // make a link to folder1/folder2
407        Process ln = Runtime.getRuntime().exec("ln -s folder1/folder2 folder2");
408        ln.waitFor();
409        File linkFile = new File("folder2");
410        linkFile.deleteOnExit();
411
412        File file = new File("folder2");
413        assertEquals(tmpFolder2.getCanonicalPath(), file.getCanonicalPath());
414
415        file = new File("folder1/folder2");
416        assertEquals(tmpFolder2.getCanonicalPath(), file.getCanonicalPath());
417
418        file = new File("folder2/folder3");
419        assertEquals(tmpFolder3.getCanonicalPath(), file.getCanonicalPath());
420
421        file = new File("folder2/folder3/folder4");
422        assertEquals(tmpFolder4.getCanonicalPath(), file.getCanonicalPath());
423
424        file = new File("folder1/folder2/folder3");
425        assertEquals(tmpFolder3.getCanonicalPath(), file.getCanonicalPath());
426
427        file = new File("folder1/folder2/folder3/folder4");
428        assertEquals(tmpFolder4.getCanonicalPath(), file.getCanonicalPath());
429    }
430}
431