KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > turbo > keys > DiskFileKeyTest


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 package org.netbeans.modules.turbo.keys;
20
21 import junit.framework.TestCase;
22 import org.openide.filesystems.FileObject;
23 import org.openide.filesystems.LocalFileSystem;
24 import org.openide.filesystems.FileUtil;
25 import org.openide.filesystems.FileSystem;
26 import org.netbeans.modules.turbo.Turbo;
27
28 import java.io.File JavaDoc;
29
30 /**
31  * Test DiskFileKey
32  *
33  * @author Petr Kuzel
34  */

35 public class DiskFileKeyTest extends TestCase {
36
37     private FileSystem fs;
38
39     // called before every method
40
protected void setUp() throws Exception JavaDoc {
41
42         // prepare simple LFS
43

44         LocalFileSystem fs = new LocalFileSystem();
45         File JavaDoc tmp = new File JavaDoc(System.getProperty("java.io.tmpdir") + File.separator + "turbo-test");
46         tmp.mkdir();
47         tmp.deleteOnExit();
48         File JavaDoc theFile = new File JavaDoc(tmp, "theFile");
49         theFile.createNewFile();
50         theFile.deleteOnExit();
51         fs.setRootDirectory(tmp);
52
53         this.fs = fs;
54     }
55
56
57     /** Test how keys handle FileObject identity problems. */
58     public void testMemoryKeys() throws Exception JavaDoc{
59         FileObject overlap = fs.getRoot().createFolder("nestedFS2");
60         try {
61             LocalFileSystem ofs = new LocalFileSystem();
62             ofs.setRootDirectory(FileUtil.toFile(overlap));
63
64             ofs.getRoot().createData("data.txt");
65
66             FileObject f1 = fs.findResource("nestedFS2/data.txt");
67             FileObject f2 = ofs.findResource("data.txt");
68
69             DiskFileKey k1 = DiskFileKey.createKey(f1);
70             DiskFileKey k2 = DiskFileKey.createKey(f2);
71
72             assertTrue(k1.equals(k2));
73
74         } finally {
75             overlap.delete(overlap.lock());
76         }
77     }
78
79     /**
80      * Netbeans fileobjects have problems with identity.
81      * Two fileobject representing same file are not guaranteed to be equivalent.
82      * Known causes: MasterFS fileobject and fileobject from wrapped
83      * filesystem (it can be spotted only by FS impl). Overlapping
84      * LocalFilesystems.
85      * <p>
86      * It uncovered Memory.DiskFileKey.hashCode problem!
87      */

88     public void testDuplicatedFileObject() throws Exception JavaDoc {
89         FileObject overlap = fs.getRoot().createFolder("nestedFS");
90         try {
91             LocalFileSystem ofs = new LocalFileSystem();
92             ofs.setRootDirectory(FileUtil.toFile(overlap));
93
94             ofs.getRoot().createData("data.txt");
95
96             FileObject f1 = fs.findResource("nestedFS/data.txt");
97             FileObject f2 = ofs.findResource("data.txt");
98             assert f1 != f2;
99             assert f1.equals(f2) == false;
100             Object JavaDoc k1 = DiskFileKey.createKey(f1);
101             Object JavaDoc k2 = DiskFileKey.createKey(f2);
102
103
104             Turbo turbo = Turbo.getDefault();
105
106             turbo.writeEntry(k1, "identity", "clash");
107             Object JavaDoc v1 = turbo.readEntry(k1, "identity");
108             Object JavaDoc v2 = turbo.readEntry(k2, "identity");
109
110             assertTrue("clash".equals(v1));
111             assertTrue("Unexpected value:" + v2, "clash".equals(v2));
112
113             turbo.writeEntry(k2, "identity", "over!");
114
115             v1 = turbo.readEntry(k1, "identity");
116             v2 = turbo.readEntry(k2, "identity");
117
118             assertTrue("over!".equals(v1));
119             assertTrue("Unexpected value:" + v2, "over!".equals(v2));
120
121         } finally {
122             overlap.delete(overlap.lock());
123         }
124     }
125
126 }
127
Popular Tags