This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 53056
Collapse All | Expand All

(-)java/j2seproject/src/org/netbeans/modules/java/j2seproject/classpath/ClassPathProviderImpl.java (-7 / +17 lines)
Lines 7-25 Link Here
7
 * http://www.sun.com/
7
 * http://www.sun.com/
8
 *
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
12
 */
13
package org.netbeans.modules.java.j2seproject.classpath;
13
package org.netbeans.modules.java.j2seproject.classpath;
14
14
15
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeEvent;
16
import java.beans.PropertyChangeListener;
16
import java.beans.PropertyChangeListener;
17
import java.io.File;
17
import java.util.Map;
18
import java.util.Map;
18
import java.util.HashMap;
19
import java.util.HashMap;
19
20
20
import org.netbeans.api.java.classpath.ClassPath;
21
import org.netbeans.api.java.classpath.ClassPath;
21
import org.netbeans.spi.java.classpath.ClassPathFactory;
22
import org.netbeans.spi.java.classpath.ClassPathFactory;
22
import org.netbeans.spi.java.classpath.ClassPathProvider;
23
import org.netbeans.spi.java.classpath.ClassPathProvider;
24
import org.netbeans.spi.java.project.classpath.support.ProjectClassPathSupport;
23
import org.netbeans.spi.project.support.ant.AntProjectHelper;
25
import org.netbeans.spi.project.support.ant.AntProjectHelper;
24
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
26
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
25
import org.netbeans.modules.java.j2seproject.SourceRoots;
27
import org.netbeans.modules.java.j2seproject.SourceRoots;
Lines 37-42 Link Here
37
    private static final String BUILD_TEST_CLASSES_DIR = "build.test.classes.dir"; // NOI18N
39
    private static final String BUILD_TEST_CLASSES_DIR = "build.test.classes.dir"; // NOI18N
38
    
40
    
39
    private final AntProjectHelper helper;
41
    private final AntProjectHelper helper;
42
    private final File projectDirectory;
40
    private final PropertyEvaluator evaluator;
43
    private final PropertyEvaluator evaluator;
41
    private final SourceRoots sourceRoots;
44
    private final SourceRoots sourceRoots;
42
    private final SourceRoots testSourceRoots;
45
    private final SourceRoots testSourceRoots;
Lines 47-52 Link Here
47
    public ClassPathProviderImpl(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots,
50
    public ClassPathProviderImpl(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots,
48
                                 SourceRoots testSourceRoots) {
51
                                 SourceRoots testSourceRoots) {
49
        this.helper = helper;
52
        this.helper = helper;
53
        this.projectDirectory = FileUtil.toFile(helper.getProjectDirectory());
54
        assert this.projectDirectory != null;
50
        this.evaluator = evaluator;
55
        this.evaluator = evaluator;
51
        this.sourceRoots = sourceRoots;
56
        this.sourceRoots = sourceRoots;
52
        this.testSourceRoots = testSourceRoots;
57
        this.testSourceRoots = testSourceRoots;
Lines 140-153 Link Here
140
            return null;
145
            return null;
141
        }
146
        }
142
        ClassPath cp = cache[2+type];
147
        ClassPath cp = cache[2+type];
143
        if ( cp == null) {
148
        if ( cp == null) {            
144
            if (type == 0) {
149
            if (type == 0) {
145
                cp = ClassPathFactory.createClassPath(
150
                cp = ClassPathFactory.createClassPath(
146
                new ProjectClassPathImplementation(helper, "javac.classpath", evaluator)); // NOI18N
151
                    ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
152
                    projectDirectory, evaluator, new String[] {"javac.classpath"})); // NOI18N
147
            }
153
            }
148
            else {
154
            else {
149
                cp = ClassPathFactory.createClassPath(
155
                cp = ClassPathFactory.createClassPath(
150
                new ProjectClassPathImplementation(helper, "javac.test.classpath", evaluator)); // NOI18N
156
                    ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
157
                    projectDirectory, evaluator, new String[] {"javac.test.classpath"})); // NOI18N
151
            }
158
            }
152
            cache[2+type] = cp;
159
            cache[2+type] = cp;
