KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > queries > SourceLevelQueryImplTest


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.web.project.queries;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Properties JavaDoc;
25 import org.netbeans.api.java.classpath.ClassPath;
26 import org.netbeans.api.java.platform.JavaPlatform;
27 import org.netbeans.api.java.platform.Specification;
28 import org.netbeans.api.java.queries.SourceLevelQuery;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.junit.NbTestCase;
32 import org.netbeans.modules.java.platform.JavaPlatformProvider;
33 import org.netbeans.modules.web.project.test.TestBase;
34 import org.netbeans.modules.web.project.test.TestUtil;
35 import org.netbeans.spi.project.support.ant.PropertyUtils;
36 import org.openide.filesystems.FileObject;
37 import org.netbeans.spi.project.support.ant.AntProjectHelper;
38 import org.netbeans.spi.project.support.ant.EditableProperties;
39 import org.netbeans.spi.project.support.ant.ProjectGenerator;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.modules.SpecificationVersion;
42
43 /**
44  * Tests for SourceLevelQueryImpl
45  *
46  * @author David Konecny, Radko Najman
47  */

48 public class SourceLevelQueryImplTest extends NbTestCase {
49     
50     public SourceLevelQueryImplTest(String JavaDoc testName) {
51         super(testName);
52     }
53     
54     private FileObject scratch;
55     private FileObject projdir;
56     private FileObject sources;
57     private ProjectManager pm;
58     private Project pp;
59     
60     protected void setUp() throws Exception JavaDoc {
61         super.setUp();
62         TestBase.setLookup(new Object JavaDoc[] {
63             new org.netbeans.modules.web.project.WebProjectType(),
64             new org.netbeans.modules.java.project.ProjectSourceLevelQueryImpl(),
65             new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(),
66             new TestPlatformProvider ()
67         });
68         Properties JavaDoc p = System.getProperties();
69         if (p.getProperty ("netbeans.user") == null) {
70             p.put("netbeans.user", FileUtil.toFile(TestUtil.makeScratchDir(this)).getAbsolutePath());
71         }
72     }
73
74     protected void tearDown() throws Exception JavaDoc {
75         scratch = null;
76         projdir = null;
77         sources = null;
78         pm = null;
79         pp = null;
80         super.tearDown();
81     }
82     
83     
84     private void prepareProject (String JavaDoc platformName) throws IOException JavaDoc {
85         scratch = TestUtil.makeScratchDir(this);
86         projdir = scratch.createFolder("proj");
87         AntProjectHelper helper = ProjectGenerator.createProject(projdir, "org.netbeans.modules.web.project");
88         pm = ProjectManager.getDefault();
89         pp = pm.findProject(projdir);
90         EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
91         props.setProperty("javac.source", "${def}");
92         props.setProperty ("platform.active",platformName);
93         props.setProperty("def", "1.2");
94         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
95         props = PropertyUtils.getGlobalProperties();
96         props.put("default.javac.source","4.3");
97         PropertyUtils.putGlobalProperties(props);
98         sources = projdir.createFolder("src");
99     }
100     
101     public void testGetSourceLevelWithValidPlatform() throws Exception JavaDoc {
102         this.prepareProject("TestPlatform");
103         FileObject file = scratch.createData("some.java");
104         String JavaDoc sl = SourceLevelQuery.getSourceLevel(file);
105         assertEquals("Non-project Java file does not have any source level", null, sl);
106         file = sources.createData("a.java");
107         sl = SourceLevelQuery.getSourceLevel(file);
108         assertEquals("Project's Java file must have project's source", "1.2", sl);
109     }
110     
111     public void testGetSourceLevelWithBrokenPlatform() throws Exception JavaDoc {
112         this.prepareProject("BrokenPlatform");
113         FileObject file = scratch.createData("some.java");
114         String JavaDoc sl = SourceLevelQuery.getSourceLevel(file);
115         assertEquals("Non-project Java file does not have any source level", null, sl);
116         file = sources.createData("a.java");
117         sl = SourceLevelQuery.getSourceLevel(file);
118         assertEquals("Project's Java file must have project's source", "4.3", sl);
119     }
120     
121     private static class TestPlatformProvider implements JavaPlatformProvider {
122         
123         private JavaPlatform platform;
124         
125         public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc listener) {
126         }
127
128         public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc listener) {
129         }
130
131         public JavaPlatform[] getInstalledPlatforms() {
132             return new JavaPlatform[] {
133                 getDefaultPlatform()
134             };
135         }
136
137         public JavaPlatform getDefaultPlatform() {
138             if (this.platform == null) {
139                 this.platform = new TestPlatform ();
140             }
141             return this.platform;
142         }
143     }
144     
145     private static class TestPlatform extends JavaPlatform {
146         
147         public FileObject findTool(String JavaDoc toolName) {
148             return null;
149         }
150
151         public String JavaDoc getVendor() {
152             return "me";
153         }
154
155         public ClassPath getStandardLibraries() {
156             return null;
157         }
158
159         public Specification getSpecification() {
160             return new Specification ("j2se", new SpecificationVersion ("1.5"));
161         }
162
163         public ClassPath getSourceFolders() {
164             return null;
165         }
166
167         public java.util.Map JavaDoc getProperties() {
168             return Collections.singletonMap("platform.ant.name","TestPlatform");
169         }
170
171         public java.util.List JavaDoc getJavadocFolders() {
172             return null;
173         }
174
175         public java.util.Collection JavaDoc getInstallFolders() {
176             return null;
177         }
178
179         public String JavaDoc getDisplayName() {
180             return "TestPlatform";
181         }
182
183         public ClassPath getBootstrapLibraries() {
184             return null;
185         }
186         
187     }
188     
189 }
190
Popular Tags