Esempio n. 1
0
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   if (runtime == null) {
     runtime = Ruby.newInstance();
   }
 }
Esempio n. 2
0
  public void testUnicodeChars() throws Exception {
    File file = new File("unicodeáéíóú");
    if (file.exists()) {
      if (!file.delete()) {
        fail("Unable to delete file: " + file);
      }
    }
    try {
      if (!file.mkdirs()) {
        fail("Unable to create directory: " + file);
      }
      Ruby runtime;
      RubyInstanceConfig.nativeEnabled = false;
      runtime = Ruby.newInstance();

      JRubyFile rubyFile = RubyFile.file(RubyString.newString(runtime, file.getAbsolutePath()));
      assertTrue(rubyFile.exists());
      assertTrue(file.exists());
      assertTrue(file.isDirectory());
      try {
        assertTrue(runtime.getPosix().stat(rubyFile.getAbsolutePath()).isDirectory());
      } catch (Exception e) {
        throw new RuntimeException("Expecting posix layer to work properly", e);
      }
    } finally {
      if (file.exists()) {
        file.delete();
      }
    }
  }
Esempio n. 3
0
 // This method is used only from ThreadLocalContextProvider.
 // Other providers should instantialte runtime in their own way.
 public Ruby getThreadSafeRuntime() {
   if (runtime == null) {
     // stopped loading java library (runtime.getLoadService().require("java");)
     // during the intialization process.
     runtime = Ruby.newInstance(config);
     initialized = true;
   }
   return runtime;
 }
Esempio n. 4
0
  /** Tests the {@link org.jruby.runtime.profile.ProfilingServiceLookup} too */
  public void testNoProfilingServerAvailableIfProfilingIsDisabled() {
    RubyInstanceConfig config = Ruby.getGlobalRuntime().getInstanceConfig();

    RubyInstanceConfig configOne = new RubyInstanceConfig(config);
    configOne.setProfilingMode(RubyInstanceConfig.ProfilingMode.OFF);

    Ruby ruby = Ruby.newInstance(configOne);

    assertNull(ruby.getProfilingService());
  }
  @Before
  public void setUp() {
    this.ruby = Ruby.newInstance();

    this.ruby.getLoadService().require("org/torquebox/web/component/mock_app");
    RubyModule mockAppClass = this.ruby.getClassFromPath("MockApp");
    this.rackApp =
        (IRubyObject)
            JavaEmbedUtils.invokeMethod(this.ruby, mockAppClass, "new", null, IRubyObject.class);
  }
