ruboto-core 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. data/README.md +3 -3
  2. data/Rakefile +75 -0
  3. data/assets/Rakefile +76 -85
  4. data/assets/res/layout/get_ruboto_core.xml +25 -0
  5. data/assets/samples/sample_broadcast_receiver.rb +1 -2
  6. data/assets/src/InheritingActivity.java +181 -5
  7. data/assets/src/InheritingBroadcastReceiver.java +22 -5
  8. data/assets/src/InheritingClass.java +4 -13
  9. data/assets/src/RubotoActivity.java +78 -118
  10. data/assets/src/RubotoBroadcastReceiver.java +31 -50
  11. data/assets/src/RubotoService.java +13 -28
  12. data/assets/src/org/ruboto/Script.java +286 -114
  13. data/assets/src/org/ruboto/test/ActivityTest.java +25 -26
  14. data/assets/src/org/ruboto/test/InstrumentationTestRunner.java +28 -29
  15. data/assets/{assets/scripts → src}/ruboto.rb +13 -8
  16. data/assets/test/assets/scripts/test_helper.rb +8 -4
  17. data/lib/ruboto/commands/base.rb +42 -6
  18. data/lib/ruboto/util/build.rb +25 -18
  19. data/lib/ruboto/util/update.rb +53 -30
  20. data/lib/ruboto/util/xml_element.rb +2 -2
  21. data/lib/ruboto/version.rb +3 -0
  22. data/test/activity/image_button_activity.rb +1 -1
  23. data/test/activity/image_button_and_button_activity.rb +1 -1
  24. data/test/activity/stack_activity.rb +21 -0
  25. data/test/activity/stack_activity_test.rb +24 -0
  26. data/test/app_test_methods.rb +25 -32
  27. data/test/rake_test.rb +15 -17
  28. data/test/ruboto_gen_test.rb +1 -16
  29. data/test/ruboto_gen_with_psych_test.rb +16 -0
  30. data/test/ruboto_update_test.rb +1 -47
  31. data/test/ruboto_update_with_psych_test.rb +18 -0
  32. data/test/service_test.rb +4 -2
  33. data/test/test_helper.rb +32 -22
  34. data/test/update_test_methods.rb +29 -0
  35. metadata +13 -22
@@ -1,11 +1,13 @@
1
1
  package org.ruboto;
2
2
 
3
3
  import android.content.Context;
4
+ import android.content.Intent;
4
5
  import android.content.pm.ApplicationInfo;
5
6
  import android.content.pm.PackageInfo;
6
7
  import android.content.pm.PackageManager;
7
8
  import android.content.pm.PackageManager.NameNotFoundException;
8
9
  import android.content.res.AssetManager;
10
+ import android.net.Uri;
9
11
  import android.os.Environment;
10
12
  import android.util.Log;
11
13
 
@@ -19,22 +21,38 @@ import java.io.IOException;
19
21
  import java.io.InputStream;
20
22
  import java.io.OutputStream;
21
23
  import java.io.PrintStream;
24
+ import java.lang.reflect.InvocationTargetException;
25
+ import java.lang.reflect.Method;
26
+ import java.util.List;
22
27
 
23
- import org.jruby.RubyInstanceConfig;
24
- import org.jruby.embed.ScriptingContainer;
25
- import org.jruby.exceptions.RaiseException;
28
+ import dalvik.system.PathClassLoader;
26
29
 
