[Jiemamy-notify:1097] commit [2420] commandのサンプル書いてみた。

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2009年 1月 15日 (木) 01:19:45 JST


Revision: 2420
          http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=2420
Author:   daisuke_m
Date:     2009-01-15 01:19:44 +0900 (Thu, 15 Jan 2009)

Log Message:
-----------
commandのサンプル書いてみた。

Modified Paths:
--------------
    sandbox/command-sample/pom.xml
    sandbox/command-sample/src/main/java/org/jiemamy/test/model/Column.java
    sandbox/command-sample/src/main/java/org/jiemamy/test/model/JiemamyModel.java
    sandbox/command-sample/src/main/java/org/jiemamy/test/model/Table.java

Added Paths:
-----------
    sandbox/command-sample/src/main/java/org/jiemamy/test/command/
    sandbox/command-sample/src/main/java/org/jiemamy/test/command/Command.java
    sandbox/command-sample/src/main/java/org/jiemamy/test/command/CommandProcessor.java
    sandbox/command-sample/src/main/java/org/jiemamy/test/command/Locator.java
    sandbox/command-sample/src/main/java/org/jiemamy/test/command/SetPropertyCommand.java
    sandbox/command-sample/src/main/java/org/jiemamy/test/model/Model.java
    sandbox/command-sample/src/test/java/org/jiemamy/test/CommandTest.java

Removed Paths:
-------------
    sandbox/command-sample/src/main/java/org/jiemamy/test/App.java
    sandbox/command-sample/src/test/java/org/jiemamy/test/AppTest.java


-------------- next part --------------
Modified: sandbox/command-sample/pom.xml
===================================================================
--- sandbox/command-sample/pom.xml	2009-01-14 15:40:35 UTC (rev 2419)
+++ sandbox/command-sample/pom.xml	2009-01-14 16:19:44 UTC (rev 2420)
@@ -11,7 +11,7 @@
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
-      <version>3.8.1</version>
+      <version>4.5</version>
       <scope>test</scope>
     </dependency>
   </dependencies>

Deleted: sandbox/command-sample/src/main/java/org/jiemamy/test/App.java
===================================================================
--- sandbox/command-sample/src/main/java/org/jiemamy/test/App.java	2009-01-14 15:40:35 UTC (rev 2419)
+++ sandbox/command-sample/src/main/java/org/jiemamy/test/App.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -1,12 +0,0 @@
-package org.jiemamy.test;
-
-/**
- * Hello world!
- *
- */
-public class App {
-	
-	public static void main(String[] args) {
-		System.out.println("Hello World!");
-	}
-}

Added: sandbox/command-sample/src/main/java/org/jiemamy/test/command/Command.java
===================================================================
--- sandbox/command-sample/src/main/java/org/jiemamy/test/command/Command.java	                        (rev 0)
+++ sandbox/command-sample/src/main/java/org/jiemamy/test/command/Command.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -0,0 +1,30 @@
+/*
+ * 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.test.command;
+
+/**
+ * TODO for daisuke
+ * 
+ * @author daisuke
+ */
+public interface Command {
+	
+	Command execute(CommandProcessor processor) throws Exception;
+	
+}


Property changes on: sandbox/command-sample/src/main/java/org/jiemamy/test/command/Command.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: sandbox/command-sample/src/main/java/org/jiemamy/test/command/CommandProcessor.java
===================================================================
--- sandbox/command-sample/src/main/java/org/jiemamy/test/command/CommandProcessor.java	                        (rev 0)
+++ sandbox/command-sample/src/main/java/org/jiemamy/test/command/CommandProcessor.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -0,0 +1,53 @@
+/*
+ * 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.test.command;
+
+import java.lang.reflect.Method;
+
+import org.jiemamy.test.model.JiemamyModel;
+
+/**
+ * TODO for daisuke
+ * 
+ * @author daisuke
+ */
+public class CommandProcessor {
+	
+	JiemamyModel model;
+	
+
+	public CommandProcessor(JiemamyModel model) {
+		this.model = model;
+	}
+	
+	public Command process(SetPropertyCommand setPropertyCommand) throws Exception {
+		String propName = setPropertyCommand.propertyName;
+		Object afterContents = setPropertyCommand.contents;
+		
+		Object target = setPropertyCommand.locator.getTarget(model);
+		Class<?> targetClass = target.getClass();
+		
+		Method getter = targetClass.getMethod("get" + propName);
+		Method setter = targetClass.getMethod("set" + propName, afterContents.getClass());
+		Object beforeContents = getter.invoke(target);
+		setter.invoke(target, afterContents);
+		return new SetPropertyCommand(setPropertyCommand.locator, propName, beforeContents);
+	}
+	
+}


