PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/autoupdate.services/test/unit/src/org/netbeans/modules/autoupdate/services/FeatureNotUpToDateTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 233 lines | 158 code | 28 blank | 47 comment | 5 complexity | 8d4ead39d4ae767ac26825c16b424923 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2012 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. import org.netbeans.api.autoupdate.UpdateUnitProvider.CATEGORY;
  46. import org.netbeans.modules.autoupdate.updateprovider.*;
  47. import org.netbeans.api.autoupdate.*;
  48. import java.io.IOException;
  49. import java.util.HashSet;
  50. import java.util.List;
  51. import java.util.Map;
  52. import java.util.Set;
  53. import org.netbeans.junit.MockServices;
  54. import org.netbeans.junit.NbTestCase;
  55. import org.netbeans.modules.autoupdate.updateprovider.InstalledModuleProvider;
  56. import org.netbeans.spi.autoupdate.UpdateItem;
  57. import org.netbeans.spi.autoupdate.UpdateProvider;
  58. import org.openide.modules.Dependency;
  59. import org.openide.modules.ModuleInfo;
  60. import org.openide.modules.SpecificationVersion;
  61. import org.openide.util.Lookup;
  62. /**
  63. *
  64. * @author Jiri Rechtacek
  65. */
  66. public class FeatureNotUpToDateTest extends NbTestCase
  67. {
  68. protected boolean modulesOnly = true;
  69. List<UpdateUnit> keepItNotToGC;
  70. public FeatureNotUpToDateTest (String testName) {
  71. super (testName);
  72. }
  73. public static class MyProvider implements UpdateProvider {
  74. public String getName () {
  75. return FeatureNotUpToDateTest.class.getName ();
  76. }
  77. public String getDisplayName () {
  78. return getName ();
  79. }
  80. public String getDescription () {
  81. return getName ();
  82. }
  83. public Map<String, UpdateItem> getUpdateItems () throws IOException {
  84. Map<String, UpdateItem> items = InstalledModuleProvider.getDefault().getUpdateItems ();
  85. assertNotNull ("Installed modules must found.", items);
  86. int size = items.size ();
  87. assertTrue ("Count of installed modules are more then once.", size > 1);
  88. String pilotName = items.keySet ().iterator ().next ();
  89. assertNotNull (pilotName + "must found", items.get (pilotName));
  90. UpdateItem pilotItem = items.get (pilotName);
  91. assertNotNull ("Impl of " + pilotItem + " available", Trampoline.SPI.impl (pilotItem));
  92. UpdateItemImpl pilotItemImpl = Trampoline.SPI.impl (pilotItem);
  93. assertTrue ("Impl of " + pilotItem + "is ModuleItem", pilotItemImpl instanceof ModuleItem);
  94. ModuleItem pilotModuleItem = (ModuleItem) pilotItemImpl;
  95. SpecificationVersion pilotSV = new SpecificationVersion (pilotModuleItem.getSpecificationVersion ());
  96. assertTrue ("a dot is present in " + pilotSV, pilotSV.toString ().indexOf ('.') != -1);
  97. int dot = pilotSV.toString ().indexOf ('.');
  98. String postSpec = pilotSV.toString ().substring (dot + 1);
  99. String preSpec = pilotSV.toString ().substring (0, dot);
  100. Integer digit = 0;
  101. try {
  102. digit = Integer.parseInt (preSpec) + 1;
  103. } catch (NumberFormatException nfe) {
  104. fail (nfe.getLocalizedMessage ());
  105. }
  106. SpecificationVersion higherSV = new SpecificationVersion (digit + "." + postSpec);
  107. assertTrue (higherSV + " is more then " + pilotSV, higherSV.compareTo (pilotSV) > 0);
  108. String higherDep = pilotModuleItem.getModuleInfo ().getCodeNameBase () + " > " + higherSV;
  109. Set<String> deps = new HashSet<String> (items.size ());
  110. for (String id : items.keySet ()) {
  111. String dep;
  112. if (! pilotName.equals (id)) {
  113. UpdateItem item = items.get (id);
  114. assertNotNull ("Impl of " + item + " available", Trampoline.SPI.impl (item));
  115. UpdateItemImpl itemImpl = Trampoline.SPI.impl (item);
  116. assertTrue ("Impl of " + item + "is ModuleItem", itemImpl instanceof ModuleItem);
  117. ModuleItem moduleItem = (ModuleItem) itemImpl;
  118. dep = moduleItem.getModuleInfo ().getCodeNameBase () + " > " + moduleItem.getSpecificationVersion ();
  119. } else {
  120. dep = higherDep;
  121. }
  122. deps.add (dep);
  123. }
  124. Map<String, UpdateItem> res = InstalledModuleProvider.getDefault().getUpdateItems ();
  125. ModuleInfo info = pilotModuleItem.getModuleInfo ();
  126. UpdateItemImpl higherItemImpl = new InstalledModuleItem (
  127. info.getCodeNameBase (),
  128. higherSV.toString (),
  129. new HackedModuleInfo (info, higherSV),
  130. null, // XXX author
  131. null, // installed cluster
  132. null);
  133. UpdateItem higherModuleItem = Utilities.createUpdateItem (higherItemImpl);
  134. res.put ("testFeatureVsStandaloneModules",
  135. UpdateItem.createFeature (
  136. "testFeatureVsStandaloneModules",
  137. "1.0",
  138. deps,
  139. null,
  140. null,
  141. null));
  142. res.put (pilotName, higherModuleItem);
  143. return res;
  144. }
  145. public boolean refresh (boolean force) throws IOException {
  146. return true;
  147. }
  148. public CATEGORY getCategory() {
  149. return CATEGORY.COMMUNITY;
  150. }
  151. }
  152. @Override
  153. protected void setUp () throws Exception {
  154. super.setUp ();
  155. this.clearWorkDir ();
  156. TestUtils.setUserDir (getWorkDirPath ());
  157. TestUtils.testInit ();
  158. MockServices.setServices (MyProvider.class);
  159. assert Lookup.getDefault ().lookup (MyProvider.class) != null;
  160. UpdateUnitProviderFactory.getDefault ().refreshProviders (null, true);
  161. }
  162. public void testNoUpToDateFeature () {
  163. assertNotNull ("A feature found.", UpdateManager.getDefault ().getUpdateUnits (UpdateManager.TYPE.FEATURE));
  164. List<UpdateUnit> units = UpdateManager.getDefault ().getUpdateUnits (UpdateManager.TYPE.FEATURE);
  165. assertEquals ("Only once feature there.", 1, units.size ());
  166. UpdateUnit feature = units.get (0);
  167. assertNotNull (feature + " is installed.", feature.getInstalled ());
  168. assertFalse (feature + " has some available updates.", feature.getAvailableUpdates ().isEmpty ());
  169. }
  170. final static class HackedModuleInfo extends ModuleInfo {
  171. private ModuleInfo info;
  172. private SpecificationVersion hackedVersion;
  173. public HackedModuleInfo (ModuleInfo info, SpecificationVersion hackedVersion) {
  174. this.info = info;
  175. this.hackedVersion = hackedVersion;
  176. }
  177. public String getCodeNameBase () {
  178. return info.getCodeNameBase ();
  179. }
  180. public int getCodeNameRelease () {
  181. return info.getCodeNameRelease ();
  182. }
  183. public String getCodeName () {
  184. return info.getCodeName ();
  185. }
  186. public SpecificationVersion getSpecificationVersion () {
  187. return hackedVersion;
  188. }
  189. public boolean isEnabled () {
  190. return info.isEnabled ();
  191. }
  192. public Object getAttribute (String attr) {
  193. return info.getAttribute (attr);
  194. }
  195. public Object getLocalizedAttribute (String attr) {
  196. return info.getLocalizedAttribute (attr);
  197. }
  198. public Set<Dependency> getDependencies () {
  199. return info.getDependencies ();
  200. }
  201. public boolean owns (Class<?> clazz) {
  202. return info.owns (clazz);
  203. }
  204. }
  205. }