KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > queries > CompiledSourceForBinaryQueryTest


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.java.j2seproject.queries;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Properties JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.netbeans.api.java.queries.SourceForBinaryQuery;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.api.project.ProjectManager;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator;
31 import org.netbeans.modules.java.j2seproject.SourceRootsTest;
32 import org.openide.filesystems.FileObject;
33 import org.netbeans.api.project.TestUtil;
34 import org.netbeans.spi.project.support.ant.AntProjectHelper;
35 import org.netbeans.spi.project.support.ant.EditableProperties;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.modules.SpecificationVersion;
38 import org.openide.util.Lookup;
39
40 /**
41  * Tests for CompiledSourceForBinaryQuery
42  *
43  * @author Tomas Zezula
44  */

45 public class CompiledSourceForBinaryQueryTest extends NbTestCase {
46     
47     public CompiledSourceForBinaryQueryTest(String JavaDoc testName) {
48         super(testName);
49     }
50     
51     private FileObject scratch;
52     private FileObject projdir;
53     private FileObject sources;
54     private FileObject buildClasses;
55     private ProjectManager pm;
56     private Project pp;
57     AntProjectHelper helper;
58     
59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61         TestUtil.setLookup(new Object JavaDoc[] {
62             new org.netbeans.modules.java.j2seproject.J2SEProjectType(),
63             new org.netbeans.modules.java.project.ProjectSourceForBinaryQuery(),
64             new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(),
65         });
66         Properties JavaDoc p = System.getProperties();
67     }
68
69     protected void tearDown() throws Exception JavaDoc {
70         scratch = null;
71         projdir = null;
72         pm = null;
73         TestUtil.setLookup(Lookup.EMPTY);
74         super.tearDown();
75     }
76     
77     
78     private void prepareProject () throws IOException JavaDoc {
79         scratch = TestUtil.makeScratchDir(this);
80         projdir = scratch.createFolder("proj");
81         J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); //NOI18N
82
helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null);
83         J2SEProjectGenerator.setDefaultSourceLevel(null); //NOI18N
84
pm = ProjectManager.getDefault();
85         pp = pm.findProject(projdir);
86         sources = projdir.getFileObject("src");
87         FileObject fo = projdir.createFolder("build");
88         buildClasses = fo.createFolder("classes");
89     }
90     
91     public void testSourceForBinaryQuery() throws Exception JavaDoc {
92         this.prepareProject();
93         FileObject folder = scratch.createFolder("SomeFolder");
94         SourceForBinaryQuery.Result result = SourceForBinaryQuery.findSourceRoots(folder.getURL());
95         assertEquals("Non-project folder does not have any source folder", 0, result.getRoots().length);
96         folder = projdir.createFolder("SomeFolderInProject");
97         result = SourceForBinaryQuery.findSourceRoots(folder.getURL());
98         assertEquals("Project non build folder does not have any source folder", 0, result.getRoots().length);
99         result = SourceForBinaryQuery.findSourceRoots(buildClasses.getURL());
100         assertEquals("Project build folder must have source folder", 1, result.getRoots().length);
101         assertEquals("Project build folder must have source folder",sources,result.getRoots()[0]);
102     }
103     
104     
105     public void testSourceForBinaryQueryListening () throws Exception JavaDoc {
106         this.prepareProject();
107         SourceForBinaryQuery.Result result = SourceForBinaryQuery.findSourceRoots(buildClasses.getURL());
108         assertEquals("Project build folder must have source folder", 1, result.getRoots().length);
109         assertEquals("Project build folder must have source folder",sources,result.getRoots()[0]);
110         TestListener tl = new TestListener ();
111         result.addChangeListener(tl);
112         EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
113         FileObject sources2 = projdir.createFolder("src2");
114         props.put ("src.dir","src2");
115         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
116         assertTrue (tl.wasEvent());
117         assertEquals("Project build folder must have source folder", 1, result.getRoots().length);
118         assertEquals("Project build folder must have source folder",sources2,result.getRoots()[0]);
119     }
120
121     public void testSourceForBinaryQueryMultipleSourceRoots () throws Exception JavaDoc {
122         this.prepareProject();
123         SourceForBinaryQuery.Result result = SourceForBinaryQuery.findSourceRoots(buildClasses.getURL());
124         assertEquals("Project build folder must have source folder", 1, result.getRoots().length);
125         assertEquals("Project build folder must have source folder",sources,result.getRoots()[0]);
126         TestListener tl = new TestListener ();
127         result.addChangeListener(tl);
128         FileObject newRoot = SourceRootsTest.addSourceRoot(helper,projdir,"src.other.dir","other");
129         assertTrue (tl.wasEvent());
130         assertEquals("Project build folder must have 2 source folders", 2, result.getRoots().length);
131         assertEquals("Project build folder must have the first source folder",sources,result.getRoots()[0]);
132         assertEquals("Project build folder must have the second source folder",newRoot,result.getRoots()[1]);
133     }
134
135     private static class TestListener implements ChangeListener JavaDoc {
136         
137         private boolean gotEvent;
138         
139         public void stateChanged(ChangeEvent JavaDoc changeEvent) {
140             this.gotEvent = true;
141         }
142         
143         public void reset () {
144             this.gotEvent = false;
145         }
146         
147         public boolean wasEvent () {
148             return this.gotEvent;
149         }
150     }
151         
152 }
153
Popular Tags