svnno****@sourc*****
svnno****@sourc*****
2009年 2月 9日 (月) 21:35:04 JST
Revision: 2639 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=2639 Author: shin1 Date: 2009-02-09 21:35:04 +0900 (Mon, 09 Feb 2009) Log Message: ----------- EventBrokerとCommand関連のインターフェースを切り出してみた。 テストケースもsample2からマージ。 Modified Paths: -------------- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/Artemis.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/EventBrokerImpl.java Added Paths: ----------- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/CommandProcessorImpl.java artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/EventBrokerTest.java Removed Paths: ------------- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/Command.java artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/CommandProcessor.java -------------- next part -------------- Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/Artemis.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/Artemis.java 2009-02-08 16:09:44 UTC (rev 2638) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/Artemis.java 2009-02-09 12:35:04 UTC (rev 2639) @@ -54,7 +54,7 @@ private final ReferenceResolver resolver = new ReferenceResolverImpl(); /** イベント通知を適切に行うブローカー */ - private final EventBrokerImpl eventBroker = new EventBrokerImpl(); + private final EventBroker eventBroker = new EventBrokerImpl(); /** 実装に施された拡張の配列 */ private final ArtemisExtender[] extensions; @@ -103,7 +103,7 @@ /** * {@inheritDoc} */ - public EventBrokerImpl getEventBroker() { + public EventBroker getEventBroker() { assert eventBroker != null; return eventBroker; } 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-08 16:09:44 UTC (rev 2638) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/EventBrokerImpl.java 2009-02-09 12:35:04 UTC (rev 2639) @@ -5,39 +5,32 @@ import org.jiemamy.editcommand.Command; import org.jiemamy.editcommand.CommandListener; -import org.jiemamy.editcommand.CommandProcessor; +import org.jiemamy.editcommand.CommandProcessorImpl; import org.jiemamy.model.JiemamyElement; /** * コマンドの実行を監視し、登録されている{@link CommandListener}にイベントを通知する。 * <p>{@link Command}が実行されたタイミングで、{@link #listeners}として保持している{@link CommandListener}の中で, * {@link Command#getTarget()}を監視する必要があるものにイベントを通知する。</p> - * <p>{@link CommandProcessor}は{@link Command}を実行した時に、どこからかコイツを取得して + * <p>{@link CommandProcessorImpl}は{@link Command}を実行した時に、どこからかコイツを取得して * {@link #fireCommandProcess(Command)}を実行する事。</p> * - * FIXME 当然こいつもインターフェースを用意してspecに置きたいが、 - * CommandProcessorのインターフェースが用意されていない問題の影響を受けてしまっている。 - * * @author shin1ogawa */ -public class EventBrokerImpl { +public class EventBrokerImpl implements EventBroker { private List<CommandListener> listeners = new ArrayList<CommandListener>(); /** - * リスナを登録する。 - * - * @param listener + * {@inheritDoc} */ public void addListener(CommandListener listener) { listeners.add(listener); } /** - * 実行されたコマンドをイベントとして通知する。 - * - * @param command + * {@inheritDoc} */ public void fireCommandProcess(Command command) { JiemamyElement target = command.getTarget(); @@ -49,9 +42,7 @@ } /** - * リスなを削除する。 - * - * @param listener + * {@inheritDoc} */ public void removeListener(CommandListener listener) { listeners.remove(listener); @@ -65,7 +56,7 @@ * @param target Commandが操作する対象の{@link JiemamyElement} * @return 通知が必要なら{@code true}、不要なら{@code false} */ - private boolean requireNotification(CommandListener l, JiemamyElement target) { + public boolean requireNotification(CommandListener l, JiemamyElement target) { return l.getTarget() == target || ReferenceResolverImpl.isChild(l.getTarget(), target); } } Deleted: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/Command.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/Command.java 2009-02-08 16:09:44 UTC (rev 2638) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/Command.java 2009-02-09 12:35:04 UTC (rev 2639) @@ -1,62 +0,0 @@ -/* - * Copyright 2007-2009 Jiemamy Project and the Others. - * Created on 2009/01/15 - * - * 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.editcommand; - -import java.util.Stack; - -import org.jiemamy.model.JiemamyElement; - -/** - * モデルを編集するためのコマンドのインターフェース - * - * FIXME specに移動した方が良いと思うが、CommandProcessorのインターフェース化ができていない。 - * - * @author daisuke - * @author shin1ogawa - */ -public interface Command { - - /** - * コマンドを実行する。 - * - * @param processor コマンドプロセッサ - * @return 逆操作を行うコマンド - * @throws Exception - */ - Command execute(CommandProcessor processor) throws Exception; - - /** - * コマンドを実行する。 - * - * <p>逆操作を行うコマンドは、与えられたスタックに追加する。</p> - * - * @param commandProcessor コマンドプロセッサ - * @param commandStack undoの為のコマンドスタック - * @throws Exception - */ - void execute(CommandProcessor commandProcessor, Stack<Command> commandStack) throws Exception; - - /** - * 操作対象の{@link JiemamyElement}を返す。 - * - * @return 操作対象の{@link JiemamyElement} - */ - JiemamyElement getTarget(); - -} Deleted: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/CommandProcessor.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/CommandProcessor.java 2009-02-08 16:09:44 UTC (rev 2638) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/CommandProcessor.java 2009-02-09 12:35:04 UTC (rev 2639) @@ -1,226 +0,0 @@ -/* - * Copyright 2007-2009 Jiemamy Project and the Others. - * Created on 2009/01/15 - * - * 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.editcommand; - -import java.lang.reflect.InvocationTargetException; - -import org.apache.commons.beanutils.BeanUtils; - -import org.jiemamy.EventBrokerImpl; -import org.jiemamy.editcommand.impl.AddColumnCommand; -import org.jiemamy.editcommand.impl.AddColumnToColumnRefListCommand; -import org.jiemamy.editcommand.impl.AddForeignKeyCommand; -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.DeleteForeignKeyCommand; -import org.jiemamy.editcommand.impl.DeletePrimaryKeyCommand; -import org.jiemamy.editcommand.impl.DeleteTableCommand; -import org.jiemamy.editcommand.impl.ModifyModelPropertyCommand; -import org.jiemamy.model.JiemamyElement; -import org.jiemamy.model.RootModel; -import org.jiemamy.model.RootModelImpl; -import org.jiemamy.model.attribute.ColumnModel; -import org.jiemamy.model.attribute.ColumnRef; -import org.jiemamy.model.attribute.ColumnRefImpl; -import org.jiemamy.model.attribute.constraint.ForeignKeyModel; -import org.jiemamy.model.attribute.constraint.PrimaryKeyModel; -import org.jiemamy.model.entity.TableModel; - -/** - * コマンドを実行するクラス。 - * <p>全部のprocessメソッドにて{@link EventBrokerImpl#fireCommandProcess(Command)}を実行する。 - * ここでイベントブローカーへの発火を行うのが本当に正しいかはワカラナイ!</p> - * - * FIXME インターフェース化してspecへ移動した方が良いと思うが、specとしてprcoess()メソッドは公開できない。 - * - * @author daisuke - * @author shin1ogawa - */ -public class CommandProcessor { - - final EventBrokerImpl eventBroker; - - - /** - * インスタンスを生成する。 - * @param eventBroker - */ - public CommandProcessor(EventBrokerImpl eventBroker) { - this.eventBroker = eventBroker; - } - - /** - * {@link ColumnModel}を{@link TableModel}に追加するコマンドを実行する。 - * - * @param newColumnCommand - */ - public void process(AddColumnCommand newColumnCommand) { - ColumnModel column = newColumnCommand.getColumn(); - TableModel table = newColumnCommand.getTable(); - table.getAttributes().add(column); - - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(newColumnCommand); - } - - /** - * {@link ColumnRef}のリストに{@link ColumnModel}を追加するコマンドを実行する。 - * - * @param command - */ - public void process(AddColumnToColumnRefListCommand command) { - int index = command.getIndex(); - if (index != -1) { - command.getColumnRefList().add(index, new ColumnRefImpl(command.getColumn())); - } else { - command.setIndex(command.getColumnRefList().size()); - command.getColumnRefList().add(new ColumnRefImpl(command.getColumn())); - } - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(command); - } - - /** - * {@link TableModel}に{@link ForeignKeyModel}を追加するコマンドを実行する。 - * - * @param newForeignKeyCommand - */ - public void process(AddForeignKeyCommand newForeignKeyCommand) { - ForeignKeyModel fk = newForeignKeyCommand.getFk(); - TableModel table = newForeignKeyCommand.getTable(); - table.getAttributes().add(fk); - - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(newForeignKeyCommand); - } - - /** - * {@link TableModel}に{@link PrimaryKeyModel}を追加するコマンドを実行する。 - * - * @param newPrimaryKeyCommand - */ - public void process(AddPrimaryKeyCommand newPrimaryKeyCommand) { - PrimaryKeyModel pk = newPrimaryKeyCommand.getPrimaryKey(); - TableModel table = newPrimaryKeyCommand.getTable(); - table.getAttributes().add(pk); - - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(newPrimaryKeyCommand); - } - - /** - * {@link RootModel}に{@link TableModel}を追加するコマンドを実行する。 - * - * @param newTableCommand - */ - public void process(AddTableCommand newTableCommand) { - TableModel table = newTableCommand.getTable(); - RootModelImpl.class.cast(newTableCommand.getTarget()).getEntities().add(table); - - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(newTableCommand); - } - - /** - * {@link TableModel}から{@link ColumnModel}を削除するコマンドを実行する。 - * - * @param deleteColumnCommand - */ - public void process(DeleteColumnCommand deleteColumnCommand) { - ColumnModel column = deleteColumnCommand.getColumn(); - TableModel table = deleteColumnCommand.getTable(); - table.getAttributes().remove(column); - - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(deleteColumnCommand); - } - - /** - * {@link ColumnRef}のリストから{@link ColumnModel}を削除するコマンドを実行する。 - * - * @param command - */ - public void process(DeleteColumnFromColumnRefListCommand command) { - int index = command.getIndex(); - command.getColumnRefList().remove(index); - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(command); - } - - /** - * {@link TableModel}から{@link ForeignKeyModel}を削除するコマンドを実行する。 - * - * @param deleteForeignKeyCommand - */ - public void process(DeleteForeignKeyCommand deleteForeignKeyCommand) { - ForeignKeyModel fk = deleteForeignKeyCommand.getFk(); - TableModel table = deleteForeignKeyCommand.getTable(); - table.getAttributes().remove(fk); - - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(deleteForeignKeyCommand); - } - - /** - * {@link TableModel}から{@link PrimaryKeyModel}を削除するコマンドを実行する。 - * - * @param deletePrimaryKeyCommand - */ - public void process(DeletePrimaryKeyCommand deletePrimaryKeyCommand) { - PrimaryKeyModel pk = deletePrimaryKeyCommand.getPrimaryKey(); - TableModel table = deletePrimaryKeyCommand.getTable(); - table.getAttributes().remove(pk); - - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(deletePrimaryKeyCommand); - } - - /** - * {@link RootModel}から{@link TableModel}を削除するコマンドを実行する。 - * - * @param deleteTableCommand - */ - public void process(DeleteTableCommand deleteTableCommand) { - deleteTableCommand.getRoot().getEntities().remove(deleteTableCommand.getTable()); - - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(deleteTableCommand); - } - - /** - * モデルの属性を変更するコマンドを実行します。 - * - * @param command - */ - public void process(ModifyModelPropertyCommand command) { - JiemamyElement target = command.getTarget(); - try { - BeanUtils.setProperty(target, command.getPropertyName(), command.getNewValue()); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } - - // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 - eventBroker.fireCommandProcess(command); - } -} Copied: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/CommandProcessorImpl.java (from rev 2635, artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/CommandProcessor.java) =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/CommandProcessorImpl.java (rev 0) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/CommandProcessorImpl.java 2009-02-09 12:35:04 UTC (rev 2639) @@ -0,0 +1,249 @@ +/* + * Copyright 2007-2009 Jiemamy Project and the Others. + * Created on 2009/01/15 + * + * 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.editcommand; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.apache.commons.beanutils.BeanUtils; + +import org.jiemamy.EventBroker; +import org.jiemamy.EventBrokerImpl; +import org.jiemamy.editcommand.impl.AddColumnCommand; +import org.jiemamy.editcommand.impl.AddColumnToColumnRefListCommand; +import org.jiemamy.editcommand.impl.AddForeignKeyCommand; +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.DeleteForeignKeyCommand; +import org.jiemamy.editcommand.impl.DeletePrimaryKeyCommand; +import org.jiemamy.editcommand.impl.DeleteTableCommand; +import org.jiemamy.editcommand.impl.ModifyModelPropertyCommand; +import org.jiemamy.exception.JiemamyRuntimeException; +import org.jiemamy.model.JiemamyElement; +import org.jiemamy.model.RootModel; +import org.jiemamy.model.RootModelImpl; +import org.jiemamy.model.attribute.ColumnModel; +import org.jiemamy.model.attribute.ColumnRef; +import org.jiemamy.model.attribute.ColumnRefImpl; +import org.jiemamy.model.attribute.constraint.ForeignKeyModel; +import org.jiemamy.model.attribute.constraint.PrimaryKeyModel; +import org.jiemamy.model.entity.TableModel; + +/** + * コマンドを実行するクラス。 + * <p>全部のprocessメソッドにて{@link EventBrokerImpl#fireCommandProcess(Command)}を実行する。 + * ここでイベントブローカーへの発火を行うのが本当に正しいかはワカラナイ!</p> + * + * @author daisuke + * @author shin1ogawa + */ +public class CommandProcessorImpl implements CommandProcessor { + + final EventBroker eventBroker; + + + /** + * インスタンスを生成する。 + * @param eventBroker + */ + public CommandProcessorImpl(EventBroker eventBroker) { + this.eventBroker = eventBroker; + } + + /** + * {@link ColumnModel}を{@link TableModel}に追加するコマンドを実行する。 + * + * @param newColumnCommand + */ + public void process(AddColumnCommand newColumnCommand) { + ColumnModel column = newColumnCommand.getColumn(); + TableModel table = newColumnCommand.getTable(); + table.getAttributes().add(column); + + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(newColumnCommand); + } + + /** + * {@link ColumnRef}のリストに{@link ColumnModel}を追加するコマンドを実行する。 + * + * @param command + */ + public void process(AddColumnToColumnRefListCommand command) { + int index = command.getIndex(); + if (index != -1) { + command.getColumnRefList().add(index, new ColumnRefImpl(command.getColumn())); + } else { + command.setIndex(command.getColumnRefList().size()); + command.getColumnRefList().add(new ColumnRefImpl(command.getColumn())); + } + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(command); + } + + /** + * {@link TableModel}に{@link ForeignKeyModel}を追加するコマンドを実行する。 + * + * @param newForeignKeyCommand + */ + public void process(AddForeignKeyCommand newForeignKeyCommand) { + ForeignKeyModel fk = newForeignKeyCommand.getFk(); + TableModel table = newForeignKeyCommand.getTable(); + table.getAttributes().add(fk); + + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(newForeignKeyCommand); + } + + /** + * {@link TableModel}に{@link PrimaryKeyModel}を追加するコマンドを実行する。 + * + * @param newPrimaryKeyCommand + */ + public void process(AddPrimaryKeyCommand newPrimaryKeyCommand) { + PrimaryKeyModel pk = newPrimaryKeyCommand.getPrimaryKey(); + TableModel table = newPrimaryKeyCommand.getTable(); + table.getAttributes().add(pk); + + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(newPrimaryKeyCommand); + } + + /** + * {@link RootModel}に{@link TableModel}を追加するコマンドを実行する。 + * + * @param newTableCommand + */ + public void process(AddTableCommand newTableCommand) { + TableModel table = newTableCommand.getTable(); + RootModelImpl.class.cast(newTableCommand.getTarget()).getEntities().add(table); + + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(newTableCommand); + } + + /** + * {@inheritDoc} + */ + public void process(Command command) { + // 実行時に無理矢理ディスパッチする…。 + Method method; + try { + method = CommandProcessorImpl.class.getMethod("process", command.getClass()); + method.invoke(this, command); + } catch (SecurityException e) { + throw new JiemamyRuntimeException(e); + } catch (NoSuchMethodException e) { + throw new JiemamyRuntimeException(e); + } catch (IllegalArgumentException e) { + throw new JiemamyRuntimeException(e); + } catch (IllegalAccessException e) { + throw new JiemamyRuntimeException(e); + } catch (InvocationTargetException e) { + throw new JiemamyRuntimeException(e); + } + } + + /** + * {@link TableModel}から{@link ColumnModel}を削除するコマンドを実行する。 + * + * @param deleteColumnCommand + */ + public void process(DeleteColumnCommand deleteColumnCommand) { + ColumnModel column = deleteColumnCommand.getColumn(); + TableModel table = deleteColumnCommand.getTable(); + table.getAttributes().remove(column); + + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(deleteColumnCommand); + } + + /** + * {@link ColumnRef}のリストから{@link ColumnModel}を削除するコマンドを実行する。 + * + * @param command + */ + public void process(DeleteColumnFromColumnRefListCommand command) { + int index = command.getIndex(); + command.getColumnRefList().remove(index); + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(command); + } + + /** + * {@link TableModel}から{@link ForeignKeyModel}を削除するコマンドを実行する。 + * + * @param deleteForeignKeyCommand + */ + public void process(DeleteForeignKeyCommand deleteForeignKeyCommand) { + ForeignKeyModel fk = deleteForeignKeyCommand.getFk(); + TableModel table = deleteForeignKeyCommand.getTable(); + table.getAttributes().remove(fk); + + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(deleteForeignKeyCommand); + } + + /** + * {@link TableModel}から{@link PrimaryKeyModel}を削除するコマンドを実行する。 + * + * @param deletePrimaryKeyCommand + */ + public void process(DeletePrimaryKeyCommand deletePrimaryKeyCommand) { + PrimaryKeyModel pk = deletePrimaryKeyCommand.getPrimaryKey(); + TableModel table = deletePrimaryKeyCommand.getTable(); + table.getAttributes().remove(pk); + + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(deletePrimaryKeyCommand); + } + + /** + * {@link RootModel}から{@link TableModel}を削除するコマンドを実行する。 + * + * @param deleteTableCommand + */ + public void process(DeleteTableCommand deleteTableCommand) { + deleteTableCommand.getRoot().getEntities().remove(deleteTableCommand.getTable()); + + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(deleteTableCommand); + } + + /** + * モデルの属性を変更するコマンドを実行します。 + * + * @param command + */ + public void process(ModifyModelPropertyCommand command) { + JiemamyElement target = command.getTarget(); + try { + BeanUtils.setProperty(target, command.getPropertyName(), command.getNewValue()); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + + // 面倒だけど、全部のprocessメソッドに入れる(Interceptorで手を抜きたい)。 + eventBroker.fireCommandProcess(command); + } +} Property changes on: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/editcommand/CommandProcessorImpl.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/EventBrokerTest.java =================================================================== --- artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/EventBrokerTest.java (rev 0) +++ artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/EventBrokerTest.java 2009-02-09 12:35:04 UTC (rev 2639) @@ -0,0 +1,323 @@ +/* + * Copyright 2007-2009 Jiemamy Project and the Others. + * Created on 2009/01/18 + * + * 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.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +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.CommandListener; +import org.jiemamy.editcommand.CommandProcessorImpl; +import org.jiemamy.editcommand.impl.AddColumnCommand; +import org.jiemamy.editcommand.impl.AddColumnToColumnRefListCommand; +import org.jiemamy.editcommand.impl.AddTableCommand; +import org.jiemamy.editcommand.impl.DeleteColumnCommand; +import org.jiemamy.editcommand.impl.DeleteColumnFromColumnRefListCommand; +import org.jiemamy.editcommand.impl.ModifyModelPropertyCommand; +import org.jiemamy.model.JiemamyElement; +import org.jiemamy.model.RootModel; +import org.jiemamy.model.attribute.ColumnModel; +import org.jiemamy.model.attribute.constraint.ConstraintModel; +import org.jiemamy.model.attribute.constraint.PrimaryKeyModel; +import org.jiemamy.model.entity.TableModel; + +/** + * {@link EventBrokerImpl}の試験クラス。 + * + * @author shin1ogawa + */ +public class EventBrokerTest { + + static final Logger LOGGER = LoggerFactory.getLogger(EventBrokerTest.class); + + private JiemamyFactory factory; + + private Jiemamy jiemamy; + + private EventBroker eventBroker; + + private CommandProcessorImpl commandProcessor; + + private Stack<Command> commandStack; + + 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); + commandStack = new Stack<Command>(); + } + + /** + * Tableが保持している{@link ConstraintModel}が参照する{@link ColumnModel}に対する変更も通知される。 + * @throws Exception + */ + @Test + public void testReferenceで保持した要素からのバブリング() throws Exception { + TableModel table = factory.newModel(TableModel.class); + 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); + table.getAttributes().add(primaryKey); + + // ここまでで以下の構造を構築した事になる。 + // table + // +attributes + // -column1 + // -column2 + // +primaryKey + // -[ref]column1 + // -[ref]column2 + + DefaultCommandListener tableListener = new DefaultCommandListener(table); + eventBroker.addListener(tableListener); + DefaultCommandListener columnListener1 = new DefaultCommandListener(column1); + eventBroker.addListener(columnListener1); + DefaultCommandListener columnListener2 = new DefaultCommandListener(column2); + eventBroker.addListener(columnListener2); + DefaultCommandListener pkeyListener = new DefaultCommandListener(primaryKey); + eventBroker.addListener(pkeyListener); + + assertThat(columnListener1.commandList.size(), is(0)); + assertThat(columnListener2.commandList.size(), is(0)); + assertThat(pkeyListener.commandList.size(), is(0)); + assertThat(tableListener.commandList.size(), is(0)); + 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())); + + new ModifyModelPropertyCommand(column1, "name", "column1").execute(commandProcessor, commandStack); + + assertThat(columnListener1.commandList.size(), is(1)); // 変更があったcolumn自身。 + assertThat(columnListener2.commandList.size(), is(0)); + assertThat(pkeyListener.commandList.size(), is(1)); // 変更があったcolumnのreferenceを保持しているから通知を受ける。 + assertThat(tableListener.commandList.size(), is(1)); // 変更があったcolumnを保持しているから通知を受ける。 + 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())); + + columnListener1.commandList.clear(); + columnListener2.commandList.clear(); + pkeyListener.commandList.clear(); + tableListener.commandList.clear(); + // columnをprimaryKeyから削除する。 + new DeleteColumnFromColumnRefListCommand(primaryKey, primaryKey.getKeyColumns(), column1).execute( + commandProcessor, commandStack); + + assertThat(columnListener1.commandList.size(), is(0)); // column自身は何も変更されていない。 + assertThat(columnListener2.commandList.size(), is(0)); + assertThat(pkeyListener.commandList.size(), is(1)); // 変更があったcolumnのreferenceを保持しているから通知を受ける。 + assertThat(tableListener.commandList.size(), is(1)); // 変更があったcolumnのreferenceを保持しているprumaruKeyを保持しているから通知を受ける。 + assertThat(table.getAttributes().size(), is(3)); // column1,column2,pkey + assertThat(primaryKey.getKeyColumns().size(), is(1)); // [ref]column2。 + + columnListener1.commandList.clear(); + columnListener2.commandList.clear(); + pkeyListener.commandList.clear(); + tableListener.commandList.clear(); + // columnをtableから削除する。 + new DeleteColumnCommand(table, column1).execute(commandProcessor); + + assertThat(columnListener1.commandList.size(), is(0)); // column自身は何も変更されていない。 + assertThat(columnListener2.commandList.size(), is(0)); + assertThat(pkeyListener.commandList.size(), is(0)); + assertThat(tableListener.commandList.size(), is(1)); + assertThat(table.getAttributes().size(), is(2)); // column2,pkey + assertThat(primaryKey.getKeyColumns().size(), is(1)); // [ref]column2。 + } + + /** + * バブリングの動作確認。 + * + * @throws Exception + */ + @Test + public void testバブリング() throws Exception { + TableModel table = factory.newModel(TableModel.class); + new AddTableCommand(rootModel, table).execute(commandProcessor, commandStack); + ColumnModel column = factory.newModel(ColumnModel.class); + new AddColumnCommand(table, column).execute(commandProcessor, commandStack); + + // ここまでで以下の構造を構築した事になる。 + // table + // +attributes + // -column + assertThat(table.getAttributes().size(), is(1)); + assertThat(rootModel.getEntities().size(), is(1)); + + DefaultCommandListener rootListener = new DefaultCommandListener(rootModel); + eventBroker.addListener(rootListener); + DefaultCommandListener tableListener = new DefaultCommandListener(table); + eventBroker.addListener(tableListener); + DefaultCommandListener columnListener = new DefaultCommandListener(column); + eventBroker.addListener(columnListener); + + new ModifyModelPropertyCommand(table, "name", "T_EMP"). // tableを変更する。 + execute(commandProcessor, commandStack); // tableListener, rootListenerへ通知されるはず。 + new ModifyModelPropertyCommand(column, "name", "ID") // columnを変更する。 + .execute(commandProcessor, commandStack); // columnListener,tableListener,rootListenerへ通知される + new AddColumnCommand(table, factory.newModel(ColumnModel.class)) // 新たに作成したcolumnを追加する。 + .execute(commandProcessor, commandStack); // tableListener, rootListenerへ通知されるはず。 + new AddTableCommand(rootModel, factory.newModel(TableModel.class)) // 新たに作成したtableを追加する。 + .execute(commandProcessor, commandStack); // rootListenerへ通知されるはず。 + + assertThat(table.getAttributes().size(), is(2)); + assertThat(rootModel.getEntities().size(), is(2)); + + assertThat(columnListener.commandList.size(), is(1)); + assertThat(columnListener.commandList.get(0), instanceOf(ModifyModelPropertyCommand.class)); + assertThat(tableListener.commandList.size(), is(3)); + assertThat(tableListener.commandList.get(0), instanceOf(ModifyModelPropertyCommand.class)); + assertThat(tableListener.commandList.get(1), instanceOf(ModifyModelPropertyCommand.class)); + assertThat(tableListener.commandList.get(2), instanceOf(AddColumnCommand.class)); + assertThat(rootListener.commandList.size(), is(4)); + assertThat(rootListener.commandList.get(0), instanceOf(ModifyModelPropertyCommand.class)); + assertThat(rootListener.commandList.get(1), instanceOf(ModifyModelPropertyCommand.class)); + assertThat(rootListener.commandList.get(2), instanceOf(AddColumnCommand.class)); + assertThat(rootListener.commandList.get(3), instanceOf(AddTableCommand.class)); + + columnListener.commandList.clear(); + tableListener.commandList.clear(); + rootListener.commandList.clear(); + + // RootModelに追加しないColumnModelを作ったり、属性を変更したり。 + ColumnModel column2 = factory.newModel(ColumnModel.class); + new ModifyModelPropertyCommand(column2, "name", "NAME").execute(commandProcessor, commandStack); + // RootModelに追加しないColumnModelを作ったり、属性を変更したり。 + TableModel table2 = factory.newModel(TableModel.class); + new ModifyModelPropertyCommand(table2, "name", "Y_DEPT").execute(commandProcessor, commandStack); + // RootModelに追加していないTableModelにColumnModelを追加してみたり。 + new AddColumnCommand(table2, column2).execute(commandProcessor, commandStack); + + assertThat(columnListener.commandList.size(), is(0)); + assertThat(tableListener.commandList.size(), is(0)); + assertThat(rootListener.commandList.size(), is(0)); + } + + /** + * シンプルなイベント通知の動作確認。 + * + * @throws Exception + */ + @Test + public void testバブリングとか無しでの通知() throws Exception { + TableModel emp = factory.newModel(TableModel.class); + TableModel dept = factory.newModel(TableModel.class); + new ModifyModelPropertyCommand(emp, "name", "T_EMP").execute(commandProcessor, commandStack); + new ModifyModelPropertyCommand(dept, "name", "T_DEPT").execute(commandProcessor, commandStack); + new AddTableCommand(rootModel, emp).execute(commandProcessor, commandStack); + new AddTableCommand(rootModel, dept).execute(commandProcessor, commandStack); + + // Listenerを作成、追加する。 + DefaultCommandListener empListener = new DefaultCommandListener(emp); + eventBroker.addListener(empListener); + DefaultCommandListener deptListener = new DefaultCommandListener(dept); + eventBroker.addListener(deptListener); + + // EMPに対して変更Commandを適用する。 + new ModifyModelPropertyCommand(emp, "name", "newEmp").execute(commandProcessor, commandStack); + assertThat(empListener.commandList.size(), is(1)); + assertThat(deptListener.commandList.size(), is(0)); + assertThat(emp.getName(), is("newEmp")); + // Undoしてみる。 + Command undoCommand1 = commandStack.pop(); + undoCommand1.execute(commandProcessor, commandStack); + assertThat(empListener.commandList.size(), is(2)); + assertThat(deptListener.commandList.size(), is(0)); + assertThat(emp.getName(), is("T_EMP")); + + // DEPTに対して変更Commandを適用する。 + empListener.commandList.clear(); + deptListener.commandList.clear(); + new ModifyModelPropertyCommand(dept, "name", "newDept").execute(commandProcessor, commandStack); + assertThat(empListener.commandList.size(), is(0)); + assertThat(deptListener.commandList.size(), is(1)); + assertThat(dept.getName(), is("newDept")); + // Undoしてみる。 + Command undoCommand2 = commandStack.pop(); + undoCommand2.execute(commandProcessor, commandStack); + assertThat(empListener.commandList.size(), is(0)); + assertThat(deptListener.commandList.size(), is(2)); + assertThat(dept.getName(), is("T_DEPT")); + } + + + /** + * TODO for shin1ogawa + * + * @author shin1ogawa + */ + public static class DefaultCommandListener implements CommandListener { + + JiemamyElement target; + + List<Command> commandList = new ArrayList<Command>(); + + + /** + * インスタンスを生成する。 + * + * @param target + */ + public DefaultCommandListener(JiemamyElement target) { + this.target = target; + } + + /** + * {@inheritDoc} + */ + public void commandProcess(Command command) { + commandList.add(command); + } + + /** + * {@inheritDoc} + */ + public JiemamyElement getTarget() { + return target; + } + } +} Property changes on: artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/EventBrokerTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain