@SuppressWarnings("unchecked")
  @Test
  public void testOptionsUsesDefaultTemplateBuilder() {
    TemplateOptions options = provideTemplateOptions();
    TemplateOptions from = provideTemplateOptions();

    Supplier<Set<? extends Location>> locations =
        Suppliers.<Set<? extends Location>>ofInstance(ImmutableSet.<Location>of());
    Supplier<Set<? extends Image>> images =
        Suppliers.<Set<? extends Image>>ofInstance(ImmutableSet.<Image>of());
    Supplier<Set<? extends Hardware>> hardwares =
        Suppliers.<Set<? extends Hardware>>ofInstance(ImmutableSet.<Hardware>of());
    Location defaultLocation = createMock(Location.class);
    Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
    Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
    TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);

    expect(templateBuilderProvider.get()).andReturn(defaultTemplate);
    expect(defaultTemplate.options(from)).andReturn(defaultTemplate);
    expect(defaultTemplate.build()).andReturn(null);
    expect(optionsProvider.get()).andReturn(from).atLeastOnce();

    replay(defaultTemplate);
    replay(defaultLocation);
    replay(optionsProvider);
    replay(templateBuilderProvider);

    TemplateBuilderImpl template =
        createTemplateBuilder(
            null,
            locations,
            images,
            hardwares,
            defaultLocation,
            optionsProvider,
            templateBuilderProvider);

    template.options(options).build();

    verify(defaultTemplate);
    verify(defaultLocation);
    verify(optionsProvider);
    verify(templateBuilderProvider);
  }
  @Override
  protected Object doExecute() throws Exception {
    ComputeService service = null;
    try {
      service = getComputeService();
    } catch (Throwable t) {
      System.err.println(t.getMessage());
      return null;
    }

    TemplateBuilder builder = service.templateBuilder();
    builder.any();
    if (smallest) {
      builder.smallest();
    }
    if (fastest) {
      builder.fastest();
    }
    if (biggest) {
      builder.biggest();
    }
    if (locationId != null) {
      builder.locationId(locationId);
    }
    if (imageId != null) {
      builder.imageId(imageId);
    }
    if (hardwareId != null) {
      builder.hardwareId(hardwareId);
    }

    if (osFamily != null) {
      builder.osFamily(OsFamily.fromValue(osFamily));
    }

    if (osVersion != null) {
      builder.osVersionMatches(osVersion);
    }

    TemplateOptions options = service.templateOptions();
    List<Statement> statements = Lists.newLinkedList();

    if (adminAccess) {
      statements.add(AdminAccess.standard());
    }
    if (recipes != null) {
      for (String recipe : recipes) {
        statements.add(recipeManager.createStatement(recipe, group));
      }
    }
    if (ec2SecurityGroups != null) {
      options.as(EC2TemplateOptions.class).securityGroups(ec2SecurityGroups);
    }
    if (ec2KeyPair != null) {
      options.as(EC2TemplateOptions.class).keyPair(ec2KeyPair);
    }
    if (ec2NoKeyPair != null) {
      options.as(EC2TemplateOptions.class).noKeyPair();
    }

    Set<? extends NodeMetadata> metadatas = null;

    if (!statements.isEmpty()) {
      options.runScript(new StatementList(statements));
    }

    try {
      metadatas = service.createNodesInGroup(group, number, builder.options(options).build());
    } catch (RunNodesException ex) {
      System.out.println("Failed to create nodes:" + ex.getMessage());
    }

    if (metadatas != null && !metadatas.isEmpty()) {
      System.out.println("Created nodes:");
      printNodes(service, metadatas, System.out);

      for (NodeMetadata node : metadatas) {
        for (String cacheKey : ServiceHelper.findCacheKeysForService(service)) {
          cacheProvider
              .getProviderCacheForType(Constants.ACTIVE_NODE_CACHE)
              .put(cacheKey, node.getId());
          cacheProvider
              .getProviderCacheForType(Constants.INACTIVE_NODE_CACHE)
              .put(cacheKey, node.getId());
          cacheProvider
              .getProviderCacheForType(Constants.SUSPENDED_NODE_CACHE)
              .put(cacheKey, node.getId());
        }
      }
    }

    return null;
  }