27
30
  public class Script {
28
31
  private static String scriptsDir = "scripts";
29
32
  private static File scriptsDirFile = null;
30
33
 
31
34
  private String name = null;
32
- private static ScriptingContainer ruby;
35
+ private static Object ruby;
33
36
  private static boolean initialized = false;
34
37
 
35
- private String contents = null;
38
+ private static String localContextScope = "SINGLETON";
39
+ private static String localVariableBehavior = "TRANSIENT";
36
40
 
37
41
  public static final String TAG = "RUBOTO"; // for logging
42
+ private static String JRUBY_VERSION;
43
+
44
+ /*************************************************************************************************
45
+ *
46
+ * Static Methods: ScriptingContainer config
47
+ */
48
+
49
+ public static void setLocalContextScope(String val) {
50
+ localContextScope = val;
51
+ }
52
+
53
+ public static void setLocalVariableBehavior(String val) {
54
+ localVariableBehavior = val;
55
+ }
38
56
 
39
57
  /*************************************************************************************************
40
58
  *
@@ -47,100 +65,189 @@ public class Script {
47
65
  }
48
66
  };
49
67
 
50
- public static boolean initialized() {
51
- return initialized;
52
- }
68
+ public static synchronized boolean isInitialized() {
69
+ return initialized;
70
+ }
53
71
 
54
- public static synchronized ScriptingContainer setUpJRuby(Context appContext) {
72
+ public static synchronized boolean setUpJRuby(Context appContext) {
55
73
  return setUpJRuby(appContext, System.out);
56
74
  }
57
75
 
58
- public static synchronized ScriptingContainer setUpJRuby(Context appContext, PrintStream out) {
59
- if (ruby == null) {
60
-
61
- /*
62
- http://www.anddev.org/view-layout-resource-problems-f27/dexclassloader-problem-t14666.html
63
- http://lsd.luminis.nl/osgi-on-google-android-using-apache-felix/
76
+ public static synchronized boolean setUpJRuby(Context appContext, PrintStream out) {
77
+ if (!initialized) {
78
+ Log.d(TAG, "Setting up JRuby runtime");
79
+ System.setProperty("jruby.bytecode.version", "1.5");
80
+ System.setProperty("jruby.interfaces.useProxy", "true");
81
+ System.setProperty("jruby.management.enabled", "false");
64
82
 
65
- String packagePath = "org.ruboto.core";
66
- String classPath = "org.jruby.embed.ScriptingContainer";
83
+ ClassLoader classLoader;
84
+ Class<?> scriptingContainerClass;
67
85
 
68
- String apkName = null;
69
86
  try {
70
- apkName = getPackageManager().getApplicationInfo(packagePath,0).sourceDir;
71
- } catch (PackageManager.NameNotFoundException e) {
72
- // catch this
87
+ scriptingContainerClass = Class.forName("org.jruby.embed.ScriptingContainer");
88
+ System.out.println("Found JRuby in this APK");
89
+ classLoader = Script.class.getClassLoader();
90
+ } catch (ClassNotFoundException e1) {
91
+ String packagePath = "org.ruboto.core";
92
+ String apkName = null;
93
+ try {
94
+ apkName = appContext.getPackageManager().getApplicationInfo(packagePath, 0).sourceDir;
95
+ } catch (PackageManager.NameNotFoundException e) {
96
+ System.out.println("JRuby not found");
97
+ return false;
98
+ }
99
+
100
+ System.out.println("Found JRuby in platform APK");
101
+ classLoader = new PathClassLoader(apkName, Script.class.getClassLoader());
102
+ try {
103
+ scriptingContainerClass = Class.forName("org.jruby.embed.ScriptingContainer", true, classLoader);
104
+ } catch (ClassNotFoundException e) {
105
+ // FIXME(uwe): ScriptingContainer not found in the platform APK...
106
+ e.printStackTrace();
107
+ return false;
108
+ }
73
109
  }
74
110
 
75
- // add path to apk that contains classes you wish to load
76
- String extraApkPath = apkName + ":/path/to/extraLib.apk"
111
+ try {
112
+ try {
113
+ JRUBY_VERSION = (String) Class.forName("org.jruby.runtime.Constants", true, classLoader).getDeclaredField("VERSION").get(String.class);
114
+ } catch (java.lang.NoSuchFieldException nsfex) {
115
+ nsfex.printStackTrace();
116
+ JRUBY_VERSION = "ERROR";
117
+ }
77
118
 
78
- PathClassLoader pathClassLoader = new dalvik.system.PathClassLoader(apkName, ClassLoader.getSystemClassLoader());
119
+ Class scopeClass = Class.forName("org.jruby.embed.LocalContextScope", true, scriptingContainerClass.getClassLoader());
120
+ Class behaviorClass = Class.forName("org.jruby.embed.LocalVariableBehavior", true, scriptingContainerClass.getClassLoader());
79
121
 
80
- try {
81
- Class<?> handler = Class.forName(classPath, true, pathClassLoader);
82
- } catch (ClassNotFoundException e) {
83
- // catch this
84
- }
85
- */
122
+ ruby = scriptingContainerClass
123
+ .getConstructor(scopeClass, behaviorClass)
124
+ .newInstance(Enum.valueOf(scopeClass, localContextScope),
125
+ Enum.valueOf(behaviorClass, localVariableBehavior));
86
126
 
87
- Log.d(TAG, "Setting up JRuby runtime");
88
- System.setProperty("jruby.bytecode.version", "1.5");
89
- System.setProperty("jruby.interfaces.useProxy", "true");
90
- System.setProperty("jruby.management.enabled", "false");
127
+ Class compileModeClass = Class.forName("org.jruby.RubyInstanceConfig$CompileMode", true, classLoader);
128
+ callScriptingContainerMethod(Void.class, "setCompileMode", Enum.valueOf(compileModeClass, "OFF"));
91
129
 
92
- // ruby = new ScriptingContainer(LocalContextScope.THREADSAFE);
93
- ruby = new ScriptingContainer();
94
- RubyInstanceConfig config = ruby.getProvider().getRubyInstanceConfig();
95
- config.setCompileMode(RubyInstanceConfig.CompileMode.OFF);
130
+ // callScriptingContainerMethod(Void.class, "setClassLoader", classLoader);
131
+ Method setClassLoaderMethod = ruby.getClass().getMethod("setClassLoader", ClassLoader.class);
132
+ setClassLoaderMethod.invoke(ruby, classLoader);
96
133
 
97
- config.setLoader(Script.class.getClassLoader());
98
- if (scriptsDir != null) {
99
- Log.d(TAG, "Setting JRuby current directory to " + scriptsDir);
100
- config.setCurrentDirectory(scriptsDir);
101
- }
102
- if (out != null) {
103
- config.setOutput(out);
104
- config.setError(out);
134
+ Thread.currentThread().setContextClassLoader(classLoader);
135
+
136
+ if (scriptsDir != null) {
137
+ Log.d(TAG, "Setting JRuby current directory to " + scriptsDir);
138
+ callScriptingContainerMethod(Void.class, "setCurrentDirectory", scriptsDir);
139
+ }
140
+ if (out != null) {
141
+ // callScriptingContainerMethod(Void.class, "setOutput", out);
142
+ Method setOutputMethod = ruby.getClass().getMethod("setOutput", PrintStream.class);
143
+ setOutputMethod.invoke(ruby, out);
144
+
145
+ // callScriptingContainerMethod(Void.class, "setError", out);
146
+ Method setErrorMethod = ruby.getClass().getMethod("setError", PrintStream.class);
147
+ setErrorMethod.invoke(ruby, out);
148
+ }
149
+ String extraScriptsDir = scriptsDirName(appContext);
150
+ Log.i(TAG, "Checking scripts in " + extraScriptsDir);
151
+ if (configDir(extraScriptsDir)) {
152
+ Log.i(TAG, "Added extra scripts path: " + extraScriptsDir);
153
+ }
154
+ initialized = true;
155
+ } catch (ClassNotFoundException e) {
156
+ handleInitException(e);
157
+ } catch (IllegalArgumentException e) {
158
+ handleInitException(e);
159
+ } catch (SecurityException e) {
160
+ handleInitException(e);
161
+ } catch (InstantiationException e) {
162
+ handleInitException(e);
163
+ } catch (IllegalAccessException e) {
164
+ handleInitException(e);
165
+ } catch (InvocationTargetException e) {
166
+ handleInitException(e);
167
+ } catch (NoSuchMethodException e) {
168
+ handleInitException(e);
105
169
  }
106
- copyScriptsIfNeeded(appContext);
107
- initialized = true;
108
- } else {
109
- while (!initialized) {
110
- Log.i(TAG, "Waiting for JRuby runtime to initialize.");
111
- try {
112
- Thread.sleep(1000);
113
- } catch (InterruptedException iex) {
114
- }
115
- }
116
- }
170
+ }
171
+ return initialized;
172
+ }
117
173
 