Property changes on: sandbox/command-sample/src/main/java/org/jiemamy/test/command/CommandProcessor.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: sandbox/command-sample/src/main/java/org/jiemamy/test/command/Locator.java
===================================================================
--- sandbox/command-sample/src/main/java/org/jiemamy/test/command/Locator.java	                        (rev 0)
+++ sandbox/command-sample/src/main/java/org/jiemamy/test/command/Locator.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -0,0 +1,55 @@
+/*
+ * 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.test.command;
+
+import java.lang.reflect.Method;
+
+import org.jiemamy.test.model.JiemamyModel;
+
+/**
+ * TODO for daisuke
+ * 
+ * @author daisuke
+ */
+public class Locator {
+	
+	private String path;
+	
+
+	public Locator(String path) {
+		this.path = path;
+	}
+	
+	public Object getTarget(JiemamyModel model) throws Exception {
+		String[] elements = path.split("/");
+		Object currentModel = model;
+		for (String element : elements) {
+			if (element.length() == 1) {
+				Method getter = currentModel.getClass().getMethod("get", int.class);
+				currentModel = getter.invoke(currentModel, Integer.valueOf(element));
+			} else {
+				Method getter = currentModel.getClass().getMethod("get" + element);
+				currentModel = getter.invoke(currentModel);
+			}
+		}
+		
+		return currentModel;
+	}
+	
+}


Property changes on: sandbox/command-sample/src/main/java/org/jiemamy/test/command/Locator.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: sandbox/command-sample/src/main/java/org/jiemamy/test/command/SetPropertyCommand.java
===================================================================
--- sandbox/command-sample/src/main/java/org/jiemamy/test/command/SetPropertyCommand.java	                        (rev 0)
+++ sandbox/command-sample/src/main/java/org/jiemamy/test/command/SetPropertyCommand.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -0,0 +1,49 @@
+/*
+ * 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.test.command;
+
+/**
+ * TODO for daisuke
+ * 
+ * @author daisuke
+ */
+public class SetPropertyCommand implements Command {
+	
+	Locator locator;
+	
+	String propertyName;
+	
+	Object contents;
+	
+
+	public SetPropertyCommand(Locator locator, String propertyName, Object contents) {
+		this.locator = locator;
+		this.propertyName = propertyName;
+		this.contents = contents;
+	}
+	
+	/**
+	 * {@inheritDoc}
+	 * @throws Exception 
+	 */
+	public Command execute(CommandProcessor processor) throws Exception {
+		return processor.process(this);
+	}
+	
+}


Property changes on: sandbox/command-sample/src/main/java/org/jiemamy/test/command/SetPropertyCommand.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: sandbox/command-sample/src/main/java/org/jiemamy/test/model/Column.java
===================================================================
--- sandbox/command-sample/src/main/java/org/jiemamy/test/model/Column.java	2009-01-14 15:40:35 UTC (rev 2419)
+++ sandbox/command-sample/src/main/java/org/jiemamy/test/model/Column.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -4,7 +4,7 @@
  * TODO describe
  * @author daisuke
  */
