Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
package org.netbeans.modules.autoupdate.services; |
45 |
|
46 |
import org.netbeans.api.autoupdate.UpdateUnitProvider.CATEGORY; |
47 |
import org.netbeans.modules.autoupdate.updateprovider.*; |
48 |
import org.netbeans.api.autoupdate.*; |
49 |
import java.io.IOException; |
50 |
import java.util.Collections; |
51 |
import java.util.HashSet; |
52 |
import java.util.List; |
53 |
import java.util.Map; |
54 |
import java.util.Set; |
55 |
import org.netbeans.junit.MockServices; |
56 |
import org.netbeans.junit.NbTestCase; |
57 |
import org.netbeans.modules.autoupdate.updateprovider.InstalledModuleProvider; |
58 |
import org.netbeans.spi.autoupdate.UpdateItem; |
59 |
import org.netbeans.spi.autoupdate.UpdateProvider; |
60 |
import org.openide.modules.Dependency; |
61 |
import org.openide.modules.ModuleInfo; |
62 |
import org.openide.modules.SpecificationVersion; |
63 |
import org.openide.util.Lookup; |
64 |
|
65 |
/** |
66 |
* @author Jaroslav Tulach |
67 |
*/ |
68 |
public class FeatureDependsOnFeatureTest extends NbTestCase |
69 |
|
70 |
{ |
71 |
|
72 |
protected boolean modulesOnly = true; |
73 |
List<UpdateUnit> keepItNotToGC; |
74 |
|
75 |
public FeatureDependsOnFeatureTest (String testName) { |
76 |
super (testName); |
77 |
} |
78 |
|
79 |
public static class MyProvider implements UpdateProvider { |
80 |
|
81 |
public String getName () { |
82 |
return FeatureDependsOnFeatureTest.class.getName (); |
83 |
} |
84 |
|
85 |
public String getDisplayName () { |
86 |
return getName (); |
87 |
} |
88 |
|
89 |
public String getDescription () { |
90 |
return getName (); |
91 |
} |
92 |
|
93 |
public Map<String, UpdateItem> getUpdateItems () throws IOException { |
94 |
Map<String, UpdateItem> items = InstalledModuleProvider.getDefault().getUpdateItems (); |
95 |
assertNotNull ("Installed modules must found.", items); |
96 |
int size = items.size (); |
97 |
assertTrue ("Count of installed modules are more then once.", size > 1); |
98 |
String pilotName = items.keySet ().iterator ().next (); |
99 |
assertNotNull (pilotName + "must found", items.get (pilotName)); |
100 |
UpdateItem pilotItem = items.get (pilotName); |
101 |
assertNotNull ("Impl of " + pilotItem + " available", Trampoline.SPI.impl (pilotItem)); |
102 |
UpdateItemImpl pilotItemImpl = Trampoline.SPI.impl (pilotItem); |
103 |
assertTrue ("Impl of " + pilotItem + "is ModuleItem", pilotItemImpl instanceof ModuleItem); |
104 |
ModuleItem pilotModuleItem = (ModuleItem) pilotItemImpl; |
105 |
SpecificationVersion pilotSV = new SpecificationVersion (pilotModuleItem.getSpecificationVersion ()); |
106 |
assertTrue ("a dot is present in " + pilotSV, pilotSV.toString ().indexOf ('.') != -1); |
107 |
int dot = pilotSV.toString ().indexOf ('.'); |
108 |
String postSpec = pilotSV.toString ().substring (dot + 1); |
109 |
String preSpec = pilotSV.toString ().substring (0, dot); |
110 |
Integer digit = 0; |
111 |
try { |
112 |
digit = Integer.parseInt (preSpec) + 1; |
113 |
} catch (NumberFormatException nfe) { |
114 |
fail (nfe.getLocalizedMessage ()); |
115 |
} |
116 |
SpecificationVersion higherSV = new SpecificationVersion (digit + "." + postSpec); |
117 |
assertTrue (higherSV + " is more then " + pilotSV, higherSV.compareTo (pilotSV) > 0); |
118 |
String higherDep = pilotModuleItem.getModuleInfo ().getCodeNameBase () + " > " + higherSV; |
119 |
|
120 |
Set<String> deps = new HashSet<String> (items.size ()); |
121 |
for (String id : items.keySet ()) { |
122 |
String dep; |
123 |
if (! pilotName.equals (id)) { |
124 |
UpdateItem item = items.get (id); |
125 |
assertNotNull ("Impl of " + item + " available", Trampoline.SPI.impl (item)); |
126 |
UpdateItemImpl itemImpl = Trampoline.SPI.impl (item); |
127 |
assertTrue ("Impl of " + item + "is ModuleItem", itemImpl instanceof ModuleItem); |
128 |
ModuleItem moduleItem = (ModuleItem) itemImpl; |
129 |
dep = moduleItem.getModuleInfo ().getCodeNameBase () + " > " + moduleItem.getSpecificationVersion (); |
130 |
} else { |
131 |
dep = higherDep; |
132 |
} |
133 |
deps.add (dep); |
134 |
} |
135 |
Map<String, UpdateItem> res = InstalledModuleProvider.getDefault().getUpdateItems (); |
136 |
ModuleInfo info = pilotModuleItem.getModuleInfo (); |
137 |
UpdateItemImpl higherItemImpl = new InstalledModuleItem ( |
138 |
info.getCodeNameBase (), |
139 |
higherSV.toString (), |
140 |
new HackedModuleInfo (info, higherSV), |
141 |
null, // XXX author |
142 |
null, // installed cluster |
143 |
null); |
144 |
UpdateItem higherModuleItem = Utilities.createUpdateItem (higherItemImpl); |
145 |
|
146 |
res.put ("testFeatueVsStandaloneModules", |
147 |
UpdateItem.createFeature ( |
148 |
"testFeatueVsStandaloneModules", |
149 |
"1.0", |
150 |
deps, |
151 |
null, |
152 |
null, |
153 |
null)); |
154 |
res.put (pilotName, higherModuleItem); |
155 |
res.put("testTransitive", UpdateItem.createFeature( |
156 |
"testTransitive", "1.3", |
157 |
Collections.singleton("testFeatueVsStandaloneModules"), |
158 |
null, null, null |
159 |
)); |
160 |
return res; |
161 |
} |
162 |
|
163 |
public boolean refresh (boolean force) throws IOException { |
164 |
return true; |
165 |
} |
166 |
|
167 |
public CATEGORY getCategory() { |
168 |
return CATEGORY.COMMUNITY; |
169 |
} |
170 |
} |
171 |
|
172 |
@Override |
173 |
protected void setUp () throws Exception { |
174 |
super.setUp (); |
175 |
this.clearWorkDir (); |
176 |
TestUtils.setUserDir (getWorkDirPath ()); |
177 |
TestUtils.testInit (); |
178 |
MockServices.setServices (MyProvider.class); |
179 |
assert Lookup.getDefault ().lookup (MyProvider.class) != null; |
180 |
UpdateUnitProviderFactory.getDefault ().refreshProviders (null, true); |
181 |
} |
182 |
|
183 |
public void testNoUpToDateFeature () { |
184 |
assertNotNull ("A feature found.", UpdateManager.getDefault ().getUpdateUnits (UpdateManager.TYPE.FEATURE)); |
185 |
List<UpdateUnit> units = UpdateManager.getDefault ().getUpdateUnits (UpdateManager.TYPE.FEATURE); |
186 |
assertEquals ("Two features there.", 2, units.size ()); |
187 |
UpdateUnit feature = units.get (0); |
188 |
if (feature.getCodeName().equals("testFeatueVsStandaloneModules")) { |
189 |
feature = units.get(1); |
190 |
} |
191 |
assertNotNull (feature + " is installed.", feature.getInstalled ()); |
192 |
assertFalse (feature + " has some available updates.", feature.getAvailableUpdates ().isEmpty ()); |
193 |
} |
194 |
|
195 |
final static class HackedModuleInfo extends ModuleInfo { |
196 |
private ModuleInfo info; |
197 |
private SpecificationVersion hackedVersion; |
198 |
|
199 |
public HackedModuleInfo (ModuleInfo info, SpecificationVersion hackedVersion) { |
200 |
this.info = info; |
201 |
this.hackedVersion = hackedVersion; |
202 |
} |
203 |
|
204 |
public String getCodeNameBase () { |
205 |
return info.getCodeNameBase (); |
206 |
} |
207 |
|
208 |
public int getCodeNameRelease () { |
209 |
return info.getCodeNameRelease (); |
210 |
} |
211 |
|
212 |
public String getCodeName () { |
213 |
return info.getCodeName (); |
214 |
} |
215 |
|
216 |
public SpecificationVersion getSpecificationVersion () { |
217 |
return hackedVersion; |
218 |
} |
219 |
|
220 |
public boolean isEnabled () { |
221 |
return info.isEnabled (); |
222 |
} |
223 |
|
224 |
public Object getAttribute (String attr) { |
225 |
return info.getAttribute (attr); |
226 |
} |
227 |
|
228 |
public Object getLocalizedAttribute (String attr) { |
229 |
return info.getLocalizedAttribute (attr); |
230 |
} |
231 |
|
232 |
public Set<Dependency> getDependencies () { |
233 |
return info.getDependencies (); |
234 |
} |
235 |
|
236 |
public boolean owns (Class<?> clazz) { |
237 |
return info.owns (clazz); |
238 |
} |
239 |
|
240 |
} |
241 |
} |