/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.ocelotds;

import java.io.File;
import java.io.FileFilter;
import java.util.Random;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.FileAsset;
import org.jboss.shrinkwrap.api.container.ClassContainer;
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.spec.JavaArchive;

/**
 *
 * @author hhfrancois
 */
public abstract class ArquillianTestCase {
	/**
	 * Build ocelot-web.jar
	 *
	 * @return
	 */
	public static JavaArchive createOcelotWebJar() {
		File bean = new File("src/main/resources/META-INF/beans.xml");
		File core = new File("src/main/resources/ocelot-core.js");
		JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class, String.format("ocelot-web-%s.jar", new Random().nextInt(10)))
				  .addClass(OcelotServices.class)
				  .addPackages(true, "org.ocelotds.encoders")
				  .addPackages(true, "org.ocelotds.exceptions")
				  .addPackages(true, "org.ocelotds.resolvers")
				  .addPackages(true, "org.ocelotds.web")
				  .addPackages(true, "org.ocelotds.core")
				  .addPackages(true, "org.ocelotds.configuration")
				  .addAsManifestResource(new FileAsset(bean), "beans.xml")
				  .addAsResource(new FileAsset(core), "ocelot-core.js");
		addJSAndProvider("target/classes", javaArchive, javaArchive);
		return javaArchive;
	}

	/**
	 * Add srv_xxxx.js and srv_xxxx.ServiceProvider.class
	 *
	 * @param root
	 * @param resourceContainer
	 * @param classContainer
	 */
	public static void addJSAndProvider(final String root, ResourceContainer resourceContainer, ClassContainer classContainer) {
		File classes = new File(root);
		File[] jsFiles = classes.listFiles(new FileFilter() {
			@Override
			public boolean accept(File file) {
				String name = file.getName();
				return file.isFile() && name.startsWith("srv_") && name.endsWith(".js");
			}
		});
		for (File file : jsFiles) {
			String jsName = file.getName();
			String providerPackage = jsName.replaceAll(".js$", "");
			classContainer.addPackage(providerPackage);
			resourceContainer.addAsResource(new FileAsset(file), file.getName());
		}
	}
	
}

@@ -568,16 +568,54 @@
 rchive;%0D
+%0Aimport org.ocelotds.objects.FakeCDI;%0D
 %0A%0D%0A/**%0D%0A
@@ -1092,16 +1092,119 @@
 class)%0D%0A
+%09%09%09%09  .addPackages(true, %22org.ocelotds.configuration%22)%0D%0A%09%09%09%09  .addPackages(true, %22org.ocelotds.core%22)%0D%0A
 %09%09%09%09  .a
@@ -1428,36 +1428,39 @@
 , %22org.ocelotds.
-core
+objects
 %22)%0D%0A%09%09%09%09  .addPa
@@ -1458,54 +1458,33 @@
 %09  .
-addPackages(true, %22org.ocelotds.configuration%22
+deleteClass(FakeCDI.class
 )%0D%0A%09

delete FakeCDI from arquillian package eleall tests fails