153
        }
160
        }
Lines 169-185 Link Here
169
        if ( cp == null) {
176
        if ( cp == null) {
170
            if (type == 0) {
177
            if (type == 0) {
171
                cp = ClassPathFactory.createClassPath(
178
                cp = ClassPathFactory.createClassPath(
172
                new ProjectClassPathImplementation(helper, "run.classpath", evaluator)); // NOI18N
179
                    ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
180
                    projectDirectory, evaluator, new String[] {"run.classpath"})); // NOI18N
173
            }
181
            }
174
            else if (type == 1) {
182
            else if (type == 1) {
175
                cp = ClassPathFactory.createClassPath(
183
                cp = ClassPathFactory.createClassPath(
176
                new ProjectClassPathImplementation(helper, "run.test.classpath", evaluator)); // NOI18N
184
                    ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
185
                    projectDirectory, evaluator, new String[] {"run.test.classpath"})); // NOI18N
177
            }
186
            }
178
            else if (type == 2) {
187
            else if (type == 2) {
179
                //Only to make the CompiledDataNode hapy
188
                //Only to make the CompiledDataNode hapy
180
                //Todo: Strictly it should return ${run.classpath} - ${build.classes.dir} + ${dist.jar}
189
                //Todo: Strictly it should return ${run.classpath} - ${build.classes.dir} + ${dist.jar}
181
                cp = ClassPathFactory.createClassPath(
190
                cp = ClassPathFactory.createClassPath(
182
                new ProjectClassPathImplementation(helper, DIST_JAR, evaluator)); // NOI18N
191
                    ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
192
                    projectDirectory, evaluator, new String[] {DIST_JAR})); // NOI18N
183
            }
193
            }
184
            cache[4+type] = cp;
194
            cache[4+type] = cp;
185
        }
195
        }
(-)java/j2seproject/src/org/netbeans/modules/java/j2seproject/classpath/ProjectClassPathImplementation.java (-132 lines)
Removed Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.modules.java.j2seproject.classpath;
15
16
import java.beans.PropertyChangeEvent;
17
import java.beans.PropertyChangeListener;
18
import java.beans.PropertyChangeSupport;
19
import java.io.File;
20
import java.net.MalformedURLException;
21
import java.net.URL;
22
import java.util.ArrayList;
23
import java.util.Collections;
24
import java.util.List;
25
import org.netbeans.api.project.ProjectManager;
26
import org.netbeans.spi.java.classpath.ClassPathImplementation;
27
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
28
import org.netbeans.spi.project.support.ant.AntProjectHelper;
29
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
30
import org.netbeans.spi.project.support.ant.PropertyUtils;
31
import org.openide.filesystems.FileUtil;
32
import org.openide.util.WeakListeners;
33
34
// XXX needs unit test!
35
36
/**
37
 * Implementation of a single classpath that is derived from one Ant property.
38
 */
