KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > unit > PUDataObjectTestBase


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.j2ee.persistence.unit;
21
22 import java.net.URI JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import org.netbeans.api.project.Project;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.spi.project.FileOwnerQueryImplementation;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.MIMEResolver;
29 import org.openide.loaders.DataLoaderPool;
30 import org.openide.util.Enumerations;
31 import org.openide.util.Lookup;
32 import org.openide.util.lookup.Lookups;
33 import org.openide.util.lookup.ProxyLookup;
34
35 /**
36  *
37  * @author Erno Mononen, Andrei Badea
38  */

39 public abstract class PUDataObjectTestBase extends NbTestCase {
40
41     static {
42         System.setProperty("org.openide.util.Lookup", Lkp.class.getName());
43         ((Lkp)Lookup.getDefault()).setLookups(new Object JavaDoc[] { new PUMimeResolver(), new Pool(), new PUFOQI() });
44
45         assertEquals("Unable to set the default lookup!", Lkp.class, Lookup.getDefault().getClass());
46         assertEquals("The default MIMEResolver is not our resolver!", PUMimeResolver.class, Lookup.getDefault().lookup(MIMEResolver.class).getClass());
47         assertEquals("The default DataLoaderPool is not our pool!", Pool.class, Lookup.getDefault().lookup(DataLoaderPool.class).getClass());
48     }
49
50     public PUDataObjectTestBase(String JavaDoc name) {
51         super(name);
52     }
53
54     /**
55      * Our default lookup.
56      */

57     public static final class Lkp extends ProxyLookup {
58
59         public Lkp() {
60             setLookups(new Object JavaDoc[0]);
61         }
62
63         public void setLookups(Object JavaDoc[] instances) {
64             ClassLoader JavaDoc l = PersistenceEditorTestBase.class.getClassLoader();
65             setLookups(new Lookup[] {
66                 Lookups.fixed(instances),
67                 Lookups.metaInfServices(l),
68                 Lookups.singleton(l)
69             });
70         }
71     }
72
73     /**
74      * DataLoaderPool which is registered in the default lookup and loads
75      * PUDataLoader.
76      */

77     public static final class Pool extends DataLoaderPool {
78
79         public Enumeration JavaDoc loaders() {
80             return Enumerations.singleton(new PUDataLoader());
81         }
82     }
83
84     /**
85      * MIME Resolver that associates persistence.xml with PUDataLoader.
86      */

87     public static final class PUMimeResolver extends MIMEResolver {
88
89         public String JavaDoc findMIMEType(FileObject fo) {
90             if (fo.getName().startsWith("persistence")){
91                 return PUDataLoader.REQUIRED_MIME;
92             }
93             return null;
94         }
95     }
96
97     /**
98      * Returns dummy project implementation. Needed since persistence unit needs
99      * to be associated with {@link Project} owner. Also see issue #74426.
100      */

101     private static final class PUFOQI implements FileOwnerQueryImplementation {
102
103         private final Project dummyProject = new Project() {
104             public Lookup getLookup() { return Lookup.EMPTY; }
105             public FileObject getProjectDirectory() { return null; }
106         };
107
108         public Project getOwner(URI JavaDoc file) {
109             return dummyProject;
110         }
111
112         public Project getOwner(FileObject file) {
113             return dummyProject;
114         }
115     }
116 }
117
Popular Tags