KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > InstanceDataObjectGetNameTest


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.openide.loaders;
21
22 import java.io.File JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import org.netbeans.junit.NbTestCase;
25 import org.openide.filesystems.FileObject;
26 import org.openide.filesystems.FileSystem;
27 import org.openide.filesystems.FileUtil;
28 import org.openide.filesystems.MultiFileSystem;
29 import org.openide.filesystems.XMLFileSystem;
30
31
32 /** Check that we cache getName
33  * @author Jaroslav Tulach
34  */

35 public class InstanceDataObjectGetNameTest extends NbTestCase {
36     private DataObject obj;
37     private FileSystem fs;
38
39     /** Creates new DataFolderTest */
40     public InstanceDataObjectGetNameTest(String JavaDoc name) {
41         super (name);
42     }
43     
44     private static String JavaDoc name;
45     private static int cnt;
46     public static String JavaDoc computeName() {
47         cnt++;
48         return name;
49     }
50     
51     protected void setUp () throws Exception JavaDoc {
52         
53         cnt = 0;
54         
55         File JavaDoc f = new File JavaDoc(getWorkDir(), "layer.xml");
56         FileWriter JavaDoc w = new FileWriter JavaDoc(f);
57         w.write("<filesystem><file name='x.instance'> ");
58         w.write(" <attr name='name' methodvalue='" + InstanceDataObjectGetNameTest.class.getName() + ".computeName'/> ");
59         w.write("</file></filesystem> ");
60         w.close();
61
62         fs = new MultiFileSystem(new FileSystem[] {
63             FileUtil.createMemoryFileSystem(),
64             new XMLFileSystem(f.toURL())
65         });
66         FileObject fo = fs.findResource("x.instance");
67         assertNotNull(fo);
68         
69         assertNull(fo.getAttribute("name"));
70         assertEquals("One call", 1, cnt);
71         // clean
72
cnt = 0;
73
74         obj = DataObject.find(fo);
75         
76         assertEquals("No calls now", 0, cnt);
77     }
78     
79     public void testNameIsCached() throws Exception JavaDoc {
80         if (!(obj instanceof InstanceDataObject)) {
81             fail("We need IDO : " + obj);
82         }
83         
84         name = "Ahoj";
85         assertEquals("We can influence a name", "Ahoj", obj.getName());
86         assertEquals("one call", 1, cnt);
87         assertEquals("Name stays the same", "Ahoj", obj.getName());
88         assertEquals("no new call", 1, cnt);
89         
90         name = "kuk";
91         assertEquals("Name stays the same", "Ahoj", obj.getName());
92         assertEquals("no new call", 1, cnt);
93
94         obj.getPrimaryFile().setAttribute("someattr", "new");
95         
96         assertEquals("Name changes as attribute changes fired", "kuk", obj.getName());
97         assertEquals("of course new call is there", 2, cnt);
98         
99     }
100 }
101
Popular Tags