[Jiemamy-notify:1437] commit [2666] 親子関係かどうかを検索するユーティリティメソッドをEventBrokerに組み込み、テストも追加。

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2009年 2月 14日 (土) 01:46:20 JST


Revision: 2666
          http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=2666
Author:   shin1
Date:     2009-02-14 01:46:20 +0900 (Sat, 14 Feb 2009)

Log Message:
-----------
親子関係かどうかを検索するユーティリティメソッドをEventBrokerに組み込み、テストも追加。
EventBrokerTestに関しても一部修正(コマンドを使わず操作している箇所があった)

Modified Paths:
--------------
    artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/EventBrokerImpl.java
    artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/ReferenceResolverImpl.java
    artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/EventBrokerTest.java

Added Paths:
-----------
    artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/ReferenceResolverImplTest.java


-------------- next part --------------
Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/EventBrokerImpl.java
===================================================================
--- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/EventBrokerImpl.java	2009-02-13 10:07:49 UTC (rev 2665)
+++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/EventBrokerImpl.java	2009-02-13 16:46:20 UTC (rev 2666)
@@ -20,6 +20,7 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.UUID;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -27,7 +28,6 @@
 import org.jiemamy.editcommand.Command;
 import org.jiemamy.editcommand.CommandListener;
 import org.jiemamy.editcommand.CommandProcessorImpl;
-import org.jiemamy.model.JiemamyElement;
 
 /**
  * コマンドの実行を監視し、登録されている{@link CommandListener}にイベントを通知する。
@@ -58,9 +58,9 @@
 	 */
 	public void fireCommandProcess(Command command) {
 		logger.debug("EventBroker is kicked: " + command.toString());
-		JiemamyElement target = command.getTarget();
+		UUID uuid = command.getTarget().getId();
 		for (CommandListener l : listeners) {
-			if (requireNotification(l, target)) {
+			if (ReferenceResolverImpl.isDescendFromElement(l.getTargetModel(), uuid)) {
 				l.commandProcess(command);
 				logger.debug("Listener is kicked: " + l.toString());
 			}
@@ -74,16 +74,4 @@
 		listeners.remove(listener);
 		logger.debug("CommandListener is unregistered: " + listener.toString());
 	}
-	
-	/**
-	 * 通知が必要なリスナかどうか。
-	 * <p>targetの親を監視したい{@link CommandListener}へも通知する必要がある。</p>
-	 * 
-	 * @param l リスナ
-	 * @param target Commandが操作する対象の{@link JiemamyElement}
-	 * @return 通知が必要なら{@code true}、不要なら{@code false}
-	 */
-	public boolean requireNotification(CommandListener l, JiemamyElement target) {
-		return l.getTargetModel() == target || ReferenceResolverImpl.isChild(l.getTargetModel(), target);
-	}
 }

Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/ReferenceResolverImpl.java
===================================================================
--- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/ReferenceResolverImpl.java	2009-02-13 10:07:49 UTC (rev 2665)
+++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/ReferenceResolverImpl.java	2009-02-13 16:46:20 UTC (rev 2666)
@@ -22,7 +22,6 @@
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 import java.util.Map.Entry;
@@ -34,12 +33,6 @@
 
 import org.jiemamy.model.ElementReference;
 import org.jiemamy.model.JiemamyElement;
-import org.jiemamy.model.RootModel;
-import org.jiemamy.model.attribute.AttributeModel;
-import org.jiemamy.model.attribute.ColumnRef;
-import org.jiemamy.model.attribute.constraint.PrimaryKeyModel;
-import org.jiemamy.model.entity.EntityModel;
-import org.jiemamy.model.entity.TableModel;
 
 /**
  * モデルの参照オブジェクトからモデルの実体を解決するリゾルバ。
@@ -57,26 +50,6 @@
 	
 
 	/**
-	 * 指定したModelが、指定したModelの配下にあるのかどうかを返す。
-	 * <p>Modelの構造の外で解決した方が良い気がするので、ここに置いてみた。</p>
-	 * <p>FIXME こんな適当な実装だとメソッドの中がエライ事になりそうだし、この実装はない。なんかうまい実装は無いものか。</p>
-	 * 
-	 * @param parent
-	 * @param child
-	 * @return Model同士が親子関係にある時は{@code true}
-	 */
-	public static boolean isChild(JiemamyElement parent, JiemamyElement child) {
-		if (parent instanceof RootModel) {
-			return isRootChild((RootModel) parent, child);
-		} else if (parent instanceof TableModel) {
-			return isTableChild((TableModel) parent, child);
-		} else if (parent instanceof PrimaryKeyModel) {
-			return isPrimaryKeyChild((PrimaryKeyModel) parent, child);
-		}
-		return false;
-	}
-	
-	/**
 	 * 指定された{@link UUID}が、指定されたElementから見た関係者かどうかを深く探索する。
 	 * <p>CollectionやMapはその要素を対象にさらに深く検索するが、ElementReferenceの中は一階層だけしか見ない。</p>
 	 * <p>イベントの通知対象かどうかを判断するために導入した。</p>
@@ -93,6 +66,9 @@
 		if (element == null) {
 			return false;
 		}
+		if (element.getId().equals(uuid)) {
+			return true;
+		}
 		try {
 			// 調査対象のモデルのフィールドを全部調べる。
 			@SuppressWarnings("unchecked")
@@ -171,40 +147,6 @@
 		return false;
 	}
 	
-	private static boolean isPrimaryKeyChild(PrimaryKeyModel parent, JiemamyElement child) {
-		List<ColumnRef> columnRefs = parent.getKeyColumns();
-		for (ColumnRef ref : columnRefs) {
-			if (ref.getReferenceId().equals(child.getId())) {
-				return true;
-			}
-		}
-		return false;
-	}
-	
-	private static boolean isRootChild(RootModel root, JiemamyElement child) {
-		for (EntityModel table : root.getEntities()) {
-			if (table == child) {
-				return true;
-			}
-			if (isChild(table, child)) {
-				return true;
-			}
-		}
-		return false;
-	}
-	
-	private static boolean isTableChild(TableModel table, JiemamyElement child) {
-		for (AttributeModel attribute : table.getAttributes()) {
-			if (attribute == child) {
-				return true;
-			}
-			if (isChild(attribute, child)) {
-				return true;
-			}
-		}
-		return false;
-	}
-	
 	/**
 	 * {@inheritDoc}
 	 */

Modified: artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/EventBrokerTest.java
===================================================================
--- artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/EventBrokerTest.java	2009-02-13 10:07:49 UTC (rev 2665)
+++ artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/EventBrokerTest.java	2009-02-13 16:46:20 UTC (rev 2666)
@@ -36,6 +36,7 @@
 import org.jiemamy.editcommand.CommandProcessorImpl;
 import org.jiemamy.editcommand.impl.AddColumnCommand;
 import org.jiemamy.editcommand.impl.AddColumnToColumnRefListCommand;
+import org.jiemamy.editcommand.impl.AddPrimaryKeyCommand;
 import org.jiemamy.editcommand.impl.AddTableCommand;
 import org.jiemamy.editcommand.impl.DeleteColumnCommand;
 import org.jiemamy.editcommand.impl.DeleteColumnFromColumnRefListCommand;
@@ -99,7 +100,7 @@
 				commandStack);
 		new AddColumnToColumnRefListCommand(primaryKey, primaryKey.getKeyColumns(), column1, 0).execute(
 				commandProcessor, commandStack);