39
final class ProjectClassPathImplementation implements ClassPathImplementation, PropertyChangeListener, Runnable {
40
    
41
    private PropertyChangeSupport support = new PropertyChangeSupport(this);
42
    private AntProjectHelper helper;
43
    private String propertyName;
44
    private List resources;
45
    private final PropertyEvaluator evaluator;
46
    private boolean dirty = false;
47
48
    /**
49
     * Construct the implementation.
50
     * @param helper an Ant project, used to resolve file paths
51
     * @param propertyName the name of an Ant property which will supply the classpath
52
     * @param evaluator a property evaluator used to find the value of the classpath
53
     */
54
    public ProjectClassPathImplementation(AntProjectHelper helper, String propertyName, PropertyEvaluator evaluator) {
55
        assert helper != null && propertyName != null;
56
        this.helper = helper;
57
        this.evaluator = evaluator;
58
        this.propertyName = propertyName;
59
        evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
60
    }
61
62
    public synchronized List /*<PathResourceImplementation>*/ getResources() {
63
        if (this.resources == null) {
64
            this.resources = this.getPath ();
65
        }
66
        return this.resources;
67
    }
68
69
    public void addPropertyChangeListener(PropertyChangeListener listener) {
70
        support.addPropertyChangeListener (listener);
71
    }
72
73
    public void removePropertyChangeListener(PropertyChangeListener listener) {
74
        support.removePropertyChangeListener (listener);
75
    }
76
77
78
    public void propertyChange(PropertyChangeEvent evt) {
79
        if (!evt.getPropertyName().equals(propertyName)) {
80
            // Not interesting to us.
81
            return;
82
        }
83
        // Coalesce changes; can come in fast after huge CP changes (#47910):
84
        if (!dirty) {
85
            dirty = true;
86
            ProjectManager.mutex().postReadRequest(this);
87
        }
88
    }
89
    
90
    public void run() {
91
        dirty = false;
92
        List newRoots = getPath ();
93
        boolean fire = false;
94
        synchronized (this) {
95
            if (this.resources != null && !this.resources.equals(newRoots)) {
96
                this.resources = newRoots;
97
                fire = true;
98
            }
99
        }
100
        if (fire) {
101
            support.firePropertyChange (PROP_RESOURCES,null,null);
102
        }
103
    }
104
    
105
    private List getPath() {
106
        List result = new ArrayList ();
107
        String prop = evaluator.getProperty(propertyName);
108
        if (prop != null) {
109
            String[] pieces = PropertyUtils.tokenizePath(prop);
110
            for (int i = 0; i < pieces.length; i++) {
111
                File f = helper.resolveFile(pieces[i]);
112
                try {
113
                    URL entry = f.toURI().toURL();
114
                    if (FileUtil.isArchiveFile(entry) || (f.isFile() && f.length()<4)) {    //XXX: Not yet closed archive file
115
                        entry = FileUtil.getArchiveRoot(entry);
116
                    } else if (!f.exists()) {
117
                        // if file does not exist (e.g. build/classes folder
118
                        // was not created yet) then corresponding File will
119
                        // not be ended with slash. Fix that.
120
                        assert !entry.toExternalForm().endsWith("/") : f; // NOI18N
121
                        entry = new URL(entry.toExternalForm() + "/"); // NOI18N
122
                    }
123
                    result.add(ClassPathSupport.createResource(entry));
124
                } catch (MalformedURLException mue) {
125
                    assert false : mue;
126
                }
127
            }
128
        }
129
        return Collections.unmodifiableList(result);
130
    }
131
132
}
(-)java/project/apichanges.xml (-1 / +19 lines)
Lines 75-81 Link Here
75
75
76
    <!-- ACTUAL CHANGES BEGIN HERE: -->
76
    <!-- ACTUAL CHANGES BEGIN HERE: -->
77
77
78
    <changes>
78
79
         <change>
80
            <api name="classpath"/>
81
            <summary>Added helper method for creating ClassPathImplementation based on the ant property</summary>
82
            <version major="1" minor="3"/>
83
            <date day="6" month="1" year="2005"/>
84
            <author login="tzezula"/>
85
            <compatibility addition="yes"/>
86
            <description>
87
                <p>
88
                Added new helper class org.netbeans.spi.java.project.classpath.support.ProjectClassPathSupport
89
                with a static method createPropertyBasedClassPathImplementation (File, PropertyEvaluator, String[]).
90
                This method creates a live classpath based on the list of Ant properties holding the path. The classpath
91
                implementation listens on changes of properties, updates state and fires events to clients.
92
                </p>
93
            </description>
94
            <class package="org.netbeans.spi.java.project.classpath.support" name="ProjectClassPathSupport"/>
95
            <issue number="42309"/>
96
        </change>    
79
        <change>
97
        <change>
80
            <api name="classpath"/>
98
            <api name="classpath"/>
81
            <summary>The ProjectClassPathExtender was moved into public SPI package.</summary>
99
            <summary>The ProjectClassPathExtender was moved into public SPI package.</summary>
(-)java/project/arch.xml (+6 lines)
Lines 57-62 Link Here
57
    classpath of a J2SE project automatically.
57
    classpath of a J2SE project automatically.
58
   </p>
58
   </p>
59
  </api>
59
  </api>
60
  <api group="java" name="ProjectClassPathSupport" type="export" category="devel">
61
   <p>
