[Jiemamy-notify] commit [2049] ModelInputStreamをイジった。まだ動いてないw

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2008年 10月 26日 (日) 22:15:41 JST


Revision: 2049
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=jiemamy&view=rev&rev=2049
Author:   daisuke_m
Date:     2008-10-26 22:15:41 +0900 (Sun, 26 Oct 2008)

Log Message:
-----------
ModelInputStreamをイジった。まだ動いてないw

Modified Paths:
--------------
    artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/JiemamySerializerFactory.java
    artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelInputStream.java
    artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelWriter.java
    artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/SerializationTest.java

Added Paths:
-----------
    artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/XmlElement.java
    artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/impl2/JiemamySerializerImpl2.java


-------------- next part --------------
Modified: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/JiemamySerializerFactory.java
===================================================================
--- artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/JiemamySerializerFactory.java	2008-10-26 13:07:46 UTC (rev 2048)
+++ artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/JiemamySerializerFactory.java	2008-10-26 13:15:41 UTC (rev 2049)
@@ -18,7 +18,7 @@
  */
 package org.jiemamy.serializer;
 
-import org.jiemamy.serializer.impl.JiemamySerializerStab;
+import org.jiemamy.serializer.impl2.JiemamySerializerImpl2;
 
 /**
  * JiemamySerializerのインスタンスを生成するためのファクトリ。
@@ -26,7 +26,7 @@
  */
 public class JiemamySerializerFactory {
 	
-	private static JiemamySerializer singleton;
+	private volatile static JiemamySerializer singleton;
 	
 
 	/**
@@ -35,7 +35,7 @@
 	 */
 	public static JiemamySerializer getInstance() {
 		if (singleton == null) {
-			singleton = (new JiemamySerializerStab());
+			singleton = (new JiemamySerializerImpl2());
 		}
 		return singleton;
 	}

Modified: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelInputStream.java
===================================================================
--- artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelInputStream.java	2008-10-26 13:07:46 UTC (rev 2048)
+++ artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelInputStream.java	2008-10-26 13:15:41 UTC (rev 2049)
@@ -20,12 +20,9 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.List;
 import java.util.Queue;
-import java.util.Stack;
 import java.util.concurrent.LinkedBlockingQueue;
 
-import org.jiemamy.spec.model.JiemamyModel;
 import org.jiemamy.spec.model.RootModel;
 
 /**
@@ -35,17 +32,11 @@
  */
 public class ModelInputStream extends InputStream {
 	
-	/** 初期化済みかどうか */
-	private boolean initialized;
-	
 	/** 出力モデルキュー */
-	private Queue<Byte> resourceQueue = new LinkedBlockingQueue<Byte>();
+	private Queue<Byte> resourceQueue;
 	
-	/** 処理中のモデル構造スタック */
-	private Stack<JiemamyModel> nextWriteModelStack = new Stack<JiemamyModel>();
+	private ModelWriter modelWriter;
 	
-	private ModelWriter modelWriter = new ModelWriter(resourceQueue);
-	
 
 	/**
 	 * コンストラクタ。
@@ -53,7 +44,8 @@
 	 * @category  instance creation
 	 */
 	public ModelInputStream(RootModel rootModel) {
-		nextWriteModelStack.push(rootModel);
+		resourceQueue = new LinkedBlockingQueue<Byte>();
+		modelWriter = new ModelWriter(rootModel, resourceQueue);
 	}
 	
 	/**
@@ -70,31 +62,12 @@
 	 */
 	@Override
 	public int read() throws IOException {
-		if (initialized == false) {
-			modelWriter.init();
-			initialized = true;
-		}
 		if (resourceQueue.size() == 0) {
-			if (loadFromModel() == false) {
+			if (modelWriter.write() == false) {
 				return -1;
 			}
 		}
 		return resourceQueue.poll().intValue();
 	}
 	
-	/**
-	 * 要求された1モデルだけを読み込み,Byteのキューに追加していく。
-	 * @return  読み込むデータがない場合false, ある場合true 
-	 * @throws IllegalArgumentException  
-	 */
-	private boolean loadFromModel() {
-		if (nextWriteModelStack.empty()) {
-			return false;
-		}
-		List<JiemamyModel> nextObjects = modelWriter.write(nextWriteModelStack.pop());
-		for (JiemamyModel next : nextObjects) {
-			nextWriteModelStack.push(next);
-		}
-		return resourceQueue.size() > 0 || nextWriteModelStack.empty() == false;
-	}
 }

Modified: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelWriter.java
===================================================================
--- artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelWriter.java	2008-10-26 13:07:46 UTC (rev 2048)
+++ artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/ModelWriter.java	2008-10-26 13:15:41 UTC (rev 2049)
@@ -19,22 +19,19 @@
 package org.jiemamy.serializer;
 
 import java.io.StringWriter;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Queue;
-import java.util.UUID;
+import java.util.Stack;
+import java.util.concurrent.LinkedBlockingQueue;
 
 import com.megginson.sax.DataWriter;
 
 import org.xml.sax.SAXException;
 
 import org.jiemamy.spec.model.JiemamyModel;
+import org.jiemamy.spec.model.RootModel;
 
 /**
  * モデルライター。
@@ -43,23 +40,34 @@
  */
 public class ModelWriter {
 	
-	private Queue<Byte> resourceQueues;
+	/** 処理中のモデル構造スタック */
+	private Stack<Queue<XmlElement>> nextWriteModelStack = new Stack<Queue<XmlElement>>();
 	
-	private DataWriter dataWriter;
+	private Queue<XmlElement> currentQueue;
 	
-	private StringWriter stringWriter;
+	private DataWriter dataWriter = new DataWriter();
 	
-	private Map<UUID, JiemamyModel> nextWriteModelMaps = new HashMap<UUID, JiemamyModel>();
+	private Queue<Byte> resourceQueue;
 	
 
 	/**
 	 * コンストラクタ。
-	 * @param resourceQueues
+	 * @param rootModel 
+	 * @param resourceQueue
 	 * @category  instance creation
 	 */
-	public ModelWriter(Queue<Byte> resourceQueues) {
-		this.resourceQueues = resourceQueues;
-		createWriters();
+	public ModelWriter(RootModel rootModel, Queue<Byte> resourceQueue) {
+		this.resourceQueue = resourceQueue;
+		dataWriter.setIndentStep(2);
+		try {
+			dataWriter.startDocument();
+		} catch (SAXException e) {
+			e.printStackTrace();
+		}
+		
+		Queue<XmlElement> rootElement = new LinkedBlockingQueue<XmlElement>();
+		rootElement.add(new XmlElement("rootModel", rootModel));
+		nextWriteModelStack.push(rootElement);
 	}
 	
 	/**
@@ -74,97 +82,92 @@
 	}
 	
 	/**
-	 * 初期化処理を行う。
+	 * 1行分をresourceQueueに出力する。
+	 * 
+	 * <p>1行というのは、
+	 * <ul>
+	 *   <li>JiemamyModelだったりCollection、Mapタグのオープン</li>
+	 *   <li>それ以外のdataElementの出力</li>
+	 *   <li>JiemamyModelだったりCollection、Mapタグのクローズ</li>
+	 * </ul>
+	 * と定義。</p>
+	 * 
+	 * @return 出力する対象がもう残っていない場合は <code>false</code>
 	 */
-	public void init() {
-		try {
-			dataWriter.startDocument();
-		} catch (SAXException e) {
-			e.printStackTrace();
+	public boolean write() {
+		StringWriter sw = new StringWriter();
+		dataWriter.setOutput(sw); // 毎回出力先を更新する
+		
+		XmlElement currentElement = null;
+		
+		// 次に出力すべきエレメントの特定
+		do {
+			if (currentQueue == null) {
+				if (nextWriteModelStack.empty()) {
+					return false;
+				}
+				currentQueue = nextWriteModelStack.peek();
+			}
+			
+			if (currentQueue.isEmpty()) {
+				nextWriteModelStack.pop();
+				currentQueue = null;
+			} else {
+				currentElement = currentQueue.poll();
+			}
+		} while (currentElement == null);
+		
+		// モデルの出力
+		if (currentElement.content instanceof JiemamyModel) {
+			writeJiemamyModel(currentElement);
+		} else if (currentElement.content instanceof Collection) {
+			Collection<?> collection = (Collection<?>) currentElement.content;
+			Iterator<?> iterator = collection.iterator();
+			Queue<XmlElement> queue = new LinkedBlockingQueue<XmlElement>();
+			while (iterator.hasNext()) {
+				queue.add(new XmlElement("TODO", iterator.next()));
+			}
+			nextWriteModelStack.push(queue);
+		} else if (currentElement.content instanceof Map) {
+			Map map = (Map) currentElement.content;
+			// TODO Mapの時の処理
+		} else {
+			write(currentElement);
 		}
+		
+		// 前回 #write以降の出力をキューに突っ込む
+		for (byte b : sw.toString().getBytes()) {
+			resourceQueue.add(b);
+		}
+		
+		return resourceQueue.size() > 0 || nextWriteModelStack.empty() == false;
+		
 	}
 	
 	/**
-	 * TODO ここはリフレクションではなく、直接クラスを指定して書き込むようにすること
-	 * @param jiemamyModel
-	 * @return  書き込まなかったモデルのリスト
+	 * TODO for daisuke
+	 * @param element
 	 */
-	public List<JiemamyModel> write(JiemamyModel jiemamyModel) {
-		List<JiemamyModel> nextWriteModelList = new ArrayList<JiemamyModel>();
-		Class<? extends Object> clazz = jiemamyModel.getClass();
-		String className = clazz.getName();
+	private void write(XmlElement element) {
 		try {
-			dataWriter.startElement(className);
-			dataWriter.dataElement("id", jiemamyModel.getId().toString());
-			while (clazz != Object.class) {
-				Method[] methods = clazz.getMethods();
-				for (Method method : methods) {
-					if ((method.getParameterTypes().length > 0) || (method.getName().startsWith("get") == false)
-							|| (method.getModifiers() == Modifier.STATIC)) {
-						continue;
-					}
-					Object value = method.invoke(jiemamyModel, new Object[0]);
-					if (value != null) {
-						if (value instanceof String) {
-							dataWriter.dataElement(method.getName(), (String) value);
-						} else if (value instanceof Number) {
-							dataWriter.dataElement(method.getName(), ((Number) value).toString());
-						} else if (value instanceof JiemamyModel) {
-							String refClassName = ((JiemamyModel) value).getClass().getName();
-							dataWriter.startElement(method.getName());
-							dataWriter.startElement(refClassName);
-							dataWriter.dataElement("refid", ((JiemamyModel) value).getId().toString());
-							dataWriter.endElement(refClassName);
-							dataWriter.endElement(method.getName());
-							if (nextWriteModelMaps.containsKey(((JiemamyModel) value).getId()) == false) {
-								nextWriteModelMaps.put(((JiemamyModel) value).getId(), ((JiemamyModel) value));
-								nextWriteModelList.add(((JiemamyModel) value));
-							}
-						} else if (value instanceof Collection) {
-							if (((Collection<?>) value).size() > 0) {
-								dataWriter.startElement(method.getName());
-								for (Object e : ((Collection<?>) value)) {
-									if (e instanceof JiemamyModel) {
-										String refClassName = ((JiemamyModel) e).getClass().getName();
-										dataWriter.startElement(refClassName);
-										dataWriter.dataElement("refid", ((JiemamyModel) e).getId().toString());
-										dataWriter.endElement(refClassName);
-										if (nextWriteModelMaps.containsKey(((JiemamyModel) e).getId()) == false) {
-											nextWriteModelMaps.put(((JiemamyModel) e).getId(), ((JiemamyModel) e));
-											nextWriteModelList.add(((JiemamyModel) e));
-										}
-									}
-								}
-								dataWriter.endElement(method.getName());
-							} else {
-								dataWriter.emptyElement(method.getName());
-							}
-						}
-					}
-				}
-				clazz = clazz.getSuperclass();
+			if (element.content == null) {
+				dataWriter.emptyElement(element.name);
+			} else {
+				dataWriter.dataElement(element.name, element.content.toString());
 			}
-			dataWriter.endElement(className);
-		} catch (SAXException e1) {
-			e1.printStackTrace();
-		} catch (IllegalArgumentException e) {
+		} catch (SAXException e) {
+			// TODO Auto-generated catch block
 			e.printStackTrace();
-		} catch (IllegalAccessException e) {
-			e.printStackTrace();
-		} catch (InvocationTargetException e) {
-			e.printStackTrace();
 		}
-		for (byte b : stringWriter.toString().getBytes()) {
-			resourceQueues.add(b);
-		}
-		createWriters();
-		return nextWriteModelList;
 	}
 	
-	private void createWriters() {
-		stringWriter = (new StringWriter());
-		dataWriter = (new DataWriter(stringWriter));
-		dataWriter.setIndentStep(2);
+	/**
+	 * TODO for daisuke
+	 * @param currentElement
+	 */
+	private void writeJiemamyModel(XmlElement currentElement) {
+		// TODO Auto-generated method stub
+		
 	}
 	
 }

Added: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/XmlElement.java
===================================================================
--- artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/XmlElement.java	                        (rev 0)
+++ artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/XmlElement.java	2008-10-26 13:15:41 UTC (rev 2049)
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2007-2008 MIYAMOTO Daisuke, jiemamy.org and the Others.
+ * Created on 2008/10/26
+ *
+ * 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.serializer;
+
+public class XmlElement {
+	
+	public String name;
+	
+	public Object content;
+	
+
+	/**
+	 * コンストラクタ。
+	 * @param name
+	 * @param content
+	 * @category  instance creation
+	 */
+	public XmlElement(String name, Object content) {
+		this.name = name;
+		this.content = content;
+	}
+	
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public String toString() {
+		return "<" + name + ">" + content + "</" + name + ">";
+	}
+	
+}


Property changes on: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/XmlElement.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/impl2/JiemamySerializerImpl2.java
===================================================================
--- artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/impl2/JiemamySerializerImpl2.java	                        (rev 0)
+++ artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/impl2/JiemamySerializerImpl2.java	2008-10-26 13:15:41 UTC (rev 2049)
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2007-2008 MIYAMOTO Daisuke, jiemamy.org and the Others.
+ * Created on 2008/10/13
+ *
+ * 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.serializer.impl2;
+
+import java.io.InputStream;
+
+import org.apache.commons.lang.SerializationException;
+
+import org.jiemamy.serializer.JiemamySerializer;
+import org.jiemamy.serializer.ModelInputStream;
+import org.jiemamy.spec.model.RootModel;
+
+/**
+ * TODO for daisuke
+ * @author daisuke
+ */
+public class JiemamySerializerImpl2 implements JiemamySerializer {
+	
+	/**
+	 * {@inheritDoc}
+	 */
+	public RootModel deserialize(InputStream in) throws SerializationException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+	
+	/**
+	 * {@inheritDoc}
+	 */
+	public InputStream serialize(RootModel rootModel) throws SerializationException {
+		return new ModelInputStream(rootModel);
+	}
+	
+}


Property changes on: artemis/trunk/org.jiemamy.serializer/src/main/java/org/jiemamy/serializer/impl2/JiemamySerializerImpl2.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/SerializationTest.java
===================================================================
--- artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/SerializationTest.java	2008-10-26 13:07:46 UTC (rev 2048)
+++ artemis/trunk/org.jiemamy.serializer/src/test/java/org/jiemamy/serializer/SerializationTest.java	2008-10-26 13:15:41 UTC (rev 2049)
@@ -1,113 +1,127 @@
-///*
-// * Copyright 2007-2008 MIYAMOTO Daisuke, jiemamy.org and the Others.
-// * Created on 2008/06/09
-// *
-// * 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.serializer;
-//
-//import static org.junit.Assert.assertTrue;
-//
-//import java.io.InputStream;
-//
-//import org.apache.commons.io.IOUtils;
-//import org.apache.commons.lang.builder.ToStringBuilder;
-//import org.junit.After;
-//import org.junit.Before;
-//import org.junit.Ignore;
-//import org.junit.Test;
-//import org.junit.runner.RunWith;
-//import org.seasar.framework.unit.Seasar2;
-//
-//import org.jiemamy.core.model.ApplicationModelCreator;
-//import org.jiemamy.spec.model.RootModel;
-//
-///**
-// * モデルのシリアライズ全体のテスト。
-// * コードで作ったモデル -(serialize)-> XML -(deserialize-> 復元モデルを比較して同一性をチェックする。
-// * @author daisuke
-// */
-//@RunWith(Seasar2.class)
-//public class SerializationTest {
-//	
-//	/** ApplicationModelCreator */
-//	private ApplicationModelCreator applicationModelCreator = new ApplicationModelCreator();
-//	
-//	/** ルートモデル */
-//	private RootModel rootModel;
-//	
-//	/** JiemamySerializer */
-//	private JiemamySerializer jiemamySerializer;
-//	
-//
-//	/**
-//	 * setup
-//	 * @throws Exception 
-//	 */
-//	@Before
-//	public void setUp() throws Exception {
-//		rootModel = applicationModelCreator.createModel();
-//	}
-//	
-//	/**
-//	 * teardown
-//	 * @throws Exception 
-//	 */
-//	@After
-//	public void tearDown() throws Exception {
-//		rootModel = null;
-//	}
-//	
-//	/**
-//	 * シリアライザの動作チェック。
-//	 * @throws Exception 
-//	 * FIXME for j5ik2o 実装が落ち着き次第 @Ignore を除去 
-//	 */
-//	@Test
-//	@Ignore
-//	public void test_シリアライザの動作チェック() throws Exception {
-//		InputStream serializeIs1 = null;
-//		InputStream serializeIs2 = null;
-//		InputStream referenceIs1 = null;
-//		InputStream referenceIs2 = null;
-//		InputStream referenceIs3 = null;
-//		try {
-//			// とりあえず表示
-//			serializeIs1 = jiemamySerializer.serialize(rootModel);
-//			System.out.println("serialized=" + IOUtils.toString(serializeIs1));
-//			referenceIs1 = SerializationTest.class.getResourceAsStream("/sample.xml");
-//			System.out.println("reference=" + IOUtils.toString(referenceIs1));
-//			
-//			// シリアライズ結果と参照ファイルの同一性試験
-//			referenceIs2 = SerializationTest.class.getResourceAsStream("/sample.xml");
-//			serializeIs2 = jiemamySerializer.serialize(rootModel);
-////			assertTrue(IOUtils.contentEquals(serializeIs2, referenceIs2));
-//			
-//			// this.rootModel と deserialized の同一性試験
-//			referenceIs3 = SerializationTest.class.getResourceAsStream("/sample.xml");
-//			RootModel deserialized = jiemamySerializer.deserialize(referenceIs3);
-//			
-//			// TODO [CORE-22] モデルに関するequalsを実装する。
-//			assertTrue(ToStringBuilder.reflectionToString(rootModel).equals(
-//					ToStringBuilder.reflectionToString(deserialized)));
-//		} finally {
-//			IOUtils.closeQuietly(serializeIs1);
-//			IOUtils.closeQuietly(serializeIs2);
-//			IOUtils.closeQuietly(referenceIs1);
-//			IOUtils.closeQuietly(referenceIs2);
-//			IOUtils.closeQuietly(referenceIs3);
-//		}
-//	}
-//}
+/*
+ * Copyright 2007-2008 MIYAMOTO Daisuke, jiemamy.org and the Others.
+ * Created on 2008/06/09
+ *
+ * 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.serializer;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.jiemamy.core.model.ApplicationModelCreator;
+import org.jiemamy.spec.model.RootModel;
+
+/**
+ * モデルのシリアライズ全体のテスト。
+ * コードで作ったモデル -(serialize)-> XML -(deserialize-> 復元モデルを比較して同一性をチェックする。
+ * @author daisuke
+ */
+public class SerializationTest {
+	
+	/** ApplicationModelCreator */
+	private ApplicationModelCreator applicationModelCreator = new ApplicationModelCreator();
+	
+	/** ルートモデル */
+	private RootModel rootModel;
+	
+	/** JiemamySerializer */
+	private JiemamySerializer jiemamySerializer = JiemamySerializerFactory.getInstance();
+	
+
+	/**
+	 * setup
+	 * @throws Exception 
+	 */
+	@Before
+	public void setUp() throws Exception {
+		rootModel = applicationModelCreator.createModel();
+	}
+	
+	/**
+	 * teardown
+	 * @throws Exception 
+	 */
+	@After
+	public void tearDown() throws Exception {
+		rootModel = null;
+	}
+	
+	/**
+	 * シリアライザの動作チェック。
+	 * @throws Exception 
+	 * FIXME for j5ik2o 実装が落ち着き次第 @Ignore を除去 
+	 */
+	@Test
+	public void test_シリアライザの動作チェック() throws Exception {
+		InputStream serializeIs1 = null;
+		InputStream serializeIs2 = null;
+		InputStream referenceIs1 = null;
+		InputStream referenceIs2 = null;
+		InputStream referenceIs3 = null;
+		try {
+			// とりあえず表示
+			serializeIs1 = jiemamySerializer.serialize(rootModel);
+			
+			System.out.println("serialized=");// + IOUtils.toString(serializeIs1));
+			print(serializeIs1);
+			referenceIs1 = SerializationTest.class.getResourceAsStream("/sample.xml");
+			System.out.println("reference=");// + IOUtils.toString(referenceIs1));
+			print(referenceIs1);
+			
+			// シリアライズ結果と参照ファイルの同一性試験
+			referenceIs2 = SerializationTest.class.getResourceAsStream("/sample.xml");
+			serializeIs2 = jiemamySerializer.serialize(rootModel);
+//			assertTrue(IOUtils.contentEquals(serializeIs2, referenceIs2));
+			
+			// this.rootModel と deserialized の同一性試験
+			referenceIs3 = SerializationTest.class.getResourceAsStream("/sample.xml");
+			RootModel deserialized = jiemamySerializer.deserialize(referenceIs3);
+			
+			// TODO [CORE-22] モデルに関するequalsを実装する。
+			assertTrue(ToStringBuilder.reflectionToString(rootModel).equals(
+					ToStringBuilder.reflectionToString(deserialized)));
+		} finally {
+			IOUtils.closeQuietly(serializeIs1);
+			IOUtils.closeQuietly(serializeIs2);
+			IOUtils.closeQuietly(referenceIs1);
+			IOUtils.closeQuietly(referenceIs2);
+			IOUtils.closeQuietly(referenceIs3);
+		}
+	}
+	
+	/**
+	 * TODO for daisuke
+	 * @param is
+	 * @throws IOException 
+	 */
+	private void print(InputStream is) throws IOException {
+		final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+		String buf;
+		while ((buf = reader.readLine()) != null) {
+			System.out.println(buf);
+		}
+	}
+}


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