001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 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: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.ruby.platform.gems;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.List;
047: import java.util.Set;
048: import org.netbeans.api.ruby.platform.RubyPlatform;
049: import org.netbeans.api.ruby.platform.RubyPlatformManager;
050: import org.netbeans.api.ruby.platform.RubyTestBase;
051: import org.netbeans.api.ruby.platform.TestUtil;
052: import org.openide.filesystems.FileObject;
053: import org.openide.filesystems.FileUtil;
054: import org.openide.util.Utilities;
055:
056: public class GemManagerTest extends RubyTestBase {
057:
058: public GemManagerTest(final String testName) {
059: super (testName);
060: TestUtil.getXTestJRubyHome();
061: }
062:
063: public void testGetGemProblem() {
064: RubyPlatform jruby = RubyPlatformManager.getDefaultPlatform();
065: GemManager gm = jruby.getGemManager();
066: assertNotNull(gm);
067: }
068:
069: public void testGetRubyLibGemDir() throws Exception {
070: RubyPlatform platform = RubyPlatformManager
071: .addPlatform(setUpRubyWithGems());
072: GemManager gemManager = platform.getGemManager();
073: assertEquals("righ gem dir", new File(platform.getLib(),
074: "ruby/gems/1.8"), new File(gemManager.getGemHome()));
075: }
076:
077: public void testGetGem() throws Exception {
078: RubyPlatform platform = RubyPlatformManager
079: .addPlatform(setUpRubyWithGems());
080: GemManager gemManager = platform.getGemManager();
081: assertEquals("righ gem dir", new File(new File(
082: getTestRubyHome(), "bin"), "gem").getAbsolutePath(),
083: gemManager.getGemTool());
084: }
085:
086: public void testGemFetching() {
087: RubyPlatform jruby = RubyPlatformManager.getDefaultPlatform();
088: GemManager gm = jruby.getGemManager();
089:
090: List<String> errors = new ArrayList<String>();
091: List<Gem> available = gm.getRemoteGems(errors);
092: assertNotNull("gem not null", available);
093: System.out.println("available: " + available.size());
094: assertTrue("no errros: " + errors, errors.isEmpty());
095:
096: List<Gem> installed = gm.getInstalledGems(errors);
097: assertNotNull("gem not null", installed);
098: System.out.println("installed: " + installed.size());
099: assertTrue("no errros", errors.isEmpty());
100:
101: gm.reloadIfNeeded(errors);
102: assertTrue("no errros", errors.isEmpty());
103: }
104:
105: public void testIsValidGemHome() throws Exception {
106: assertFalse("not valid", GemManager
107: .isValidGemHome(getWorkDir()));
108: assertTrue("valid", GemManager.isValidGemHome(new File(
109: RubyPlatformManager.getDefaultPlatform().getInfo()
110: .getGemHome())));
111: RubyPlatform platform = RubyPlatformManager
112: .addPlatform(setUpRubyWithGems());
113: assertTrue("valid", GemManager.isValidGemHome(new File(platform
114: .getInfo().getGemHome())));
115: }
116:
117: public void testGetRepositories() throws Exception {
118: RubyPlatform platform = RubyPlatformManager
119: .getDefaultPlatform();
120: GemManager gemManager = platform.getGemManager();
121: Set<? extends File> paths = gemManager.getRepositories();
122: assertEquals("one path element", 1, paths.size());
123: assertEquals("same as Gem Home", gemManager.getGemHomeF(),
124: paths.iterator().next());
125: assertEquals("same as Gem Home", gemManager.getGemHome(),
126: platform.getInfo().getGemPath());
127: }
128:
129: public void testAddRemoveRepository() throws Exception {
130: RubyPlatform platform = RubyPlatformManager
131: .getDefaultPlatform();
132: GemManager gemManager = platform.getGemManager();
133: File dummyRepo = new File(getWorkDirPath(), "/a");
134: gemManager.addGemPath(dummyRepo);
135: assertEquals("two repositories", 2, gemManager
136: .getRepositories().size());
137: assertTrue("two repositories in info's gempath",
138: platform.getInfo().getGemPath().indexOf(
139: File.pathSeparatorChar) != -1);
140: gemManager.removeGemPath(dummyRepo);
141: assertEquals("one repositories", 1, gemManager
142: .getRepositories().size());
143: assertTrue("one repositories in info's gempath",
144: platform.getInfo().getGemPath().indexOf(
145: File.pathSeparatorChar) == -1);
146: }
147:
148: public void testAddTheSameRepositoryTwice() {
149: RubyPlatform platform = RubyPlatformManager
150: .getDefaultPlatform();
151: GemManager gemManager = platform.getGemManager();
152: File dummyRepo = new File(getWorkDirPath(), "/a");
153: assertTrue("successfuly added", gemManager
154: .addGemPath(dummyRepo));
155: assertFalse("failed to add second time", gemManager
156: .addGemPath(dummyRepo));
157: }
158:
159: public void testInitializeRepository() throws Exception {
160: FileObject gemRepo = FileUtil.toFileObject(getWorkDir())
161: .createFolder("gem-repo");
162: GemManager.initializeRepository(gemRepo);
163: GemManager.isValidGemHome(FileUtil.toFile(gemRepo));
164: }
165:
166: public void testInitializeRepositoryFile() throws Exception {
167: File gemRepo = new File(getWorkDir(), "gem-repo");
168: GemManager.initializeRepository(gemRepo);
169: GemManager.isValidGemHome(gemRepo);
170: }
171:
172: public void testGetVersionForPlatform() throws IOException {
173: RubyPlatform platform = RubyPlatformManager
174: .getDefaultPlatform();
175: GemManager gemManager = platform.getGemManager();
176: RubyPlatform jruby = RubyPlatformManager.getDefaultPlatform();
177: FileObject gemRepo = FileUtil.toFileObject(getWorkDir())
178: .createFolder("gem-repo");
179: GemManager.initializeRepository(gemRepo);
180: jruby.setGemHome(FileUtil.toFile(gemRepo));
181: String version = Utilities.isWindows() ? "0.9.3" : "0.1.10";
182: installFakeGem("ruby-debug-base", version, platform);
183: assertEquals("native fast debugger available", version,
184: gemManager.getVersion("ruby-debug-base"));
185: assertNull("no jruby fast debugger available", gemManager
186: .getVersionForPlatform("ruby-debug-base"));
187: uninstallFakeGem("ruby-debug-base", version, platform);
188: installFakeGem("ruby-debug-base", version, "java", platform);
189: assertEquals("no jruby fast debugger available", version,
190: gemManager.getVersionForPlatform("ruby-debug-base"));
191: }
192:
193: public void testCompareGemVersions() {
194: assertTrue(GemManager.compareGemVersions("1.0.0", "0.9.9") > 0);
195: assertTrue(GemManager.compareGemVersions("0.4.0", "0.3.0") > 0);
196: assertTrue(GemManager.compareGemVersions("0.4.0", "0.3.9") > 0);
197: assertTrue(GemManager.compareGemVersions("0.0.2", "0.0.1") > 0);
198: assertTrue(GemManager.compareGemVersions("0.10.0", "0.9.0") > 0);
199: assertTrue(GemManager.compareGemVersions("0.9.0", "0.10.0") < 0);
200: assertTrue(GemManager.compareGemVersions("1.0.0", "4.9.9") < 0);
201: assertTrue(GemManager.compareGemVersions("0.3.0", "0.4.0") < 0);
202: assertTrue(GemManager.compareGemVersions("0.3.9", "0.4.0") < 0);
203: assertTrue(GemManager.compareGemVersions("0.0.1", "0.0.2") < 0);
204: assertTrue(GemManager.compareGemVersions("4.4.4", "4.4.4") == 0);
205: assertTrue(GemManager.compareGemVersions("4.4.4-platform",
206: "4.4.4") != 0);
207: assertTrue(GemManager
208: .compareGemVersions("0.10.0-ruby", "0.9.0") > 0);
209: assertTrue(GemManager
210: .compareGemVersions("0.9.0-ruby", "0.10.0") < 0);
211: assertTrue(GemManager
212: .compareGemVersions("0.10.0", "0.9.0-ruby") > 0);
213: assertTrue(GemManager
214: .compareGemVersions("0.9.0", "0.10.0-ruby") < 0);
215: }
216:
217: public void testChooseGems() throws Exception {
218: RubyPlatform platform = RubyPlatformManager
219: .addPlatform(setUpRubyWithGems());
220: GemManager gemManager = platform.getGemManager();
221:
222: String gemLibs = gemManager.getGemHome();
223: File specs = new File(new File(gemManager.getGemHome()),
224: "specifications");
225:
226: // Put gems into the gemLibs dir
227: String[] gemDirs = new String[] { "foo-1.0.0", "notagem",
228: "pdf-writer-0.1.1", "mongrel-1.0.0-mswin",
229: "bar-baz-0.3.3-ruby", "activerecord-1.15.1.6752",
230: "activerecord-1.15.3.6752" };
231: for (String gemDir : gemDirs) {
232: new File(gemLibs, gemDir).mkdir();
233: new File(specs, gemDir + ".gemspec").createNewFile();
234: }
235:
236: // Test for 106862
237: new File(gemLibs, "sqlite-2.0.1").mkdirs();
238: new File(gemLibs, "sqlite3-ruby-1.2.0").mkdirs();
239:
240: // Now introspect on the structure
241: Set<String> installedGems = gemManager.getInstalledGemsFiles();
242: assertTrue(installedGems.contains("foo"));
243: assertTrue(installedGems.contains("pdf-writer"));
244: assertTrue(installedGems.contains("mongrel"));
245: assertTrue(installedGems.contains("bar-baz"));
246: assertTrue(installedGems.contains("activerecord"));
247: assertFalse(installedGems.contains("notagem"));
248: assertFalse(installedGems.contains("whatever"));
249: assertFalse(installedGems.contains("sqlite"));
250: assertFalse(installedGems.contains("sqlite3-ruby"));
251:
252: assertEquals("1.0.0", gemManager.getVersion("foo"));
253: assertEquals(null, gemManager.getVersion("notagem"));
254: assertEquals(null, gemManager.getVersion("nosuchgem"));
255: assertEquals(null, gemManager.getVersion("sqlite"));
256: assertEquals(null, gemManager.getVersion("sqlite3-ruby"));
257: assertEquals("1.0.0", gemManager.getVersion("mongrel"));
258: assertEquals("0.3.3", gemManager.getVersion("bar-baz"));
259: assertEquals("0.1.1", gemManager.getVersion("pdf-writer"));
260: assertEquals("1.15.3.6752", gemManager
261: .getVersion("activerecord"));
262: }
263:
264: public void testInstallLocal() throws IOException {
265: RubyPlatform platform = RubyPlatformManager
266: .getDefaultPlatform();
267: GemManager gemManager = platform.getGemManager();
268: RubyPlatform jruby = RubyPlatformManager.getDefaultPlatform();
269: FileObject gemRepo = FileUtil.toFileObject(getWorkDir())
270: .createFolder("gem-repo");
271: GemManager.initializeRepository(gemRepo);
272: jruby.setGemHome(FileUtil.toFile(gemRepo));
273: jruby.getInfo().setGemPath("");
274: File rakeGem = getRakeGem();
275: assertNull("rake is not installed", gemManager
276: .getVersion("rake"));
277: gemManager.installLocal(rakeGem, null, false, false, false,
278: null);
279: assertNotNull("rake is installed", gemManager
280: .getVersion("rake"));
281: }
282:
283: private File getRakeGem() throws IOException {
284: File rakeGem = new File(TestUtil.getXTestJRubyHome(),
285: "lib/ruby/gems/1.8/cache/rake-0.7.3.gem");
286: assertNotNull("rake gem found", rakeGem);
287: return rakeGem;
288: }
289:
290: // XXX
291: // public void testFindGemExecutableWith_GEM_HOME() throws Exception {
292: // File gemRepo = new File(getWorkDir(), "gemrepo");
293: // File gemRepoBinF = new File(gemRepo, "bin");
294: // gemRepoBinF.mkdirs();
295: // RubyPlatform platform = RubyPlatformManager.addPlatform(setUpRuby(), "ruby");
296: // GemManager.TEST_GEM_HOME = gemRepo.getAbsolutePath();
297: // touch("rdebug-ide", gemRepoBinF.getAbsolutePath());
298: // assertNotNull(platform.getGemManager().findGemExecutable("rdebug-ide"));
299: // }
300:
301: }
|