// Path: src/test/java/org/springside/modules/test/spring/Profiles.java
// public class Profiles {
// 
// 	public static final String ACTIVE_PROFILE = "spring.profiles.active";
// 	public static final String DEFAULT_PROFILE = "spring.profiles.default";
// 
// 	public static final String PRODUCTION = "production";
// 	public static final String DEVELOPMENT = "development";
// 	public static final String UNIT_TEST = "test";
// 	public static final String FUNCTIONAL_TEST = "functional";
// 
// 	/**
// 	 * 在Spring启动前，设置profile的环境变量。
// 	 */
// 	public static void setProfileAsSystemProperty(String profile) {
// 		System.setProperty(ACTIVE_PROFILE, profile);
// 	}
// }


import java.net.URL;
import java.sql.Driver;
import org.eclipse.jetty.server.Server;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springside.examples.quickstart.QuickStartServer;
import org.springside.modules.test.data.DataFixtures;
import org.springside.modules.test.jetty.JettyFactory;
import org.springside.modules.test.spring.Profiles;
import org.springside.modules.utils.PropertiesLoader;
/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package org.springside.examples.quickstart.functional;



/**
 * 功能测试基类.
 * 
 * 在整个测试期间启动一次Jetty Server和 Selenium，在JVM退出时关闭两者。
 * 在每个TestCase Class执行前重新载入默认数据.
 * 
 * @author calvin
 */
public class BaseFunctionalTestCase {

	protected static String baseUrl;

	protected static Server jettyServer;

	protected static SimpleDriverDataSource dataSource;

	protected static PropertiesLoader propertiesLoader = new PropertiesLoader("classpath:/application.properties",
			"classpath:/application.functional.properties", "classpath:/application.functional-local.properties");

	private static Logger logger = LoggerFactory.getLogger(BaseFunctionalTestCase.class);

	@BeforeClass
	public static void initFunctionalTestEnv() throws Exception {
		baseUrl = propertiesLoader.getProperty("baseUrl");

		// 如果是目标地址是localhost，则启动嵌入式jetty。如果指向远程地址，则不需要启动Jetty.
		boolean isEmbedded = new URL(baseUrl).getHost().equals("localhost")
				&& propertiesLoader.getBoolean("embeddedForLocal");

		if (isEmbedded) {
			startJettyOnce();
		}

		buildDataSourceOnce();
		reloadSampleData();
	}

	/**
	 * 启动Jetty服务器, 仅启动一次.
	 */
	protected static void startJettyOnce() throws Exception {
		if (jettyServer == null) {
			// 设定Spring的profile
// Path: src/test/java/org/springside/modules/test/spring/Profiles.java
// public class Profiles {
// 
// 	public static final String ACTIVE_PROFILE = "spring.profiles.active";
// 	public static final String DEFAULT_PROFILE = "spring.profiles.default";
// 
// 	public static final String PRODUCTION = "production";
// 	public static final String DEVELOPMENT = "development";
// 	public static final String UNIT_TEST = "test";
// 	public static final String FUNCTIONAL_TEST = "functional";
// 
// 	/**
// 	 * 在Spring启动前，设置profile的环境变量。
// 	 */
// 	public static void setProfileAsSystemProperty(String profile) {
// 		System.setProperty(ACTIVE_PROFILE, profile);
// 	}
// }


// Path: src/test/functional/org/springside/examples/quickstart/functional/BaseFunctionalTestCase.java
import java.net.URL;
import java.sql.Driver;
import org.eclipse.jetty.server.Server;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springside.examples.quickstart.QuickStartServer;
import org.springside.modules.test.data.DataFixtures;
import org.springside.modules.test.jetty.JettyFactory;
import org.springside.modules.test.spring.Profiles;
import org.springside.modules.utils.PropertiesLoader;

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package org.springside.examples.quickstart.functional;



/**
 * 功能测试基类.
 * 
 * 在整个测试期间启动一次Jetty Server和 Selenium，在JVM退出时关闭两者。
 * 在每个TestCase Class执行前重新载入默认数据.
 * 
 * @author calvin
 */
public class BaseFunctionalTestCase {

	protected static String baseUrl;

	protected static Server jettyServer;

	protected static SimpleDriverDataSource dataSource;

	protected static PropertiesLoader propertiesLoader = new PropertiesLoader("classpath:/application.properties",
			"classpath:/application.functional.properties", "classpath:/application.functional-local.properties");

	private static Logger logger = LoggerFactory.getLogger(BaseFunctionalTestCase.class);

	@BeforeClass
	public static void initFunctionalTestEnv() throws Exception {
		baseUrl = propertiesLoader.getProperty("baseUrl");

		// 如果是目标地址是localhost，则启动嵌入式jetty。如果指向远程地址，则不需要启动Jetty.
		boolean isEmbedded = new URL(baseUrl).getHost().equals("localhost")
				&& propertiesLoader.getBoolean("embeddedForLocal");

		if (isEmbedded) {
			startJettyOnce();
		}

		buildDataSourceOnce();
		reloadSampleData();
	}

	/**
	 * 启动Jetty服务器, 仅启动一次.
	 */
	protected static void startJettyOnce() throws Exception {
		if (jettyServer == null) {
			// 设定Spring的profile
			Profiles.setProfileAsSystemProperty(Profiles.FUNCTIONAL_TEST);