62
    Support class containg helper method for creating ClassPathImplementation based
63
    on the Ant properies.
64
   </p>
65
  </api>
60
   </li>
66
   </li>
61
  </ul>
67
  </ul>
62
 </answer>
68
 </answer>
(-)java/project/nbproject/project.properties (-2 / +5 lines)
Lines 17-27 Link Here
17
javadoc.arch=${basedir}/arch.xml
17
javadoc.arch=${basedir}/arch.xml
18
javadoc.apichanges=${basedir}/apichanges.xml
18
javadoc.apichanges=${basedir}/apichanges.xml
19
19
20
test.unit.cp.extra=${nb_all}/projects/projectapi/build/test/unit/classes
20
test.unit.cp.extra=${nb_all}/projects/projectapi/build/test/unit/classes:\
21
    ${nb_all}/ant/project/build/test/unit/classes
21
# masterfs needed for the usual reasons; tools.jar needed for Ant's <javac> to work
22
# masterfs needed for the usual reasons; tools.jar needed for Ant's <javac> to work
22
test.unit.run.cp.extra=\
23
test.unit.run.cp.extra=\
23
    ${openide/masterfs.dir}/${nb.modules.dir}/org-netbeans-modules-masterfs.jar:\
24
    ${openide/masterfs.dir}/${nb.modules.dir}/org-netbeans-modules-masterfs.jar:\
24
    ${jdkhome}/lib/tools.jar
25
    ${jdkhome}/lib/tools.jar:\
26
    ${diff.dir}/${nb.modules.dir}/org-netbeans-modules-diff.jar:\
27
    ${libs/xerces.dir}/${nb.modules/autoload.dir}/ext/xerces-2.6.2.jar
25
test-unit-sys-prop.test.data.dir=${nb_all}/java/project/test/unit/data
28
test-unit-sys-prop.test.data.dir=${nb_all}/java/project/test/unit/data
26
test-unit-sys-prop.test.bridge.jar=${ant.dir}/ant/nblib/bridge.jar
29
test-unit-sys-prop.test.bridge.jar=${ant.dir}/ant/nblib/bridge.jar
27
# May be overridden to e.g. test against a different version of Ant:
30
# May be overridden to e.g. test against a different version of Ant:
(-)java/project/nbproject/project.xml (+1 lines)
Lines 135-140 Link Here
135
                <package>org.netbeans.spi.java.project.support.ui</package>
135
                <package>org.netbeans.spi.java.project.support.ui</package>
136
                <package>org.netbeans.spi.java.project.support.ui.templates</package>
136
                <package>org.netbeans.spi.java.project.support.ui.templates</package>
137
                <package>org.netbeans.spi.java.project.classpath</package>
137
                <package>org.netbeans.spi.java.project.classpath</package>
138
                <package>org.netbeans.spi.java.project.classpath.support</package>
138
            </public-packages>
139
            </public-packages>
139
            <javadoc/>
140
            <javadoc/>
140
        </data>
141
        </data>
