KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > 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.java.j2seproject.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.spi.project.support.ant.PropertyUtils;
34 import org.openide.filesystems.FileObject;
35 import org.netbeans.api.project.TestUtil;
36 import org.netbeans.spi.project.support.ant.AntProjectHelper;
37 import org.netbeans.spi.project.support.ant.EditableProperties;
38 import org.netbeans.spi.project.support.ant.ProjectGenerator;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.modules.SpecificationVersion;
41
42 /**
43  * Tests for SourceLevelQueryImpl
44  *
45  * @author David Konecny
46  */

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