KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
23 import org.netbeans.junit.NbTestCase;
24 import org.openide.filesystems.FileObject;
25 import org.openide.filesystems.FileSystem;
26 import org.openide.filesystems.FileUtil;
27 import org.openide.filesystems.Repository;
28 import org.openide.filesystems.URLMapper;
29 import org.openide.util.Lookup;
30 import org.openide.util.lookup.Lookups;
31 import org.openide.util.lookup.ProxyLookup;
32
33 /** Test that URL is not requested if there are no broken shadows.
34  * @author Jaroslav Tulach
35  */

36 public class DataShadowBrokenAreNotTestedTest extends NbTestCase {
37     
38     static {
39         System.setProperty("org.openide.util.Lookup", Lkp.class.getName());
40     }
41     
42     public DataShadowBrokenAreNotTestedTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     protected void setUp() throws Exception JavaDoc {
47         
48         FileSystem lfs = Repository.getDefault().getDefaultFileSystem();
49         
50         FileObject[] delete = lfs.getRoot().getChildren();
51         for (int i = 0; i < delete.length; i++) {
52             delete[i].delete();
53         }
54         
55         UM.cnt = 0;
56     }
57     
58     public void testNoURLMapperQueried() throws Exception JavaDoc {
59         FileSystem lfs = Repository.getDefault().getDefaultFileSystem();
60         FileObject fo = FileUtil.createData(lfs.getRoot(), getName() + "/folder/original.txt");
61         assertNotNull(fo);
62         
63         assertEquals("No queries to UM yet", 0, UM.cnt);
64         DataObject original = DataObject.find(fo);
65         
66         assertEquals("No queries to UM after creation of data object", 0, UM.cnt);
67     }
68     
69     public void testQueriedWhenBrokenShadowsExists() throws Exception JavaDoc {
70         
71         //
72
// Note: if anyone lowers the number of queries done here,
73
// then go on, this test is here just to describe the current behaviour
74
//
75

76         
77         FileSystem lfs = Repository.getDefault().getDefaultFileSystem();
78         FileObject f1 = FileUtil.createData(lfs.getRoot(), getName() + "/folder/original.txt");
79         assertNotNull(f1);
80         FileObject f2 = FileUtil.createData(lfs.getRoot(), getName() + "/any/folder/original.txt");
81         assertNotNull(f2);
82         
83         assertEquals("No queries to UM yet", 0, UM.cnt);
84         DataObject original = DataObject.find(f1);
85         assertEquals("No queries to UM still", 0, UM.cnt);
86         DataShadow s = original.createShadow(original.getFolder());
87         assertEquals("One query to create the shadow and one to create the instance", 2, UM.cnt);
88         original.delete();
89         assertEquals("One additional query to delete", 3, UM.cnt);
90         DataObject brokenShadow = DataObject.find(s.getPrimaryFile());
91         assertEquals("Creating one broken shadow", 5, UM.cnt);
92         
93         DataObject original2 = DataObject.find(f2);
94         assertEquals("Additional query per very data object creation", 6, UM.cnt);
95     }
96     
97     private static final class UM extends URLMapper {
98         public static int cnt;
99         
100         public URL JavaDoc getURL(FileObject fo, int type) {
101             cnt++;
102             return null;
103         }
104         
105         public FileObject[] getFileObjects(URL JavaDoc url) {
106             cnt++;
107             return null;
108         }
109     }
110     
111     public static final class Lkp extends ProxyLookup {
112         public Lkp() {
113             super(new Lookup[] {
114                 Lookups.singleton(new UM()),
115                 Lookups.metaInfServices(Lkp.class.getClassLoader()),
116             });
117         }
118     }
119     
120 }
121
Popular Tags