/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package integration.anyframe.services.idgeneration;

import integration.anyframe.services.AbstractTest;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import anyframe.core.idgen.IIdGenerationService;

/**
 * IdGenerationService�? ?��공하?�� 기능?�� ?��?��?��?���? ?��?�� ?��?�� ?��?��?�� 코드
 */
public class IdGenerationServiceTest extends AbstractTest {

	public static void main(String[] args) throws Exception {

		IdGenerationServiceTest idgenerationTest = new IdGenerationServiceTest();

		// 1. initialize context
		idgenerationTest.setup();
		// 2. test (UUIdGeneration, TableIdGeneration, SequenceIdGeneration?�� ?���?�? 경우�? 존재)
		idgenerationTest.testUUIdGeneration();
		idgenerationTest.testTableIdGeneration();
		idgenerationTest.testSequenceIdGeneration();
		// 3. close context
		idgenerationTest.teardown();

		System.out.println("Successful!!!!!!");
	}

	/**
	 * ?��?���?, ?��????��?�� ?��?��?�� ?��?�� setup
	 */
	public void setup() {
		super.setup();
		try {
			DataSource dataSource = (DataSource) context
					.getBean("common_datasource");
			Connection conn = dataSource.getConnection();
			try {
				Statement statement = conn.createStatement();

				// 1. table�? sequence�? ?��?��?���? ?��?�� drop?��?��.
				try {
					statement.executeUpdate("DROP TABLE ids");
				} catch (SQLException e) {}				
				try {
					statement.executeUpdate("DROP TABLE DUAL");
				} catch (SQLException e) {}

				try {
					statement.executeUpdate("DROP SEQUENCE idsequence");
				}
				catch (SQLException e) {}

				// 2. test?�� ?��?��?�� table�? sequence�? ?��?��?��?��
				// 만약 ?��?��?��?�� ?��?��?��베이?���? ?��르면 그에 ?��?�� statement�? ?��?��?��?��. ?��기서?�� HSQL 1.8 DB�? ?��?��?��?��.
				statement.executeUpdate("CREATE TABLE ids ( "
						+ "table_name varchar(16) NOT NULL, "
						+ "next_id DECIMAL(30) NOT NULL, "
						+ "PRIMARY KEY (table_name))");
				
				statement.executeUpdate("INSERT INTO ids VALUES('test','0')");
				
				statement.executeUpdate("CREATE SEQUENCE idsequence START WITH 0");
				
				statement.executeUpdate("CREATE TABLE DUAL ( "
						+ "dummy varchar(1)) ");	
				statement.executeUpdate("INSERT INTO DUAL VALUES('1')");
			} finally {
				conn.close();
			}
		} catch (SQLException e) {
			System.err.println("Unable to initialize database for test." + e);
		}
	}
	
	/**
	 * ?��?���?, ?��????��?�� drop?�� ?��?�� teardown
	 */
	public void teardown(){
		try {
			DataSource dataSource = (DataSource) context
					.getBean("common_datasource");
			Connection conn = dataSource.getConnection();
			try {
				Statement statement = conn.createStatement();

				// 1. ?��?��?���? ?��?�� 만들?�� ?��??? ?��?��블과 ?��????���? ?��?��?��?��.
				statement.executeUpdate("DROP TABLE ids");
				statement.executeUpdate("DROP SEQUENCE idsequence");
			} finally {
				conn.close();
			}
		} catch (SQLException e) {
			System.out.println("Unable to cleanup database after test." + e);
		}
		super.teardown();
	}

	/**
	 * UUIdGeneration?�� ?��?��?�� testcase
	 */
	public void testUUIdGeneration() throws Exception {
		IIdGenerationService id = (IIdGenerationService) context
				.getBean("UUIdGenerationService");
		// ?���? ID�? 받아?��?��.	
		String newId=id.getNextStringId();
		if(newId==null)
			throw new Exception("fail to get UUIdgeneration");
	}
	
	/**
	 * TableIdGeneration ?��?��?�� testcase
	 */
	public void testTableIdGeneration() throws Exception {
		IIdGenerationService id = (IIdGenerationService) context
				.getBean("TableIdGenerationService");
		// ?��?�� ?��?���? ID�? 받아?��?��.
		String newId = id.getNextStringId();
		// ?��?�� ?��?���? ID?�� 초기�? 0?�� 주었?���?�? "TEST-****0"?�� ?�� 것이?��. 
		// cf)INSERT INTO ids VALUES('test','0')
		// 0?��?�� prefix?�� configuration?�� mixPrefix?��?�� ?��?��?�� ?�� ?��?��.
		if (!newId.equals("TEST-****0"))
			throw new Exception("fail to get TableIdgeneration");
	}
	
	/**
	 * SequenceIdGeneration ?��?��?�� testcase
	 */
	public void testSequenceIdGeneration() throws Exception {
		IIdGenerationService id = (IIdGenerationService) context
				.getBean("SequenceIdGenerationService");
		// ?��?�� ?��?��링ID�? 받아?��?��.
		String newId = id.getNextStringId();
		// ?��?�� ?��?���? ID?�� 초기�? 0?�� 주었?���?�? 0?�� ?�� 것이?��. 
		// cf)CREATE SEQUENCE idsequence START WITH 0
		if (!newId.equals("0"))
			throw new Exception("fail to get SequenceIdgeneration");
	}	
	protected String[] getConfigLocations() {
		// TODO Auto-generated method stub
		return new String[]{"classpath*:/common/applicationContext-*.xml","classpath*:/services/idgeneration/applicationContext-*.xml","classpath*:/services/datasource/applicationContext-*.xml"};
	}
}

	



