svnno****@sourc*****
svnno****@sourc*****
2009年 4月 6日 (月) 03:50:43 JST
Revision: 3133 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=3133 Author: daisuke_m Date: 2009-04-06 03:50:43 +0900 (Mon, 06 Apr 2009) Log Message: ----------- PostgreSQLを使用したテスト開始。 Modified Paths: -------------- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultSqlEmitter.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/internal/test/CoreTestModelBuilder2.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/EntityDependencyCalculator.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/SqlExecuter.java artemis/trunk/jiemamy-dialect-postgresql/src/main/java/org/jiemamy/dialect/postgresql/PostgreSqlEmitter.java Added Paths: ----------- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/internal/test/ artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/internal/test/TestModelsTestBase.java artemis/trunk/jiemamy-artemis-test/src/test/resources/postgresql-8.3-603.jdbc3.jar Property Changed: ---------------- artemis/trunk/jiemamy-artemis-test/src/test/resources/ -------------- next part -------------- Added: artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java =================================================================== --- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java (rev 0) +++ artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java 2009-04-05 18:50:43 UTC (rev 3133) @@ -0,0 +1,117 @@ +/* + * Copyright 2007-2009 Jiemamy Project and the Others. + * Created on 2009/04/06 + * + * 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.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; + +import java.io.File; +import java.io.InputStream; +import java.net.URL; +import java.sql.Connection; +import java.sql.Driver; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.jiemamy.composer.exporter.DefaultSqlExportConfig; +import org.jiemamy.dialect.postgresql.PostgresqlDialect; +import org.jiemamy.internal.test.TestModelEntry; +import org.jiemamy.internal.test.TestModelsTestBase; +import org.jiemamy.model.RootModel; +import org.jiemamy.model.sql.SqlStatement; +import org.jiemamy.utils.DriverUtil; +import org.jiemamy.utils.JmIOUtil; +import org.jiemamy.utils.SqlExecuter; + +/** + * TODO for daisuke + * + * @author daisuke + */ +public class DatabaseTest extends TestModelsTestBase { + + private static Logger logger = LoggerFactory.getLogger(DatabaseTest.class); + + + @Override + public void doTest(TestModelEntry entry) throws Exception { + Jiemamy jiemamy = entry.getBuiltModel(); + + RootModel rootModel = jiemamy.getFactory().getRootModel(); + rootModel.setDialectClassName(PostgresqlDialect.class.getName()); + + DefaultSqlExportConfig config = new DefaultSqlExportConfig(); + config.setEmitDropStatements(true); + config.setDataSetIndex(-1); // FIXME この行削除しても動くように + List<SqlStatement> statements = jiemamy.emitStatements(rootModel, config); + + Properties propFile = new Properties(); + InputStream in = null; + + try { + in = DatabaseTest.class.getResourceAsStream("/database.properties"); + if (in == null) { + // This accout is only available from Hudson + propFile.put("postgres_uri", "jdbc:postgresql://griffon.jiemamy.org:5432/jiemamy01"); + propFile.put("postgres_user", "jiemamy01"); + propFile.put("postgres_password", "dummy"); + } else { + propFile.load(in); + } + } finally { + IOUtils.closeQuietly(in); + } + + Connection connection = null; + try { + + Properties props = new Properties(); + props.setProperty("user", propFile.getProperty("postgres_user")); + props.setProperty("password", propFile.getProperty("postgres_password")); + + URL[] paths = new URL[] { + new File("./src/test/resources/postgresql-8.3-603.jdbc3.jar").toURL() + }; + + Driver driver = DriverUtil.getDriverInstance(paths, "org.postgresql.Driver"); + connection = driver.connect(propFile.getProperty("postgres_uri"), props); + + assertThat(connection, is(notNullValue())); + + 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()); + } + } + } finally { + JmIOUtil.closeQuietly(connection); + } + } + +} Property changes on: artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/DatabaseTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/internal/test/TestModelsTestBase.java =================================================================== --- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/internal/test/TestModelsTestBase.java (rev 0) +++ artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/internal/test/TestModelsTestBase.java 2009-04-05 18:50:43 UTC (rev 3133) @@ -0,0 +1,79 @@ +/* + * Copyright 2007-2009 Jiemamy Project and the Others. + * Created on 2009/04/04 + * + * 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.internal.test; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link TestModelBuilder}で生成したモデルを使用してテストを行う為の基底クラス。 + * + * @author daisuke + */ +public abstract class TestModelsTestBase { + + private static Logger logger = LoggerFactory.getLogger(TestModelsTestBase.class); + + private TestModelBuilderProvider provider; + + + /** + * テストを行う。 + * + * @param entry テストモデルのエントリ + * @throws Exception 例外が発生した場合 + */ + public abstract void doTest(TestModelEntry entry) throws Exception; + + /** + * テストを初期化する。 + * + * @throws Exception 例外が発生した場合 + */ + @Before + public void setUp() throws Exception { + provider = new TestModelBuilderProvider(); + } + + /** + * テストの情報を破棄する。 + * + * @throws Exception 例外が発生した場合 + */ + @After + public void tearDown() throws Exception { + provider = null; + } + + /** + * 全ての {@link TestModelBuilder}でテストを行うテストメソッド。 + * + * @throws Exception 例外が発生した場合 + */ + @Test + public void testAllEntries() throws Exception { + for (TestModelEntry entry : provider) { + logger.info("==== Test Model: " + entry.getName()); + doTest(entry); + } + } +} Property changes on: artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/internal/test/TestModelsTestBase.java ___________________________________________________________________ Added: svn:mime-type + text/plain Property changes on: artemis/trunk/jiemamy-artemis-test/src/test/resources ___________________________________________________________________ Added: svn:ignore + database.properties Added: artemis/trunk/jiemamy-artemis-test/src/test/resources/postgresql-8.3-603.jdbc3.jar =================================================================== (Binary files differ) Property changes on: artemis/trunk/jiemamy-artemis-test/src/test/resources/postgresql-8.3-603.jdbc3.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultSqlEmitter.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultSqlEmitter.java 2009-04-05 17:37:18 UTC (rev 3132) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/dialect/internal/impl/DefaultSqlEmitter.java 2009-04-05 18:50:43 UTC (rev 3133) @@ -19,11 +19,12 @@ package org.jiemamy.dialect.internal.impl; import java.util.List; -import java.util.Map.Entry; +import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.Validate; +import org.jiemamy.Jiemamy; import org.jiemamy.JiemamyFactory; import org.jiemamy.ReferenceResolver; import org.jiemamy.dialect.DataTypeResolver; @@ -101,9 +102,22 @@ } if (StringUtils.isEmpty(rootModel.getSchemaName()) == false) { + if (config.emitDropStatements()) { + result.add(emitDropSchemaStatement(rootModel.getSchemaName())); + } + result.add(emitCreateSchemaStatement(rootModel.getSchemaName())); } + if (config.emitDropStatements()) { + List<EntityModel> dropList = EntityDependencyCalculator.getSortedEntityList(rootModel); + EntityModel[] array = dropList.toArray(new EntityModel[dropList.size()]); + ArrayUtils.reverse(array); + for (EntityModel entityModel : array) { + result.add(emitDropEntityStatement(entityModel)); + } + } + for (EntityModel entityModel : EntityDependencyCalculator.getSortedEntityList(rootModel)) { if (entityModel.hasAdapter(Disablable.class) && Boolean.TRUE.equals(entityModel.getAdapter(Disablable.class).isDisabled())) { @@ -113,10 +127,6 @@ result.add(new SqlStatementImpl(Literal.of(entityModel.getBeginScript(), LiteralType.FRAGMENT))); } - if (config.emitDropStatements()) { - result.add(emitDropEntityStatement(entityModel)); - } - result.add(emitCreateStatement(entityModel)); if (entityModel instanceof TableModel) { @@ -141,15 +151,27 @@ int dataSetIndex = config.getDataSetIndex(); if (dataSetIndex >= 0 && dataSetIndex < rootModel.getDataSets().size()) { - ReferenceResolver referenceResolver = rootModel.getJiemamy().getReferenceResolver(); + Jiemamy jiemamy = rootModel.getJiemamy(); + JiemamyFactory factory = jiemamy.getFactory(); + DataSetModel dataSetModel = rootModel.getDataSets().get(dataSetIndex); - for (Entry<TableRef, List<RecordModel>> entry : dataSetModel.getRecords().entrySet()) { - TableModel tableModel = referenceResolver.resolve(entry.getKey()); - List<RecordModel> records = entry.getValue(); + + for (EntityModel entityModel : EntityDependencyCalculator.getSortedEntityList(rootModel)) { + if (entityModel.hasAdapter(Disablable.class) + && Boolean.TRUE.equals(entityModel.getAdapter(Disablable.class).isDisabled())) { + continue; + } + if (entityModel instanceof TableModel) { + TableModel tableModel = (TableModel) entityModel; + TableRef tableRef = factory.newReference(tableModel); + List<RecordModel> records = dataSetModel.getRecords().get(tableRef); + if (records != null) { + for (RecordModel recordModel : records) { + result.add(emitInsertStatement(tableModel, recordModel)); + } + } + } - for (RecordModel recordModel : records) { - result.add(emitInsertStatement(tableModel, recordModel)); - } } } @@ -249,6 +271,21 @@ } /** + * スキーマのDROP文を出力する。 + * + * @param schemaName スキーマ名 + * @return DROP文 + */ + protected SqlStatement emitDropSchemaStatement(String schemaName) { + List<Token> tokens = CollectionsUtil.newArrayList(); + tokens.add(Keyword.DROP); + tokens.add(Keyword.SCHEMA); + tokens.add(Identifier.of(schemaName)); + tokens.add(Separator.SEMICOLON); + return new SqlStatementImpl(tokens); + } + + /** * インデックスカラムの定義句を出力する。 * * @param indexColumnModel 対象インデックスカラム @@ -373,7 +410,9 @@ tokens.add(Identifier.of(checkConstraint.getName())); } tokens.add(Keyword.CHECK); + tokens.add(Separator.LEFT_PAREN); tokens.add(Literal.of(checkConstraint.getExpression(), LiteralType.FRAGMENT)); + tokens.add(Separator.RIGHT_PAREN); } return tokens; Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/internal/test/CoreTestModelBuilder2.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/internal/test/CoreTestModelBuilder2.java 2009-04-05 17:37:18 UTC (rev 3132) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/internal/test/CoreTestModelBuilder2.java 2009-04-05 18:50:43 UTC (rev 3133) @@ -411,7 +411,7 @@ result.add(record); record = factory.newModel(RecordModel.class, uuid.get("23665133-7f4e-4f65-af7c-0de1b655f7c9")); - RecordUtil.addValue(record, detailId, "5"); + RecordUtil.addValue(record, detailId, "6"); RecordUtil.addValue(record, detailOrderId, ID_3); RecordUtil.addValue(record, detailItemId, ID_2); RecordUtil.addValue(record, detailQuantity, "1"); @@ -466,7 +466,7 @@ result.add(record); record = factory.newModel(RecordModel.class, uuid.get("654ceb1b-b6eb-406d-8eac-aa07c7c41e62")); - RecordUtil.addValue(record, orderId, ID_2); + RecordUtil.addValue(record, orderId, ID_3); RecordUtil.addValue(record, orderUserId, ID_2); RecordUtil.addValue(record, orderDate, "2009-01-24 00:33:44"); result.add(record); @@ -559,7 +559,7 @@ result.add(record); record = factory.newModel(RecordModel.class, uuid.get("2abef8cc-bfb7-4565-ba4e-afca3bf98811")); - RecordUtil.addValue(record, detailId, "5"); + RecordUtil.addValue(record, detailId, "6"); RecordUtil.addValue(record, detailOrderId, ID_3); RecordUtil.addValue(record, detailItemId, ID_2); RecordUtil.addValue(record, detailQuantity, "1"); Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/EntityDependencyCalculator.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/EntityDependencyCalculator.java 2009-04-05 17:37:18 UTC (rev 3132) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/EntityDependencyCalculator.java 2009-04-05 18:50:43 UTC (rev 3133) @@ -71,8 +71,8 @@ for (EntityModel dependent : EntityUtil.getReferenceEntities(entityModel, false)) { if (dependent.equals(entityModel) == false) { addDependsdentsToResult(dependent); + addToResult(dependent); } - addToResult(dependent); } } Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/SqlExecuter.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/SqlExecuter.java 2009-04-05 17:37:18 UTC (rev 3132) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/utils/SqlExecuter.java 2009-04-05 18:50:43 UTC (rev 3133) @@ -58,6 +58,9 @@ */ public ResultSet execute(String sql) throws SQLException { logger.info(sql); + + connection.setAutoCommit(false); + Statement statement = null; ResultSet resultSet = null; try { @@ -65,9 +68,28 @@ if (statement.execute(sql)) { resultSet = statement.getResultSet(); } + connection.commit(); } finally { JmIOUtil.closeQuietly(statement); } return resultSet; } + + /** + * TODO for daisuke + * + * @param sql 実行するSQL + */ + public void executeIgnoreSqlException(String sql) { + try { + execute(sql); + } catch (SQLException e) { + logger.warn("SQLException ignored: " + e.getMessage()); + try { + connection.commit(); + } catch (SQLException e2) { + logger.error("SQLException ignored: " + e.getMessage()); + } + } + } } Modified: artemis/trunk/jiemamy-dialect-postgresql/src/main/java/org/jiemamy/dialect/postgresql/PostgreSqlEmitter.java =================================================================== --- artemis/trunk/jiemamy-dialect-postgresql/src/main/java/org/jiemamy/dialect/postgresql/PostgreSqlEmitter.java 2009-04-05 17:37:18 UTC (rev 3132) +++ artemis/trunk/jiemamy-dialect-postgresql/src/main/java/org/jiemamy/dialect/postgresql/PostgreSqlEmitter.java 2009-04-05 18:50:43 UTC (rev 3133) @@ -18,6 +18,7 @@ */ package org.jiemamy.dialect.postgresql; +import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; @@ -26,9 +27,7 @@ import org.jiemamy.ReferenceResolver; import org.jiemamy.dialect.DataTypeResolver; import org.jiemamy.dialect.internal.SqlEmitter; -import org.jiemamy.dialect.internal.TokenResolver; import org.jiemamy.dialect.internal.impl.DefaultSqlEmitter; -import org.jiemamy.dialect.internal.impl.DefaultTokenResolver; import org.jiemamy.dialect.postgresql.adapter.IndexColumnOption; import org.jiemamy.dialect.postgresql.adapter.IndexMethodType; import org.jiemamy.dialect.postgresql.adapter.IndexOption; @@ -43,7 +42,6 @@ import org.jiemamy.model.entity.TableModel; import org.jiemamy.model.index.IndexColumnModel; import org.jiemamy.model.index.IndexModel; -import org.jiemamy.model.index.IndexColumnModel.SortOrder; import org.jiemamy.model.sql.SqlStatement; import org.jiemamy.model.sql.Token; import org.jiemamy.utils.CollectionsUtil; @@ -57,8 +55,6 @@ private static Logger logger = LoggerFactory.getLogger(PostgreSqlEmitter.class); - private TokenResolver tokenResolver = new DefaultTokenResolver(); - /** * インスタンスを生成する。 @@ -130,16 +126,38 @@ } } +// @Override +// protected SqlStatement emitDropEntityStatement(EntityModel entityModel) { +// SqlStatement stmt = super.emitDropEntityStatement(entityModel); +// insertIfExists(stmt); +// return stmt; +// } +// +// @Override +// protected SqlStatement emitDropIndexStatement(IndexModel indexModel) { +// SqlStatement stmt = super.emitDropIndexStatement(indexModel); +// insertIfExists(stmt); +// return stmt; +// } +// +// @Override +// protected SqlStatement emitDropSchemaStatement(String schemaName) { +// SqlStatement stmt = super.emitDropSchemaStatement(schemaName); +// insertIfExists(stmt); +// return stmt; +// } + @Override protected List<Token> emitIndexColumnClause(IndexColumnModel indexColumnModel) { + List<Token> tokens = CollectionsUtil.newArrayList(); + ReferenceResolver referenceResolver = indexColumnModel.getJiemamy().getReferenceResolver(); + ColumnRef columnRef = indexColumnModel.getColumnRef(); + ColumnModel columnModel = referenceResolver.resolve(columnRef); + tokens.add(Identifier.of(columnModel.getName())); if (indexColumnModel.hasAdapter(IndexColumnOption.class)) { - List<Token> tokens = CollectionsUtil.newArrayList(); - ReferenceResolver referenceResolver = indexColumnModel.getJiemamy().getReferenceResolver(); - ColumnRef columnRef = indexColumnModel.getColumnRef(); - ColumnModel columnModel = referenceResolver.resolve(columnRef); - tokens.add(Identifier.of(columnModel.getName())); - SortOrder sortOrder = indexColumnModel.getSortOrder(); - tokens.addAll(tokenResolver.resolve(sortOrder)); + // FIXME "CREATE INDEX IDX_EMP_NAME ON T_EMP(EMP_NAME DESC);" がエラーを起こす。原因不明。 +// SortOrder sortOrder = indexColumnModel.getSortOrder(); +// tokens.addAll(tokenResolver.resolve(sortOrder)); IndexColumnOption indexColumnOption = indexColumnModel.getAdapter(IndexColumnOption.class); if (indexColumnOption.getNullOrder() != null) { @@ -154,10 +172,19 @@ tokens.remove(tokens.size() - 1); } } - - return tokens; } else { - return super.emitIndexColumnClause(indexColumnModel); + // FIXME "CREATE INDEX IDX_EMP_NAME ON T_EMP(EMP_NAME DESC);" がエラーを起こす。原因不明。 +// SortOrder sortOrder = indexColumnModel.getSortOrder(); +// tokens.addAll(tokenResolver.resolve(sortOrder)); } + return tokens; } + + private void insertIfExists(SqlStatement stmt) { + SqlStatementImpl stmt2 = (SqlStatementImpl) stmt; + ArrayList<Token> tokens = CollectionsUtil.newArrayList(); + tokens.add(Keyword.of("IF")); + tokens.add(Keyword.of("EXISTS")); + stmt2.insert(2, tokens); + } }