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.api.ruby.platform;
042:
043: import java.io.ByteArrayOutputStream;
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.IOException;
047: import java.io.InputStream;
048: import java.util.Properties;
049: import org.netbeans.api.ruby.platform.RubyPlatform.Info;
050: import org.netbeans.junit.NbTestCase;
051: import org.netbeans.modules.ruby.platform.gems.GemManager;
052: import org.openide.filesystems.FileObject;
053: import org.openide.filesystems.FileUtil;
054:
055: public abstract class RubyTestBase extends NbTestCase {
056:
057: private FileObject testRubyHome;
058:
059: public RubyTestBase(final String name) {
060: super (name);
061: }
062:
063: protected @Override
064: void setUp() throws Exception {
065: super .setUp();
066: clearWorkDir();
067: TestUtil.getXTestJRubyHome();
068: System.setProperty("netbeans.user", getWorkDirPath());
069: }
070:
071: public File getTestRubyHome() {
072: return FileUtil.toFile(testRubyHome);
073: }
074:
075: private static File resolveFile(final String property,
076: final boolean mandatory) {
077: String path = System.getProperty(property);
078: assertTrue("must set " + property, !mandatory || (path != null));
079: return path == null ? null : new File(path);
080: }
081:
082: static File getFile(final String property, boolean mandatory) {
083: File file = resolveFile(property, mandatory);
084: assertTrue(file + " is file", !mandatory || file.isFile());
085: return file;
086:
087: }
088:
089: static File getDirectory(final String property,
090: final boolean mandatory) {
091: File directory = resolveFile(property, mandatory);
092: assertTrue(directory + " is directory", !mandatory
093: || directory.isDirectory());
094: return directory;
095: }
096:
097: protected File setUpRuby() throws Exception {
098: return setUpRuby(false, "");
099: }
100:
101: protected File setUpRubyWithGems() throws Exception {
102: return setUpRuby(true, "");
103: }
104:
105: protected File setUpRuby(final boolean withGems, final String suffix)
106: throws Exception {
107: // Ensure that $GEM_HOME isn't picked up
108: // I can't do this:
109: // System.getenv().remove("GEM_HOME");
110: // because the environment variable map is unmodifiable. So instead
111: // side effect to ensure that the GEM_HOME check isn't run
112: GemManager.TEST_GEM_HOME = "invalid"; // non null but also invalid dir, will bypass $GEM_HOME lookup
113:
114: // Build a fake ruby structure
115: testRubyHome = FileUtil.createFolder(FileUtil
116: .toFileObject(getWorkDir()), "test_ruby");
117:
118: FileObject bin = testRubyHome.createFolder("bin");
119: FileObject libRuby = FileUtil.createFolder(testRubyHome,
120: "lib/ruby");
121: libRuby.createFolder(RubyPlatform.DEFAULT_RUBY_RELEASE);
122: FileObject interpreter = bin.createData("ruby" + suffix);
123: String[] binaries = { "rdoc", "gem" };
124: for (String binary : binaries) {
125: bin.createData(binary + suffix);
126: }
127:
128: Properties props = new Properties();
129: props.put(Info.RUBY_KIND, "Ruby");
130: props.put(Info.RUBY_VERSION, "0.1");
131: props.put(Info.RUBY_RELEASE_DATE, "2000-01-01");
132: props.put(Info.RUBY_PLATFORM, "abcd");
133: if (withGems) {
134: // Build a fake rubygems repository
135: FileObject gemRepo = FileUtil.createFolder(libRuby, "gems/"
136: + RubyPlatform.DEFAULT_RUBY_RELEASE);
137: GemManager.initializeRepository(gemRepo);
138: gemRepo.createFolder("bin");
139: props.put(Info.GEM_HOME, FileUtil.toFile(gemRepo)
140: .getAbsolutePath());
141: props.put(Info.GEM_PATH, "/tmp/a/b/c");
142: props.put(Info.GEM_VERSION, "0.2");
143: }
144:
145: RubyPlatformManager.TEST_RUBY_PROPS = props;
146: return FileUtil.toFile(interpreter);
147: }
148:
149: protected static void installFakeFastRubyDebugger(
150: RubyPlatform platform) throws IOException {
151: String gemplaf = platform.isJRuby() ? "java" : "";
152: installFakeGem("ruby-debug-base",
153: RubyPlatform.RDEBUG_BASE_VERSION, gemplaf, platform);
154: installFakeGem("ruby-debug-ide",
155: RubyPlatform.RDEBUG_IDE_VERSION, gemplaf, platform);
156: }
157:
158: protected static void uninstallFakeGem(final String name,
159: final String version, final String actualPlatform,
160: final RubyPlatform platform) throws IOException {
161: FileObject gemHome = platform.getGemManager().getGemHomeFO();
162: String gemplaf = actualPlatform == null ? "" : "-"
163: + actualPlatform;
164: FileObject gem = gemHome.getFileObject("specifications/" + name
165: + '-' + version + gemplaf + ".gemspec");
166: gem.delete();
167: platform.getGemManager().reset();
168: }
169:
170: protected static void uninstallFakeGem(final String name,
171: final String version, final RubyPlatform platform)
172: throws IOException {
173: uninstallFakeGem(name, version, null, platform);
174: }
175:
176: protected static void installFakeGem(final String name,
177: final String version, final String actualPlatform,
178: final RubyPlatform platform) throws IOException {
179: FileObject gemHome = platform.getGemManager().getGemHomeFO();
180: String gemplaf = actualPlatform == null ? "" : "-"
181: + actualPlatform;
182: FileUtil.createData(gemHome, "specifications/" + name + '-'
183: + version + gemplaf + ".gemspec");
184: platform.getGemManager().reset();
185: }
186:
187: protected static void installFakeGem(final String name,
188: final String version, final RubyPlatform platform)
189: throws IOException {
190: installFakeGem(name, version, null, platform);
191: }
192:
193: protected FileObject getTestFile(String relFilePath) {
194: File wholeInputFile = new File(getDataDir(), relFilePath);
195: if (!wholeInputFile.exists()) {
196: NbTestCase.fail("File " + wholeInputFile + " not found.");
197: }
198: FileObject fo = FileUtil.toFileObject(wholeInputFile);
199: assertNotNull(fo);
200: return fo;
201: }
202:
203: protected FileObject touch(final String dir, final String path)
204: throws IOException {
205: return touch(new File(dir), path);
206: }
207:
208: protected FileObject touch(final File dir, final String binary)
209: throws IOException {
210: if (!dir.isDirectory()) {
211: assertTrue("success to create " + dir, dir.mkdirs());
212: }
213: FileObject dirFO = FileUtil.toFileObject(FileUtil
214: .normalizeFile(dir));
215: return FileUtil.createData(dirFO, binary);
216: }
217:
218: /** Copy-pasted from APISupport. */
219: protected static String slurp(File file) throws IOException {
220: InputStream is = new FileInputStream(file);
221: try {
222: ByteArrayOutputStream baos = new ByteArrayOutputStream();
223: FileUtil.copy(is, baos);
224: return baos.toString("UTF-8");
225: } finally {
226: is.close();
227: }
228: }
229:
230: }
|