-public class Column {
+public class Column implements Model {
 	
 	private String name;
 	

Modified: sandbox/command-sample/src/main/java/org/jiemamy/test/model/JiemamyModel.java
===================================================================
--- sandbox/command-sample/src/main/java/org/jiemamy/test/model/JiemamyModel.java	2009-01-14 15:40:35 UTC (rev 2419)
+++ sandbox/command-sample/src/main/java/org/jiemamy/test/model/JiemamyModel.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -7,7 +7,7 @@
  * TODO describe
  * @author daisuke
  */
-public class JiemamyModel {
+public class JiemamyModel implements Model {
 	
 	private List<Table> tables = new ArrayList<Table>();
 	

Added: sandbox/command-sample/src/main/java/org/jiemamy/test/model/Model.java
===================================================================
--- sandbox/command-sample/src/main/java/org/jiemamy/test/model/Model.java	                        (rev 0)
+++ sandbox/command-sample/src/main/java/org/jiemamy/test/model/Model.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -0,0 +1,29 @@
+/*
+ * 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.test.model;
+
+
+/**
+ * TODO for daisuke
+ * 
+ * @author daisuke
+ */
+public interface Model {
+	
+}


Property changes on: sandbox/command-sample/src/main/java/org/jiemamy/test/model/Model.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: sandbox/command-sample/src/main/java/org/jiemamy/test/model/Table.java
===================================================================
--- sandbox/command-sample/src/main/java/org/jiemamy/test/model/Table.java	2009-01-14 15:40:35 UTC (rev 2419)
+++ sandbox/command-sample/src/main/java/org/jiemamy/test/model/Table.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -7,7 +7,7 @@
  * TODO describe
  * @author daisuke
  */
-public class Table {
+public class Table implements Model {
 	
 	private String name;
 	

Deleted: sandbox/command-sample/src/test/java/org/jiemamy/test/AppTest.java
===================================================================
--- sandbox/command-sample/src/test/java/org/jiemamy/test/AppTest.java	2009-01-14 15:40:35 UTC (rev 2419)
+++ sandbox/command-sample/src/test/java/org/jiemamy/test/AppTest.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -1,34 +0,0 @@
-package org.jiemamy.test;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-/**
- * Unit test for simple App.
- */
-public class AppTest extends TestCase {
-	
-	/**
-	 * Create the test case
-	 *
-	 * @param testName name of the test case
-	 */
-	public AppTest(String testName) {
-		super(testName);
-	}
-	
-	/**
-	 * @return the suite of tests being tested
-	 */
-	public static Test suite() {
-		return new TestSuite(AppTest.class);
-	}
-	
-	/**
-	 * Rigourous Test :-)
-	 */
-	public void testApp() {
-		assertTrue(true);
-	}
-}

Added: sandbox/command-sample/src/test/java/org/jiemamy/test/CommandTest.java
===================================================================
--- sandbox/command-sample/src/test/java/org/jiemamy/test/CommandTest.java	                        (rev 0)
+++ sandbox/command-sample/src/test/java/org/jiemamy/test/CommandTest.java	2009-01-14 16:19:44 UTC (rev 2420)
@@ -0,0 +1,68 @@
+/*
+ * 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.test;
+
+import junit.framework.TestCase;
+
+import org.junit.Test;
+
+import org.jiemamy.test.command.Command;
+import org.jiemamy.test.command.CommandProcessor;
+import org.jiemamy.test.command.Locator;
+import org.jiemamy.test.command.SetPropertyCommand;
+import org.jiemamy.test.model.Column;
+import org.jiemamy.test.model.JiemamyModel;
+import org.jiemamy.test.model.Table;
+
+/**
+ * TODO for daisuke
+ * 
+ * @author daisuke
+ */
+public class CommandTest extends TestCase {
+	
+	@Test
+	public void testname() throws Exception {
+		JiemamyModel jiemamyModel = new JiemamyModel();
+		jiemamyModel.getTables().add(new Table());
+		jiemamyModel.getTables().add(new Table());
+		jiemamyModel.getTables().add(new Table());
+		jiemamyModel.getTables().add(new Table());
+		jiemamyModel.getTables().add(new Table());
+		
+		jiemamyModel.getTables().get(3).getColumns().add(new Column());
+		jiemamyModel.getTables().get(3).getColumns().add(new Column());
+		jiemamyModel.getTables().get(3).getColumns().add(new Column());
+		jiemamyModel.getTables().get(3).getColumns().add(new Column());
+		jiemamyModel.getTables().get(3).getColumns().add(new Column());
+		
+		jiemamyModel.getTables().get(3).getColumns().get(2).setName("foo");
+		
+		System.out.println("before: " + jiemamyModel.getTables().get(3).getColumns().get(2).getName());
+		Command execCommand = new SetPropertyCommand(new Locator("Tables/3/Columns/2"), "Name", "hoge");
+		Command undoCommand = execCommand.execute(new CommandProcessor(jiemamyModel));
+		
+		System.out.println("exec: " + jiemamyModel.getTables().get(3).getColumns().get(2).getName());
+		
+		undoCommand.execute(new CommandProcessor(jiemamyModel));
+		
+		System.out.println("undo: " + jiemamyModel.getTables().get(3).getColumns().get(2).getName());
+		
+	}
+}


Property changes on: sandbox/command-sample/src/test/java/org/jiemamy/test/CommandTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain


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