(-)java/project/src/org/netbeans/spi/java/project/classpath/support/ProjectClassPathImplementation.java (+137 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.spi.java.project.classpath.support;
15
16
import java.beans.PropertyChangeEvent;
17
import java.beans.PropertyChangeListener;
18
import java.beans.PropertyChangeSupport;
19
import java.io.File;
20
import java.net.MalformedURLException;
21
import java.net.URL;
22
import java.util.ArrayList;
23
import java.util.Arrays;
24
import java.util.Collections;
25
import java.util.HashSet;
26
import java.util.List;
27
import java.util.Set;
28
import org.netbeans.api.project.ProjectManager;
29
import org.netbeans.spi.java.classpath.ClassPathImplementation;
30
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
31
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
32
import org.netbeans.spi.project.support.ant.PropertyUtils;
33
import org.openide.filesystems.FileUtil;
34
import org.openide.util.WeakListeners;
35
36
37
/**
38
 * Implementation of a single classpath that is derived from list of Ant properties.
39
 */
40
final class ProjectClassPathImplementation implements ClassPathImplementation, PropertyChangeListener, Runnable {
41
    
42
    private final PropertyChangeSupport support = new PropertyChangeSupport(this);
43
    private final File projectFolder;
44
    private final String[] propertyNames;
45
    private List resources;
46
    private final PropertyEvaluator evaluator;
47
    private boolean dirty = false;
48
    private final Set/*<String>*/ propertyNamesSet = new HashSet ();
49
50
    /**
51
     * Construct the implementation.
52
     * @param projectFolder the folder containing the project, used to resolve relative paths
53
     * @param propertyNames the names of an Ant properties which will supply the classpath
54
     * @param evaluator a property evaluator used to find the value of the classpath
55
     */
56
    public ProjectClassPathImplementation(File projectFolder, String[] propertyNames, PropertyEvaluator evaluator) {
57
        assert projectFolder != null && propertyNames != null && evaluator != null;
58
        this.projectFolder = projectFolder;
59
        this.evaluator = evaluator;
60
        this.propertyNames = propertyNames;
61
        this.propertyNamesSet.addAll(Arrays.asList(propertyNames));
62
        evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
63
    }
64
65
    public synchronized List /*<PathResourceImplementation>*/ getResources() {
66
        if (this.resources == null) {
67
            this.resources = this.getPath ();
68
        }
69
        return this.resources;
70
    }
71
72
    public void addPropertyChangeListener(PropertyChangeListener listener) {
73
        support.addPropertyChangeListener (listener);
74
    }
75
76
    public void removePropertyChangeListener(PropertyChangeListener listener) {
77
        support.removePropertyChangeListener (listener);
78
    }
79
80
81
    public void propertyChange(PropertyChangeEvent evt) {        
82
        if (!this.propertyNamesSet.contains(evt.getPropertyName())) {
83
            // Not interesting to us.
84
            return;
85
        }
86
        // Coalesce changes; can come in fast after huge CP changes (#47910):
87
        if (!dirty) {
88
            dirty = true;
89
            ProjectManager.mutex().postReadRequest(this);
90
        }
91
    }
92
    
93
    public void run() {
94
        dirty = false;
95
        List newRoots = getPath ();
96
        boolean fire = false;
97
        synchronized (this) {
98
            if (this.resources != null && !this.resources.equals(newRoots)) {
99
                this.resources = newRoots;
100
                fire = true;
101
            }
102
        }
103
        if (fire) {
104
            support.firePropertyChange (PROP_RESOURCES,null,null);
105
        }
106
    }
107
    
108
    private List getPath() {
109
        List result = new ArrayList ();
110
        for (int j=0; j< propertyNames.length; j++) {
111
            String prop = evaluator.getProperty(propertyNames[j]);
112
            if (prop != null) {
113
                String[] pieces = PropertyUtils.tokenizePath(prop);
114
                for (int i = 0; i < pieces.length; i++) {                    
115
                    File f = PropertyUtils.resolveFile(this.projectFolder, pieces[i]); 
116
                    try {
117
                        URL entry = f.toURI().toURL();
118
                        if (FileUtil.isArchiveFile(entry) || (f.isFile() && f.length()<4)) {    //XXX: Not yet closed archive file
119
                            entry = FileUtil.getArchiveRoot(entry);
120
                        } else if (!f.exists()) {
121
                            // if file does not exist (e.g. build/classes folder
122
                            // was not created yet) then corresponding File will
123
                            // not be ended with slash. Fix that.
124
                            assert !entry.toExternalForm().endsWith("/") : f; // NOI18N
125
                            entry = new URL(entry.toExternalForm() + "/"); // NOI18N
126
                        }
127
                        result.add(ClassPathSupport.createResource(entry));
128
                    } catch (MalformedURLException mue) {
129
                        assert false : mue;
130
                    }
131
                }
132
            }
133
        }
134
        return Collections.unmodifiableList(result);
135
    }
136
137
}
(-)java/project/src/org/netbeans/spi/java/project/classpath/support/ProjectClassPathSupport.java (+48 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
package org.netbeans.spi.java.project.classpath.support;
14
15
import java.io.File;
16
import org.netbeans.spi.java.classpath.ClassPathImplementation;
17
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
18
19
/**
20
 * ProjectClassPathSupport is a support class for creating classpath based
21
 * on the list of ant properties.
22
 * @since org.netbeans.modules.java.project/1 1.3 
23
 * @author Tomas Zezula
24
 */
25
public class ProjectClassPathSupport {
26
        
27
    /** Creates a new instance of NewClass */
28
    private ProjectClassPathSupport() {
29
    }
30
    
31
    
32
    /**
33
     * Creates new classpath based on the ant property. The returned classpath
34
     * listens on changes of property value.
35
     * @param projectFolder {@link File} the project folder used to resolve relative paths
36
     * @param evaluator {@link PropertyEvaluator} used for obtaining the value of
37
     * given property and listening on value changes.
38
     * @param propertyNames the names of ant properties the classpath will be build on,
39
     * can't be or contain null. It can contain duplicates, in this case the duplicated property
40
     * is used multiple times. The values of given properties are concatenated into a single path.
41
     * @return an {@link ClassPathImplementation} based on the given ant properties.
42
     */
43
    public static ClassPathImplementation createPropertyBasedClassPathImplementation (File projectFolder,
44
            PropertyEvaluator evaluator, String[] propertyNames) {
45
        return new ProjectClassPathImplementation (projectFolder, propertyNames, evaluator);
46
    }
47
    
48
}
(-)java/project/test/unit/src/org/netbeans/spi/java/project/classpath/support/ProjectClassPathImplementationTest.java (+153 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.spi.java.project.classpath.support;
15
16
17
import java.io.File;
18
import java.io.IOException;
19
import java.net.URL;
20
import java.util.ArrayList;
21
import java.util.Arrays;
22
import java.util.Collection;
23
import java.util.Collections;
24
import java.util.List;
25
import java.util.Map;
26
import java.util.Properties;
27
import org.netbeans.api.java.classpath.ClassPath;
28
import org.netbeans.api.project.Project;
29
import org.netbeans.api.project.ProjectManager;
30
import org.netbeans.junit.NbTestCase;
31
import org.netbeans.spi.java.classpath.ClassPathFactory;
32
import org.netbeans.spi.java.classpath.ClassPathImplementation;
33
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
34
import org.netbeans.spi.project.support.ant.AntBasedTestUtil;
35
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
36
import org.netbeans.spi.project.support.ant.PropertyUtils;
37
import org.openide.filesystems.FileObject;
38
import org.netbeans.api.project.TestUtil;
39
import org.netbeans.spi.project.support.ant.AntProjectHelper;
40
import org.netbeans.spi.project.support.ant.EditableProperties;
41
import org.netbeans.spi.project.support.ant.ProjectGenerator;
42
import org.openide.filesystems.FileUtil;
43
import org.openide.filesystems.Repository;
44
import org.openide.util.Lookup;
45
46
/**
47
 * Tests for {@link ProjectClassPathImplementation}.
48
 * @author Tomas Zezula
49
 */
50
public class ProjectClassPathImplementationTest extends NbTestCase {
51
    
52
    private static final String PROP_NAME_1 = "classpath1"; //NOI18N
53
    private static final String PROP_NAME_2 = "classpath2"; //NOI18N
54
    
55
    public ProjectClassPathImplementationTest(String testName) {
56
        super(testName);
57
    }
58
    
59
    private FileObject scratch;
60
    private FileObject projdir;
61
    private FileObject[] cpRoots1;
62
    private FileObject[] cpRoots2;
63
    private AntProjectHelper helper;
64
    private PropertyEvaluator evaluator;
65
    
66
    protected void setUp() throws Exception {
67
        super.setUp();
68
        TestUtil.setLookup(new Object[] {
69
            new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(),
70
            AntBasedTestUtil.testAntBasedProjectType(),
71
        }, ProjectClassPathImplementation.class.getClassLoader());
72
    }
73
74
    protected void tearDown() throws Exception {
75
        scratch = null;
76
        projdir = null;
77
        cpRoots1 = null;
78
        cpRoots2 = null;
79
        helper = null;
80
        evaluator = null;
81
        TestUtil.setLookup(Lookup.EMPTY);
82
        super.tearDown();
83
    }
84
    
85
    
86
    private void prepareProject () throws IOException {
87
        scratch = TestUtil.makeScratchDir(this);
88
        projdir = scratch.createFolder("proj"); //NOI18N
89
        cpRoots1 = new FileObject[2];
90
        cpRoots1[0] = scratch.createFolder("cpRoot1"); //NOI18N
91
        cpRoots1[1] = scratch.createFolder("cpRoot2"); //NOI18N
92
        cpRoots2 = new FileObject[2];
93
        cpRoots2[0] = scratch.createFolder("cpRoot3"); //NOI18N
94
        cpRoots2[1] = scratch.createFolder("cpRoot4"); //NOI18N
95
        helper = ProjectGenerator.createProject(projdir, "test"); //NOI18N                
96
        evaluator = helper.getStandardPropertyEvaluator();
97
        setClassPath(new String[] {PROP_NAME_1, PROP_NAME_2}, new FileObject[][] {cpRoots1, cpRoots2});
98
    }
99
    
100
    public void testBootClassPathImplementation () throws Exception {
101
        prepareProject();
102
        ClassPathImplementation cpImpl = ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
103
                FileUtil.toFile(helper.getProjectDirectory()), evaluator, new String[] {PROP_NAME_1, PROP_NAME_2});
104
        ClassPath cp = ClassPathFactory.createClassPath(cpImpl);
105
        FileObject[] fo = cp.getRoots();
106
        List expected = new ArrayList ();
107
        expected.addAll(Arrays.asList(cpRoots1));
108
        expected.addAll(Arrays.asList(cpRoots2));
109
        assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo));   //NOI18N
110
        cpRoots1 = new FileObject[] {cpRoots1[0]};
111
        setClassPath(new String[] {PROP_NAME_1}, new FileObject[][]{cpRoots1});
112
        fo = cp.getRoots();
113
        expected = new ArrayList ();
114
        expected.addAll(Arrays.asList(cpRoots1));
115
        expected.addAll(Arrays.asList(cpRoots2));
116
        assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo));   //NOI18N
