svnno****@sourc*****
svnno****@sourc*****
2009年 4月 8日 (水) 21:29:04 JST
Revision: 3193 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=3193 Author: daisuke_m Date: 2009-04-08 21:29:04 +0900 (Wed, 08 Apr 2009) Log Message: ----------- JUnit Theory を使ってテストを整理。 Modified Paths: -------------- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/TestDatabaseInstanceTest.java Removed Paths: ------------- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatafileTest.java -------------- next part -------------- Modified: artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java =================================================================== --- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java 2009-04-08 11:26:30 UTC (rev 3192) +++ artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java 2009-04-08 12:29:04 UTC (rev 3193) @@ -18,18 +18,36 @@ */ package org.jiemamy; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileFilter; import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.net.URL; import java.sql.Connection; import java.sql.Driver; +import java.util.Collection; import java.util.List; import java.util.Properties; import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.CharEncoding; +import org.custommonkey.xmlunit.DetailedDiff; +import org.custommonkey.xmlunit.Diff; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,78 +55,230 @@ import org.jiemamy.composer.importer.DefaultDatabaseImportConfig; import org.jiemamy.dialect.Dialect; import org.jiemamy.internal.test.TestModelBuilders; -import org.jiemamy.internal.test.TestModelsTestBase; import org.jiemamy.model.RootModel; import org.jiemamy.model.sql.SqlStatement; +import org.jiemamy.serializer.JiemamySerializer; +import org.jiemamy.serializer.SerializationException; import org.jiemamy.utils.DatabaseCleaner; import org.jiemamy.utils.DatabaseConnectionConfig; import org.jiemamy.utils.DriverUtil; import org.jiemamy.utils.JmIOUtil; import org.jiemamy.utils.SqlExecuter; +import org.jiemamy.utils.ValidatorUtil; +import org.jiemamy.utils.model.EqualsUtil; +import org.jiemamy.validator.AllValidator; +import org.jiemamy.validator.Problem; +import org.jiemamy.validator.Validator; +import org.jiemamy.validator.Problem.Severity; /** * TODO for daisuke * * @author daisuke */ -public class DatabaseTest extends TestModelsTestBase { + @ RunWith(Theories.class) +public class DatabaseTest { private static Logger logger = LoggerFactory.getLogger(DatabaseTest.class); + /** テスト結果の参考ファイル配置パス */ + private static final String OUTPUT_PATH = "./target/datafiletest/"; + + /** テスト対象ファイル配置パス */ + private static final String TARGET_PATH = "./src/test/resources/datafiles/"; + + /** テスト対象ファイルの配列 */ + @DataPoints + public static final File[] listFiles = new File(TARGET_PATH).listFiles(new FileFilter() { + + public boolean accept(File pathname) { + return pathname.isDirectory() == false && FilenameUtils.getExtension(pathname.getName()).equals("jer"); + } + + }); + + /** テストモデルビルダー情報の配列 */ + @DataPoints + public static final TestModelBuilders[] builders = TestModelBuilders.values(); + + /** テストDBの配列 */ + @DataPoints + public static final TestDatabaseInstance[] testDatabases = TestDatabaseInstance.values(); + - @Override - public void doTest(TestModelBuilders entry) throws Exception { - final Jiemamy jiemamy = entry.getBuiltModel(); + /** + * DB適用テスト。 + * + * @param builder テスト対象データファイル + * @param testDatabase テストDB + * @throws Exception 例外が発生した場合 + */ + @Theory + public void test01_テストモデルビルダのDB適用テスト(TestModelBuilders builder, TestDatabaseInstance testDatabase) throws Exception { + logger.info("== Database: " + testDatabase.name()); - for (TestDatabaseInstance db : TestDatabaseInstance.values()) { - logger.info("== Database: " + db.name()); + // FIXME Oracleのdialectが整備されるまでskip + if (testDatabase == TestDatabaseInstance.Oracle) { + logger.warn(testDatabase.name() + " skip"); + return; + } + // skip処理ここまで + + Jiemamy jiemamy = builder.getBuiltModel(); + + doDatabaseTest(testDatabase, jiemamy); + } + + /** + * DB適用テスト。 + * + * @param file テスト対象データファイル + * @param testDatabase テストDB + * @throws Exception 例外が発生した場合 + */ + @Theory + public void test02_テストデータファイルのDB適用テスト(File file, TestDatabaseInstance testDatabase) throws Exception { + logger.info("== Database: " + testDatabase.name()); + + // FIXME Oracleのdialectが整備されるまでskip + if (testDatabase == TestDatabaseInstance.Oracle) { + logger.warn(testDatabase.name() + " skip"); + return; + } + // skip処理ここまで + + Jiemamy jiemamy = Jiemamy.newInstance(); + InputStream in = null; + try { + in = FileUtils.openInputStream(file); + jiemamy.getSerializer().deserialize(in); + } finally { + IOUtils.closeQuietly(in); + } + + doDatabaseTest(testDatabase, jiemamy); + } + + /** + * 往復テスト(XRXR)。 + * + * @param file テスト対象データファイル + * @throws Exception 例外が発生した場合 + */ + @Theory + public void test99_往復テスト_XRXR(File file) throws Exception { + String baseName = FilenameUtils.getBaseName(file.getName()); + + Jiemamy jiemamy1 = Jiemamy.newInstance(new Artemis(new ArtemisView())); + Jiemamy jiemamy2 = Jiemamy.newInstance(new Artemis(new ArtemisView())); + JiemamySerializer serializer1 = jiemamy1.getSerializer(); + JiemamySerializer serializer2 = jiemamy2.getSerializer(); + Validator validator = new AllValidator(); + + String xml1 = FileUtils.readFileToString(file, CharEncoding.UTF_8); + + // xml1 -> rootModel1 + RootModel rootModel1 = convertXmlToRootModel(xml1, serializer1); + Collection<Problem> problem1 = validator.validate(rootModel1); + for (Problem problem : problem1) { + logger.error(problem.getMessage()); + } + + // rootModel1 -> xml2 + String xml2 = convertRootModelToXml(serializer2, rootModel1); + + // xml1 -> rootModel2 + RootModel rootModel2 = convertXmlToRootModel(xml2, serializer2); + Collection<Problem> problem2 = validator.validate(rootModel2); + + // 参考のため、targetディレクトリに出力XMLを記録 + FileUtils.writeStringToFile(new File(OUTPUT_PATH + baseName + "_xml1.xml"), xml1, CharEncoding.UTF_8); + + FileUtils.writeStringToFile(new File(OUTPUT_PATH + baseName + "_xml2.xml"), xml2, CharEncoding.UTF_8); + + EqualsUtil.equals(rootModel2, rootModel1); + + FileUtils.writeStringToFile(new File(OUTPUT_PATH + baseName + "_gtree1.txt"), EqualsUtil.rightGTree, + CharEncoding.UTF_8); + + FileUtils.writeStringToFile(new File(OUTPUT_PATH + baseName + "_gtree2.txt"), EqualsUtil.leftGTree, + CharEncoding.UTF_8); + + DetailedDiff diff = new DetailedDiff(new Diff(xml1, xml2)); + assertThat(diff.getAllDifferences().toString(), diff.similar(), is(true)); + assertEquals(EqualsUtil.rightGTree, EqualsUtil.leftGTree); + + assertThat(ValidatorUtil.sizeOf(problem1, Severity.ERROR), is(0)); // 一応バリデーションを行っておく + assertThat(ValidatorUtil.sizeOf(problem2, Severity.ERROR), is(0)); // 一応バリデーションを行っておく + } + + private String convertRootModelToXml(JiemamySerializer serializer, RootModel rootModel) + throws UnsupportedEncodingException, SerializationException { + String xml; + ByteArrayOutputStream out = null; + + try { + out = new ByteArrayOutputStream(); + serializer.serialize(rootModel, out); + xml = out.toString(CharEncoding.UTF_8); + } finally { + IOUtils.closeQuietly(out); + } + return xml; + } + + private RootModel convertXmlToRootModel(String xml, JiemamySerializer serializer) + throws UnsupportedEncodingException, SerializationException { + RootModel rootModel; + InputStream in = null; + try { + in = new ByteArrayInputStream(xml.getBytes(CharEncoding.UTF_8)); + rootModel = serializer.deserialize(in); + } finally { + IOUtils.closeQuietly(in); + } + return rootModel; + } + + private void doDatabaseTest(TestDatabaseInstance testDatabase, Jiemamy jiemamy) throws Exception { + List<SqlStatement> statements = emitStatements(jiemamy, testDatabase); + + final DatabaseConnectionConfig connectionConfig = testDatabase.getConfig(); + Connection connection = null; + try { + Properties props = new Properties(); + props.setProperty("user", connectionConfig.getUsername()); + props.setProperty("password", connectionConfig.getPassword()); - // FIXME Oracleのdialectが整備されるまでskip - if (db == TestDatabaseInstance.Oracle) { - logger.warn(db.name() + " skip"); - continue; - } - // skip処理ここまで + URL[] paths = connectionConfig.getDriverJarPaths(); + String className = connectionConfig.getDriverClassName(); + Driver driver = DriverUtil.getDriverInstance(paths, className); + connection = driver.connect(connectionConfig.getUri(), props); - List<SqlStatement> statements = emitStatements(jiemamy, db); + assertThat(connection, is(notNullValue())); - final DatabaseConnectionConfig connectionConfig = db.getConfig(); - Connection connection = null; - try { - Properties props = new Properties(); - props.setProperty("user", connectionConfig.getUsername()); - props.setProperty("password", connectionConfig.getPassword()); - - URL[] paths = connectionConfig.getDriverJarPaths(); - String className = connectionConfig.getDriverClassName(); - Driver driver = DriverUtil.getDriverInstance(paths, className); - connection = driver.connect(connectionConfig.getUri(), props); - - assertThat(connection, is(notNullValue())); - - DatabaseCleaner databaseCleaner = new DatabaseCleaner(); - DefaultDatabaseImportConfig importConfig = new DefaultDatabaseImportConfig(); - BeanUtils.copyProperties(importConfig, connectionConfig); - importConfig.setDialect(jiemamy.getDialect(jiemamy.getFactory().getRootModel())); - importConfig.setSchema(jiemamy.getFactory().getRootModel().getSchemaName()); - - databaseCleaner.clean(importConfig); - - SqlExecuter executer = new SqlExecuter(connection); - - for (SqlStatement stmt : statements) { - boolean ignoreSqlException = stmt.toString().startsWith("DROP"); - if (ignoreSqlException) { - executer.executeIgnoreSqlException(stmt.toString()); - } else { - executer.execute(stmt.toString()); - } + DatabaseCleaner databaseCleaner = new DatabaseCleaner(); + DefaultDatabaseImportConfig importConfig = new DefaultDatabaseImportConfig(); + BeanUtils.copyProperties(importConfig, connectionConfig); + importConfig.setDialect(jiemamy.getDialect(jiemamy.getFactory().getRootModel())); + importConfig.setSchema(jiemamy.getFactory().getRootModel().getSchemaName()); + + databaseCleaner.clean(importConfig); + + SqlExecuter executer = new SqlExecuter(connection); + + for (SqlStatement stmt : statements) { + boolean ignoreSqlException = stmt.toString().startsWith("DROP"); + if (ignoreSqlException) { + executer.executeIgnoreSqlException(stmt.toString()); + } else { + executer.execute(stmt.toString()); } - } catch (FileNotFoundException e) { - logger.warn(db.name() + " skip"); - } finally { - JmIOUtil.closeQuietly(connection); } + } catch (FileNotFoundException e) { + logger.warn(testDatabase.name() + " skip"); + } finally { + JmIOUtil.closeQuietly(connection); } } @@ -119,7 +289,6 @@ * @param db テストデータベース情報 * @return SQL文のリスト * @throws ClassNotFoundException {@link RootModel}に設定された{@link Dialect}の実装クラスが見つからない場合 - */ private List<SqlStatement> emitStatements(Jiemamy jiemamy, TestDatabaseInstance db) throws ClassNotFoundException { RootModel rootModel = jiemamy.getFactory().getRootModel(); Deleted: artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatafileTest.java =================================================================== --- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatafileTest.java 2009-04-08 11:26:30 UTC (rev 3192) +++ artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatafileTest.java 2009-04-08 12:29:04 UTC (rev 3193) @@ -1,293 +0,0 @@ -/* - * Copyright 2007-2009 Jiemamy Project and the Others. - * Created on 2009/04/08 - * - * This file is part of Jiemamy. - * - * 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.jiemamy; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileFilter; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.UnsupportedEncodingException; -import java.net.URL; -import java.sql.Connection; -import java.sql.Driver; -import java.util.Collection; -import java.util.List; -import java.util.Properties; - -import org.apache.commons.beanutils.BeanUtils; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.FilenameUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.CharEncoding; -import org.custommonkey.xmlunit.DetailedDiff; -import org.custommonkey.xmlunit.Diff; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.jiemamy.composer.exporter.DefaultSqlExportConfig; -import org.jiemamy.composer.importer.DefaultDatabaseImportConfig; -import org.jiemamy.dialect.Dialect; -import org.jiemamy.model.RootModel; -import org.jiemamy.model.sql.SqlStatement; -import org.jiemamy.serializer.JiemamySerializer; -import org.jiemamy.serializer.SerializationException; -import org.jiemamy.utils.DatabaseCleaner; -import org.jiemamy.utils.DatabaseConnectionConfig; -import org.jiemamy.utils.DriverUtil; -import org.jiemamy.utils.JmIOUtil; -import org.jiemamy.utils.SqlExecuter; -import org.jiemamy.utils.ValidatorUtil; -import org.jiemamy.utils.model.EqualsUtil; -import org.jiemamy.validator.AllValidator; -import org.jiemamy.validator.Problem; -import org.jiemamy.validator.Validator; -import org.jiemamy.validator.Problem.Severity; - -/** - * TODO for daisuke - * - * @author daisuke - */ -public class DatafileTest { - - /** TODO for daisuke */ - private static final String OUTPUT_PATH = "./target/datafiletest/"; - - private static Logger logger = LoggerFactory.getLogger(DatafileTest.class); - - private static final File TARGET_DIR = new File("./src/test/resources/datafiles"); - - private static final FileFilter FILTER = new FileFilter() { - - public boolean accept(File pathname) { - return pathname.isDirectory() == false && FilenameUtils.getExtension(pathname.getName()).equals("jer"); - } - - }; - - private File[] listFiles; - - - /** - * テストを初期化する。 - * - * @throws Exception 例外が発生した場合 - */ - @Before - public void setUp() throws Exception { - listFiles = TARGET_DIR.listFiles(FILTER); - } - - /** - * テストの情報を破棄する。 - * - * @throws Exception 例外が発生した場合 - */ - @After - public void tearDown() throws Exception { - listFiles = null; - } - - /** - * TODO for daisuke - * - * @throws Exception 例外が発生した場合 - */ - @Test - public void test() throws Exception { - for (File file : listFiles) { - logger.info("==== " + file); - 往復テスト_XRXR(file); - DB適用テスト(file); - } - } - - private String convertRootModelToXml(JiemamySerializer serializer, RootModel rootModel) - throws UnsupportedEncodingException, SerializationException { - String xml; - ByteArrayOutputStream out = null; - - try { - out = new ByteArrayOutputStream(); - serializer.serialize(rootModel, out); - xml = out.toString(CharEncoding.UTF_8); - } finally { - IOUtils.closeQuietly(out); - } - return xml; - } - - private RootModel convertXmlToRootModel(String xml, JiemamySerializer serializer) - throws UnsupportedEncodingException, SerializationException { - RootModel rootModel; - InputStream in = null; - try { - in = new ByteArrayInputStream(xml.getBytes(CharEncoding.UTF_8)); - rootModel = serializer.deserialize(in); - } finally { - IOUtils.closeQuietly(in); - } - return rootModel; - } - - private void DB適用テスト(File file) throws Exception { - Jiemamy jiemamy = Jiemamy.newInstance(); - InputStream in = null; - try { - in = FileUtils.openInputStream(file); - jiemamy.getSerializer().deserialize(in); - } finally { - IOUtils.closeQuietly(in); - } - - for (TestDatabaseInstance db : TestDatabaseInstance.values()) { - logger.info("== Database: " + db.name()); - - // FIXME Oracleのdialectが整備されるまでskip - if (db == TestDatabaseInstance.Oracle) { - logger.warn(db.name() + " skip"); - continue; - } - // skip処理ここまで - - List<SqlStatement> statements = emitStatements(jiemamy, db); - - final DatabaseConnectionConfig connectionConfig = db.getConfig(); - Connection connection = null; - try { - Properties props = new Properties(); - props.setProperty("user", connectionConfig.getUsername()); - props.setProperty("password", connectionConfig.getPassword()); - - URL[] paths = connectionConfig.getDriverJarPaths(); - String className = connectionConfig.getDriverClassName(); - Driver driver = DriverUtil.getDriverInstance(paths, className); - connection = driver.connect(connectionConfig.getUri(), props); - - assertThat(connection, is(notNullValue())); - - DatabaseCleaner databaseCleaner = new DatabaseCleaner(); - DefaultDatabaseImportConfig importConfig = new DefaultDatabaseImportConfig(); - BeanUtils.copyProperties(importConfig, connectionConfig); - importConfig.setDialect(jiemamy.getDialect(jiemamy.getFactory().getRootModel())); - importConfig.setSchema(jiemamy.getFactory().getRootModel().getSchemaName()); - - databaseCleaner.clean(importConfig); - - SqlExecuter executer = new SqlExecuter(connection); - - for (SqlStatement stmt : statements) { - boolean ignoreSqlException = stmt.toString().startsWith("DROP"); - if (ignoreSqlException) { - executer.executeIgnoreSqlException(stmt.toString()); - } else { - executer.execute(stmt.toString()); - } - } - } catch (FileNotFoundException e) { - logger.warn(db.name() + " skip"); - } finally { - JmIOUtil.closeQuietly(connection); - } - } - } - - /** - * SQL文を出力する。 - * - * @param jiemamy 対象のJiemamyコンテキスト - * @param db テストデータベース情報 - * @return SQL文のリスト - * @throws ClassNotFoundException {@link RootModel}に設定された{@link Dialect}の実装クラスが見つからない場合 - - */ - private List<SqlStatement> emitStatements(Jiemamy jiemamy, TestDatabaseInstance db) throws ClassNotFoundException { - RootModel rootModel = jiemamy.getFactory().getRootModel(); - rootModel.setDialectClassName(db.getDialectClassName()); - - DefaultSqlExportConfig exportConfig = new DefaultSqlExportConfig(); - exportConfig.setEmitDropStatements(false); // TODO trueの場合もテスト - exportConfig.setDataSetIndex(-1); // FIXME この行削除しても動くように - List<SqlStatement> statements = jiemamy.emitStatements(rootModel, exportConfig); - return statements; - } - - /** - * 往復テスト(XRXR)。 - * - * @param file テスト対象データファイル - * @throws Exception 例外が発生した場合 - */ - private void 往復テスト_XRXR(File file) throws Exception { - String baseName = FilenameUtils.getBaseName(file.getName()); - - Jiemamy jiemamy1 = Jiemamy.newInstance(new Artemis(new ArtemisView())); - Jiemamy jiemamy2 = Jiemamy.newInstance(new Artemis(new ArtemisView())); - JiemamySerializer serializer1 = jiemamy1.getSerializer(); - JiemamySerializer serializer2 = jiemamy2.getSerializer(); - Validator validator = new AllValidator(); - - String xml1 = FileUtils.readFileToString(file, CharEncoding.UTF_8); - - // xml1 -> rootModel1 - RootModel rootModel1 = convertXmlToRootModel(xml1, serializer1); - Collection<Problem> problem1 = validator.validate(rootModel1); - for (Problem problem : problem1) { - logger.error(problem.getMessage()); - } - - // rootModel1 -> xml2 - String xml2 = convertRootModelToXml(serializer2, rootModel1); - - // xml1 -> rootModel2 - RootModel rootModel2 = convertXmlToRootModel(xml2, serializer2); - Collection<Problem> problem2 = validator.validate(rootModel2); - - // 参考のため、targetディレクトリに出力XMLを記録 - FileUtils.writeStringToFile(new File(OUTPUT_PATH + baseName + "_xml1.xml"), xml1, CharEncoding.UTF_8); - - FileUtils.writeStringToFile(new File(OUTPUT_PATH + baseName + "_xml2.xml"), xml2, CharEncoding.UTF_8); - - EqualsUtil.equals(rootModel2, rootModel1); - - FileUtils.writeStringToFile(new File(OUTPUT_PATH + baseName + "_gtree1.txt"), EqualsUtil.rightGTree, - CharEncoding.UTF_8); - - FileUtils.writeStringToFile(new File(OUTPUT_PATH + baseName + "_gtree2.txt"), EqualsUtil.leftGTree, - CharEncoding.UTF_8); - - DetailedDiff diff = new DetailedDiff(new Diff(xml1, xml2)); - assertThat(diff.getAllDifferences().toString(), diff.similar(), is(true)); - assertEquals(EqualsUtil.rightGTree, EqualsUtil.leftGTree); - - assertThat(ValidatorUtil.sizeOf(problem1, Severity.ERROR), is(0)); // 一応バリデーションを行っておく - assertThat(ValidatorUtil.sizeOf(problem2, Severity.ERROR), is(0)); // 一応バリデーションを行っておく - } - -} Modified: artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/TestDatabaseInstanceTest.java =================================================================== --- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/TestDatabaseInstanceTest.java 2009-04-08 11:26:30 UTC (rev 3192) +++ artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/TestDatabaseInstanceTest.java 2009-04-08 12:29:04 UTC (rev 3193) @@ -21,6 +21,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; +import static org.junit.Assume.assumeThat; import java.io.File; import java.net.URISyntaxException; @@ -29,7 +30,10 @@ import java.sql.Driver; import java.util.Properties; -import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,46 +46,46 @@ * * @author daisuke */ + @ RunWith(Theories.class) public class TestDatabaseInstanceTest { private static Logger logger = LoggerFactory.getLogger(TestDatabaseInstanceTest.class); + /** テストDBの配列 */ + @DataPoints + public static final TestDatabaseInstance[] values = TestDatabaseInstance.values(); + /** * 全てのテストDBに接続できること。 * + * @param db テストDB * @throws Exception 例外が発生した場合 */ - @Test - public void test01_全てのテストDBに接続できること() throws Exception { - for (TestDatabaseInstance db : TestDatabaseInstance.values()) { - logger.info(db.name()); - DatabaseConnectionConfig config = db.getConfig(); - Properties props = new Properties(); - props.setProperty("user", config.getUsername()); - props.setProperty("password", config.getPassword()); - - URL[] paths = config.getDriverJarPaths(); - if (existsAll(paths) == false) { - // driver jarファイルが見つからない場合、テストをスキップする - logger.warn("skip"); - continue; - } - String className = config.getDriverClassName(); - - Connection connection = null; - try { - Driver driver = DriverUtil.getDriverInstance(paths, className); - connection = driver.connect(config.getUri(), props); - assertThat(connection, is(notNullValue())); - logger.info("success"); - } catch (Exception e) { - logger.error("failed", e); - throw e; - } finally { - JmIOUtil.closeQuietly(connection); - } - + @Theory + public void test01_全てのテストDBに接続できること(TestDatabaseInstance db) throws Exception { + logger.info(db.name()); + DatabaseConnectionConfig config = db.getConfig(); + Properties props = new Properties(); + props.setProperty("user", config.getUsername()); + props.setProperty("password", config.getPassword()); + + URL[] paths = config.getDriverJarPaths(); + assumeThat(existsAll(paths), is(true)); + + String className = config.getDriverClassName(); + + Connection connection = null; + try { + Driver driver = DriverUtil.getDriverInstance(paths, className); + connection = driver.connect(config.getUri(), props); + assertThat(connection, is(notNullValue())); + logger.info("success"); + } catch (Exception e) { + logger.error("failed", e); + throw e; + } finally { + JmIOUtil.closeQuietly(connection); } }