View Javadoc

1   package net.sf.jpkgmk.util;
2   
3   import java.io.File;
4   import java.util.Arrays;
5   
6   import junit.framework.AssertionFailedError;
7   import junit.framework.TestCase;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  /**
13   * @author gommma (gommma AT users.sourceforge.net)
14   * @author Last changed by: $Author: gommma $
15   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
16   * @since 1.0
17   */
18  public abstract class FsTestCase extends TestCase 
19  {
20  	private String basedirString = System.getProperty("java.io.tmpdir");
21  	private File tmpDir = new File(basedirString, getClass().getName());
22  	private Log log = LogFactory.getLog(FsTestCase.class);
23  	
24  	
25  	@Override
26  	protected void setUp() throws Exception
27  	{
28  		tmpDir.mkdirs();
29  		assertTrue(tmpDir.exists());
30  	}
31  	
32  	@Override
33  	protected void tearDown()
34  	{
35  		// delete files again
36  		FileUtil.deleteRecursively(tmpDir);
37  	}
38  
39  	/**
40  	 * @return The directory to be used for the unit test
41  	 */
42  	public File getDirectory()
43  	{
44  		return this.tmpDir;
45  	}
46  
47  	public void createDir(File dir) {
48  		FileUtil.createDir(dir, true);
49  	}
50  
51  	public void assertArraysEqual(File[] expected, File[] actual) {
52  
53  		assertArraysEqual(expected, actual, true);
54  	}
55  	
56  	
57  	public void assertArraysEqual(File[] expected, File[] actual, boolean honorOrder) {
58  
59  		if(! (expected.length == actual.length)) {
60  			throw new AssertionFailedError("Arrays have different lengths: expected=" + expected.length + ", actual=" + actual.length +"(expected=" + Arrays.asList(expected) + ", actual=" + Arrays.asList(actual) + ")");
61  		}
62  		
63  		if(honorOrder) {
64  			if(!Arrays.equals(expected, actual)) {
65  				throw new AssertionFailedError("Arrays not equal: expected=" + Arrays.asList(expected) + ", actual=" + Arrays.asList(actual));
66  			}
67  			else {
68  				log.debug("All right. Arrays are equal (honoring order): expected=" + expected + ", actual=" + actual);
69  			}
70  		}
71  		else {
72  			// Compare and ignore the order
73  			for (int i = 0; i < expected.length; i++) {
74  				File currentExpected = expected[i];
75  				if(!contains(actual, currentExpected)) {
76  					throw new AssertionFailedError("Arrays not equal: actual array does not contain object '" + currentExpected + "'. expected=" + Arrays.asList(expected) + ", actual=" + Arrays.asList(actual));
77  				}
78  			}
79  			
80  			log.debug("All right. Arrays are equal (ignoring order): expected=" + expected + ", actual=" + actual);
81  
82  		}
83  	}
84  
85  	
86  	private boolean contains(Object[] array, Object objectToSearch) {
87  		for (int i = 0; i < array.length; i++) {
88  			if(array[i].equals(objectToSearch)) {
89  				return true;
90  			}
91  		}
92  		return false;
93  	}
94  	
95  	
96  }