-		table.getAttributes().add(primaryKey);
+		new AddPrimaryKeyCommand(table, primaryKey).execute(commandProcessor, commandStack);
 		
 		// ここまでで以下の構造を構築した事になる。
 		// table

Added: artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/ReferenceResolverImplTest.java
===================================================================
--- artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/ReferenceResolverImplTest.java	                        (rev 0)
+++ artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/ReferenceResolverImplTest.java	2009-02-13 16:46:20 UTC (rev 2666)
@@ -0,0 +1,177 @@
+/*
+ * Copyright 2007-2009 Jiemamy Project and the Others.
+ * Created on 2009/02/14
+ *
+ * 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.junit.Assert.assertThat;
+
+import java.util.Stack;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.jiemamy.editcommand.Command;
+import org.jiemamy.editcommand.CommandProcessorImpl;
+import org.jiemamy.editcommand.impl.AddColumnCommand;
+import org.jiemamy.editcommand.impl.AddColumnToColumnRefListCommand;
+import org.jiemamy.editcommand.impl.AddPrimaryKeyCommand;
+import org.jiemamy.editcommand.impl.AddTableCommand;
+import org.jiemamy.editcommand.impl.DeleteColumnCommand;
+import org.jiemamy.editcommand.impl.DeleteColumnFromColumnRefListCommand;
+import org.jiemamy.editcommand.impl.DeleteTableCommand;
+import org.jiemamy.model.RootModel;
+import org.jiemamy.model.attribute.ColumnModel;
+import org.jiemamy.model.attribute.constraint.PrimaryKeyModel;
+import org.jiemamy.model.entity.TableModel;
+
+/**
+ * TODO for shin1ogawa
+ * 
+ * @author shin1ogawa
+ */
+public class ReferenceResolverImplTest {
+	
+	static final Logger LOGGER = LoggerFactory.getLogger(EventBrokerTest.class);
+	
+	private JiemamyFactory factory;
+	
+	private Jiemamy jiemamy;
+	
+	private EventBroker eventBroker;
+	
+	private CommandProcessorImpl commandProcessor;
+	
+	private Stack<Stack<Command>> commandStackStack;
+	
+	private RootModel rootModel;
+	
+
+	/**
+	 * テストの準備
+	 */
+	@Before
+	public void setUp() {
+		jiemamy = Jiemamy.newInstance();
+		factory = jiemamy.getFactory();
+		rootModel = factory.newModel(RootModel.class);
+		eventBroker = jiemamy.getEventBroker();
+		commandProcessor = new CommandProcessorImpl(eventBroker);
+		commandStackStack = new Stack<Stack<Command>>();
+	}
+	
+	/**
+	 * {@link ReferenceResolverImpl#isDescendFromElement(org.jiemamy.model.JiemamyElement, java.util.UUID)}
+	 * 
+	 * @throws Exception
+	 */
+	@Test
+	public void test01親子関係の判断() throws Exception {
+		Stack<Command> commandStack = new Stack<Command>();
+		TableModel table = factory.newModel(TableModel.class);
+		new AddTableCommand(rootModel, table).execute(commandProcessor, commandStack);
+		ColumnModel column1 = factory.newModel(ColumnModel.class);
+		new AddColumnCommand(table, column1).execute(commandProcessor, commandStack);
+		ColumnModel column2 = factory.newModel(ColumnModel.class);
+		new AddColumnCommand(table, column2).execute(commandProcessor, commandStack);
+		
+		PrimaryKeyModel primaryKey = factory.newModel(PrimaryKeyModel.class);
+		new AddColumnToColumnRefListCommand(primaryKey, primaryKey.getKeyColumns(), column2).execute(commandProcessor,
+				commandStack);
+		new AddColumnToColumnRefListCommand(primaryKey, primaryKey.getKeyColumns(), column1, 0).execute(
+				commandProcessor, commandStack);
+		new AddPrimaryKeyCommand(table, primaryKey).execute(commandProcessor, commandStack);
+		commandStackStack.push(commandStack); // 構築操作を行ったCommandStackをpushする。
+		
+		assertThat(table.getAttributes().size(), is(3)); // column1,column2,pkey
+		assertThat(primaryKey.getKeyColumns().size(), is(2)); // [ref]column1, [ref]column2。
+		assertThat(primaryKey.getKeyColumns().get(0).getReferenceId(), is(column1.getId()));
+		assertThat(primaryKey.getKeyColumns().get(1).getReferenceId(), is(column2.getId()));
+		
+		// ここまでで以下の構造を構築した事になる。
+		// table
+		//  +attributes
+		//   -column1
+		//   -column2
+		//   +primaryKey
+		//    -[ref]column1
+		//    -[ref]column2
+		
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, column1.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, primaryKey.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, table.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(table, column1.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(table, primaryKey.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(primaryKey, column1.getId()), Matchers.is(true));
+		
+		// 新たなStackを用意する。
+		commandStack = new Stack<Command>();
+		
+		// columnをprimaryKeyから削除する。
+		new DeleteColumnFromColumnRefListCommand(primaryKey, primaryKey.getKeyColumns(), column1).execute(
+				commandProcessor, commandStack);
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, column1.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, primaryKey.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, table.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(table, column1.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(table, primaryKey.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(primaryKey, column1.getId()), Matchers.is(false));
+		
+		// columnをtableから削除する。
+		new DeleteColumnCommand(table, column1).execute(commandProcessor, commandStack);
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, column1.getId()), Matchers.is(false));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, primaryKey.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, table.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(table, column1.getId()), Matchers.is(false));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(table, primaryKey.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(primaryKey, column1.getId()), Matchers.is(false));
+		
+		// talbeをRootModelから削除する。
+		new DeleteTableCommand(rootModel, table).execute(commandProcessor, commandStack);
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, column1.getId()), Matchers.is(false));
+		Assert
+			.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, primaryKey.getId()), Matchers.is(false));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, table.getId()), Matchers.is(false));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(table, column1.getId()), Matchers.is(false));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(table, primaryKey.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(primaryKey, column1.getId()), Matchers.is(false));
+		commandStackStack.push(commandStack); // 削除操作を行ったCommandStackをpushする。
+		
+		// 一連の削除操作を全て取り消す。
+		Stack<Command> deleteCommands = commandStackStack.pop();
+		while (deleteCommands.isEmpty() == false) {
+			Command command = deleteCommands.pop();
+			command.execute(commandProcessor);
+		}
+		
+		assertThat(table.getAttributes().size(), is(3)); // column1,column2,pkey
+		assertThat(primaryKey.getKeyColumns().size(), is(2)); // [ref]column1, [ref]column2。
+		assertThat(primaryKey.getKeyColumns().get(0).getReferenceId(), is(column1.getId()));
+		assertThat(primaryKey.getKeyColumns().get(1).getReferenceId(), is(column2.getId()));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, column1.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, primaryKey.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(rootModel, table.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(table, column1.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(table, primaryKey.getId()), Matchers.is(true));
+		Assert.assertThat(ReferenceResolverImpl.isDescendFromElement(primaryKey, column1.getId()), Matchers.is(true));
+	}
+}


Property changes on: artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/ReferenceResolverImplTest.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain



Jiemamy-notify メーリングリストの案内
Back to archive index