118
- return ruby;
174
+ private static void handleInitException(Exception e) {
175
+ Log.e(TAG, "Exception starting JRuby");
176
+ Log.e(TAG, e.getMessage() != null ? e.getMessage() : e.getClass().getName());
177
+ e.printStackTrace();
178
+ ruby = null;
119
179
  }
120
180
 
121
- public static String execute(String code) {
181
+ @SuppressWarnings("unchecked")
182
+ public static <T> T callScriptingContainerMethod(Class<T> returnType, String methodName, Object... args) {
183
+ Class<?>[] argClasses = new Class[args.length];
184
+ for (int i = 0; i < argClasses.length; i++) {
185
+ argClasses[i] = args[i].getClass();
186
+ }
122
187
  try {
123
- return getRuby().callMethod(exec(code), "inspect", String.class);
124
- } catch (RaiseException re) {
125
- re.printStackTrace(ruby.getError());
126
- return null;
188
+ Method method = ruby.getClass().getMethod(methodName, argClasses);
189
+ System.out.println("callScriptingContainerMethod: method: " + method);
190
+ T result = (T) method.invoke(ruby, args);
191
+ System.out.println("callScriptingContainerMethod: result: " + result);
192
+ return result;
193
+ } catch (RuntimeException re) {
194
+ re.printStackTrace();
195
+ } catch (IllegalAccessException e) {
196
+ // TODO Auto-generated catch block
197
+ e.printStackTrace();
198
+ } catch (InvocationTargetException e) {
199
+ try {
200
+ e.printStackTrace();
201
+ } catch (NullPointerException npe) {
202
+ }
203
+ } catch (NoSuchMethodException e) {
204
+ // TODO Auto-generated catch block
205
+ e.printStackTrace();
127
206
  }
207
+ return null;
128
208
  }
129
209
 
130
- public static Object exec(String code) throws RaiseException {
131
- return ruby.runScriptlet(code);
210
+ public static String execute(String code) {
211
+ Object result = exec(code);
212
+ return result != null ? result.toString() : "nil";
213
+ // TODO: Why is callMethod returning "main"?
214
+ // return result != null ? callMethod(result, "inspect", String.class) : "null";
215
+ }
216
+
217
+ public static Object exec(String code) {
218
+ // return callScriptingContainerMethod(Object.class, "runScriptlet", code);
219
+ try {
220
+ Method runScriptletMethod = ruby.getClass().getMethod("runScriptlet", String.class);
221
+ return runScriptletMethod.invoke(ruby, code);
222
+ } catch (NoSuchMethodException nsme) {
223
+ throw new RuntimeException(nsme);
224
+ } catch (IllegalAccessException iae) {
225
+ throw new RuntimeException(iae);
226
+ } catch (java.lang.reflect.InvocationTargetException ite) {
227
+ throw ((RuntimeException) ite.getCause());
228
+ }
132
229
  }
133
230
 
134
231
  public static void defineGlobalConstant(String name, Object object) {
135
- ruby.put(name, object);
232
+ put(name, object);
136
233
  }
137
-
138
- public static void defineGlobalVariable(String name, Object object) {
139
- ruby.put(name, object);
234
+
235
+ public static void put(String name, Object object) {
236
+ // callScriptingContainerMethod(Void.class, "put", name, object);
237
+ try {
238
+ Method putMethod = ruby.getClass().getMethod("put", String.class, Object.class);
239
+ putMethod.invoke(ruby, name, object);
240
+ } catch (NoSuchMethodException nsme) {
241
+ throw new RuntimeException(nsme);
242
+ } catch (IllegalAccessException iae) {
243
+ throw new RuntimeException(iae);
244
+ } catch (java.lang.reflect.InvocationTargetException ite) {
245
+ throw new RuntimeException(ite);
246
+ }
140
247
  }
141
248
 
142
- public static ScriptingContainer getRuby() {
143
- return ruby;
249
+ public static void defineGlobalVariable(String name, Object object) {
250
+ defineGlobalConstant(name, object);
144
251
  }
145
252
 
146
253
  /*************************************************************************************************
@@ -151,11 +258,11 @@ public class Script {
151
258
  public static void setDir(String dir) {
152
259
  scriptsDir = dir;
153
260
  scriptsDirFile = new File(dir);
154
- if (ruby != null) {
261
+ if (ruby != null) {
155
262
  Log.d(TAG, "Changing JRuby current directory to " + scriptsDir);
156
- ruby.setCurrentDirectory(scriptsDir);
263
+ callScriptingContainerMethod(Void.class, "setCurrentDirectory", scriptsDir);
157
264
  }
158
- }
265
+ }
159
266
 
160
267
  public static String getDir() {
161
268
  return scriptsDir;
@@ -165,25 +272,34 @@ public class Script {
165
272
  return scriptsDirFile;
166
273
  }
167
274
 
168
- public static Boolean configDir(String noSdcard) {
169
- setDir(noSdcard);
170
- if (!ruby.getLoadPaths().contains(noSdcard)) {
171
- Log.i(TAG, "Adding scripts dir to load path: " + noSdcard);
172
- java.util.List paths = ruby.getLoadPaths();
173
- paths.add(noSdcard);
174
- ruby.setLoadPaths(paths);
275
+ public static Boolean configDir(String scriptsDir) {
276
+ setDir(scriptsDir);
277
+ Method getLoadPathsMethod;
278
+ List<String> loadPath = callScriptingContainerMethod(List.class, "getLoadPaths");
279
+
280
+ if (!loadPath.contains(scriptsDir)) {
281
+ Log.i(TAG, "Adding scripts dir to load path: " + scriptsDir);
282
+ loadPath.add(0, scriptsDir);
283
+ // callScriptingContainerMethod(Void.class, "setLoadPaths", loadPath);
284
+ try {
285
+ Method setLoadPathsMethod = ruby.getClass().getMethod("setLoadPaths", List.class);
286
+ setLoadPathsMethod.invoke(ruby, loadPath);
287
+ } catch (NoSuchMethodException nsme) {
288
+ throw new RuntimeException(nsme);
289
+ } catch (IllegalAccessException iae) {
290
+ throw new RuntimeException(iae);
291
+ } catch (java.lang.reflect.InvocationTargetException ite) {
292
+ throw new RuntimeException(ite);
293
+ }
175
294
  }
176
295
 
177
- /* Create directory if it doesn't exist */
178
- if (!scriptsDirFile.exists()) {
179
- boolean dirCreatedOk = scriptsDirFile.mkdirs();
180
- if (!dirCreatedOk) {
181
- throw new RuntimeException("Unable to create script directory");
182
- }
296
+ if (scriptsDirFile.exists()) {
297
+ Log.i(TAG, "Found extra scripts dir: " + scriptsDir);
183
298
  return true;
299
+ } else {
300
+ Log.i(TAG, "Extra scripts dir not present: " + scriptsDir);
301
+ return false;
184
302
  }
185
-
186
- return false;
187
303
  }
188
304
 
189
305
  private static void copyScripts(String from, File to, AssetManager assets) {
@@ -240,35 +356,35 @@ public class Script {
240
356
  }
241
357
 
242
358
  private static String scriptsDirName(Context context) {
243
- File toFile = null;
359
+ File storageDir = null;
244
360
  if (isDebugBuild(context)) {
245
361
 
246
- // FIXME(uwe): Simplify this as soon as we drop support for android-7 or JRuby 1.5.6 or JRuby 1.6.2
247
- Log.i(TAG, "JRuby VERSION: " + org.jruby.runtime.Constants.VERSION);
248
- if (!org.jruby.runtime.Constants.VERSION.equals("1.5.6") && !org.jruby.runtime.Constants.VERSION.equals("1.6.2") && android.os.Build.VERSION.SDK_INT >= 8) {
249
- ruby.put("script_context", context);
250
- toFile = (File) exec("script_context.getExternalFilesDir(nil)");
362
+ // FIXME(uwe): Simplify this as soon as we drop support for android-7 or JRuby 1.5.6 or JRuby 1.6.2
363
+ Log.i(TAG, "JRuby VERSION: " + JRUBY_VERSION);
364
+ if (!JRUBY_VERSION.equals("1.5.6") && !JRUBY_VERSION.equals("1.6.2") && android.os.Build.VERSION.SDK_INT >= 8) {
365
+ put("script_context", context);
366
+ storageDir = (File) exec("script_context.getExternalFilesDir(nil)");
251
367
  } else {
252
- toFile = new File(Environment.getExternalStorageDirectory(), "Android/data/" + context.getPackageName() + "/files");
253
- Log.e(TAG, "Calculated path to sdcard the old way: " + toFile);
368
+ storageDir = new File(Environment.getExternalStorageDirectory(), "Android/data/" + context.getPackageName() + "/files");
369
+ Log.e(TAG, "Calculated path to sdcard the old way: " + storageDir);
254
370
  }
255
371
  // FIXME end
256
372
 
257
- if (toFile == null || (!toFile.exists() && !toFile.mkdirs())) {
258
- Log.e(TAG,
373
+ if (storageDir == null || (!storageDir.exists() && !storageDir.mkdirs())) {
374
+ Log.e(TAG,
259
375
  "Development mode active, but sdcard is not available. Make sure you have added\n<uses-permission android:name='android.permission.WRITE_EXTERNAL_STORAGE' />\nto your AndroidManifest.xml file.");
260
- toFile = context.getFilesDir();
376
+ storageDir = context.getFilesDir();
261
377
  }
262
- } else {
263
- toFile = context.getFilesDir();
378
+ } else {
379
+ storageDir = context.getFilesDir();
264
380
  }
265
- String to = toFile.getAbsolutePath() + "/scripts";
266
- return to;
381
+ return storageDir.getAbsolutePath() + "/scripts";
267
382
  }
268
383
 
269
384
  private static void copyScriptsIfNeeded(Context context) {
270
385
  String to = scriptsDirName(context);
271
386
  Log.i(TAG, "Checking scripts in " + to);
387
+
272
388
  /* the if makes sure we only do this the first time */
273
389
  if (configDir(to)) {
274
390
  Log.i(TAG, "Copying scripts to " + to);
@@ -283,12 +399,7 @@ public class Script {
283
399
  */
284
400
 
285
401
  public Script(String name) {
286
- this(name, null);
287
- }
288
-
289
- public Script(String name, String contents) {
290
402
  this.name = name;
291
- this.contents = contents;
292
403
  }
293
404
 
294
405
  /*************************************************************************************************
@@ -306,12 +417,17 @@ public class Script {
306
417
 
307
418
  public Script setName(String name) {
308
419
  this.name = name;
309
- // TODO: Other states possible
310
420
  return this;
311
421
  }
312
422
 
313
423
  public String getContents() throws IOException {
314
- BufferedReader buffer = new BufferedReader(new FileReader(getFile()), 8192);
424
+ InputStream is;
425
+ if (new File(scriptsDir + "/" + name).exists()) {
426
+ is = new java.io.FileInputStream(scriptsDir + "/" + name);
427
+ } else {
428
+ is = getClass().getClassLoader().getResourceAsStream(name);
429
+ }
430
+ BufferedReader buffer = new BufferedReader(new java.io.InputStreamReader(is), 8192);
315
431
  StringBuilder source = new StringBuilder();
316
432
  while (true) {
317
433
  String line = buffer.readLine();
@@ -321,8 +437,7 @@ public class Script {
321
437
  source.append(line).append("\n");
322
438
  }
323
439
  buffer.close();
324
- contents = source.toString();
325
- return contents;
440
+ return source.toString();
326
441
  }
327
442
 
328
443
  /*************************************************************************************************
@@ -330,8 +445,65 @@ public class Script {
330
445
  * Script Actions
331
446
  */
332
447
 
448
+ public static String getScriptFilename() {
449
+ return callScriptingContainerMethod(String.class, "getScriptFilename");
450
+ }
451
+
452
+ public static void setScriptFilename(String name) {
453
+ callScriptingContainerMethod(Void.class, "setScriptFilename", name);
454
+ }
455
+
333
456
  public String execute() throws IOException {
334
- Script.getRuby().setScriptFilename(name);
457
+ Script.setScriptFilename(getClass().getClassLoader().getResource(name).getPath());
335
458
  return Script.execute(getContents());
336
459
  }
460
+
461
+ public static void callMethod(Object receiver, String methodName, Object[] args) {
462
+ // callScriptingContainerMethod(Void.class, "callMethod", receiver, methodName, args);
463
+ try {
464
+ Method callMethodMethod = ruby.getClass().getMethod("callMethod", Object.class, String.class, Object[].class);
465
+ callMethodMethod.invoke(ruby, receiver, methodName, args);
466
+ } catch (NoSuchMethodException nsme) {
467
+ throw new RuntimeException(nsme);
468
+ } catch (IllegalAccessException iae) {
469
+ throw new RuntimeException(iae);
470
+ } catch (java.lang.reflect.InvocationTargetException ite) {
471
+ throw (RuntimeException)(ite.getCause());
472
+ }
473
+ }
474
+
475
+ public static void callMethod(Object object, String methodName, Object arg) {
476
+ callMethod(object, methodName, new Object[] { arg });
477
+ }
478
+
479
+ public static void callMethod(Object object, String methodName) {
480
+ callMethod(object, methodName, new Object[] {});
481
+ }
482
+
483
+ @SuppressWarnings("unchecked")
484
+ public static <T> T callMethod(Object receiver, String methodName, Object[] args, Class<T> returnType) {
485
+ // return callScriptingContainerMethod(returnType, "callMethod", receiver, methodName, args, returnType);
486
+ try {
487
+ Method callMethodMethod = ruby.getClass().getMethod("callMethod", Object.class, String.class, Object[].class, Class.class);
488
+ return (T) callMethodMethod.invoke(ruby, receiver, methodName, args, returnType);
489
+ } catch (NoSuchMethodException nsme) {
490
+ throw new RuntimeException(nsme);
491
+ } catch (IllegalAccessException iae) {
492
+ throw new RuntimeException(iae);
493
+ } catch (java.lang.reflect.InvocationTargetException ite) {
494
+ throw (RuntimeException) ite.getCause();
495
+ }
496
+ }
497
+
498
+ public static <T> T callMethod(Object receiver, String methodName,
499
+ Object arg, Class<T> returnType) {
500
+ return callMethod(receiver, methodName, new Object[]{arg}, returnType);
501
+ }
502
+
503
+ public static <T> T callMethod(Object receiver, String methodName,
504
+ Class<T> returnType) {
505
+ return callMethod(receiver, methodName, new Object[]{}, returnType);
506
+ }
507
+
337
508
  }
509
+
@@ -13,18 +13,14 @@ import junit.framework.AssertionFailedError;
13
13
  import junit.framework.Test;
14
14
  import junit.framework.TestResult;
15
15
  import junit.framework.TestSuite;
16
- import org.jruby.exceptions.RaiseException;
17
- import org.jruby.javasupport.JavaUtil;
18
- import org.jruby.javasupport.util.RuntimeHelpers;
19
- import org.jruby.runtime.builtin.IRubyObject;
20
16
  import org.ruboto.Script;
21
17
 
22
18
  public class ActivityTest extends ActivityInstrumentationTestCase2 {
23
19
  private final Object setup;
24
- private final IRubyObject block;
20
+ private final Object block;
25
21
  private final String filename;
26
22
 
27
- public ActivityTest(Class activityClass, String filename, IRubyObject setup, String name, IRubyObject block) {
23
+ public ActivityTest(Class activityClass, String filename, Object setup, String name, Object block) {
28
24
  super(activityClass.getPackage().getName(), activityClass);
29
25
  this.filename = filename;
30
26
  this.setup = setup;
@@ -36,29 +32,32 @@ public class ActivityTest extends ActivityInstrumentationTestCase2 {
36
32
  public void runTest() throws Exception {
37
33
  Log.i(getClass().getName(), "runTest");
38
34
  Log.i(getClass().getName(), "runTest: " + getName());
39
- Script.setUpJRuby(getActivity());
40
- Log.i(getClass().getName(), "ruby ok");
41
- try {
42
- final Activity activity = getActivity();
43
- Log.i(getClass().getName(), "activity ok");
44
- runTestOnUiThread(new Runnable() {
45
- public void run() {
46
- String oldFile = Script.getRuby().getScriptFilename();
35
+ if (Script.setUpJRuby(getActivity())) {
36
+ Log.i(getClass().getName(), "ruby ok");
37
+ try {
38
+ final Activity activity = getActivity();
39
+ Log.i(getClass().getName(), "activity ok");
40
+ runTestOnUiThread(new Runnable() {
41
+ public void run() {
42
+ String oldFile = Script.getScriptFilename();
47
43
 
48
- Log.i(getClass().getName(), "calling setup");
49
- Script.getRuby().setScriptFilename(filename);
50
- Script.getRuby().callMethod(setup, "call", activity);
51
- Log.i(getClass().getName(), "setup ok");
44
+ Log.i(getClass().getName(), "calling setup");
45
+ Script.setScriptFilename(filename);
46
+ Script.callMethod(setup, "call", activity);
47
+ Log.i(getClass().getName(), "setup ok");
52
48
 
53
- Script.getRuby().setScriptFilename(filename);
54
- Script.getRuby().callMethod(block, "call", activity);
55
- Script.getRuby().setScriptFilename(oldFile);
56
- }
57
- });
58
- } catch (Throwable t) {
59
- throw new AssertionFailedError(t.getMessage());
49
+ Script.setScriptFilename(filename);
50
+ Script.callMethod(block, "call", activity);
51
+ Script.setScriptFilename(oldFile);
52
+ }
53
+ });
54
+ } catch (Throwable t) {
55
+ throw new AssertionFailedError(t.getMessage() != null ? t.getMessage() : t.getClass().getName());
56
+ }
57
+ Log.i(getClass().getName(), "runTest ok");
58
+ } else {
59
+ throw new AssertionFailedError("Ruboto Core platform is missing.");
60
60
  }
61
- Log.i(getClass().getName(), "runTest ok");
62
61
  }
63
62
 
64
63
  }