package org.jboss.forge.projects.impl;

/*
 * Copyright 2012 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.arquillian.Addon;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.container.addons.AddonId;
import org.jboss.forge.container.repositories.AddonDependencyEntry;
import org.jboss.forge.container.util.Files;
import org.jboss.forge.resource.DirectoryResource;
import org.jboss.forge.resource.ResourceFactory;
import org.jboss.forge.ui.context.UIBuilder;
import org.jboss.forge.ui.context.UIContext;
import org.jboss.forge.ui.context.UIContextBase;
import org.jboss.forge.ui.context.UIValidationContext;
import org.jboss.forge.ui.input.InputComponent;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class NewProjectWizardTest
{
   @Deployment
   @Dependencies({
            @Addon(name = "org.jboss.forge:projects", version = "2.0.0-SNAPSHOT"),
            @Addon(name = "org.jboss.forge:ui", version = "2.0.0-SNAPSHOT")
   })
   public static ForgeArchive getDeployment()
   {
      ForgeArchive archive = ShrinkWrap
               .create(ForgeArchive.class)
               .addBeansXML()
               .addAsAddonDependencies(
                        AddonDependencyEntry.create(AddonId.from("org.jboss.forge:resources", "2.0.0-SNAPSHOT")),
                        AddonDependencyEntry.create(AddonId.from("org.jboss.forge:projects", "2.0.0-SNAPSHOT")),
                        AddonDependencyEntry.create(AddonId.from("org.jboss.forge:ui", "2.0.0-SNAPSHOT"))
               );

      return archive;
   }

   @Inject
   private NewProjectWizard command;
   @Inject
   private NewProjectWizard command2;

   @Inject
   private ResourceFactory factory;

   @Test
   public void testInjectionNotNull()
   {
      Assert.assertNotNull(command);
   }

   @Test
   public void testInvokeCommand() throws Exception
   {
      final List<InputComponent<?, ?>> inputs = new ArrayList<InputComponent<?, ?>>();

      final UIContext context = new UIContextBase();
      final UIBuilder builder = new UIBuilder()
      {
         @Override
         public UIBuilder add(InputComponent<?, ?> input)
         {
            inputs.add(input);
            return this;
         }

         @Override
         public UIContext getUIContext()
         {
            return context;
         }
      };

      File tempDir = File.createTempFile("forge", "projectTests");
      tempDir.delete();

      try
      {
         command.initializeUI(builder);

         Assert.assertFalse(command.getOverwrite().isEnabled());

         command.getTargetLocation().setValue(factory.create(DirectoryResource.class, tempDir));
         command.getNamed().setValue("test");

         Assert.assertFalse(command.getOverwrite().isEnabled());

         command.getTopLevelPackage().setValue("org.example");

         command.validate(new UIValidationContext()
         {
            @Override
            public UIContext getUIContext()
            {
               return context;
            }

            @Override
            public void addValidationError(InputComponent<?, ?> input, String errorMessage)
            {
            }
         });

         DirectoryResource targetDirectory = command.getTargetLocation().getValue().getChildDirectory("test");

         Assert.assertFalse(targetDirectory.exists());
         command.execute(context);
         Assert.assertTrue(targetDirectory.exists());

         targetDirectory.delete(true);
      }
      finally
      {
         Files.delete(tempDir, true);
      }
   }

   @Test
   public void testOverwriteEnabledWhenTargetDirectoryExistsNotEmpty() throws Exception
   {
      final List<InputComponent<?, ?>> inputs = new ArrayList<InputComponent<?, ?>>();

      final UIContext context = new UIContextBase();
      final UIBuilder builder = new UIBuilder()
      {
         @Override
         public UIBuilder add(InputComponent<?, ?> input)
         {
            inputs.add(input);
            return this;
         }

         @Override
         public UIContext getUIContext()
         {
            return context;
         }
      };

      File tempDir = File.createTempFile("forge", "projectTests");
      tempDir.delete();
      new File(tempDir, "test/something").mkdirs();

      try
      {
         command2.initializeUI(builder);

         Assert.assertFalse(command2.getOverwrite().isEnabled());

         command2.getTargetLocation().setValue(factory.create(DirectoryResource.class, tempDir));
         command2.getNamed().setValue("test");

         Assert.assertTrue(command2.getOverwrite().isEnabled());
      }
      finally
      {
         Files.delete(tempDir, true);
      }
   }
}

@@ -5176,9 +5176,8 @@
 %7D%0A   %7D%0A%7D
-%0A

Refactor ProjectType API to decouple ProjectFactory from UI