summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.core/junit/org/gluster/storage/management/core/utils/FileUtilTest.java
blob: d68ad9a13ba3f584187109e9f077f1cd0ddd8ea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package org.gluster.storage.management.core.utils;

import static org.junit.Assert.*;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import org.gluster.storage.management.core.exceptions.GlusterRuntimeException;
import org.gluster.storage.management.core.utils.FileUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;



/**
 * The class <code>FileUtilTest</code> contains tests for the class <code>{@link FileUtil}</code>.
 *
 * @generatedBy CodePro at 9/29/11 2:39 PM
 * @author root
 * @version $Revision: 1.0 $
 */
public class FileUtilTest {
	

	private static final String TEST_FILE_PATH = FileUtil.getTempDirName() + "/test.txt";
	private static final String TEST_FILE_CONTENT = "Welcome to Gluster Storage Management console.";
	

	/**
	 * To write the text into given file. 
	 *
	 * @generatedBy 
	 */
	private boolean writeToFile(String fileName, String text) {
		try {
			Writer output = null;
			File file = new File(fileName);
			output = new BufferedWriter(new FileWriter(file));
			output.write(text);
			output.close();
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	private boolean createNestedDir(String path) {
		File file = new File(path);
		return file.mkdirs();
	}
	
	private boolean createEmptyFile(String fileName) {
		File file = new File(fileName);
		if (file.exists()) {
			return true;
		}
		
		try {
			return file.createNewFile();
		} catch (IOException e) {
			return false;
		}
	}
	
	/**
	 * Run the File createTempDir() method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test
	public void testCreateTempDir_1()
		throws Exception {

		File result = FileUtil.createTempDir();
		
		assertNotNull(result);
		assertTrue(File.class.equals(result.getClass()));
	}

	/**
	 * Run the File createTempDir() method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test
	public void testCreateTempDir_2()
		throws Exception {

		File result1 = FileUtil.createTempDir();
		File result2 = FileUtil.createTempDir();

		assertNotSame(result1, result2);
	}

	
	/**
	 * Run the void createTextFile(String,String) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test(expected=GlusterRuntimeException.class)
	public void testCreateTextFile_1()
		throws Exception {
		String fileName = "";
		String contents = "";
		FileUtil.createTextFile(fileName, contents);
	}

	/**
	 * Run the String getTempDirName() method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test
	public void testGetTempDirName_1()
		throws Exception {
		String result = FileUtil.getTempDirName();

		// while running on linux
		assertEquals("/tmp", result);
	}

	/**
	 * Run the byte[] readFileAsByteArray(File) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */

	@Test(expected=GlusterRuntimeException.class)
	public void testReadFileAsByteArray_1()
		throws Exception {
		File file = new File("");
		
		FileUtil.readFileAsByteArray(file);
	}

	/**
	 * Run the byte[] readFileAsByteArray(File) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test
	public void testReadFileAsByteArray_2()
		throws Exception {
		File file = new File(TEST_FILE_PATH);

		byte[] result = FileUtil.readFileAsByteArray(file);
		assertNotNull(result);
		assertTrue(result instanceof byte[]);
		assertTrue(TEST_FILE_CONTENT.equals(new String(result)));
	}

	
	/**
	 * Run the String readFileAsString(File) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */

	@Test(expected=GlusterRuntimeException.class)
	public void testReadFileAsString_1()
		throws Exception {
		File file = new File("");
		
		FileUtil.readFileAsString(file);
	}
	
	
	/**
	 * Run the String readFileAsString(File) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test
	public void testReadFileAsString_2()
		throws Exception {
		File file = new File(TEST_FILE_PATH);
		String result = FileUtil.readFileAsString(file);

		assertNotNull(result);
		assertTrue(result instanceof String);
		assertTrue(result.equals(TEST_FILE_CONTENT));
	}

	/**
	 * Run the void recursiveDelete(File) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test
	public void testRecursiveDelete_1()
		throws Exception {
		
		//Delete empty directories recursively
		File fileOrDir = new File(FileUtil.getTempDirName() + "/rd");
		
		FileUtil.recursiveDelete(fileOrDir);
		assertTrue(!fileOrDir.exists());
	}

	/**
	 * Run the void recursiveDelete(File) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */

	@Test(expected=GlusterRuntimeException.class)
	public void testRecursiveDelete_2()
		throws Exception {
		//Delete directories recursively (with some files) 
		File fileOrDir = new File(FileUtil.getTempDirName() + "/rdx");

		FileUtil.recursiveDelete(fileOrDir);
		assertTrue(!fileOrDir.exists());
	}

	/**
	 * Run the void recursiveDelete(File) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test
	public void testRecursiveDelete_3()
		throws Exception {
		File fileOrDir = new File(FileUtil.getTempDirName() + "/rd");
		FileUtil.recursiveDelete(fileOrDir);
		assertTrue(!fileOrDir.exists());
	}

	/**
	 * Run the void recursiveDelete(File) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test
	public void testRecursiveDelete_4()
		throws Exception {
		File file = new File(FileUtil.getTempDirName() + "/rd/b/bc/mydoc.txt");
		assertTrue(!file.exists());
		
		file = new File(FileUtil.getTempDirName() + "/rd//b/bc");
		assertTrue(!file.exists());
	}

	/**
	 * Run the void recursiveDelete(File) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test(expected=GlusterRuntimeException.class)
	public void testRecursiveDelete_5()
		throws Exception {
		File fileOrDir = new File(FileUtil.getTempDirName() + "/rd/*"); //Wild cards 
		FileUtil.recursiveDelete(fileOrDir);
	}
	
	
	/**
	 * Run the void recursiveDelete(File) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test(expected=GlusterRuntimeException.class)
	public void testRecursiveDelete_6()
		throws Exception {
		File fileOrDir = new File(FileUtil.getTempDirName() + "/abcxyz");
		FileUtil.recursiveDelete(fileOrDir);
	}
	
	/**
	 * Run the void renameFile(String,String) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */

	@Test(expected=GlusterRuntimeException.class)
	public void testRenameFile_1()
		throws Exception {
		String fromPath = FileUtil.getTempDirName() + "/test.txt";
		new File(fromPath).createNewFile();
		String toPath = "~/abc.txt"; // Relative path
		
		FileUtil.renameFile(fromPath, toPath);
		assertTrue(!new File(fromPath).exists());
		assertTrue(new File(toPath).exists());
	}

	/**
	 * Run the void renameFile(String,String) method test.
	 *
	 * @throws Exception
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Test
	public void testRenameFile_2()
		throws Exception {
		String fromPath = FileUtil.getTempDirName() + "/test.txt";
		new File(fromPath).createNewFile();
		
		String toPath = FileUtil.getTempDirName() + "/abc.txt"; // Absolute path example
		FileUtil.renameFile(fromPath, toPath);
		assertTrue(!new File(fromPath).exists());
		assertTrue(new File(toPath).exists());
	}
	
	@Test
	public void testRenameFile_3()
		throws Exception {
		String fromPath = FileUtil.getTempDirName() + "/test.txt";
		new File(fromPath).createNewFile();
		
		String toPath = FileUtil.getTempDirName() + "/renamefile.txt"; 
		FileUtil.renameFile(fromPath, toPath);
		assertTrue(!new File(fromPath).exists());
		assertTrue(new File(toPath).exists());
	}
	
	/**
	 * Perform pre-test initialization.
	 *
	 * @throws Exception
	 *         if the initialization fails for some reason
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@Before
	public void setUp()
		throws Exception {

		// testReadFileAsByteArray_2()
		if (!writeToFile(TEST_FILE_PATH, TEST_FILE_CONTENT)) {
			fail("Setup: Text file creation error!");
		}

		// testRecursiveDelete_1()
		if (!createNestedDir(FileUtil.getTempDirName() + "/rd/b/c/d")) {
			createEmptyFile(FileUtil.getTempDirName() + "/rd/b/mydoc.txt");
			createEmptyFile(FileUtil.getTempDirName() + "/rd/b/songs.mp3");
			createEmptyFile(FileUtil.getTempDirName() + "/rd/b/mysetup.cfg");
			
			createEmptyFile(FileUtil.getTempDirName() + "/rd/b/bc/mydoc.txt");
			createEmptyFile(FileUtil.getTempDirName() + "/rd/songs.mp3");
			createEmptyFile(FileUtil.getTempDirName() + "/rd/b/bc/mysetup.cfg");
			
			createEmptyFile(FileUtil.getTempDirName() + "/rd//b/mydoc.txt");
			createEmptyFile(FileUtil.getTempDirName() + "/rd/b/bc/songs.mp3");
			
		}
		
		if (! createEmptyFile(FileUtil.getTempDirName() + "/renamefile.txt") ) {
			fail("Failed to create file [/renamefile.txt]");
		}
	}

	/**
	 * Perform post-test clean-up.
	 *
	 * @throws Exception
	 *         if the clean-up fails for some reason
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	@After
	public void tearDown()
		throws Exception {
		File file = new File(TEST_FILE_PATH);
		file.delete();
		file = new File(FileUtil.getTempDirName() + "/rd/b/c/d");
		if (file.exists()) {
			file.delete();
		}
		
		file = new File(FileUtil.getTempDirName() + "/rd/b/c");
		if (file.exists()) {
			file.delete();
		}
		
		file = new File(FileUtil.getTempDirName() + "/rd/b/mydoc.txt");
		if (file.exists()) {
			file.delete();
		}
		file = new File(FileUtil.getTempDirName() + "/rd/b/songs.mp3");
		if (file.exists()) {
			file.delete();
		}
		file = new File(FileUtil.getTempDirName() + "/rd/b/mysetup.cfg");
		if (file.exists()) {
			file.delete();
		}
		
		file = new File(FileUtil.getTempDirName() + "/rd/b/bc/mydoc.txt");
		if (file.exists()) {
			file.delete();
		}
		file = new File(FileUtil.getTempDirName() + "/rd/b/bc/mysetup.cfg");
		if (file.exists()) {
			file.delete();
		}
		file = new File(FileUtil.getTempDirName() + "/rd/b/bc/songs.mp3");
		if (file.exists()) {
			file.delete();
		}
		
		file = new File(FileUtil.getTempDirName() + "/rd/b/bc");
		if (file.exists()) {
			file.delete();
		}
		
		file = new File(FileUtil.getTempDirName() + "/rd/b");
		if (file.exists()) {
			file.delete();
		}
			
		file = new File(FileUtil.getTempDirName() + "/rd");
		if (file.exists()) {
			file.delete();
		}
			
		file = new File(FileUtil.getTempDirName() + "/abc.txt");
		if (file.exists()) {
			file.delete();
		}
		file = new File("~/abc.txt");
		if (file.exists()) {
			file.delete();
		}
		file = new File(FileUtil.getTempDirName() + "/test.txt");
		if (file.exists()) {
			file.delete();
		}
		file = new File(FileUtil.getTempDirName() + "/renamefile.txt");
		if (file.exists()) {
			file.delete();
		}
	}

	/**
	 * Launch the test.
	 *
	 * @param args the command line arguments
	 *
	 * @generatedBy CodePro at 9/29/11 2:39 PM
	 */
	public static void main(String[] args) {
		new org.junit.runner.JUnitCore().run(FileUtilTest.class);
	}
}