001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.project.libraries;
021:
022: import java.beans.PropertyChangeListener;
023: import java.beans.PropertyChangeSupport;
024: import java.io.IOException;
025: import java.net.MalformedURLException;
026: import java.net.URL;
027: import java.util.ArrayList;
028: import java.util.Arrays;
029: import java.util.HashMap;
030: import java.util.HashSet;
031: import java.util.List;
032: import java.util.Map;
033: import java.util.Set;
034: import java.util.regex.Matcher;
035: import java.util.regex.Pattern;
036: import org.netbeans.spi.project.libraries.ArealLibraryProvider;
037: import org.netbeans.spi.project.libraries.LibraryImplementation;
038: import org.netbeans.spi.project.libraries.LibraryProvider;
039: import org.netbeans.spi.project.libraries.LibraryStorageArea;
040: import org.netbeans.spi.project.libraries.support.LibrariesSupport;
041: import org.openide.util.WeakSet;
042:
043: /**
044: * Common support classes for unit tests in this module.
045: */
046: public class TestUtil {
047:
048: private TestUtil() {
049: }
050:
051: public static class NWLP implements
052: LibraryProvider<LibraryImplementation> {
053:
054: public final List<LibraryImplementation> libs = new ArrayList<LibraryImplementation>();
055: final PropertyChangeSupport pcs = new PropertyChangeSupport(
056: this );
057:
058: public void set(LibraryImplementation... nue) {
059: libs.clear();
060: libs.addAll(Arrays.asList(nue));
061: pcs.firePropertyChange(PROP_LIBRARIES, null, null);
062: }
063:
064: public LibraryImplementation[] getLibraries() {
065: return libs.toArray(new LibraryImplementation[0]);
066: }
067:
068: public void addPropertyChangeListener(
069: PropertyChangeListener listener) {
070: pcs.addPropertyChangeListener(listener);
071: }
072:
073: public void removePropertyChangeListener(
074: PropertyChangeListener listener) {
075: pcs.removePropertyChangeListener(listener);
076: }
077:
078: }
079:
080: public static final class WLP extends NWLP implements
081: WritableLibraryProvider<LibraryImplementation> {
082:
083: public void addLibrary(LibraryImplementation library)
084: throws IOException {
085: libs.add(library);
086: pcs.firePropertyChange(PROP_LIBRARIES, null, null);
087: }
088:
089: public void removeLibrary(LibraryImplementation library)
090: throws IOException {
091: libs.remove(library);
092: pcs.firePropertyChange(PROP_LIBRARIES, null, null);
093: }
094:
095: public void updateLibrary(LibraryImplementation oldLibrary,
096: LibraryImplementation newLibrary) throws IOException {
097: libs.remove(oldLibrary);
098: libs.add(newLibrary);
099: pcs.firePropertyChange(PROP_LIBRARIES, null, null);
100: }
101:
102: }
103:
104: public static final class Area implements LibraryStorageArea {
105:
106: final String id;
107:
108: public Area(String id) {
109: this .id = id;
110: }
111:
112: public URL getLocation() {
113: try {
114: return new URL("http://nowhere.net/" + id);
115: } catch (MalformedURLException x) {
116: throw new AssertionError(x);
117: }
118: }
119:
120: public String getDisplayName() {
121: return id;
122: }
123:
124: public boolean equals(Object obj) {
125: return obj instanceof Area && ((Area) obj).id.equals(id);
126: }
127:
128: public int hashCode() {
129: return id.hashCode();
130: }
131:
132: public @Override
133: String toString() {
134: return "Area[" + id + "]";
135: }
136:
137: }
138:
139: public static final class ALP implements
140: ArealLibraryProvider<Area, LibraryImplementation> {
141:
142: final PropertyChangeSupport pcs = new PropertyChangeSupport(
143: this );
144: public final Map<Area, List<LibraryImplementation>> libs = new HashMap<Area, List<LibraryImplementation>>();
145: final Set<LP> lps = new WeakSet<LP>();
146: final Set<Area> open = new HashSet<Area>();
147:
148: public void addPropertyChangeListener(
149: PropertyChangeListener listener) {
150: pcs.addPropertyChangeListener(listener);
151: }
152:
153: public void removePropertyChangeListener(
154: PropertyChangeListener listener) {
155: pcs.removePropertyChangeListener(listener);
156: }
157:
158: public Class<Area> areaType() {
159: return Area.class;
160: }
161:
162: public Class<LibraryImplementation> libraryType() {
163: return LibraryImplementation.class;
164: }
165:
166: public Area createArea() {
167: return new Area("new");
168: }
169:
170: public Area loadArea(URL location) {
171: Matcher m = Pattern.compile("http://nowhere\\.net/(.+)$")
172: .matcher(location.toExternalForm());
173: if (m.matches()) {
174: return new Area(m.group(1));
175: } else {
176: return null;
177: }
178: }
179:
180: public Set<Area> getOpenAreas() {
181: return open;
182: }
183:
184: public void setOpen(Area... areas) {
185: open.clear();
186: open.addAll(Arrays.asList(areas));
187: pcs.firePropertyChange(PROP_OPEN_AREAS, null, null);
188: }
189:
190: private class LP implements
191: LibraryProvider<LibraryImplementation> {
192:
193: final PropertyChangeSupport pcs = new PropertyChangeSupport(
194: this );
195: final Area area;
196:
197: LP(Area area) {
198: this .area = area;
199: lps.add(this );
200: }
201:
202: public LibraryImplementation[] getLibraries() {
203: if (libs.containsKey(area)) {
204: return libs.get(area).toArray(
205: new LibraryImplementation[0]);
206: } else {
207: return new LibraryImplementation[0];
208: }
209: }
210:
211: public void addPropertyChangeListener(
212: PropertyChangeListener listener) {
213: pcs.addPropertyChangeListener(listener);
214: }
215:
216: public void removePropertyChangeListener(
217: PropertyChangeListener listener) {
218: pcs.removePropertyChangeListener(listener);
219: }
220:
221: }
222:
223: public LibraryProvider<LibraryImplementation> getLibraries(
224: Area area) {
225: return new LP(area);
226: }
227:
228: public LibraryImplementation createLibrary(String type,
229: String name, Area area, Map<String, List<URL>> contents)
230: throws IOException {
231: LibraryImplementation lib = LibrariesSupport
232: .createLibraryImplementation(type, contents
233: .keySet().toArray(new String[0]));
234: lib.setName(name);
235: for (Map.Entry<String, List<URL>> entry : contents
236: .entrySet()) {
237: lib.setContent(entry.getKey(), entry.getValue());
238: }
239: List<LibraryImplementation> l = libs.get(area);
240: if (l == null) {
241: l = new ArrayList<LibraryImplementation>();
242: libs.put(area, l);
243: }
244: l.add(lib);
245: for (LP lp : lps) {
246: if (lp.area.equals(area)) {
247: lp.pcs.firePropertyChange(
248: LibraryProvider.PROP_LIBRARIES, null, null);
249: }
250: }
251: return lib;
252: }
253:
254: public void remove(LibraryImplementation library)
255: throws IOException {
256: for (Map.Entry<Area, List<LibraryImplementation>> entry : libs
257: .entrySet()) {
258: if (entry.getValue().remove(library)) {
259: for (LP lp : lps) {
260: if (lp.area.equals(entry.getKey())) {
261: lp.pcs.firePropertyChange(
262: LibraryProvider.PROP_LIBRARIES,
263: null, null);
264: }
265: }
266: }
267: }
268: }
269:
270: }
271:
272: public static URL mkJar(String name) throws MalformedURLException {
273: return new URL("jar:http://nowhere.net/" + name + "!/");
274: }
275:
276: }
|