001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.turbo.keys;
042:
043: import junit.framework.TestCase;
044: import org.openide.filesystems.FileObject;
045: import org.openide.filesystems.LocalFileSystem;
046: import org.openide.filesystems.FileUtil;
047: import org.openide.filesystems.FileSystem;
048: import org.netbeans.modules.turbo.Turbo;
049:
050: import java.io.File;
051:
052: /**
053: * Test DiskFileKey
054: *
055: * @author Petr Kuzel
056: */
057: public class DiskFileKeyTest extends TestCase {
058:
059: private FileSystem fs;
060:
061: // called before every method
062: protected void setUp() throws Exception {
063:
064: // prepare simple LFS
065:
066: LocalFileSystem fs = new LocalFileSystem();
067: File tmp = new File(System.getProperty("java.io.tmpdir")
068: + File.separator + "turbo-test");
069: tmp.mkdir();
070: tmp.deleteOnExit();
071: File theFile = new File(tmp, "theFile");
072: theFile.createNewFile();
073: theFile.deleteOnExit();
074: fs.setRootDirectory(tmp);
075:
076: this .fs = fs;
077: }
078:
079: /** Test how keys handle FileObject identity problems. */
080: public void testMemoryKeys() throws Exception {
081: FileObject overlap = fs.getRoot().createFolder("nestedFS2");
082: try {
083: LocalFileSystem ofs = new LocalFileSystem();
084: ofs.setRootDirectory(FileUtil.toFile(overlap));
085:
086: ofs.getRoot().createData("data.txt");
087:
088: FileObject f1 = fs.findResource("nestedFS2/data.txt");
089: FileObject f2 = ofs.findResource("data.txt");
090:
091: DiskFileKey k1 = DiskFileKey.createKey(f1);
092: DiskFileKey k2 = DiskFileKey.createKey(f2);
093:
094: assertTrue(k1.equals(k2));
095:
096: } finally {
097: overlap.delete(overlap.lock());
098: }
099: }
100:
101: /**
102: * Netbeans fileobjects have problems with identity.
103: * Two fileobject representing same file are not guaranteed to be equivalent.
104: * Known causes: MasterFS fileobject and fileobject from wrapped
105: * filesystem (it can be spotted only by FS impl). Overlapping
106: * LocalFilesystems.
107: * <p>
108: * It uncovered Memory.DiskFileKey.hashCode problem!
109: */
110: public void testDuplicatedFileObject() throws Exception {
111: FileObject overlap = fs.getRoot().createFolder("nestedFS");
112: try {
113: LocalFileSystem ofs = new LocalFileSystem();
114: ofs.setRootDirectory(FileUtil.toFile(overlap));
115:
116: ofs.getRoot().createData("data.txt");
117:
118: FileObject f1 = fs.findResource("nestedFS/data.txt");
119: FileObject f2 = ofs.findResource("data.txt");
120: assert f1 != f2;
121: assert f1.equals(f2) == false;
122: Object k1 = DiskFileKey.createKey(f1);
123: Object k2 = DiskFileKey.createKey(f2);
124:
125: Turbo turbo = Turbo.getDefault();
126:
127: turbo.writeEntry(k1, "identity", "clash");
128: Object v1 = turbo.readEntry(k1, "identity");
129: Object v2 = turbo.readEntry(k2, "identity");
130:
131: assertTrue("clash".equals(v1));
132: assertTrue("Unexpected value:" + v2, "clash".equals(v2));
133:
134: turbo.writeEntry(k2, "identity", "over!");
135:
136: v1 = turbo.readEntry(k1, "identity");
137: v2 = turbo.readEntry(k2, "identity");
138:
139: assertTrue("over!".equals(v1));
140: assertTrue("Unexpected value:" + v2, "over!".equals(v2));
141:
142: } finally {
143: overlap.delete(overlap.lock());
144: }
145: }
146:
147: }
|