svnno****@sourc*****
svnno****@sourc*****
2009年 3月 20日 (金) 01:35:53 JST
Revision: 2959 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=2959 Author: daisuke_m Date: 2009-03-20 01:35:53 +0900 (Fri, 20 Mar 2009) Log Message: ----------- テストを移動させるのを忘れた。。 Added Paths: ----------- artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/ artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/exporter/ artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/exporter/SqlExporterTest.java artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/importer/ artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/importer/DatabaseImporterTest.java Removed Paths: ------------- charon/jiemamy-html-exporter/trunk/src/test/java/org/jiemamy/composer/exporter/SqlExporterTest.java charon/jiemamy-html-exporter/trunk/src/test/java/org/jiemamy/composer/importer/ -------------- next part -------------- Added: artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/exporter/SqlExporterTest.java =================================================================== --- artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/exporter/SqlExporterTest.java (rev 0) +++ artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/exporter/SqlExporterTest.java 2009-03-19 16:35:53 UTC (rev 2959) @@ -0,0 +1,128 @@ +/* + * Copyright 2007-2009 Jiemamy Project and the Others. + * Created on 2008/07/12 + * + * 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.composer.exporter; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.apache.commons.io.IOUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.jiemamy.Artemis; +import org.jiemamy.Jiemamy; +import org.jiemamy.JiemamyFactory; +import org.jiemamy.composer.Exporter; +import org.jiemamy.model.RootModel; +import org.jiemamy.test.ReflectionDialectProvider; +import org.jiemamy.test.TestModelBuilder; + +/** + * {@link SqlExporter}のテストクラス。 + * @author daisuke + */ +public class SqlExporterTest { + + private static Logger logger = LoggerFactory.getLogger(SqlExporterTest.class); + + /** ${WORKSPACE}/org.jiemamy.composer/target/sqlExporterTest1.sql */ + private static final String FILENAME = "./target/sqlExporterTest1.sql"; + + /** テスト対象のエクスポータ */ + private Exporter<SqlExportConfig> exporter = new SqlExporter(); + + private JiemamyFactory factory; + + + /** + * テストを初期化する。 + * + * @throws Exception 例外が発生した場合 + */ + @Before + public void setUp() throws Exception { + Jiemamy jiemamy = Jiemamy.newInstance(new Artemis(), new ReflectionDialectProvider()); + TestModelBuilder mb = new TestModelBuilder(jiemamy); + mb.build(); + factory = jiemamy.getFactory(); + } + + /** + * テストの情報を破棄する。 + * + * @throws Exception 例外が発生した場合 + */ + @After + public void tearDown() throws Exception { + factory = null; + } + + /** + * モデルからSQLファイルがエクスポートできることを確認する。 + * + * @throws Exception 例外が発生した場合 + */ + @Test + public void test01_モデルからSQLファイルがエクスポートできることを確認する() throws Exception { + File outputFile = new File(FILENAME); + deleteFile(outputFile); + assertThat(outputFile.exists(), is(false)); + + RootModel model = factory.getRootModel(); + model.setDialectClassName("org.jiemamy.dialect.mysql.MySqlDialect"); + + BufferedReader reader = null; + try { + DefaultSqlExportConfig config = new DefaultSqlExportConfig(); + config.setOutputFile(outputFile); + config.setOverwrite(true); + exporter.exportModel(model, config); + + assertThat(outputFile.exists(), is(true)); + + reader = new BufferedReader(new FileReader(outputFile)); + String line; + while ((line = reader.readLine()) != null) { + logger.info(line); + } + + // UNDONE sqlExporterTest1.sqlの内容確認 + + } finally { + IOUtils.closeQuietly(reader); + } + } + + private void deleteFile(File outputFile) { + if (outputFile.exists() == false) { + return; + } + if (outputFile.delete() == false) { + fail("Cannot delete file: " + outputFile.getPath()); + } + } +} Added: artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/importer/DatabaseImporterTest.java =================================================================== --- artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/importer/DatabaseImporterTest.java (rev 0) +++ artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/composer/importer/DatabaseImporterTest.java 2009-03-19 16:35:53 UTC (rev 2959) @@ -0,0 +1,239 @@ +/* + * Copyright 2007-2009 Jiemamy Project and the Others. + * Created on 2008/09/10 + * + * 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.composer.importer; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.collection.IsIn.isIn; +import static org.junit.Assert.assertThat; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collection; +import java.util.List; +import java.util.UUID; + +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.jiemamy.Artemis; +import org.jiemamy.Jiemamy; +import org.jiemamy.JiemamyFactory; +import org.jiemamy.composer.Importer; +import org.jiemamy.dialect.Dialect; +import org.jiemamy.model.RootModel; +import org.jiemamy.model.attribute.ColumnModel; +import org.jiemamy.model.attribute.ColumnRef; +import org.jiemamy.model.attribute.constraint.ForeignKey; +import org.jiemamy.model.datatype.BuiltinDataType; +import org.jiemamy.model.datatype.DataTypeCategory; +import org.jiemamy.model.datatype.adapter.SizedDataTypeAdapter; +import org.jiemamy.model.entity.EntityModel; +import org.jiemamy.model.entity.TableModel; +import org.jiemamy.test.ReflectionDialectProvider; +import org.jiemamy.utils.CollectionsUtil; +import org.jiemamy.utils.model.DataTypeUtil; +import org.jiemamy.utils.model.EntityUtil; +import org.jiemamy.utils.model.RootModelUtil; +import org.jiemamy.utils.model.TableUtil; + +/** + * {@link DatabaseImporter}のテストクラス。 + * + * <p>テストを実行する環境の前提:<br/> + * <ul> + * <li>PostgreSQL Server稼働</li> + * <li>user=postgres, pass=postgres</li> + * <li>database=jpoll に JiemamyPollsのスキーマが存在すること</li> + * </ul> + * </p> + * + * @author daisuke + */ +public class DatabaseImporterTest { + + private static Logger logger = LoggerFactory.getLogger(DatabaseImporterTest.class); + + /** {@link DatabaseImporter} */ + public Importer<DatabaseImportConfig> importer = new DatabaseImporter(); + + /** SQL方言 */ + private Dialect dialect; + + private RootModel rootModel; + + private Jiemamy jiemamy; + + private JiemamyFactory factory; + + + /** + * テストを初期化する。 + * + * @throws Exception 例外が発生した場合 + */ + @Before + public void setUp() throws Exception { + jiemamy = Jiemamy.newInstance(new Artemis(), new ReflectionDialectProvider()); + factory = jiemamy.getFactory(); + rootModel = factory.getRootModel(); + rootModel.setDialectClassName("org.jiemamy.dialect.h2.H2Dialect"); + + dialect = jiemamy.getDialect(rootModel); + + Class.forName("org.h2.Driver"); + } + + /** + * テストの情報を破棄する。 + * + * @throws Exception 例外が発生した場合 + */ + @After + public void tearDown() throws Exception { + dialect = null; + rootModel = null; + jiemamy = null; + } + + /** + * DBからのインポートテスト。 + * + * @throws Exception 例外が発生した場合 + */ + @Test + @Ignore("H2Dialectが無いとテストできない") + public void test01_DBからのインポートテスト() throws Exception { + DatabaseImportConfig config = createConfig(); + logger.info("START import..."); + importer.importModel(rootModel, config); + logger.info("FINISH import."); + + // テスト用DBには、ノードが4つ、コネクションが5つあるはず + assertThat(rootModel.getEntities().size(), is(4)); + assertThat(RootModelUtil.getAllForeignKeys(rootModel).size(), is(5)); + + TableModel pollTable = RootModelUtil.getEntity(rootModel, TableModel.class, "T_POLL"); + logger.info("Table - " + pollTable.getName()); + + assertThat(pollTable.getName(), is("T_POLL")); + assertThat(TableUtil.getColumns(pollTable).size(), is(11)); + + List<UUID> pkColumnIds = CollectionsUtil.newArrayList(); + for (ColumnRef ref : TableUtil.getPrimaryKey(pollTable).getKeyColumns()) { + pkColumnIds.add(ref.getReferenceId()); + } + + ColumnModel columnModel1 = TableUtil.getColumns(pollTable).get(0); + logger.info(" Column - " + columnModel1.getName()); + BuiltinDataType builtinDataType1 = + DataTypeUtil.toBuiltinDataType(columnModel1.getDataType(), jiemamy.getReferenceResolver()); + assertThat(columnModel1.getName(), is("POLL_ID")); + assertThat(builtinDataType1.getCategory(), is(DataTypeCategory.INTEGER)); + assertThat(builtinDataType1.getTypeName(), is("serial")); + // FIXME この仕様はまだ未実装 +// assertThat(column1.getDataType().getAdapter(SerialDataTypeAdapter.class).isSerial(), is(true)); + assertThat(columnModel1.getId(), isIn(pkColumnIds)); + + ColumnModel columnModel2 = TableUtil.getColumns(pollTable).get(1); + logger.info(" Column - " + columnModel2.getName()); + BuiltinDataType builtinDataType2 = + DataTypeUtil.toBuiltinDataType(columnModel2.getDataType(), jiemamy.getReferenceResolver()); + assertThat(columnModel2.getName(), is("TITLE")); + assertThat(builtinDataType2.getCategory(), is(DataTypeCategory.VARCHAR)); + assertThat(builtinDataType2.getTypeName(), is("varchar")); + assertThat(builtinDataType2.getAdapter(SizedDataTypeAdapter.class).getSize(), is(128)); + assertThat(columnModel2.getId(), not(isIn(pkColumnIds))); + + TableModel choiceTable = RootModelUtil.getEntity(rootModel, TableModel.class, "t_choice"); + logger.info("Table - " + choiceTable.getName()); + + assertThat(choiceTable.getName(), is("t_choice")); + assertThat(TableUtil.getColumns(choiceTable).size(), is(6)); + + ForeignKey foreignKey = TableUtil.getAttributes(choiceTable, ForeignKey.class).get(0); + logger.info("FK - " + foreignKey.getName()); + assertThat(foreignKey.getName(), is("fkey_t_choice_t_poll")); + + TableModel voteTable = RootModelUtil.getEntity(rootModel, TableModel.class, "t_vote"); + TableModel userTable = RootModelUtil.getEntity(rootModel, TableModel.class, "t_user"); + + // t_choice → t_poll の関係 + Collection<EntityModel> dependentEntities = EntityUtil.getDependentEntities(pollTable, false); + for (EntityModel dependenetEntity : dependentEntities) { + logger.info("t_poll's dependent: " + dependenetEntity.getName()); + } + assertThat(dependentEntities.size(), is(2)); + assertThat(dependentEntities, hasItems((EntityModel) choiceTable, voteTable)); + + Collection<EntityModel> referenceEntities = EntityUtil.getReferenceEntities(pollTable, false); + for (EntityModel entityModel : referenceEntities) { + logger.info("t_poll's reference: " + entityModel.getName()); + } + assertThat(referenceEntities.size(), is(1)); + assertThat(referenceEntities, hasItems((EntityModel) userTable)); + } + + /** + * インポートコンテキスト情報を生成する。 + * + * @return インポートコンテキスト情報 + * @throws MalformedURLException + */ + private DatabaseImportConfig createConfig() throws MalformedURLException { + DefaultDatabaseImportConfig config = new DefaultDatabaseImportConfig(); + config.setDialect(dialect); + config.setDatabaseName("jpoll"); + config.setDriverJarPath(new URL[] { + new File("./src/test/resources/h2-1.1.102.jar").toURL() + }); + config.setDriverClassName("org.h2.Driver"); + config.setUri("jdbc:h2:./src/test/resources/database/testdb"); + config.setSchema(""); + config.setUsername("sa"); + config.setPassword(""); + config.setSelectedEntities(null); + config.setEntityTypes(null); + + return config; + } + +// private DatabaseImportConfig createConfig() throws MalformedURLException { +// ImportConfig config = new ImportConfigImpl(); +// config.setValue(DatabaseImporter.DIALECT, dialect); +// config.setValue(DatabaseImporter.DATABASE_NAME, "jpoll"); +// config.setValue(DatabaseImporter.DRIVER_JAR_PATH, new URL[] { +// new File("./src/test/resources/postgresql-8.3-603.jdbc3.jar").toURL() +// }); +// config.setValue(DatabaseImporter.DRIVER_CLASS_NAME, "org.postgresql.Driver"); +// config.setValue(DatabaseImporter.URI, "jdbc:postgresql://localhost/jpoll"); +// config.setValue(DatabaseImporter.SCHEMA, ""); +// config.setValue(DatabaseImporter.USERNAME, "postgres"); +// config.setValue(DatabaseImporter.PASSWORD, "postgres"); +// config.setValue(DatabaseImporter.SELECTED_ENTITIES, null); +// config.setValue(DatabaseImporter.ENTITY_TYPES, null); +// return config; +// } +} Deleted: charon/jiemamy-html-exporter/trunk/src/test/java/org/jiemamy/composer/exporter/SqlExporterTest.java =================================================================== --- charon/jiemamy-html-exporter/trunk/src/test/java/org/jiemamy/composer/exporter/SqlExporterTest.java 2009-03-19 15:58:26 UTC (rev 2958) +++ charon/jiemamy-html-exporter/trunk/src/test/java/org/jiemamy/composer/exporter/SqlExporterTest.java 2009-03-19 16:35:53 UTC (rev 2959) @@ -1,128 +0,0 @@ -/* - * Copyright 2007-2009 Jiemamy Project and the Others. - * Created on 2008/07/12 - * - * 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.composer.exporter; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; - -import org.apache.commons.io.IOUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.jiemamy.Artemis; -import org.jiemamy.Jiemamy; -import org.jiemamy.JiemamyFactory; -import org.jiemamy.composer.Exporter; -import org.jiemamy.model.RootModel; -import org.jiemamy.test.ReflectionDialectProvider; -import org.jiemamy.test.TestModelBuilder; - -/** - * {@link SqlExporter}のテストクラス。 - * @author daisuke - */ -public class SqlExporterTest { - - private static Logger logger = LoggerFactory.getLogger(SqlExporterTest.class); - - /** ${WORKSPACE}/org.jiemamy.composer/target/sqlExporterTest1.sql */ - private static final String FILENAME = "./target/sqlExporterTest1.sql"; - - /** テスト対象のエクスポータ */ - private Exporter<SqlExportConfig> exporter = new SqlExporter(); - - private JiemamyFactory factory; - - - /** - * テストを初期化する。 - * - * @throws Exception 例外が発生した場合 - */ - @Before - public void setUp() throws Exception { - Jiemamy jiemamy = Jiemamy.newInstance(new Artemis(), new ReflectionDialectProvider()); - TestModelBuilder mb = new TestModelBuilder(jiemamy); - mb.build(); - factory = jiemamy.getFactory(); - } - - /** - * テストの情報を破棄する。 - * - * @throws Exception 例外が発生した場合 - */ - @After - public void tearDown() throws Exception { - factory = null; - } - - /** - * モデルからSQLファイルがエクスポートできることを確認する。 - * - * @throws Exception 例外が発生した場合 - */ - @Test - public void test01_モデルからSQLファイルがエクスポートできることを確認する() throws Exception { - File outputFile = new File(FILENAME); - deleteFile(outputFile); - assertThat(outputFile.exists(), is(false)); - - RootModel model = factory.getRootModel(); - model.setDialectClassName("org.jiemamy.dialect.mysql.MySqlDialect"); - - BufferedReader reader = null; - try { - DefaultSqlExportConfig config = new DefaultSqlExportConfig(); - config.setOutputFile(outputFile); - config.setOverwrite(true); - exporter.exportModel(model, config); - - assertThat(outputFile.exists(), is(true)); - - reader = new BufferedReader(new FileReader(outputFile)); - String line; - while ((line = reader.readLine()) != null) { - logger.info(line); - } - - // UNDONE sqlExporterTest1.sqlの内容確認 - - } finally { - IOUtils.closeQuietly(reader); - } - } - - private void deleteFile(File outputFile) { - if (outputFile.exists() == false) { - return; - } - if (outputFile.delete() == false) { - fail("Cannot delete file: " + outputFile.getPath()); - } - } -}