1 32 package org.jruby.javasupport.test; 33 34 import java.io.FileOutputStream ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.net.URL ; 38 39 import junit.framework.TestCase; 40 41 import org.jruby.Ruby; 42 import org.jruby.RubyKernel; 43 import org.jruby.javasupport.JavaUtil; 44 import org.jruby.runtime.builtin.IRubyObject; 45 import org.jruby.util.NormalizedFile; 46 47 public class RubyTestCase extends TestCase { 48 private static final IRubyObject[] EMPTY_ARRAY = IRubyObject.NULL_ARRAY; 49 50 public RubyTestCase(String name) { 51 super(name); 52 } 53 54 protected Ruby createRuby(URL url) throws IOException { 55 if (url == null) { 56 throw new NullPointerException ("url was null"); 57 } 58 InputStream in = url.openStream(); 59 NormalizedFile f = (NormalizedFile)NormalizedFile.createTempFile("rtc", ".rb"); 60 FileOutputStream out = new FileOutputStream (f); 61 62 int length; 63 byte[] buf = new byte[8096]; 64 while ((length = in.read(buf, 0, buf.length)) >= 0) { 65 out.write(buf, 0, length); 66 } 67 in.close(); 68 out.close(); 69 70 String filePath = f.getAbsolutePath(); 71 Ruby runtime = Ruby.getDefaultInstance(); 72 initRuby(runtime); 73 RubyKernel.require(runtime.getTopSelf(), runtime.newString(filePath), null); 74 f.delete(); 75 return runtime; 76 } 77 78 protected void initRuby(Ruby runtime) { 80 IRubyObject empty = 81 JavaUtil.convertJavaToRuby( 82 runtime, 83 EMPTY_ARRAY, 84 EMPTY_ARRAY.getClass()); 85 86 runtime.defineReadonlyVariable("$-p", runtime.getNil()); 87 runtime.defineReadonlyVariable("$-n", runtime.getNil()); 88 runtime.defineReadonlyVariable("$-a", runtime.getNil()); 89 runtime.defineReadonlyVariable("$-l", runtime.getNil()); 90 runtime.defineReadonlyVariable("$\"", empty); 91 runtime.defineReadonlyVariable("$*", empty); 92 runtime.defineReadonlyVariable("$:", empty); 93 runtime.defineGlobalConstant("ARGV", empty); 94 } 95 } 96 97 | Popular Tags |