Java tutorial
/* * Copyright 2006-2011 the Seasar Foundation and the Others. * * 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 org.seasar.s2click.test; import java.io.File; import java.lang.reflect.Method; import java.lang.reflect.Type; import javax.transaction.TransactionManager; import org.apache.commons.lang.StringUtils; import org.seasar.extension.dataset.DataSet; import org.seasar.extension.dataset.impl.SqlReader; import org.seasar.extension.jdbc.JdbcManager; import org.seasar.framework.container.SingletonS2Container; import org.seasar.framework.exception.ResourceNotFoundRuntimeException; import org.seasar.framework.util.ClassUtil; import org.seasar.framework.util.FileOutputStreamUtil; import org.seasar.framework.util.ResourceUtil; import org.seasar.framework.util.tiger.GenericUtil; import org.seasar.s2click.annotation.Experimental; /** * ???? * <code>S2TestCase</code>??????????????????? * * <dl> * <dt>??</dt> * <dd> * <strong>??_??_data.xls</strong> ???????Excel? * ??????????????? * </dd> * * <dt>Excel??</dt> * <dd> * ?{@link GenerateExcel}????????? * ???????Excel?????????? * ??Excel?<strong>??_??_expect.xls</strong>????????? * ?????????? * ???Excel?????????<br> * <code>GenerateExcel</code>?????????Excel???????? * ????Excel????????????????Excel?????????? * </dd> * * <dt>DB??Excel?</dt> * <dd> * ?{@link Assert}????????? * ???<strong>??_??_expect.xls</strong> * ???????Excel?????????? * ?????Excel??????????? * ?????????<code>GenerateExcel</code>??????? * ?????<br> * <code>Assert</code>??????????????????? * </dd> * </dl> * ???Excel?????{@link S2ClickTestConfig}dicon??? * <code>sourceDir</code>???????????? * ???dicon????????????? * <pre> * <component class="org.seasar.s2click.test.S2ClickTestConfig" instance="singleton"> * <property name="sourceDir">"test"</property> * </component> </pre> * * * @author Naoki Takezoe * * @param <T> ? */ @Experimental public abstract class S2ClickServiceTestCase<T> extends S2ClickTestCase { /** * ?? */ protected T service; protected JdbcManager jdbcManager; protected S2ClickTestConfig config; @Override @SuppressWarnings({ "unchecked", "rawtypes" }) protected void setUpAfterContainerInit() throws Throwable { super.setUpAfterContainerInit(); Type type = GenericUtil.getTypeVariableMap(getClass()) .get(S2ClickServiceTestCase.class.getTypeParameters()[0]); service = (T) SingletonS2Container.getComponent((Class) type); } protected void doRunTest() throws Throwable { TransactionManager tm = null; if (needTransaction()) { try { tm = (TransactionManager) getComponent(TransactionManager.class); tm.begin(); } catch (Throwable t) { System.err.println(t); } } try { try { // ?? readXlsAllReplaceDb(getClass().getSimpleName() + "_" + getTargetMethod().getName() + "_data.xls"); } catch (ResourceNotFoundRuntimeException ex) { // ?????? } // ???Excel?? Method method = getTargetMethod(); GenerateExcel annGenExcel = method.getAnnotation(GenerateExcel.class); if (annGenExcel != null) { Table[] tables = annGenExcel.tables(); // Excel?????? createExcelFile(tables); } // ? runTest(); // Assert?? Assert annAssert = method.getAnnotation(Assert.class); if (annAssert != null) { Table[] tables = annAssert.tables(); // Excel?????? createExcelFile(tables); // ? DataSet ds = getExpectDataSet(); for (Table table : tables) { String tableName = table.name(); assertEquals(ds.getTable(tableName), readDbByTable(tableName)); } } } finally { if (tm != null) { tm.rollback(); } } } // protected DataSet getInitDataSet() { // return readXls(getClass().getSimpleName() + "_" + getTargetMethod().getName() + "_data.xls"); // } /** * ??????Excel?<code>DataSet</code>???? * Excel??????<code>ResourceNotFoundRuntimeException</code>???? */ protected DataSet getExpectDataSet() { return readXls(getClass().getSimpleName() + "_" + getTargetMethod().getName() + "_expect.xls"); } /** * Excel???? * * @param tables ??? * @throws Exception Excel?????? */ private void createExcelFile(Table[] tables) throws Exception { if (config == null) { System.err.println( "S2ClickTestConfig?S2Container??????Excel?????"); return; } if (StringUtils.isEmpty(config.sourceDir)) { System.err.println( "S2ClickTestConfig?sourceDir??????Excel?????"); return; } String packageName = ClassUtil.getPackageName(getClass()); String srcDir = config.sourceDir; String packagePath = packageName.replace('.', '/'); String fileName = getClass().getSimpleName() + "_" + getTargetMethod().getName() + "_expect.xls"; File dir = new File(srcDir + "/" + packagePath); File file = new File(dir, fileName); if (file.exists()) { return; } SqlReader reader = new SqlReader(getDataSource()); for (int i = 0; i < tables.length; i++) { reader.addTable(tables[i].name(), tables[i].where(), tables[i].orderBy()); } DataSet result = reader.read(); // ?? XlsWriter writer = new XlsWriter(FileOutputStreamUtil.create(file)); for (int i = 0; i < tables.length; i++) { writer.setIncludeColumns(tables[i].name(), tables[i].includeColumns()); writer.setExcludeColumns(tables[i].name(), tables[i].excludeColumns()); } writer.write(result); // ???? File buildDir = ResourceUtil.getBuildDir(getClass()); writer.setOutputStream( FileOutputStreamUtil.create(new File(buildDir, convertPath(packagePath + "/" + fileName)))); writer.write(result); } }