com.xyxy.platform.examples.showcase.functional.BaseFunctionalTestCase.java Source code

Java tutorial

Introduction

Here is the source code for com.xyxy.platform.examples.showcase.functional.BaseFunctionalTestCase.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.xyxy.platform.examples.showcase.functional;

import java.net.URL;
import java.sql.Driver;

import com.xyxy.platform.examples.showcase.ShowcaseServer;
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 com.xyxy.platform.modules.core.test.data.DataFixtures;
import com.xyxy.platform.modules.core.test.jetty.JettyFactory;
import com.xyxy.platform.modules.core.test.spring.Profiles;
import com.xyxy.platform.modules.core.utils.PropertiesLoader;

/**
 * .
 * 
 * ?Jetty Server, ?TestCase Class???.
 * 
 *
 */
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");

        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) {
            System.out.println("[HINT] Don't forget to set -XX:MaxPermSize=128m");

            // Springprofile
            Profiles.setProfileAsSystemProperty(Profiles.FUNCTIONAL_TEST);

            jettyServer = JettyFactory.createServerInSource(new URL(baseUrl).getPort(), ShowcaseServer.CONTEXT);
            JettyFactory.setTldJarNames(jettyServer, ShowcaseServer.TLD_JAR_NAMES);
            jettyServer.start();

            logger.info("Jetty Server started at {}", baseUrl);
        }
    }

    /**
     * ??.
     */
    protected static void buildDataSourceOnce() throws ClassNotFoundException {
        if (dataSource == null) {
            dataSource = new SimpleDriverDataSource();
            dataSource.setDriverClass(
                    (Class<? extends Driver>) Class.forName(propertiesLoader.getProperty("jdbc.driver")));
            dataSource.setUrl(propertiesLoader.getProperty("jdbc.url"));
            dataSource.setUsername(propertiesLoader.getProperty("jdbc.username"));
            dataSource.setPassword(propertiesLoader.getProperty("jdbc.password"));

        }
    }

    /**
     * ?.
     */
    protected static void reloadSampleData() throws Exception {
        String dbType = propertiesLoader.getProperty("db.type", "h2");
        DataFixtures.executeScript(dataSource, "classpath:data/" + dbType + "/cleanup-data.sql",
                "classpath:data/" + dbType + "/import-data.sql");
    }
}