1 19 20 package org.netbeans.modules.masterfs.filebasedfs.naming; 21 22 import java.lang.ref.WeakReference ; 23 import org.netbeans.junit.NbTestCase; 24 import java.io.File ; 25 import java.lang.ref.Reference ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 33 public class FileNameTest extends NbTestCase { 34 private File f1; 35 private File f2; 36 private File f3; 37 private FileNaming n1; 38 private FileNaming n2; 39 private FileNaming n3; 40 41 public FileNameTest(String testName) { 42 super(testName); 43 } 44 45 protected void setUp() throws Exception { 46 clearWorkDir(); 47 48 f1 = getTestFile(); 49 f2 = new File (f1.getAbsolutePath()); 50 f3 = f1.getParentFile(); 51 n1 = NamingFactory.fromFile(f1); 52 n2 = NamingFactory.fromFile(f2); 53 n3 = NamingFactory.fromFile(f3); 54 } 55 56 protected File getTestFile() throws Exception { 57 File retVal = new File (getWorkDir(), "namingTest"); 58 if (!retVal.exists()) { 59 retVal.createNewFile(); 60 } 61 return retVal; 62 } 63 64 protected void tearDown() throws Exception { 65 super.tearDown(); 66 n1 = null; 67 n2 = null; 68 n3 = null; 69 } 70 71 public void test69450() throws Exception { 72 File fA = new File (getWorkDir(),"A"); 73 if (fA.exists()) { 74 assertTrue(fA.delete()); 75 } 76 File fa = new File (getWorkDir(),"a"); 77 if (!fa.exists()) { 78 assertTrue(fa.createNewFile()); 79 } 80 boolean isCaseSensitive = !fa.equals(fA); 81 FileNaming na = NamingFactory.fromFile(fa); 82 assertEquals(fa.getName(),NamingFactory.fromFile(fa).getName()); 83 if (isCaseSensitive) { 84 assertFalse(fa.getName().equals(NamingFactory.fromFile(fA).getName())); 85 assertFalse(NamingFactory.fromFile(fa).equals(NamingFactory.fromFile(fA))); 86 assertNotSame(NamingFactory.fromFile(fa),NamingFactory.fromFile(fA)); 87 assertTrue(fa.delete()); 88 assertTrue(fA.createNewFile()); 89 assertFalse(fA.getName().equals(na.getName())); 90 assertFalse(na.equals(NamingFactory.fromFile(fA))); 91 assertFalse(fA.getName().equals(na.getName())); 92 } else { 93 assertSame(na,NamingFactory.fromFile(fA)); 94 assertEquals(fa.getName(),na.getName()); 95 assertEquals(fa.getName(),NamingFactory.fromFile(fA).getName()); 96 assertEquals(NamingFactory.fromFile(fa),NamingFactory.fromFile(fA)); 97 assertSame(NamingFactory.fromFile(fa),NamingFactory.fromFile(fA)); 98 assertTrue(fa.delete()); 100 assertTrue(fA.createNewFile()); 101 assertFalse(fA.getName().equals(na.getName())); 102 assertEquals(na,NamingFactory.fromFile(fA)); 103 assertSame(na, NamingFactory.fromFile(fA)); 104 assertFalse(fA.getName() + " / " + na.getName(),fA.getName().equals(na.getName())); 105 NamingFactory.checkCaseSensitivity(na,fA); 106 assertTrue(fA.getName() + " / " + na.getName(),fA.getName().equals(na.getName())); 107 108 } 109 } 110 111 114 public void testEquals () throws Exception { 115 assertEquals(n1, n2); 116 assertSame(n1, n2); 117 assertNotSame(n3, n1); 118 assertNotSame(n3, n2); 119 assertEquals(n3, n1.getParent()); 120 assertEquals(n3, n2.getParent()); 121 assertSame(n3, n1.getParent()); 122 assertSame(n3, n2.getParent()); 123 } 124 125 public void testHashcode () throws Exception { 126 assertEquals(n3.hashCode(), n1.getParent().hashCode()); 127 assertEquals(n3.hashCode(), n2.getParent().hashCode()); 128 } 129 130 public void testWeakReferenced () throws Exception { 131 List l = new ArrayList (); 132 FileNaming current = n1; 133 while (current != null) { 134 l.add(new WeakReference (current)); 135 current = current.getParent(); 136 } 137 138 current = null; 139 n1 = null; 140 n2 = null; 141 n3 = null; 142 143 for (int i = 0; i < l.size(); i++) { 144 WeakReference weakReference = (WeakReference ) l.get(i); 145 assertGC("Shoul be GCed: "+((FileNaming)weakReference.get()), weakReference); 146 } 147 } 148 149 public void testFileConversion () throws Exception { 150 FileNaming[] all = new FileNaming [] {n1, n2, n3}; 151 File [] files = new File [] {f1, f2, f3}; 152 for (int i = 0; i < all.length; i++) { 153 FileNaming current = all[i]; 154 File currentFile = files[i]; 155 156 while (current != null) { 157 assertEquals (current.getFile(), currentFile); 158 current = current.getParent(); 159 currentFile = currentFile.getParentFile(); 160 } 161 } 162 } 163 164 public void testFileExist () throws Exception { 165 FileNaming[] all = new FileNaming [] {n1, n2, n3}; 166 for (int i = 0; i < all.length; i++) { 167 FileNaming current = all[i]; 168 while (current != null) { 169 File file = current.getFile(); 170 assertTrue(file.getAbsolutePath(), file.exists()); 171 current = current.getParent(); 172 } 173 } 174 } 175 176 177 180 public void testRename() throws Exception { 181 File f = f1; 182 assertTrue(f.exists()); 183 FileNaming pi = NamingFactory.fromFile(f); 184 assertTrue(pi.rename("renamed3")); 185 File f2 = pi.getFile(); 186 assertFalse(f.exists()); 187 assertTrue(f2.exists()); 188 assertFalse(f2.equals(f)); 189 assertTrue (f2.getName().equals("renamed3")); 190 } 191 192 } 193 | Popular Tags |