svnno****@sourc*****
svnno****@sourc*****
2009年 4月 9日 (木) 01:21:42 JST
Revision: 3200 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=3200 Author: daisuke_m Date: 2009-04-09 01:21:42 +0900 (Thu, 09 Apr 2009) Log Message: ----------- DBインポートのPrecisionedDataType情報取得部分を記述。/ DBインポートのForeignKeyの、Deferrability, ReferentialAction情報取得部分を記述。 / refactor Modified Paths: -------------- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultEntityImportVisitor.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultForeignKeyImportVisitor.java artemis/trunk/jiemamy-dialect-mysql/src/main/java/org/jiemamy/dialect/mysql/MySqlEmitter.java leto/jiemamy-test-helper/trunk/src/main/java/org/jiemamy/internal/test/TestModelBuilders.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 16:16:28 UTC (rev 3199) +++ artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java 2009-04-08 16:21:42 UTC (rev 3200) @@ -47,6 +47,7 @@ import org.apache.commons.lang.CharEncoding; import org.custommonkey.xmlunit.DetailedDiff; import org.custommonkey.xmlunit.Diff; +import org.junit.Before; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; @@ -55,6 +56,7 @@ import org.slf4j.LoggerFactory; import org.jiemamy.composer.exporter.DefaultSqlExportConfig; +import org.jiemamy.composer.importer.DatabaseImporter; import org.jiemamy.composer.importer.DefaultDatabaseImportConfig; import org.jiemamy.dialect.Dialect; import org.jiemamy.internal.test.TestModelBuilders; @@ -85,7 +87,7 @@ private static Logger logger = LoggerFactory.getLogger(DatabaseTest.class); /** テスト結果の参考ファイル配置パス */ - private static final String OUTPUT_PATH = "./target/datafiletest/"; + private static final String OUTPUT_PATH = "./target/databasetest/"; /** テスト対象ファイル配置パス */ private static final String TARGET_PATH = "./src/test/resources/datafiles/"; @@ -110,14 +112,29 @@ /** - * DB適用テスト。 + * テストを初期化する。 * + * @throws Exception 例外が発生した場合 + */ + @Before + public void setUp() throws Exception { + File outDir = new File(OUTPUT_PATH); + if (outDir.exists() == false) { + boolean mkdirs = outDir.mkdirs(); + assertThat(mkdirs, is(true)); + } + } + + /** + * テストモデルビルダのDB適用と読み出しテスト。 + * * @param builder テスト対象データファイル * @param testDatabase テストDB * @throws Exception 例外が発生した場合 */ @Theory - public void test01_テストモデルビルダのDB適用テスト(TestModelBuilders builder, TestDatabaseInstance testDatabase) throws Exception { + public void test01_テストモデルビルダのDB適用と読み出しテスト(TestModelBuilders builder, TestDatabaseInstance testDatabase) + throws Exception { logger.info("== Database: " + testDatabase.name()); // FIXME Oracleのdialectが整備されるまでskip @@ -126,7 +143,17 @@ Jiemamy jiemamy = builder.getBuiltModel(); try { - doDatabaseTest(testDatabase, jiemamy); + applyModelToDatabase(testDatabase, jiemamy); + Jiemamy jiemamy2 = readDatabase(testDatabase); + + // TODO jiemamy と jiemamy2 の比較アサーション + EqualsUtil.equals(jiemamy.getFactory().getRootModel(), jiemamy2.getFactory().getRootModel()); + + FileUtils.writeStringToFile(new File(OUTPUT_PATH + "test01_" + testDatabase.name() + "_" + builder.name() + + "_gtree1.txt"), EqualsUtil.rightGTree, CharEncoding.UTF_8); + + FileUtils.writeStringToFile(new File(OUTPUT_PATH + "test01_" + testDatabase.name() + "_" + builder.name() + + "_gtree2.txt"), EqualsUtil.leftGTree, CharEncoding.UTF_8); } catch (Exception e) { String message = "failed on " + testDatabase.name() + " with " + builder.name(); logger.error(message, e); @@ -135,14 +162,14 @@ } /** - * DB適用テスト。 + * テストデータファイルのDB適用と読み出しテスト。 * * @param file テスト対象データファイル * @param testDatabase テストDB * @throws Exception 例外が発生した場合 */ @Theory - public void test02_テストデータファイルのDB適用テスト(File file, TestDatabaseInstance testDatabase) throws Exception { + public void test02_テストデータファイルのDB適用と読み出しテスト(File file, TestDatabaseInstance testDatabase) throws Exception { logger.info("== Database: " + testDatabase.name()); // FIXME Oracleのdialectが整備されるまでskip @@ -158,11 +185,24 @@ } try { - doDatabaseTest(testDatabase, jiemamy); + applyModelToDatabase(testDatabase, jiemamy); + Jiemamy jiemamy2 = readDatabase(testDatabase); + + // TODO jiemamy と jiemamy2 の比較アサーション + EqualsUtil.equals(jiemamy.getFactory().getRootModel(), jiemamy2.getFactory().getRootModel()); + + FileUtils.writeStringToFile(new File(OUTPUT_PATH + "test02_" + testDatabase.name() + "_" + + FilenameUtils.getName(file.getAbsolutePath()) + "_gtree1.txt"), EqualsUtil.rightGTree, + CharEncoding.UTF_8); + + FileUtils.writeStringToFile(new File(OUTPUT_PATH + "test02_" + testDatabase.name() + "_" + + FilenameUtils.getName(file.getAbsolutePath()) + "_gtree2.txt"), EqualsUtil.leftGTree, + CharEncoding.UTF_8); } catch (Exception e) { String message = "failed on " + testDatabase.name() + " with " + file.getAbsolutePath(); logger.error(message, e); - fail(message); + Thread.sleep(10000); + fail(message + " " + e.getMessage()); } } @@ -173,7 +213,7 @@ * @throws Exception 例外が発生した場合 */ @Theory - public void test99_往復テスト_XRXR(File file) throws Exception { + public void test03_往復テスト_XRXR(File file) throws Exception { String baseName = FilenameUtils.getBaseName(file.getName()); Jiemamy jiemamy1 = Jiemamy.newInstance(new Artemis(new ArtemisView())); @@ -199,16 +239,18 @@ 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 + "test03_" + baseName + "_xml1.xml"), xml1, + CharEncoding.UTF_8); - FileUtils.writeStringToFile(new File(OUTPUT_PATH + baseName + "_xml2.xml"), xml2, CharEncoding.UTF_8); + FileUtils.writeStringToFile(new File(OUTPUT_PATH + "test03_" + 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 + "test03_" + baseName + "_gtree1.txt"), + EqualsUtil.rightGTree, CharEncoding.UTF_8); - FileUtils.writeStringToFile(new File(OUTPUT_PATH + baseName + "_gtree2.txt"), EqualsUtil.leftGTree, + FileUtils.writeStringToFile(new File(OUTPUT_PATH + "test03_" + baseName + "_gtree2.txt"), EqualsUtil.leftGTree, CharEncoding.UTF_8); DetailedDiff diff = new DetailedDiff(new Diff(xml1, xml2)); @@ -219,35 +261,7 @@ 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 { + private void applyModelToDatabase(TestDatabaseInstance testDatabase, Jiemamy jiemamy) throws Exception { List<SqlStatement> statements = emitStatements(jiemamy, testDatabase); final DatabaseConnectionConfig connectionConfig = testDatabase.getConfig(); @@ -289,6 +303,34 @@ } } + 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; + } + /** * SQL文を出力する。 * @@ -307,4 +349,23 @@ List<SqlStatement> statements = jiemamy.emitStatements(rootModel, exportConfig); return statements; } + + private Jiemamy readDatabase(TestDatabaseInstance testDatabase) throws Exception { + Jiemamy jiemamy = Jiemamy.newInstance(); + JiemamyFactory factory = jiemamy.getFactory(); + RootModel rootModel = factory.getRootModel(); + rootModel.setDialectClassName(testDatabase.getDialectClassName()); + + DatabaseConnectionConfig connectionConfig = testDatabase.getConfig(); + + DefaultDatabaseImportConfig importConfig = new DefaultDatabaseImportConfig(); + BeanUtils.copyProperties(importConfig, connectionConfig); + importConfig.setDialect(jiemamy.getDialect(rootModel)); + importConfig.setSchema(rootModel.getSchemaName()); + + DatabaseImporter importer = new DatabaseImporter(); + importer.importModel(rootModel, importConfig); + + return jiemamy; + } } Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultEntityImportVisitor.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultEntityImportVisitor.java 2009-04-08 16:16:28 UTC (rev 3199) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultEntityImportVisitor.java 2009-04-08 16:21:42 UTC (rev 3200) @@ -41,6 +41,7 @@ import org.jiemamy.model.attribute.constraint.NotNullConstraint; import org.jiemamy.model.attribute.constraint.PrimaryKey; import org.jiemamy.model.datatype.BuiltinDataType; +import org.jiemamy.model.datatype.adapter.PrecisionedDataTypeAdapter; import org.jiemamy.model.datatype.adapter.SizedDataTypeAdapter; import org.jiemamy.model.entity.EntityModel; import org.jiemamy.model.entity.TableModel; @@ -189,18 +190,25 @@ if (dataType.hasAdapter(SizedDataTypeAdapter.class)) { dataType.getAdapter(SizedDataTypeAdapter.class).setSize(element.columnSize); logger.debug(" " + element.columnName + " = " + element.typeName + "(" - + dataType.getAdapter(SizedDataTypeAdapter.class).getSize() + ")"); + + element.columnSize + ")"); + } else if (dataType.hasAdapter(PrecisionedDataTypeAdapter.class)) { + dataType.getAdapter(PrecisionedDataTypeAdapter.class).setPrecision(element.columnSize); + dataType.getAdapter(PrecisionedDataTypeAdapter.class).setScale(element.decimalDigits); + logger.debug(" " + element.columnName + " = " + element.typeName + "(" + + element.columnSize + ", " + element.decimalDigits + ")"); } else { logger.debug(" " + element.columnName + " = " + element.typeName); } columnModel.setDefaultValue(element.columnDef); - if (Nullable.NO_NULLS.equals(element.nullable) == false) { + if (element.nullable == Nullable.NO_NULLS) { NotNullConstraint notNullConstraint = factory.newModel(NotNullConstraint.class); columnModel.setNotNullConstraint(notNullConstraint); } + // TODO check制約のインポート + finalResult.add(columnModel); return null; } Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultForeignKeyImportVisitor.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultForeignKeyImportVisitor.java 2009-04-08 16:16:28 UTC (rev 3199) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultForeignKeyImportVisitor.java 2009-04-08 16:21:42 UTC (rev 3200) @@ -24,7 +24,10 @@ import org.jiemamy.dialect.internal.ForeignKeyImportVisitor; import org.jiemamy.model.RootModel; import org.jiemamy.model.attribute.ColumnModel; +import org.jiemamy.model.attribute.constraint.Deferrability; import org.jiemamy.model.attribute.constraint.ForeignKey; +import org.jiemamy.model.attribute.constraint.Deferrability.InitiallyCheckTime; +import org.jiemamy.model.attribute.constraint.ForeignKey.ReferentialAction; import org.jiemamy.model.entity.TableModel; import org.jiemamy.utils.metadata.KeyMeta; import org.jiemamy.utils.model.ForeignKeyUtil; @@ -73,6 +76,41 @@ ColumnModel fkColumn = TableUtil.getColumn(constrainedTable, keys.fkColumnName); ColumnModel pkColumn = TableUtil.getColumn(referenceTable, keys.pkColumnName); ForeignKeyUtil.addMapping(foreignKey, fkColumn, pkColumn); + + if (keys.updateRule != null) { + ReferentialAction onUpdate = ReferentialAction.valueOf(keys.updateRule.name()); + foreignKey.setOnUpdate(onUpdate); + } + + if (keys.deleteRule != null) { + ReferentialAction onDelete = ReferentialAction.valueOf(keys.deleteRule.name()); + foreignKey.setOnDelete(onDelete); + } + + Deferrability deferrability; + switch (keys.deferrability) { + case INITIALLY_DEFERRED: + deferrability = factory.newModel(Deferrability.class); + deferrability.setDeferrable(true); + deferrability.setInitiallyCheckTime(InitiallyCheckTime.DEFERRED); + break; + + case INITIALLY_IMMEDIATE: + deferrability = factory.newModel(Deferrability.class); + deferrability.setDeferrable(true); + deferrability.setInitiallyCheckTime(InitiallyCheckTime.IMMEDIATE); + break; + + case NOT_DEFERRABLE: + deferrability = factory.newModel(Deferrability.class); + deferrability.setDeferrable(false); + break; + + default: + deferrability = null; + break; + } + foreignKey.setDeferrability(deferrability); } return null; } Modified: artemis/trunk/jiemamy-dialect-mysql/src/main/java/org/jiemamy/dialect/mysql/MySqlEmitter.java =================================================================== --- artemis/trunk/jiemamy-dialect-mysql/src/main/java/org/jiemamy/dialect/mysql/MySqlEmitter.java 2009-04-08 16:16:28 UTC (rev 3199) +++ artemis/trunk/jiemamy-dialect-mysql/src/main/java/org/jiemamy/dialect/mysql/MySqlEmitter.java 2009-04-08 16:21:42 UTC (rev 3200) @@ -226,17 +226,9 @@ tokens.add(Keyword.UPDATE); tokens.addAll(tokenResolver.resolve(foreignKey.getOnUpdate())); } - // TODO MySQLのDEFERRABLEについて、いちい君に確認の後、対策。 -// if (foreignKey.getDeferrability() != null) { -// Deferrability deferrability = foreignKey.getDeferrability(); -// if (deferrability.isDeferrable() == false) { -// tokens.add(Keyword.NOT); -// } -// tokens.add(Keyword.DEFERRABLE); -// if (deferrability.getInitiallyCheckTime() != null) { -// tokens.addAll(tokenResolver.resolve(deferrability.getInitiallyCheckTime())); -// } -// } + + // MySQLは制約の遅延評価に対応しないので、出力しない + return tokens; } }, Modified: leto/jiemamy-test-helper/trunk/src/main/java/org/jiemamy/internal/test/TestModelBuilders.java =================================================================== --- leto/jiemamy-test-helper/trunk/src/main/java/org/jiemamy/internal/test/TestModelBuilders.java 2009-04-08 16:16:28 UTC (rev 3199) +++ leto/jiemamy-test-helper/trunk/src/main/java/org/jiemamy/internal/test/TestModelBuilders.java 2009-04-08 16:21:42 UTC (rev 3200) @@ -74,4 +74,12 @@ return builder.build(); } + /** + * TODO for daisuke + * @return the xml + */ + public String getXml() { + return xml; + } + }