コード例 #1
0
 /** @return true if the class is RunWithCucumber annotated, false otherwise */
 private boolean readRunWithCucumberAnnotation(Class<?> clazz) {
   RunWithCucumber annotation = clazz.getAnnotation(RunWithCucumber.class);
   if (annotation != null) {
     // isEmpty() only available in Android API 9+
     packageOfTests = annotation.glue().equals("") ? defaultGlue() : annotation.glue();
     features = annotation.features().equals("") ? defaultFeatures() : annotation.features();
     return true;
   }
   return false;
 }
コード例 #2
0
  @Override
  public void onCreate(Bundle arguments) {
    super.onCreate(arguments);

    if (arguments == null) {
      throw new CucumberException("No arguments");
    }
    Context context = getContext();
    classLoader = context.getClassLoader();

    String apkPath = context.getPackageCodePath();
    Reflections reflections = new DexReflections(newDexFile(apkPath));

    // For glue and features either use the provided arguments or try to find a RunWithCucumber
    // annotated class.
    // If nothing works, default values will be used instead.
    if (arguments.containsKey(ARGUMENT_TEST_CLASS)
        || arguments.containsKey(ARGUMENT_TEST_PACKAGE)) {

      String testClass = arguments.getString(ARGUMENT_TEST_CLASS);
      testClass = testClass != null ? testClass : "null";
      packageOfTests = arguments.getString(ARGUMENT_TEST_PACKAGE);

      try {
        Class<?> clazz = classLoader.loadClass(testClass);
        boolean annotationWasPresent = readRunWithCucumberAnnotation(clazz);

        // If the class is not RunWithCucumber annotated, maybe it's Cucumber annotated?
        if (!annotationWasPresent) {
          SEARCH_ANNOTATION:
          for (Method m : clazz.getMethods()) {
            for (Annotation a : m.getAnnotations()) {
              if (a.annotationType().getName().startsWith("cucumber") && packageOfTests == null) {
                packageOfTests = testClass.substring(0, testClass.lastIndexOf("."));
                break SEARCH_ANNOTATION;
              }
            }
          }
        }
      } catch (ClassNotFoundException e) {
        Log.w(TAG, e.toString());
      }
    } else {
      for (Class<?> clazz : reflections.getDescendants(Object.class, context.getPackageName())) {
        if (readRunWithCucumberAnnotation(clazz)) break;
      }
    }

    Properties properties = new Properties();
    packageOfTests = packageOfTests != null ? packageOfTests : defaultGlue();
    features = features != null ? features : defaultFeatures();

    properties.setProperty("cucumber.options", String.format("-g %s %s", packageOfTests, features));
    runtimeOptions = new RuntimeOptions(properties);

    resourceLoader = new AndroidResourceLoader(context);

    List<Backend> backends = new ArrayList<Backend>();
    backends.add(new JavaBackend(new AndroidObjectFactory(this), reflections));
    runtime = new Runtime(resourceLoader, classLoader, backends, runtimeOptions);

    start();
  }