/*-
 * Copyright (C) 2013-2014 The JBromo Authors.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jbromo.common.test.arquillian;

import org.apache.commons.lang3.StringUtils;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.descriptor.api.beans10.BeansDescriptor;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.jbromo.common.test.cdi.CdiRunner;

/**
 * Define utility class for Arquillian Test.
 * @author qjafcunuas
 */
public final class ArquillianUtil {

    /**
     * Default constructor.
     */
    private ArquillianUtil() {
        super();
    }

    /**
     * Creates a new archive of the specified type. The archive will be backed by the default without beans.xml file.
     * @param classToTest the class to test.
     * @param packageToLoad the package to load into the archive.
     * @return the archive to deploy.
     */
    private static JavaArchive createArchive(final Class<?> classToTest, final Package... packageToLoad) {
        return ShrinkWrap.create(JavaArchive.class, classToTest.getSimpleName() + ".jar").addPackages(true, packageToLoad)
                .deleteClass(CdiRunner.class);
    }

    /**
     * Creates a new archive of the specified type. The archive will be be backed by the default.
     * @param classToTest the class to test.
     * @param beans the beans descriptor.
     * @param packageToLoad the package to load into the archive.
     * @return the archive to deploy.
     */
    public static JavaArchive createTestArchive(final Class<?> classToTest, final BeansDescriptor beans, final Package... packageToLoad) {
        final JavaArchive arch = createArchive(classToTest, packageToLoad);
        setBeansDescriptor(beans, arch);
        return arch;
    }

    /**
     * Creates a new archive of the specified type. The archive will be be backed by the default
     * @param classToTest the class to test.
     * @param beans the beans descriptor.
     * @param packageToLoad the package to load into the archive.
     * @return the archive to deploy.
     */
    public static JavaArchive createTestDependenciesArchive(final Class<?> classToTest, final BeansDescriptor beans, final Package... packageToLoad) {

        final JavaArchive arch = createTestArchive(classToTest, null, packageToLoad);
        loadRuntimeAndTestDependencies(arch);
        setBeansDescriptor(beans, arch);

        return arch;
    }

    /**
     * Set beans.xml file into archive.
     * @param beans the beans to set. if null, set an empty file.
     * @param arch the archive to set file.
     */
    private static void setBeansDescriptor(final BeansDescriptor beans, final JavaArchive arch) {
        if (beans == null) {
            arch.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
        } else {
            arch.addAsManifestResource(new StringAsset(beans.exportAsString()), "beans.xml");
        }
    }

    /**
     * Load runtime and test dependencies libraries into an archive.
     * @param arch the archive to add dependencies libraries.
     */
    private static void loadRuntimeAndTestDependencies(final JavaArchive arch) {
        final JavaArchive[] libs = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeAndTestDependencies().resolve().withTransitivity()
                .as(JavaArchive.class);
        for (final JavaArchive one : libs) {
            if (one.getName().contains("bromo") || one.getName().contains("commons-lang3") || one.getName().contains("commons-collections")) {
                for (final ArchivePath path : one.getContent().keySet()) {
                    if (path.get().indexOf(".class") > 0) {
                        arch.addClass(path.get().replace("/", ".").substring(1, path.get().indexOf(".class")));
                    }
                }
            }
        }
        arch.addClass(StringUtils.class);
        arch.deleteClass(CdiRunner.class);
    }

}

@@ -1178,45 +1178,23 @@
 ort 
-org.apache.commons.lang3.StringUtils;
+java.util.Set;%0A
 %0Aimp
@@ -1552,16 +1552,50 @@
 .Maven;%0A
+import org.jbromo.common.SetUtil;%0A
 import o
@@ -4386,32 +4386,36 @@
 %7B%0A        final 
+Set%3C
 JavaArchive%5B%5D li
@@ -4413,17 +4413,138 @@
 hive
-%5B%5D
+%3E
  libs =
+ SetUtil.toSet();%0A        // Add compile and runtime dependencies, except jbromo lib.%0A        for (final JavaArchive one :
  Mav
@@ -4590,30 +4590,33 @@
 ).import
+CompileAnd
 Runtime
-AndTest
 Dependen
@@ -4693,54 +4693,109 @@
 ass)
-;%0A        for (final JavaArchive one : libs) %7B
+) %7B%0A            if (!one.getName().contains(%22jbromo%22)) %7B%0A                libs.add(one);%0A            %7D
 %0A   
@@ -4803,147 +4803,420 @@
     
+%7D%0A
     
-if (one.getName().contains(%22bromo%22) %7C%7C one.getName().contains(%22commons-lang3%22) %7C%7C one.getName().contains(%22commons-collections%22)) %7B%0A    
+    // Add jbromo dependencies.%0A        for (final JavaArchive one : Maven.resolver().loadPomFromFile(%22pom.xml%22).importRuntimeAndTestDependencies().resolve().withTransitivity()%0A                .as(JavaArchive.class)) %7B%0A            if (one.getName().contains(%22jbromo%22)) %7B%0A                libs.add(one);%0A            %7D%0A        %7D%0A        // Add classes to archive.%0A        for (final JavaArchive one : libs) %7B%0A
     
@@ -5294,28 +5294,24 @@
             
-    
 if (path.get
@@ -5334,28 +5334,24 @@
 ss%22) %3E 0) %7B%0A
-    
             
@@ -5450,30 +5450,8 @@
 ));%0A
-                    %7D%0A
     
@@ -5491,24 +5491,27 @@
    %7D%0A       
+ //
  arch.addCla

Load dependencies.