117
        cpRoots2 = new FileObject[] {cpRoots2[0]};
118
        setClassPath(new String[] {PROP_NAME_2}, new FileObject[][]{cpRoots2});
119
        fo = cp.getRoots();
120
        expected = new ArrayList ();
121
        expected.addAll(Arrays.asList(cpRoots1));
122
        expected.addAll(Arrays.asList(cpRoots2));
123
        assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo));   //NOI18N
124
    }        
125
    
126
    
127
    
128
    private void setClassPath (String[] propNames, FileObject[][] cpRoots) {
129
        EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
130
        for (int i=0; i< propNames.length; i++) {
131
            props.setProperty (propNames[i],toPath(cpRoots[i]));
132
        }                
133
        helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
134
    }
135
    
136
    
137
    private static String toPath (FileObject[] cpRoots) {
138
        StringBuffer result = new StringBuffer ();
139
        for (int i=0; i<cpRoots.length; i++) {
140
            if (i>0) {
141
                result.append(':'); //NOI18N
142
            }
143
            File f = FileUtil.toFile (cpRoots[i]);
144
            result.append (f.getAbsolutePath());
145
        }
146
        return result.toString();
147
    }
148
    
149
    
150
    
151
    
152
    
153
}
(-)ide/golden/public-packages.txt (+1 lines)
Lines 231-236 Link Here
231
org.netbeans.spi.java.classpath.support
231
org.netbeans.spi.java.classpath.support
232
org.netbeans.spi.java.platform
232
org.netbeans.spi.java.platform
233
org.netbeans.spi.java.project.classpath
233
org.netbeans.spi.java.project.classpath
234
org.netbeans.spi.java.project.classpath.support
234
org.netbeans.spi.java.project.support.ui
235
org.netbeans.spi.java.project.support.ui
235
org.netbeans.spi.java.project.support.ui.templates
236
org.netbeans.spi.java.project.support.ui.templates
236
org.netbeans.spi.java.queries
237
org.netbeans.spi.java.queries

Return to bug 53056