/*
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.xdi.oxauth;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.eu.ingwar.tools.arquillian.extension.suite.annotations.ArquillianSuiteDeployment;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.junit.BeforeClass;
import org.testng.ITestContext;
import org.testng.annotations.BeforeSuite;
import org.xdi.oxauth.util.Deployments;
import org.xdi.util.StringHelper;
import org.xdi.util.properties.FileConfiguration;

/**
 * Base class for all seam test which requre external configuration
 * 
 * @author Yuriy Movchan Date: 05/16/2016
 */
@ArquillianSuiteDeployment
public abstract class ConfigurableTest extends Arquillian {

	public static FileConfiguration testData;

	@Deployment
	public static Archive<?> createDeployment() {
		return Deployments.createDeployment();
	}

	/**
	 * Prepare configuration before tests execution
	 */
	@BeforeClass
	public static void initTest() {
		String propertiesFile = "./target/test-classes/testng.properties";
		testData = new FileConfiguration(propertiesFile);
	}

	/**
	 * Get configuration
	 */
	public FileConfiguration getTestData() {
		return testData;
	}

	@BeforeSuite
	public void initTestSuite(ITestContext context) throws FileNotFoundException, IOException {
		String propertiesFile = context.getCurrentXmlTest().getParameter("propertiesFile");
		if (StringHelper.isEmpty(propertiesFile)) {
			propertiesFile = "target/test-classes/testng.properties";
		}

		// Load test parameters
		FileInputStream conf = new FileInputStream(propertiesFile);
		Properties prop = new Properties();
		prop.load(conf);

		Map<String, String> parameters = new HashMap<String, String>();
		for (Entry<Object, Object> entry : prop.entrySet()) {
			Object key = entry.getKey();
			Object value = entry.getValue();

			if (StringHelper.isEmptyString(key) || StringHelper.isEmptyString(value)) {
				continue;
			}
			parameters.put(key.toString(), value.toString());
		}

		// Overrided test parameters
		context.getSuite().getXmlSuite().setParameters(parameters);
	}

}

@@ -380,24 +380,63 @@
 perties;%0D%0A%0D%0A
+import org.apache.commons.io.IOUtils;%0D%0A
 import org.e
@@ -1282,352 +1282,8 @@
 %0D%0A%0D%0A
-%09/**%0D%0A%09 * Prepare configuration before tests execution%0D%0A%09 */%0D%0A%09@BeforeClass%0D%0A%09public static void initTest() %7B%0D%0A%09%09String propertiesFile = %22./target/test-classes/testng.properties%22;%0D%0A%09%09testData = new FileConfiguration(propertiesFile);%0D%0A%09%7D%0D%0A%0D%0A%09/**%0D%0A%09 * Get configuration%0D%0A%09 */%0D%0A%09public FileConfiguration getTestData() %7B%0D%0A%09%09return testData;%0D%0A%09%7D%0D%0A%0D%0A
 %09@Be
@@ -1680,18 +1680,24 @@
 File);%0D%0A
-%09%09
+        
 Properti
@@ -1699,16 +1699,41 @@
 perties 
+prop;%0D%0A        try %7B%0D%0A%09%09%09
 prop = n
@@ -1752,16 +1752,17 @@
 s();%0D%0A%09%09
+%09
 prop.loa
@@ -1770,16 +1770,68 @@
 (conf);%0D
+%0A%09%09%7D finally %7B%0D%0A%09%09%09IOUtils.closeQuietly(conf);%0D%0A%09%09%7D%0D
 %0A%0D%0A%09%09Map

Fix resource leak in test suite