001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * FileBuiltQueryImplTest.java
044: * JUnit based test
045: *
046: * Created on 14 February 2006, 16:42
047: */
048: package org.netbeans.modules.mobility.project.queries;
049:
050: import java.beans.PropertyChangeListener;
051: import javax.swing.event.ChangeEvent;
052: import javax.swing.event.ChangeListener;
053: import junit.framework.*;
054: import java.io.File;
055: import java.net.URL;
056: import java.util.logging.Handler;
057: import java.util.logging.LogRecord;
058: import java.util.logging.Logger;
059: import org.netbeans.api.java.platform.JavaPlatform;
060: import org.netbeans.api.java.queries.JavadocForBinaryQuery;
061: import org.netbeans.api.project.ProjectManager;
062: import org.netbeans.api.queries.FileBuiltQuery;
063: import org.netbeans.junit.NbTestCase;
064: import org.netbeans.modules.java.platform.JavaPlatformProvider;
065: import org.netbeans.modules.mobility.cldcplatform.J2MEPlatform;
066: import org.netbeans.modules.mobility.project.J2MEActionProvider;
067: import org.netbeans.modules.mobility.project.J2MEProject;
068: import org.netbeans.modules.mobility.project.J2MEProjectGenerator;
069: import org.netbeans.modules.mobility.project.TestUtil;
070: import org.netbeans.spi.project.support.ant.AntProjectHelper;
071: import org.netbeans.spi.project.support.ant.EditableProperties;
072: import org.openide.filesystems.FileObject;
073: import org.openide.filesystems.FileUtil;
074: import org.openide.loaders.DataObject;
075: import org.openide.util.Exceptions;
076: import org.openide.util.Lookup;
077: import org.openide.util.lookup.Lookups;
078:
079: /**
080: *
081: * @author lukas
082: */
083: public class FileBuiltQueryImplTest extends NbTestCase {
084: static AntProjectHelper aph = null;
085: static FileObject projDir = null;
086:
087: static class MyProvider implements JavaPlatformProvider {
088: final static J2MEPlatform.Device devices[];
089: final static J2MEPlatform plat;
090:
091: static {
092: devices = new J2MEPlatform.Device[] { new J2MEPlatform.Device(
093: "d1", "d2", null, new J2MEPlatform.J2MEProfile[0],
094: null) };
095: plat = new J2MEPlatform("n1", "cp", "t1", "d1", null, null,
096: null, null, null, devices);
097: }
098:
099: public JavaPlatform[] getInstalledPlatforms() {
100: return new JavaPlatform[] { plat };
101: }
102:
103: public JavaPlatform getDefaultPlatform() {
104: return plat;
105: }
106:
107: public void addPropertyChangeListener(
108: PropertyChangeListener listener) {
109: }
110:
111: public void removePropertyChangeListener(
112: PropertyChangeListener listener) {
113: }
114: }
115:
116: static final Object syncObj = new Object();
117: static {
118: TestUtil
119: .setLookup(
120: new Object[] {
121: TestUtil.testProjectFactory(),
122: TestUtil.testFileLocator(),
123: TestUtil.testProjectChooserFactory(),
124: TestUtil
125: .testLogger("J2MEActionProvider.COMMAND_COMPILE_SINGLE"),
126: new MyProvider() },
127: FileBuiltQueryImplTest.class.getClassLoader());
128:
129: Logger.getLogger("org.openide.util.RequestProcessor")
130: .addHandler(new Handler() {
131: public void publish(LogRecord record) {
132: String s = record.getMessage();
133: if (s == null)
134: return;
135: if (s.startsWith("Work finished")
136: && s.indexOf("J2MEProject$6") != -1
137: && s.indexOf("RequestProcessor") != -1) {
138: synchronized (syncObj) {
139: syncObj.notify();
140: }
141: }
142: }
143:
144: public void flush() {
145: }
146:
147: public void close() throws SecurityException {
148: }
149: });
150: }
151:
152: void waitFinished() {
153: while (true) {
154: try {
155: syncObj.wait();
156: break;
157: } catch (InterruptedException ex) {
158: Exceptions.printStackTrace(ex);
159: }
160: }
161: }
162:
163: public FileBuiltQueryImplTest(String testName) {
164: super (testName);
165: }
166:
167: protected void setUp() throws Exception {
168: clearWorkDir();
169: TestUtil.setEnv();
170: File wtkdemo = getGoldenFile("WTKDemo");
171: projDir = FileUtil.toFileObject(getWorkDir()).createFolder(
172: "Demo");
173: TestUtil.cpDir(FileUtil.toFileObject(wtkdemo), projDir);
174:
175: synchronized (syncObj) {
176: aph = J2MEProjectGenerator.createProjectFromWtkProject(
177: FileUtil.toFile(projDir), "WTKDemo", null, FileUtil
178: .toFile(projDir).getAbsolutePath());
179: waitFinished();
180: }
181: assertNotNull(aph);
182: TestUtil.setHelper(aph);
183: File build = File.createTempFile("build", ".properties",
184: FileUtil.toFile(projDir));
185: System.setProperty("user.properties.file", build
186: .getAbsolutePath());
187: }
188:
189: protected void tearDown() throws Exception {
190: }
191:
192: public static Test suite() {
193: TestSuite suite = new TestSuite(FileBuiltQueryImplTest.class);
194:
195: return suite;
196: }
197:
198: /**
199: * Test of JavadocForBinaryQueryImpl class
200: */
201: public void testJavadocForBinaryQuery() throws Exception {
202: System.out.println("getStatus");
203: J2MEProject p = (J2MEProject) ProjectManager.getDefault()
204: .findProject(projDir);
205: JavadocForBinaryQueryImpl instance = p.getLookup().lookup(
206: JavadocForBinaryQueryImpl.class);
207: assertNotNull(instance);
208: EditableProperties ep = aph
209: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
210: projDir.getParent().createFolder("doc2");
211: ep.setProperty("libs.classpath", "../doc2");
212: aph.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
213:
214: JavadocForBinaryQuery.Result result = instance
215: .findJavadoc(projDir.getURL());
216: assertNotNull(result);
217: URL roots[] = result.getRoots();
218: assertNotNull(roots);
219: assertTrue(roots.length == 0);
220:
221: result = instance.findJavadoc(aph.resolveFile("dist").toURL());
222: assertNotNull(result);
223: roots = result.getRoots();
224: assertNotNull(roots);
225: assertTrue(roots.length > 0);
226:
227: assertEquals(roots[0].getFile(), projDir.getURL().getFile()
228: + "dist/doc/");
229:
230: //To cover property changes listener
231: ChangeListener list = new ChangeListener() {
232: public void stateChanged(ChangeEvent e) {
233: }
234: };
235: result.addChangeListener(list);
236:
237: ep = aph
238: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
239: ep.setProperty("libs.classpath", "../doc");
240: aph.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
241:
242: result.removeChangeListener(list);
243: }
244:
245: /**
246: * Test of getStatus method, of class org.netbeans.modules.mobility.project.queries.FileBuiltQueryImpl.
247: */
248: public void testGetStatus() throws Exception {
249: System.out.println("getStatus");
250: J2MEProject p = (J2MEProject) ProjectManager.getDefault()
251: .findProject(projDir);
252: FileBuiltQueryImpl instance = p.getLookup().lookup(
253: FileBuiltQueryImpl.class);
254: assertNotNull(instance);
255: FileObject fo = projDir.getFileObject("src/hello/Midlet.java");
256: FileBuiltQuery.Status result = instance.getStatus(projDir);
257: assertNull(result);
258: result = instance.getStatus(fo);
259: assertNotNull(result);
260: //To cover property changes listener
261: ChangeListener list = new ChangeListener() {
262: public void stateChanged(ChangeEvent e) {
263: }
264: };
265: result.addChangeListener(list);
266: boolean res = result.isBuilt();
267: assertFalse(res);
268: J2MEActionProvider actionProvider = p.getLookup().lookup(
269: J2MEActionProvider.class);
270: DataObject dobj = DataObject.find(fo);
271: assertNotNull(dobj);
272: Lookup context = Lookups.fixed(new DataObject[] { dobj });
273: String ret = null;
274: synchronized (TestUtil.rootStr) {
275: actionProvider.invokeAction(
276: J2MEActionProvider.COMMAND_COMPILE_SINGLE, context);
277: ret = TestUtil.waitFinished();
278: }
279: //Check for the build exception
280: assertNull(ret, ret);
281:
282: EditableProperties ep = aph
283: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
284: ep.setProperty("src.dir", "src2");
285: aph.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
286:
287: //And now extend test coverage
288: res = result.isBuilt();
289: assertTrue(res);
290: String s = result.toString();
291: assertNotNull(s);
292: assertTrue(s.indexOf(dobj.getName()) != -1);
293: dobj.rename("NewMidlet.java");
294: result = instance.getStatus(dobj.getPrimaryFile());
295: assertNotNull(result);
296: res = result.isBuilt();
297: assertFalse(res);
298: //Let the RequestProcessor finish its tasks
299: Thread.sleep(10);
300: dobj.delete();
301:
302: result.removeChangeListener(list);
303: }
304: }
|