Esempio n. 6
0
  private void setupJRuby(Activity startActivity) {
    System.setProperty("jruby.bytecode.version", "1.5");

    // enable proxy classes
    System.setProperty(
        "jruby.ji.proxyClassFactory", "com.rickbutton.rubydroid.DalvikProxyClassFactory");
    System.setProperty("jruby.ji.upper.case.package.name.allowed", "true");
    System.setProperty("jruby.class.cache.path", appContext.getDir("dex", 0).getAbsolutePath());

    // setup jruby home
    String apkName = getApkName();
    String jrubyHome = "file:" + apkName + "!/jruby.home";
    System.setProperty("jruby.home", jrubyHome);

    // configure jruby
    System.setProperty("jruby.compile.mode", "OFF"); // OFF OFFIR JITIR? FORCE FORCEIR
    // System.setProperty("jruby.compile.backend", "DALVIK");
    System.setProperty("jruby.bytecode.version", "1.6");
    System.setProperty("jruby.interfaces.useProxy", "true");
    System.setProperty("jruby.management.enabled", "false");
    System.setProperty("jruby.objectspace.enabled", "false");
    System.setProperty("jruby.thread.pooling", "true");
    System.setProperty("jruby.native.enabled", "false");
    System.setProperty("jruby.ir.passes", "LocalOptimizationPass,DeadCodeElimination");
    System.setProperty("jruby.backtrace.style", "normal"); // normal raw full mri

    ClassLoader loader = new PathClassLoader(apkName, RubySystem.class.getClassLoader());

    // disable gems
    RubyInstanceConfig config = new RubyInstanceConfig();
    config.setDisableGems(true);
    config.setLoader(loader);
    Ruby.newInstance(config);

    container =
        new ScriptingContainer(LocalContextScope.SINGLETON, LocalVariableBehavior.PERSISTENT);
    container.setClassLoader(loader);

    Thread.currentThread().setContextClassLoader(loader);

    if (appContext.getFilesDir() != null) {
      container.setCurrentDirectory(appContext.getFilesDir().getPath());
    }

    container.put("$package_name", appContext.getPackageName());

    container.put("app_context", appContext);
    container.put("system", this);
    rubyContext = container.runScriptlet(PathType.CLASSPATH, "ruby/droid/init.rb");

    Object mainActivity = container.get("MainActivity");
    container.callMethod(rubyContext, "start_ruby_activity", mainActivity, startActivity);
  }
  @Test
  public void shouldWrapJavaIOExceptions() throws Exception {
    Ruby ruby = Ruby.newInstance();
    RackInput rackInput = mock(RackInput.class);
    when(rackInput.read(null)).thenThrow(new IOException("fake"));

    JRubyRackInput subject = new JRubyRackInput(ruby, rackInput);
    GlobalVariables globalVariables = ruby.getGlobalVariables();
    globalVariables.set("$rack_input", subject);

    IRubyObject result =
        ruby.evalScriptlet(
            "begin; $rack_input.read; rescue IOError => e; \"rescued #{e.message}\"; end");
    assertThat(result.asJavaString()).isEqualTo("rescued fake");
  }
Esempio n. 8
0
  public ErbScriptEngine(ErbScriptEngineFactory factory) {
    super(factory);

    final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
      runtime = Ruby.newInstance();
      runtime.evalScriptlet(
          "require 'java';require 'erb';self.send :include, ERB::Util;class ERB;def get_binding;binding;end;attr_reader :props;def set_props(p);@props = p;"
              + "for name,v in @props;instance_eval \"def #{name}; @props['#{name}'];end\";end;end;end;");

      erbModule = runtime.getClassFromPath("ERB");
      bindingSym = RubySymbol.newSymbol(runtime, "get_binding");
    } finally {
      Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
  }
Esempio n. 9
0
  /** Tests the {@link org.jruby.runtime.profile.ProfilingServiceLookup} too */
  public void testProfilingServiceLookupWorks() {
    try {
      RubyInstanceConfig config = Ruby.getGlobalRuntime().getInstanceConfig();

      RubyInstanceConfig configOne = new RubyInstanceConfig(config);

      configOne.setProfilingService(TestProfilingService.class.getName());
      configOne.setProfilingMode(RubyInstanceConfig.ProfilingMode.SERVICE);
      Ruby ruby = Ruby.newInstance(configOne);

      assertNotNull(ruby.getProfilingService());
      assertTrue(ruby.getProfilingService() instanceof TestProfilingService);
    } catch (RaiseException e) {
      // e.printStackTrace();
      // TODO hwo to mock org.jruby.exceptions.RaiseException: (LoadError) no such file to load --
      // jruby/profiler/shutdown_hook
    }
  }
Esempio n. 10
0
 public void setUp() {
   runtime = Ruby.newInstance();
 }
