Пример #1
0
  protected void configureMethod(ITestResult testResult) {
    if (SystemProperties.isSeleniumDebug()) {
      System.out.println("$configureMethod");
    }

    Method realMethod = getCurrentRealMethod();

    if (realMethod == null) {
      throw new IllegalStateException("can't find more configured methods");
    }

    Class<?> realClass = realMethod.getDeclaringClass();

    final Object testInstance = testResult.getInstance();
    Configuration configuration = configurations.get(realClass).get(realMethod);
    if (!configuration.hasNext()) {
      throw new IllegalStateException("can't find more configurations");
    }
    currentConfiguration = configuration.next();

    try {
      for (Entry<Field, Object> entry : currentConfiguration.entrySet()) {
        final Field field = entry.getKey();
        final Object assignment = entry.getValue();

        setDeclaredFieldValue(testInstance, field, assignment);
      }
    } catch (Exception e) {
      throw new IllegalStateException("Cannot configure method", e);
    }
  }
  private void injectTestResult(final ITestResult testResult) throws ReflectiveOperationException {

    final Object testInstance = testResult.getInstance();
    if (testInstance == null) {
      return;
    }

    final Class<?> testClass = testInstance.getClass();
    if (TESTS.containsKey(testClass)) {
      return;
    }

    final Dagger dagger = testClass.getAnnotation(Dagger.class);
    if (dagger == null) {
      return;
    }

    final Class<?>[] moduleClasses = dagger.modules();
    final Object[] moduleInstances = new Object[moduleClasses.length];
    for (int i = 0; i < moduleClasses.length; i++) {
      final Class<?> moduleClass = moduleClasses[i];
      if (moduleClass.getAnnotation(Module.class) == null) {
        continue;
      }
      moduleInstances[i] = MODULES.get(moduleClass);
      if (moduleInstances[i] == null) {
        moduleInstances[i] = moduleClass.newInstance();
        MODULES.put(moduleClass, moduleInstances[i]);
      }
    }

    ObjectGraph.create(moduleInstances).inject(testInstance);

    TESTS.put(testClass, testInstance);
  }
  @Override
  public void onTestStart(ITestResult result) {
    testResult.set(result);
    final Class<?> realTestClass = obtainJavaTestClass(result);
    final Method realTestMethod = obtainJavaTestMethod(result);

    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    KnowledgeBase knowledgeBaseForClass =
        createNewKnowledgeBaseInstanceForClass(realTestClass, knowledgeBuilder);
    KnowledgeBase knowledgeBaseForMethod =
        completeKnowledgeBaseForMethod(
            knowledgeBaseForClass, realTestMethod, knowledgeBaseForClass);

    knowledgeBaseForMethod.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());

    injectKnowledgeBase(realTestClass, result.getInstance(), knowledgeBaseForMethod, result);
  }