/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package org.arquillian.liferay.maven.generator;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

import org.jboss.arquillian.config.descriptor.api.ArquillianDescriptor;
import org.jboss.arquillian.config.descriptor.api.ContainerDef;
import org.jboss.arquillian.container.spi.client.deployment.DeploymentDescription;
import org.jboss.arquillian.container.test.impl.client.deployment.AnnotationDeploymentScenarioGenerator;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.test.spi.TestClass;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.archive.importer.MavenImporter;
import org.jboss.shrinkwrap.resolver.impl.maven.util.Validate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author <a href="mailto:kamesh.sampath@liferay.com">Kamesh Sampath</a>
 */
public class MavenDeploymentScenarioGenerator
	extends AnnotationDeploymentScenarioGenerator {

	/**
	 * (non-Javadoc)
	 * @see org.jboss.arquillian.container.test.impl.client.deployment.
	 * AnnotationDeploymentScenarioGenerator
	 * #generate(org.jboss.arquillian.test.spi.TestClass)
	 */
	@Override
	public List<DeploymentDescription> generate(TestClass testClass) {
		containerDef();

		List<DeploymentDescription> descriptions = super.generate(testClass);

		if (descriptions == null) {
			descriptions = new ArrayList<>();
		}

		log.info("Generating Deployment for Liferay Plugin ");

		DeploymentDescription deploymentDecription =
			createLiferayPluginDeployment();

		if (deploymentDecription != null) {
			descriptions.add(deploymentDecription);
		}

		return descriptions;
	}

	private ContainerDef containerDef() {
		List<ContainerDef> containers = descriptor.get().getContainers();

		ContainerDef defaultContainer = null;

		for (int i = 0; i < containers.size(); i++) {
			defaultContainer = containers.get(i);

			if (defaultContainer.isDefault()) {
				String containerName = defaultContainer.getContainerName();
				Validate.notNullOrEmpty(
					containerName,
					"At least one default container must be defined in " +
						"arquillian.xml");

				if (containerName.contains("tomcat")) {
					System.setProperty("appServerType", "tomcat");
				}
				else if (containerName.contains("jboss")) {
					System.setProperty("appServerType", "jboss");
				}

				return defaultContainer;
			}
		}

		return null;
	}

	private DeploymentDescription createLiferayPluginDeployment() {
		log.debug("Building Liferay Plugin from project pom.xml");

		File pomFile = new File("pom.xml");

		if ((pomFile != null) && pomFile.exists()) {
			log.debug(
				"Loading project from pom file:" + pomFile.getAbsolutePath());

			WebArchive archive =
				ShrinkWrap.create(MavenImporter.class).loadPomFromFile(
					pomFile).importBuildOutput().as(WebArchive.class);

			DeploymentDescription deploymentDescription =
				new DeploymentDescription("_DEFAULT", archive);

			deploymentDescription.shouldBeTestable(true);

			return deploymentDescription;
		}

		return null;
	}

	private static final Logger log = LoggerFactory.getLogger(
		MavenDeploymentScenarioGenerator.class);

	@Inject
	private Instance<ArquillianDescriptor> descriptor;

}
@@ -1352,24 +1352,110 @@
 enImporter;%0A
+import org.jboss.shrinkwrap.resolver.api.maven.archive.importer.PomlessMavenImporter;%0A
 import org.j
@@ -3479,33 +3479,305 @@
 %0A%09%09%09
-WebArchive archive =%0A%09%09%09%09
+String globalSettings = System.getProperty(%0A%09%09%09%09%22maven.execution.global-settings%22);%0A%0A%09%09%09String userSettings = System.getProperty(%0A%09%09%09%09%22maven.execution.user-settings%22);%0A%0A%09%09%09String profilesString = System.getProperty(%0A%09%09%09%09%22maven.execution.active-profiles%22);%0A%0A%09%09%09PomlessMavenImporter mavenImporter = 
 Shri
@@ -3790,16 +3790,21 @@
 .create(
+%0A%09%09%09%09
 MavenImp
@@ -3815,16 +3815,446 @@
 r.class)
+;%0A%0A%09%09%09if (globalSettings != null) %7B%0A%09%09%09%09mavenImporter =%0A%09%09%09%09%09((MavenImporter)mavenImporter).configureFromFile(%0A%09%09%09%09%09%09globalSettings);%0A%09%09%09%7D%0A%0A%09%09%09if (userSettings != null) %7B%0A%09%09%09%09mavenImporter =%0A%09%09%09%09%09((MavenImporter)mavenImporter).configureFromFile(%0A%09%09%09%09%09%09userSettings);%0A%09%09%09%7D%0A%0A%09%09%09String%5B%5D profiles = new String%5B0%5D;%0A%0A%09%09%09if (profilesString != null) %7B%0A%09%09%09%09profiles = profilesString.split(%22,%22);%0A%09%09%09%7D%0A%0A%09%09%09WebArchive archive = mavenImporter
 .loadPom
@@ -4271,16 +4271,25 @@
 %09%09%09%09
-%09
 pomFile
+, profiles
 ).im

Use propagated environment