svnno****@sourc*****
svnno****@sourc*****
2009年 2月 2日 (月) 01:17:49 JST
Revision: 2596 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=2596 Author: daisuke_m Date: 2009-02-02 01:17:49 +0900 (Mon, 02 Feb 2009) Log Message: ----------- SerializationTestを充実。 Modified Paths: -------------- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/serializer/SerializationTest.java -------------- next part -------------- Modified: artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/serializer/SerializationTest.java =================================================================== --- artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/serializer/SerializationTest.java 2009-02-01 16:17:18 UTC (rev 2595) +++ artemis/trunk/jiemamy-artemis-test/src/test/java/org/jiemamy/serializer/SerializationTest.java 2009-02-01 16:17:49 UTC (rev 2596) @@ -18,17 +18,15 @@ */ package org.jiemamy.serializer; -import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; -import java.sql.Types; +import java.io.UnsupportedEncodingException; import java.util.Collection; import java.util.List; @@ -47,19 +45,14 @@ import org.jiemamy.ArtemisView; import org.jiemamy.Jiemamy; import org.jiemamy.JiemamyFactory; -import org.jiemamy.model.DiagramPresentations; import org.jiemamy.model.RootModel; import org.jiemamy.model.attribute.AttributeModel; -import org.jiemamy.model.attribute.ColumnModel; -import org.jiemamy.model.attribute.constraint.CheckConstraintModel; -import org.jiemamy.model.attribute.constraint.NotNullConstraintModel; import org.jiemamy.model.datatype.DomainModel; -import org.jiemamy.model.datatype.DomainRef; import org.jiemamy.model.entity.TableModel; import org.jiemamy.model.entity.ViewModel; import org.jiemamy.test.ViewTestModelBuilder; +import org.jiemamy.utils.model.EqualsUtil; import org.jiemamy.utils.model.RootModelUtil; -import org.jiemamy.utils.model.TableUtil; /** * モデルのシリアライズ全体のテスト。 @@ -174,48 +167,21 @@ @Test public void test03_往復テスト1_RXRX() throws Exception { Jiemamy jiemamy1 = Jiemamy.newInstance(new Artemis(new ArtemisView())); - new ViewTestModelBuilder(jiemamy1).build(); + Jiemamy jiemamy2 = Jiemamy.newInstance(new Artemis(new ArtemisView())); JiemamySerializer serializer1 = jiemamy1.getSerializer(); + JiemamySerializer serializer2 = jiemamy2.getSerializer(); - RootModel rootModel1 = factory.getRootModel(); - String xml1; - /* rootModel1 -> xml1 */{ - ByteArrayOutputStream out = null; - - try { - out = new ByteArrayOutputStream(); - serializer1.serialize(rootModel1, out); - xml1 = out.toString(CharEncoding.UTF_8); - } finally { - IOUtils.closeQuietly(out); - } - } + new ViewTestModelBuilder(jiemamy1).build(); + RootModel rootModel1 = jiemamy1.getFactory().getRootModel(); - Jiemamy jiemamy2 = Jiemamy.newInstance(new Artemis(new ArtemisView())); - JiemamySerializer serializer2 = jiemamy2.getSerializer(); + // rootModel1 -> xml1 + String xml1 = convertRootModelToXml(serializer1, rootModel1); - RootModel rootModel2; - /* xml1 -> rootModel2 */{ - InputStream in = null; - try { - in = new ByteArrayInputStream(xml1.getBytes(CharEncoding.UTF_8)); - rootModel2 = serializer2.deserialize(in); - } finally { - IOUtils.closeQuietly(in); - } - } + // xml1 -> rootModel2 + RootModel rootModel2 = convertXmlToRootModel(xml1, serializer2); - String xml2; - /* rootModel2 -> xml2 */{ - ByteArrayOutputStream out = null; - try { - out = new ByteArrayOutputStream(); - serializer2.serialize(rootModel2, out); - xml2 = out.toString(CharEncoding.UTF_8); - } finally { - IOUtils.closeQuietly(out); - } - } + // rootModel2 -> xml2 + String xml2 = convertRootModelToXml(serializer2, rootModel2); // 参考のため、targetディレクトリに出力XMLを記録 FileUtils.writeStringToFile(new File("./target/" + ClassUtils.getShortClassName(this, "null") + "_1_xml1.xml"), @@ -224,59 +190,28 @@ FileUtils.writeStringToFile(new File("./target/" + ClassUtils.getShortClassName(this, "null") + "_1_xml2.xml"), xml2, CharEncoding.UTF_8); - TableModel emp = RootModelUtil.getEntity(rootModel2, TableModel.class, "T_EMP"); - ColumnModel id = TableUtil.getColumn(emp, "ID"); - - assertThat(id.getDataType(), instanceOf(DomainRef.class)); - - assertThat(rootModel2.getDomains().size(), is(rootModel1.getDomains().size())); - - assertThat(rootModel1.getDomains().get(0).getLogicalName(), is(nullValue())); - assertThat(rootModel2.getDomains().get(0).getLogicalName(), is(nullValue())); - - assertThat(rootModel1.getDomains().get(0).getConstraints().get(0), is(instanceOf(NotNullConstraintModel.class))); - assertThat(rootModel1.getDomains().get(0).getConstraints().get(1), is(instanceOf(CheckConstraintModel.class))); - assertThat(rootModel2.getDomains().get(0).getConstraints().get(0), is(instanceOf(NotNullConstraintModel.class))); - assertThat(rootModel2.getDomains().get(0).getConstraints().get(1), is(instanceOf(CheckConstraintModel.class))); - - assertThat(rootModel1.getDomains().get(0).getDataType().getSqlType(), is(Types.INTEGER)); - assertThat(rootModel2.getDomains().get(0).getDataType().getSqlType(), is(Types.INTEGER)); - + assertThat(EqualsUtil.equals(rootModel2, rootModel1), is(true)); assertThat(xml2, is(xml1)); } @Test - public void test04_往復テスト2_XRX() throws Exception { + public void test04_往復テスト2_XRXR() throws Exception { Jiemamy jiemamy1 = Jiemamy.newInstance(new Artemis(new ArtemisView())); + Jiemamy jiemamy2 = Jiemamy.newInstance(new Artemis(new ArtemisView())); JiemamySerializer serializer1 = jiemamy1.getSerializer(); + JiemamySerializer serializer2 = jiemamy2.getSerializer(); String xml1 = IOUtils.toString(SerializationTest.class.getResourceAsStream("/sample.xml"), CharEncoding.UTF_8); - RootModel rootModel1; - /* xml1 -> rootModel1 */{ - InputStream in = null; - try { - in = new ByteArrayInputStream(xml1.getBytes(CharEncoding.UTF_8)); - rootModel1 = serializer1.deserialize(in); - } finally { - IOUtils.closeQuietly(in); - } - } - Jiemamy jiemamy2 = Jiemamy.newInstance(new Artemis(new ArtemisView())); - JiemamySerializer serializer2 = jiemamy2.getSerializer(); + // xml1 -> rootModel1 + RootModel rootModel1 = convertXmlToRootModel(xml1, serializer1); - String xml2; - /* rootModel1 -> xml2 */{ - ByteArrayOutputStream out = null; - try { - out = new ByteArrayOutputStream(); - serializer2.serialize(rootModel1, out); - xml2 = out.toString(CharEncoding.UTF_8); - } finally { - IOUtils.closeQuietly(out); - } - } + // rootModel1 -> xml2 + String xml2 = convertRootModelToXml(serializer2, rootModel1); + // xml1 -> rootModel2 + RootModel rootModel2 = convertXmlToRootModel(xml2, serializer2); + // 参考のため、targetディレクトリに出力XMLを記録 FileUtils.writeStringToFile(new File("./target/" + ClassUtils.getShortClassName(this, "null") + "_2_xml1.xml"), xml1, CharEncoding.UTF_8); @@ -284,11 +219,35 @@ FileUtils.writeStringToFile(new File("./target/" + ClassUtils.getShortClassName(this, "null") + "_2_xml2.xml"), xml2, CharEncoding.UTF_8); - assertThat(rootModel1.getDomains().size(), is(2)); - assertThat(rootModel1.getEntities().size(), is(3)); - assertThat(rootModel1.getDataSets().size(), is(2)); - assertThat(rootModel1.getDataSets().get(0).getRecords().size(), is(2)); - assertThat(rootModel1.getAdapter(DiagramPresentations.class).size(), is(2)); + assertThat(EqualsUtil.equals(rootModel2, rootModel1), is(true)); assertThat(xml2, is(xml1)); } + + private String convertRootModelToXml(JiemamySerializer serializer, RootModel rootModel) + throws UnsupportedEncodingException, SerializationException { + String xml; + ByteArrayOutputStream out = null; + + try { + out = new ByteArrayOutputStream(); + serializer.serialize(rootModel, out); + xml = out.toString(CharEncoding.UTF_8); + } finally { + IOUtils.closeQuietly(out); + } + return xml; + } + + private RootModel convertXmlToRootModel(String xml, JiemamySerializer serializer) + throws UnsupportedEncodingException, SerializationException { + RootModel rootModel; + InputStream in = null; + try { + in = new ByteArrayInputStream(xml.getBytes(CharEncoding.UTF_8)); + rootModel = serializer.deserialize(in); + } finally { + IOUtils.closeQuietly(in); + } + return rootModel; + } }