KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > naming > FileNameTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.masterfs.filebasedfs.naming;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import org.netbeans.junit.NbTestCase;
24 import java.io.File JavaDoc;
25 import java.lang.ref.Reference JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  *
31  * @author Radek Matous
32  */

33 public class FileNameTest extends NbTestCase {
34     private File JavaDoc f1;
35     private File JavaDoc f2;
36     private File JavaDoc f3;
37     private FileNaming n1;
38     private FileNaming n2;
39     private FileNaming n3;
40
41     public FileNameTest(String JavaDoc testName) {
42         super(testName);
43     }
44
45     protected void setUp() throws Exception JavaDoc {
46         clearWorkDir();
47         
48         f1 = getTestFile();
49         f2 = new File JavaDoc (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 JavaDoc getTestFile() throws Exception JavaDoc {
57         File JavaDoc retVal = new File JavaDoc (getWorkDir(), "namingTest");
58         if (!retVal.exists()) {
59             retVal.createNewFile();
60         }
61         return retVal;
62     }
63
64     protected void tearDown() throws Exception JavaDoc {
65         super.tearDown();
66         n1 = null;
67         n2 = null;
68         n3 = null;
69     }
70
71     public void test69450() throws Exception JavaDoc {
72         File JavaDoc fA = new File JavaDoc(getWorkDir(),"A");
73         if (fA.exists()) {
74             assertTrue(fA.delete());
75         }
76         File JavaDoc fa = new File JavaDoc(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             //#69450
99
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     /**
112      * Test of equals method, of class org.netbeans.modules.masterfs.pathtree.PathItem.
113      */

114    public void testEquals () throws Exception JavaDoc {
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 JavaDoc {
126         assertEquals(n3.hashCode(), n1.getParent().hashCode());
127         assertEquals(n3.hashCode(), n2.getParent().hashCode());
128     }
129     
130     public void testWeakReferenced () throws Exception JavaDoc {
131         List JavaDoc l = new ArrayList JavaDoc ();
132         FileNaming current = n1;
133         while (current != null) {
134             l.add(new WeakReference JavaDoc (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 JavaDoc weakReference = (WeakReference JavaDoc) l.get(i);
145             assertGC("Shoul be GCed: "+((FileNaming)weakReference.get()), weakReference);
146         }
147     }
148     
149     public void testFileConversion () throws Exception JavaDoc {
150         FileNaming[] all = new FileNaming [] {n1, n2, n3};
151         File JavaDoc[] files = new File JavaDoc [] {f1, f2, f3};
152         for (int i = 0; i < all.length; i++) {
153             FileNaming current = all[i];
154             File JavaDoc 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 JavaDoc {
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 JavaDoc file = current.getFile();
170                 assertTrue(file.getAbsolutePath(), file.exists());
171                 current = current.getParent();
172             }
173         }
174     }
175
176
177     /**
178      * Test of rename method, of class org.netbeans.modules.masterfs.naming.PathItem.
179      */

180     public void testRename() throws Exception JavaDoc {
181         File JavaDoc f = f1;
182         assertTrue(f.exists());
183         FileNaming pi = NamingFactory.fromFile(f);
184         assertTrue(pi.rename("renamed3"));
185         File JavaDoc 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