Example #1
0
  private Task createTaskObject(
      Project project, Class<? extends Task> type, String name, boolean generateGetters) {
    if (!Task.class.isAssignableFrom(type)) {
      throw new GradleException(
          String.format(
              "Cannot create task of type '%s' as it does not implement the Task interface.",
              type.getSimpleName()));
    }

    Class<? extends Task> generatedType;
    if (generateGetters) {
      generatedType = generator.generate(type);
    } else {
      generatedType = type;
    }

    Constructor<? extends Task> constructor = null;
    Object[] params = null;
    try {
      constructor = generatedType.getDeclaredConstructor();
      params = new Object[0];
    } catch (NoSuchMethodException e) {
      // Ignore
    }
    try {
      constructor = generatedType.getDeclaredConstructor(Project.class, String.class);
      params = new Object[] {project, name};
    } catch (NoSuchMethodException e) {
      // Ignore
    }
    if (constructor == null) {
      throw new GradleException(
          String.format(
              "Cannot create task of type '%s' as it does not have an appropriate public constructor.",
              type.getSimpleName()));
    }

    AbstractTask.injectIntoNextInstance(project, name);
    try {
      return constructor.newInstance(params);
    } catch (InvocationTargetException e) {
      throw new GradleException(
          String.format("Could not create task of type '%s'.", type.getSimpleName()), e.getCause());
    } catch (Exception e) {
      throw new GradleException(
          String.format("Could not create task of type '%s'.", type.getSimpleName()), e);
    } finally {
      AbstractTask.injectIntoNextInstance(null, null);
    }
  }
Example #2
0
  @Test
  public void testExecuteDelegatesToTaskExecuter() {
    final AbstractTask task = getTask();

    final TaskExecuter executer = context.mock(TaskExecuter.class);
    task.setExecuter(executer);

    context.checking(
        new Expectations() {
          {
            one(executer).execute(with(sameInstance(task)), with(notNullValue(TaskState.class)));
          }
        });

    task.execute();
  }
Example #3
0
 @Test
 public void testCanSpecifyOnlyIfPredicateUsingClosure() {
   AbstractTask task = getTask();
   task.onlyIf(HelperUtil.toClosure("{ task -> false }"));
   assertFalse(task.getOnlyIf().isSatisfiedBy(task));
 }