/*
 * 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.aop.aspect.xml;

import integration.anyframe.services.AbstractTest;
import integration.anyframe.services.aop.service.UserService;
import anyframe.core.query.IQueryService;

import com.sds.emp.common.EmpException;
import com.sds.emp.user.services.UserVO;

public class PrintStringAspectUsingXMLTest extends AbstractTest {
	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {
		PrintStringAspectUsingXMLTest aspectTest = new PrintStringAspectUsingXMLTest();

		// 1. initialize context
		aspectTest.setup();
		// 2. test
		aspectTest.testGetUser();
		aspectTest.testUpdateUser();
		// 3. close context
		aspectTest.teardown();

		System.out.println("Successful!!!!!");
	}

	protected void setup() {
		super.setup();
		try {
			testInit();
		} catch (Exception e) {
			// ignore this exception
		}
	}

	public void testInit() throws Exception {
		IQueryService m_service = (IQueryService) context
				.getBean("queryService");

		// Try to drop the table. It may not exist and throw an exception.
		m_service.updateBySQL("DROP TABLE IF EXISTS USERS", new String[] {},
				new Object[] {});

		// Create the table that we will use in this test.
		// Different depending on the db. Please add new statements as new
		// databases are tested.
		m_service.updateBySQL("CREATE TABLE USERS ("
				+ "USER_ID VARCHAR(20) NOT NULL, "
				+ "USER_NAME VARCHAR(50) NOT NULL, "
				+ "PASSWORD VARCHAR(10) NOT NULL, " + "ROLE VARCHAR(5), "
				+ "SSN VARCHAR(13), " + "SL_YN CHAR(1), "
				+ "BIRTH_DAY VARCHAR(8), " + "AGE NUMERIC(3), "
				+ "CELL_PHONE VARCHAR(14), " + "ADDR VARCHAR(100), "
				+ "EMAIL VARCHAR(50), " + "EMAIL_YN CHAR(1), "
				+ "IMAGE_FILE VARCHAR(100), " + "REG_DATE DATE )",
				new String[] {}, new Object[] {});

		m_service
				.updateBySQL(
						"INSERT INTO USERS (USER_ID, USER_NAME, PASSWORD) VALUES ('woos41', 'woos', 'woos123')",
						new String[] {}, new Object[] {});
	}

	public void testGetUser() throws Exception {
		UserService userService = (UserService) context
				.getBean(UserService.ROLE);
		String userID = "woos41";

		// 1. get user
		userService.getUser(userID);

		// 2. get user with exception
		try {
			userService.getUserWithException();
		} catch (EmpException e) {
			// ignore
		}
	}

	public void testUpdateUser() throws EmpException {
		UserService userService = (UserService) context
				.getBean(UserService.ROLE);

		UserVO userVO = new UserVO();
		userVO.setUserId("woos41");
		userVO.setUserName("updgang");
		userVO.setCellPhone("9876543210");
		userVO.setAddr("kamala road --");

		// 1. update user
		userService.updateUser(userVO);
	}

	@Override
	protected String[] getConfigLocations() {
		// TODO Auto-generated method stub
		return new String[] {
				"classpath*:/common/applicationContext-*.xml",
				"classpath*:/services/aspect/applicationContext-user.xml",
				"classpath*:/services/aspect/applicationContext-aop-xml.xml",
				"classpath*:/services/query/applicationContext-*.xml",
				"classpath*:/services/datasource/applicationContext-*.xml",
				"classpath*:/services/properties/applicationContext-*.xml" };
	}
}