Esempio n. 11
0
  @Test
  public void testCallableProcToIfaceMatchIsNotOrderSensitive() throws Exception {
    final Ruby runtime = Ruby.newInstance();

    final Method list1 = java.io.File.class.getMethod("listFiles", java.io.FileFilter.class);
    final Method list2 = java.io.File.class.getMethod("listFiles", java.io.FilenameFilter.class);

    IntHashMap cache;
    JavaMethod[] methods;
    Binding binding = new Binding(new Frame(), null, new BacktraceElement());
    JavaMethod result;
    IRubyObject[] args;

    // arity 1 :

    BlockBody body1 =
        new NullBlockBody() {
          // @Override public Arity arity() { return Arity.ONE_ARGUMENT; }
          @Override
          public Signature getSignature() {
            return Signature.ONE_ARGUMENT;
          }
        };
    RubyProc dummyProc = RubyProc.newProc(runtime, new Block(body1, binding), Block.Type.PROC);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list1), new JavaMethod(runtime, list2)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);

    cache = IntHashMap.nullMap();
    args = new IRubyObject[] {dummyProc};
    result = CallableSelector.matchingCallableArityN(runtime, cache, methods, args);
    assertEquals(new JavaMethod(runtime, list1), result);

    cache = IntHashMap.nullMap();
    methods =
        new JavaMethod[] { // "reverse" method order
          new JavaMethod(runtime, list2), new JavaMethod(runtime, list1)
        };
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);

    cache = IntHashMap.nullMap();
    args = new IRubyObject[] {dummyProc};
    result = CallableSelector.matchingCallableArityN(runtime, cache, methods, args);
    assertEquals(new JavaMethod(runtime, list1), result);

    // arity 2 :

    BlockBody body2 =
        new NullBlockBody() {
          // @Override public Arity arity() { return Arity.TWO_ARGUMENTS; }
          @Override
          public Signature getSignature() {
            return Signature.TWO_ARGUMENTS;
          }
        };
    dummyProc = RubyProc.newProc(runtime, new Block(body2, binding), Block.Type.PROC);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list1), new JavaMethod(runtime, list2)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list2), result);

    cache = IntHashMap.nullMap();
    args = new IRubyObject[] {dummyProc};
    result = CallableSelector.matchingCallableArityN(runtime, cache, methods, args);
    assertEquals(new JavaMethod(runtime, list2), result);

    cache = IntHashMap.nullMap();
    methods =
        new JavaMethod[] { // "reverse" method order
          new JavaMethod(runtime, list2), new JavaMethod(runtime, list1)
        };
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list2), result);

    cache = IntHashMap.nullMap();
    args = new IRubyObject[] {dummyProc};
    result = CallableSelector.matchingCallableArityN(runtime, cache, methods, args);
    assertEquals(new JavaMethod(runtime, list2), result);

    // arity -1 :

    BlockBody body_1 = new NullBlockBody() { // arity -1
          // @Override public Arity arity() { return Arity.OPTIONAL; }
          @Override
          public Signature getSignature() {
            return Signature.OPTIONAL;
          }
        };
    dummyProc = RubyProc.newProc(runtime, new Block(body_1, binding), Block.Type.PROC);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list1), new JavaMethod(runtime, list2)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list2), new JavaMethod(runtime, list1)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);

    // arity -3 :

    BlockBody body_3 = new NullBlockBody() { // arity -3
          // @Override public Arity arity() { return Arity.TWO_REQUIRED; }
          @Override
          public Signature getSignature() {
            return Signature.TWO_REQUIRED;
          }
        };
    dummyProc = RubyProc.newProc(runtime, new Block(body_3, binding), Block.Type.PROC);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list1), new JavaMethod(runtime, list2)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list2), result);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list2), new JavaMethod(runtime, list1)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list2), result);

    // arity -2 :

    BlockBody body_2 = new NullBlockBody() { // arity -2 (arg1, *rest) should prefer (single)
          // @Override public Arity arity() { return Arity.ONE_REQUIRED; }
          @Override
          public Signature getSignature() {
            return Signature.ONE_REQUIRED;
          }
        };
    dummyProc = RubyProc.newProc(runtime, new Block(body_2, binding), Block.Type.PROC);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list1), new JavaMethod(runtime, list2)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);

    cache = IntHashMap.nullMap();
    methods = new JavaMethod[] {new JavaMethod(runtime, list2), new JavaMethod(runtime, list1)};
    result = CallableSelector.matchingCallableArityOne(runtime, cache, methods, dummyProc);
    assertEquals(new JavaMethod(runtime, list1), result);
  }