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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.java.api.common.queries;
041:
042: import java.io.File;
043: import java.net.URL;
044: import org.netbeans.api.java.queries.JavadocForBinaryQuery.Result;
045: import org.netbeans.api.project.Project;
046: import org.netbeans.api.project.ProjectManager;
047: import org.netbeans.junit.NbTestCase;
048: import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation;
049: import org.netbeans.spi.project.support.ant.AntBasedTestUtil;
050: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
051: import org.openide.filesystems.FileObject;
052: import org.netbeans.spi.project.support.ant.AntProjectHelper;
053: import org.netbeans.spi.project.support.ant.EditableProperties;
054: import org.netbeans.spi.project.support.ant.ProjectGenerator;
055: import org.openide.filesystems.FileStateInvalidException;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.util.Mutex;
058: import org.openide.util.test.MockLookup;
059:
060: /**
061: * Tests for {@link JavadocForBinaryQueryImpl}.
062: *
063: * @author Tomas Mysik
064: */
065: public class JavadocForBinaryQueryImplTest extends NbTestCase {
066:
067: private FileObject projdir;
068: private AntProjectHelper helper;
069: private PropertyEvaluator eval;
070: private Project prj;
071: private FileObject builddir;
072: private static final String BUILD_CLASSES_DIR = "build.classes.dir";
073: private static final String JAVADOC_DIR = "dist.javadoc.dir";
074:
075: private static final String JAVADOC_1 = "javadoc1";
076: private static final String JAVADOC_2 = "javadoc2";
077:
078: public JavadocForBinaryQueryImplTest(String testName) {
079: super (testName);
080: }
081:
082: @Override
083: protected void setUp() throws Exception {
084: MockLookup.setInstances(AntBasedTestUtil
085: .testAntBasedProjectType());
086: super .setUp();
087: this .clearWorkDir();
088: File wd = getWorkDir();
089: FileObject scratch = FileUtil.toFileObject(wd);
090: assertNotNull(wd);
091: projdir = scratch.createFolder("proj");
092: helper = ProjectGenerator.createProject(projdir, "test");
093: assertNotNull(helper);
094: eval = helper.getStandardPropertyEvaluator();
095: assertNotNull(eval);
096: prj = ProjectManager.getDefault().findProject(projdir);
097: assertNotNull(prj);
098: builddir = projdir.createFolder("build");
099: assertNotNull(builddir);
100: }
101:
102: public void testJavadocForBinaryQuery() throws Exception {
103: setProjectDirectory(BUILD_CLASSES_DIR, builddir);
104: JavadocForBinaryQueryImplementation javadocForBinaryQuery = QuerySupport
105: .createJavadocForBinaryQuery(helper, eval);
106:
107: setProjectDirectory(JAVADOC_DIR, projdir
108: .createFolder(JAVADOC_1));
109: Result javadoc = javadocForBinaryQuery.findJavadoc(builddir
110: .getURL());
111: assertNotNull(javadoc);
112:
113: URL[] roots = javadoc.getRoots();
114: assertEquals(1, roots.length);
115: assertEquals(getJavadocUrl(JAVADOC_1), roots[0]);
116:
117: // change javadoc directory
118: setProjectDirectory(JAVADOC_DIR, projdir
119: .createFolder(JAVADOC_2));
120: roots = javadoc.getRoots();
121: assertEquals(1, roots.length);
122: assertEquals(getJavadocUrl(JAVADOC_2), roots[0]);
123: }
124:
125: private URL getJavadocUrl(String javadoc)
126: throws FileStateInvalidException {
127: return projdir.getFileObject(javadoc).getURL();
128: }
129:
130: private void setProjectDirectory(final String property,
131: final FileObject directory) throws Exception {
132: assertTrue(directory.isFolder());
133: ProjectManager.mutex().writeAccess(
134: new Mutex.ExceptionAction<Void>() {
135: public Void run() throws Exception {
136: EditableProperties ep = helper
137: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
138: ep.setProperty(property, directory.getName());
139: helper
140: .putProperties(
141: AntProjectHelper.PROJECT_PROPERTIES_PATH,
142: ep);
143: ProjectManager.getDefault().saveProject(prj);
144: return null;
145: }
146: });
147: }
148: }
|