KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > test > TestBase


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.ejbcore.test;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants;
26 import org.netbeans.modules.java.source.usages.IndexUtil;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.filesystems.Repository;
30 import org.openide.util.Lookup;
31 import org.openide.util.lookup.Lookups;
32 import org.openide.util.lookup.ProxyLookup;
33
34 /**
35  * Common ancestor for all test classes.
36  *
37  * @author Andrei Badea
38  * @author Martin Adamek
39  */

40 public class TestBase extends NbTestCase {
41
42     private EjbJarProviderImpl ejbJarProvider;
43     private ClassPathProviderImpl classPathProvider;
44     private FileOwnerQueryImpl fileOwnerQuery;
45     
46     protected FileObject dataDir;
47     protected FileObject testFO;
48     
49     private TestModule testModuleEJB14;
50     private TestModule testModuleEJB50;
51
52     static {
53         // set the lookup which will be returned by Lookup.getDefault()
54
System.setProperty("org.openide.util.Lookup", Lkp.class.getName());
55         assertEquals("Unable to set the default lookup!", Lkp.class, Lookup.getDefault().getClass());
56         setLookups();
57         assertEquals(RepositoryImpl.class, Lookup.getDefault().lookup(Repository.class).getClass());
58         assertEquals("The default Repository is not our repository!", RepositoryImpl.class, Repository.getDefault().getClass());
59     }
60     
61     public TestBase(String JavaDoc name) {
62         super(name);
63     }
64     
65     public TestModule ejb14() {
66         if (testModuleEJB14 == null) {
67             testModuleEJB14 = new TestModule(dataDir.getFileObject("EJBModule_1_4"), EjbProjectConstants.J2EE_14_LEVEL);
68         }
69         activate(testModuleEJB14);
70         return testModuleEJB14;
71     }
72     
73     public TestModule ejb50() {
74         if (testModuleEJB50 == null) {
75             testModuleEJB50 = new TestModule(dataDir.getFileObject("EJBModule_5_0"), EjbProjectConstants.JAVA_EE_5_LEVEL);
76         }
77         activate(testModuleEJB50);
78         return testModuleEJB50;
79     }
80     
81     protected void setUp() throws IOException JavaDoc {
82         clearWorkDir();
83         File JavaDoc file = new File JavaDoc(getWorkDir(),"cache"); //NOI18N
84
file.mkdirs();
85         IndexUtil.setCacheFolder(file);
86         ejbJarProvider = new EjbJarProviderImpl();
87         classPathProvider = new ClassPathProviderImpl();
88         fileOwnerQuery = new FileOwnerQueryImpl();
89         setLookups(
90                 ejbJarProvider,
91                 classPathProvider,
92                 fileOwnerQuery,
93                 new FakeJavaDataLoaderPool()
94                 );
95         dataDir = FileUtil.toFileObject(getDataDir());
96         FileObject workDir = FileUtil.toFileObject(getWorkDir());
97         testFO = workDir.createData("TestClass.java");
98     }
99
100     public static void setLookups(Object JavaDoc... lookups) {
101         ((Lkp)Lookup.getDefault()).setProxyLookups(Lookups.fixed(lookups));
102     }
103     
104     public static final class Lkp extends ProxyLookup {
105         
106         private final Repository repository = new RepositoryImpl();
107         
108         public Lkp() {
109             setProxyLookups(new Lookup[0]);
110         }
111         
112         private void setProxyLookups(Lookup... lookups) {
113             Lookup[] allLookups = new Lookup[lookups.length + 3];
114             ClassLoader JavaDoc classLoader = TestBase.class.getClassLoader();
115             allLookups[0] = Lookups.singleton(classLoader);
116             allLookups[1] = Lookups.singleton(repository);
117             System.arraycopy(lookups, 0, allLookups, 2, lookups.length);
118             allLookups[allLookups.length - 1] = Lookups.metaInfServices(classLoader);
119             setLookups(allLookups);
120         }
121         
122     }
123     
124     private void activate(TestModule testModule) {
125         fileOwnerQuery.setProject(testModule.project);
126         ejbJarProvider.setEjbModule(testModule.javaeeLevel, testModule.deploymentDescriptor, testModule.sources);
127         classPathProvider.setClassPath(testModule.sources);
128     }
129     
130     protected static class TestModule {
131         
132         private final String JavaDoc javaeeLevel;
133         private final FileObject deploymentDescriptor;
134         private final FileObject[] sources;
135         private final ProjectImpl project;
136         
137         public TestModule(FileObject projectDir, String JavaDoc javaeeLevel) {
138             this.javaeeLevel = javaeeLevel;
139             this.deploymentDescriptor = projectDir.getFileObject("src/conf/ejb-jar.xml");
140             this.sources = new FileObject[] {projectDir.getFileObject("src/java")};
141             this.project = new ProjectImpl("2.1");
142             project.setProjectDirectory(projectDir);
143         }
144      
145         public FileObject getDeploymentDescriptor() { return deploymentDescriptor; }
146         public FileObject[] getSources() { return sources; }
147         
148     }
149     
150 }
151
Popular Tags