From bc34e78968806876de18ebb05c66fd919c8a5b29 Mon Sep 17 00:00:00 2001 From: ParkSeongMin Date: Tue, 23 May 2017 14:54:22 +0900 Subject: [PATCH] =?UTF-8?q?DataSet=EC=9C=BC=EB=A1=9C=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=20=EB=B3=80=ED=99=98=20=EC=8B=9C=20=EC=8A=A4=ED=82=A4?= =?UTF-8?q?=EB=A7=88=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EC=85=8B=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=EB=B0=8F=20=EC=8A=A4=ED=82=A4=EB=A7=88=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=98=B5=EC=85=98=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 386 ++++----- .../data/convert/ConvertDefinition.java | 179 ++-- .../support/AbstractDataSetConverter.java | 27 +- .../data/support/ListToDataSetConverter.java | 268 +++--- .../support/ObjectToDataSetConverter.java | 216 ++--- .../support/ListToDataSetConverterTest.java | 206 ++++- .../spring/data/support/NexacroTestUtil.java | 792 +++++++++--------- .../support/ObjectToDataSetConverterTest.java | 119 +++ 8 files changed, 1301 insertions(+), 892 deletions(-) diff --git a/pom.xml b/pom.xml index 8b6be07..8d529d3 100644 --- a/pom.xml +++ b/pom.xml @@ -1,193 +1,193 @@ - - 4.0.0 - com.nexacro.spring - 1.0.0 - nexacro-core - jar - nexacro-core - http://maven.apache.org - - nexacro-core Dev Group - - Description of nexacro-core - - - com.nexacro - nexacro-pom-parent - 1.0.0 - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - org.apache.maven.plugins - maven-source-plugin - - - org.maven.ide.eclipse - lifecycle-mapping - 0.9.9-SNAPSHOT - - generic - - - - - - - - - - - com.nexacro - nexacro-xapi - 1.0 - compile - - - - - - org.springframework - spring-core - - - org.springframework - spring-beans - - - org.springframework - spring-context - - - org.springframework - spring-web - - - org.springframework - spring-webmvc - - - org.springframework - spring-aop - - - - org.springframework - spring-test - ${spring.maven.artifact.version} - test - - - - org.springmodules - spring-modules-jakarta-commons - 0.8a - - - javax.servlet - servlet-api - - - - - - commons-configuration - commons-configuration - 1.6 - - - - - javax.servlet - javax.servlet-api - 3.0.1 - provided - - - - javax.servlet.jsp - javax.servlet.jsp-api - 2.2.1 - provided - - - - - org.apache.logging.log4j - log4j-slf4j-impl - 2.1 - - - org.apache.logging.log4j - log4j-core - 2.1 - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j-version} - runtime - - - - - - - junit - junit - 4.8.1 - test - - - - commons-io - commons-io - 2.4 - test - - - - xmlunit - xmlunit - 1.2 - test - - - - - - 1.7.5 - - + + 4.0.0 + com.nexacro.spring + 1.0.1 + nexacro-core + jar + nexacro-core + http://maven.apache.org + + nexacro-core Dev Group + + Description of nexacro-core + + + com.nexacro + nexacro-pom-parent + 1.0.0 + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + org.apache.maven.plugins + maven-source-plugin + + + org.maven.ide.eclipse + lifecycle-mapping + 0.9.9-SNAPSHOT + + generic + + + + + + + + + + + com.nexacro + nexacro-xapi + 1.0 + compile + + + + + + org.springframework + spring-core + + + org.springframework + spring-beans + + + org.springframework + spring-context + + + org.springframework + spring-web + + + org.springframework + spring-webmvc + + + org.springframework + spring-aop + + + + org.springframework + spring-test + ${spring.maven.artifact.version} + test + + + + org.springmodules + spring-modules-jakarta-commons + 0.8a + + + javax.servlet + servlet-api + + + + + + commons-configuration + commons-configuration + 1.6 + + + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + + javax.servlet.jsp + javax.servlet.jsp-api + 2.2.1 + provided + + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.1 + + + org.apache.logging.log4j + log4j-core + 2.1 + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j-version} + runtime + + + + + + + junit + junit + 4.8.1 + test + + + + commons-io + commons-io + 2.4 + test + + + + xmlunit + xmlunit + 1.2 + test + + + + + + 1.7.5 + + diff --git a/src/main/java/com/nexacro/spring/data/convert/ConvertDefinition.java b/src/main/java/com/nexacro/spring/data/convert/ConvertDefinition.java index b05d2b2..a5fcab2 100644 --- a/src/main/java/com/nexacro/spring/data/convert/ConvertDefinition.java +++ b/src/main/java/com/nexacro/spring/data/convert/ConvertDefinition.java @@ -1,71 +1,108 @@ -package com.nexacro.spring.data.convert; - - -/** - *

데이터 변환에 대한 정보를 저장한다. - *

DataSet 혹은 Variable의 명칭을 가지며, List 형식의 데이터 변환시 Generic Type 정보를 가진다. - * - * @author Park SeongMin - * @since 07.28.2015 - * @version 1.0 - * @see - */ - -public class ConvertDefinition { - - private String name; - private Class genericType; // for generic - private boolean isIgnoreException = false; - - public ConvertDefinition(String name) { - setName(name); - } - - /** - * @return the name - */ - public String getName() { - return name; - } - - /** - * @param name - * the name to set - */ - public void setName(String name) { - if(name == null || name.length() == 0) { - throw new IllegalArgumentException("converted name must be not null"); - } - this.name = name; - } - - /** - * @return the isIgnoreException - */ - public boolean isIgnoreException() { - return isIgnoreException; - } - - /** - * @param isIgnoreException - * the isIgnoreException to set - */ - public void setIgnoreException(boolean isIgnoreException) { - this.isIgnoreException = isIgnoreException; - } - - /** - * @return the genericType - */ - public Class getGenericType() { - return genericType; - } - - /** - * @param genericType the genericType to set - */ - public void setGenericType(Class genericType) { - this.genericType = genericType; - } - -} +package com.nexacro.spring.data.convert; + +import com.nexacro.xapi.data.DataSet; + + +/** + *

데이터 변환에 대한 정보를 저장한다. + *

DataSet 혹은 Variable의 명칭을 가지며, List 형식의 데이터 변환시 Generic Type 정보를 가진다. + * + * @author Park SeongMin + * @since 07.28.2015 + * @version 1.0 + * @see + */ + +public class ConvertDefinition { + + private String name; + private Class genericType; // for generic + private boolean isIgnoreException = false; + + private DataSet schemaDataSet; + private boolean disallowChangeStructure; // (v1.0.0에서 허용 하게 되어 있음) + + public ConvertDefinition(String name) { + setName(name); + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name + * the name to set + */ + public void setName(String name) { + if(name == null || name.length() == 0) { + throw new IllegalArgumentException("converted name must be not null"); + } + this.name = name; + } + + /** + * @return the isIgnoreException + */ + public boolean isIgnoreException() { + return isIgnoreException; + } + + /** + * @param isIgnoreException + * the isIgnoreException to set + */ + public void setIgnoreException(boolean isIgnoreException) { + this.isIgnoreException = isIgnoreException; + } + + /** + * @return the genericType + */ + public Class getGenericType() { + return genericType; + } + + /** + * @param genericType the genericType to set + */ + public void setGenericType(Class genericType) { + this.genericType = genericType; + } + + /** + * + * @return disallow structure change + */ + public boolean isDisallowChangeStructure() { + return disallowChangeStructure; + } + + /** + * + * @param disallow structure change + */ + public void setDisallowChangeStructure(boolean disallowChangeStructure) { + this.disallowChangeStructure = disallowChangeStructure; + } + + /** + * + * @return the schema dataSet + */ + public DataSet getSchemaDataSet() { + return schemaDataSet; + } + + /** + * + * @param schemaDataSet + */ + public void setSchemaDataSet(DataSet schemaDataSet) { + this.schemaDataSet = schemaDataSet; + } + +} diff --git a/src/main/java/com/nexacro/spring/data/support/AbstractDataSetConverter.java b/src/main/java/com/nexacro/spring/data/support/AbstractDataSetConverter.java index 9b19448..886b928 100644 --- a/src/main/java/com/nexacro/spring/data/support/AbstractDataSetConverter.java +++ b/src/main/java/com/nexacro/spring/data/support/AbstractDataSetConverter.java @@ -37,7 +37,7 @@ public class AbstractDataSetConverter extends AbstractListenerHandler { * @param map * @throws NexacroConvertException */ - protected void addRowIntoDataSet(DataSet ds, Map map) throws NexacroConvertException { + protected void addRowIntoDataSet(DataSet ds, Map map, boolean disallowChangeStructure) throws NexacroConvertException { // ignore null data. if(map == null) { return; @@ -62,11 +62,15 @@ public class AbstractDataSetConverter extends AbstractListenerHandler { int columnIndex = ds.indexOfColumn(columnName); if(columnIndex < 0) { - // flexible map data. 'null' data should be ignored - ds.setChangeStructureWithData(true); - - if(!addColumnByMap(ds, columnName, value)) { - continue; + if(disallowChangeStructure) { + // ignore + continue; + } else { + // flexible map data. 'null' data should be ignored + ds.setChangeStructureWithData(true); + if(!addColumnByMap(ds, columnName, value)) { + continue; + } } } // fire event @@ -108,7 +112,13 @@ public class AbstractDataSetConverter extends AbstractListenerHandler { // Byte[] 변환 Object object = NexacroConverterHelper.toObject(propertyValue); + // 컬럼은 무조건 존재한다. int columnIndex = ds.indexOfColumn(propertyName); + if(columnIndex < 0) { + // Map과 달리 이미 구조 변경에 대한 처리가 되었기 때문에 컬럼이 존재하지 않을 경우 무시하도록 한다. + continue; + } + // fire event object = fireDataSetConvertedValue(ds, object, newRow, columnIndex, false, false); @@ -127,6 +137,9 @@ public class AbstractDataSetConverter extends AbstractListenerHandler { * @throws NexacroConvertException */ protected void addColumnIntoDataSet(DataSet ds, Map map) throws NexacroConvertException { + + ds.setChangeStructureWithData(true); + Iterator iterator = map.keySet().iterator(); while(iterator.hasNext()) { Object key = iterator.next(); @@ -149,6 +162,8 @@ public class AbstractDataSetConverter extends AbstractListenerHandler { */ protected void addColumnIntoDataSet(DataSet ds, Object availableFirstData) { + ds.setChangeStructureWithData(true); + NexacroBeanWrapper beanWrapper = NexacroBeanWrapper.createBeanWrapper(availableFirstData); NexacroBeanProperty[] beanProperties = beanWrapper.getProperties(); diff --git a/src/main/java/com/nexacro/spring/data/support/ListToDataSetConverter.java b/src/main/java/com/nexacro/spring/data/support/ListToDataSetConverter.java index 189a18b..a91f5c4 100644 --- a/src/main/java/com/nexacro/spring/data/support/ListToDataSetConverter.java +++ b/src/main/java/com/nexacro/spring/data/support/ListToDataSetConverter.java @@ -1,126 +1,142 @@ -package com.nexacro.spring.data.support; - -import java.util.List; -import java.util.Map; - -import com.nexacro.spring.data.convert.ConvertDefinition; -import com.nexacro.spring.data.convert.NexacroConvertException; -import com.nexacro.spring.data.convert.NexacroConverter; -import com.nexacro.spring.data.metadata.NexacroMetaData; -import com.nexacro.xapi.data.DataSet; - -/** - *

List에서 DataSet으로 데이터로 변환을 수행한다. - * - * @author Park SeongMin - * @since 07.28.2015 - * @version 1.0 - * @see - */ -public class ListToDataSetConverter extends AbstractDataSetConverter implements NexacroConverter { - - private static final int CHECK_INDEX = 0; - - @Override - public boolean canConvert(Class source, Class target) { - - if(source == null || target == null) { - return false; - } - - // support list sub class - if(List.class.isAssignableFrom(source) && DataSet.class.equals(target)) { - return true; - } - - return false; - } - - /* - * @see com.nexacro.spring.data.NexacroConverter#convert(java.lang.Object) - */ - @Override - public DataSet convert(List source, ConvertDefinition definition) throws NexacroConvertException { - - if(definition ==null) { - throw new IllegalArgumentException(ConvertDefinition.class.getSimpleName()+" must not be null."); - } - if(source == null) { - return new DataSet(definition.getName()); - } - - // check first value - Object availableFirstData = checkAvailable(source); - if(availableFirstData == null) { - return new DataSet(definition.getName()); - } - - // DataSet SavedType은 처리 하지 않는다. (UI 처리 불필요) - DataSet ds = null; - if(availableFirstData instanceof Map) { - ds = convertListMapToDataSet(source, definition, (Map) availableFirstData); - } else { - ds = convertListBeanToDataSet(source, definition, availableFirstData); - } - - return ds; - } - - private Object checkAvailable(List source) { - - // for ibatis empty data - if(source instanceof NexacroMetaData) { - NexacroMetaData metaData = (NexacroMetaData) source; - return metaData.getMetaData(); - } - - if(source.size() == 0) { - return null; - } - for(Object obj: source) { - if(obj != null) { - return obj; - } - } - - return null; - } - - private DataSet convertListMapToDataSet(List source, ConvertDefinition definition, Map availableFirstData) throws NexacroConvertException { - - DataSet ds = new DataSet(definition.getName()); - - // dynamic하게 변경 된 데이터는 처리 하지 않는다. - addColumnIntoDataSet(ds, availableFirstData); - - for(Object obj: source) { - - if(!(obj instanceof Map)) { - throw new NexacroConvertException("list should use the generic type. target="+ds.getName()); - } - - addRowIntoDataSet(ds, (Map) obj); - } - - return ds; - } - - private DataSet convertListBeanToDataSet(List source, ConvertDefinition definition, Object availableFirstData) throws NexacroConvertException { - - // 지원하는 형식인지 확인 한다. - if(!NexacroConverterHelper.isSupportedBean(availableFirstData.getClass())) { - throw new NexacroConvertException("unsupported generic type. type="+availableFirstData.getClass()); - } - - DataSet ds = new DataSet(definition.getName()); - - addColumnIntoDataSet(ds, availableFirstData); - - for(Object obj: source) { - addRowIntoDataSet(ds, obj); - } - - return ds; - } - -} +package com.nexacro.spring.data.support; + +import java.util.List; +import java.util.Map; + +import com.nexacro.spring.data.convert.ConvertDefinition; +import com.nexacro.spring.data.convert.NexacroConvertException; +import com.nexacro.spring.data.convert.NexacroConverter; +import com.nexacro.spring.data.metadata.NexacroMetaData; +import com.nexacro.xapi.data.DataSet; + +/** + *

List에서 DataSet으로 데이터로 변환을 수행한다. + * + * @author Park SeongMin + * @since 07.28.2015 + * @version 1.0 + * @see + */ +public class ListToDataSetConverter extends AbstractDataSetConverter implements NexacroConverter { + + private static final int CHECK_INDEX = 0; + + @Override + public boolean canConvert(Class source, Class target) { + + if(source == null || target == null) { + return false; + } + + // support list sub class + if(List.class.isAssignableFrom(source) && DataSet.class.equals(target)) { + return true; + } + + return false; + } + + /* + * @see com.nexacro.spring.data.NexacroConverter#convert(java.lang.Object) + */ + @Override + public DataSet convert(List source, ConvertDefinition definition) throws NexacroConvertException { + + if(definition ==null) { + throw new IllegalArgumentException(ConvertDefinition.class.getSimpleName()+" must not be null."); + } + if(source == null) { + return new DataSet(definition.getName()); + } + + // check first value + Object availableFirstData = checkAvailable(source); + if(availableFirstData == null) { + return new DataSet(definition.getName()); + } + + // DataSet SavedType은 처리 하지 않는다. (UI 처리 불필요) + DataSet ds = null; + if(availableFirstData instanceof Map) { + ds = convertListMapToDataSet(source, definition, (Map) availableFirstData); + } else { + ds = convertListBeanToDataSet(source, definition, availableFirstData); + } + + return ds; + } + + private Object checkAvailable(List source) { + + // for ibatis empty data + if(source instanceof NexacroMetaData) { + NexacroMetaData metaData = (NexacroMetaData) source; + return metaData.getMetaData(); + } + + if(source.size() == 0) { + return null; + } + for(Object obj: source) { + if(obj != null) { + return obj; + } + } + + return null; + } + + private DataSet convertListMapToDataSet(List source, ConvertDefinition definition, Map availableFirstData) throws NexacroConvertException { + + DataSet ds = null; + + if(definition.getSchemaDataSet() != null) { + // set schema dataSet + ds = definition.getSchemaDataSet(); + } else { + ds = new DataSet(definition.getName()); + addColumnIntoDataSet(ds, availableFirstData); + } + + for(Object obj: source) { + + if(!(obj instanceof Map)) { + throw new NexacroConvertException("list should use the generic type. target="+ds.getName()); + } + + addRowIntoDataSet(ds, (Map) obj, definition.isDisallowChangeStructure()); + } + + return ds; + } + + private DataSet convertListBeanToDataSet(List source, ConvertDefinition definition, Object availableFirstData) throws NexacroConvertException { + + // 지원하는 형식인지 확인 한다. + if(!NexacroConverterHelper.isSupportedBean(availableFirstData.getClass())) { + throw new NexacroConvertException("unsupported generic type. type="+availableFirstData.getClass()); + } + + DataSet ds = null; + if(definition.getSchemaDataSet() != null) { + // set schema dataSet + ds = definition.getSchemaDataSet(); + + // map과 달리 bean은 이미 정의가 되어 있기 때문에 row를 추가할때 컬럼을 추가하지 않고, 미리 설정한다. + if(!definition.isDisallowChangeStructure()) { + addColumnIntoDataSet(ds, availableFirstData); + } + + } else { + ds = new DataSet(definition.getName()); + addColumnIntoDataSet(ds, availableFirstData); + } + + for(Object obj: source) { + addRowIntoDataSet(ds, obj); + } + + return ds; + } + +} diff --git a/src/main/java/com/nexacro/spring/data/support/ObjectToDataSetConverter.java b/src/main/java/com/nexacro/spring/data/support/ObjectToDataSetConverter.java index 961b4e0..17d7c0b 100644 --- a/src/main/java/com/nexacro/spring/data/support/ObjectToDataSetConverter.java +++ b/src/main/java/com/nexacro/spring/data/support/ObjectToDataSetConverter.java @@ -1,98 +1,118 @@ -package com.nexacro.spring.data.support; - -import java.util.List; -import java.util.Map; - -import com.nexacro.spring.data.convert.ConvertDefinition; -import com.nexacro.spring.data.convert.NexacroConvertException; -import com.nexacro.spring.data.convert.NexacroConverter; -import com.nexacro.xapi.data.DataSet; - -/** - *

Object에서 DataSet으로 데이터로 변환을 수행한다. - * - * @author Park SeongMin - * @since 08.17.2015 - * @version 1.0 - * @see - */ -public class ObjectToDataSetConverter extends AbstractDataSetConverter implements NexacroConverter { - - @Override - public boolean canConvert(Class source, Class target) { - - if(source == null || target == null) { - return false; - } - - // support type - if(!List.class.isAssignableFrom(source) && NexacroConverterHelper.isSupportedBean(source) && DataSet.class.equals(target)) { - return true; - } - - return false; - } - - @Override - public DataSet convert(Object source, ConvertDefinition definition) throws NexacroConvertException { - - if(definition ==null) { - throw new IllegalArgumentException(ConvertDefinition.class.getSimpleName()+" must not be null."); - } - - if(source == null) { - return new DataSet(definition.getName()); - } - - // DataSet SavedType은 처리 하지 않는다. (UI 처리 불필요) - DataSet ds = null; - if(source instanceof Map) { - ds = convertMapToDataSet((Map)source, definition); - } else { - ds = convertBeanToDataSet(source, definition); - } - - return ds; - } - - private DataSet convertBeanToDataSet(Object source, ConvertDefinition definition) throws NexacroConvertException { - - // 지원하는 형식인지 확인 한다. - if(!NexacroConverterHelper.isSupportedBean(source.getClass())) { - throw new NexacroConvertException("unsupported source type. type="+source.getClass()); - } - - DataSet ds = new DataSet(definition.getName()); - addColumnIntoDataSet(ds, source); - addRowIntoDataSet(ds, source); - return ds; - } - - private DataSet convertMapToDataSet(Map source, ConvertDefinition definition) throws NexacroConvertException { - DataSet ds = new DataSet(definition.getName()); - addColumnIntoDataSet(ds, source); - addRowIntoDataSet(ds, source); - return ds; - } - - @Override - public void addColumnIntoDataSet(DataSet ds, Map source) throws NexacroConvertException { - super.addColumnIntoDataSet(ds, source); - } - - @Override - public void addColumnIntoDataSet(DataSet ds, Object source) { - super.addColumnIntoDataSet(ds, source); - } - - @Override - public void addRowIntoDataSet(DataSet ds, Map source) throws NexacroConvertException { - super.addRowIntoDataSet(ds, source); - } - - @Override - public void addRowIntoDataSet(DataSet ds, Object source) { - super.addRowIntoDataSet(ds, source); - } - -} +package com.nexacro.spring.data.support; + +import java.util.List; +import java.util.Map; + +import com.nexacro.spring.data.convert.ConvertDefinition; +import com.nexacro.spring.data.convert.NexacroConvertException; +import com.nexacro.spring.data.convert.NexacroConverter; +import com.nexacro.xapi.data.DataSet; + +/** + *

Object에서 DataSet으로 데이터로 변환을 수행한다. + * + * @author Park SeongMin + * @since 08.17.2015 + * @version 1.0 + * @see + */ +public class ObjectToDataSetConverter extends AbstractDataSetConverter implements NexacroConverter { + + @Override + public boolean canConvert(Class source, Class target) { + + if(source == null || target == null) { + return false; + } + + // support type + if(!List.class.isAssignableFrom(source) && NexacroConverterHelper.isSupportedBean(source) && DataSet.class.equals(target)) { + return true; + } + + return false; + } + + @Override + public DataSet convert(Object source, ConvertDefinition definition) throws NexacroConvertException { + + if(definition ==null) { + throw new IllegalArgumentException(ConvertDefinition.class.getSimpleName()+" must not be null."); + } + + if(source == null) { + return new DataSet(definition.getName()); + } + + // DataSet SavedType은 처리 하지 않는다. (UI 처리 불필요) + DataSet ds = null; + if(source instanceof Map) { + ds = convertMapToDataSet((Map)source, definition); + } else { + ds = convertBeanToDataSet(source, definition); + } + + return ds; + } + + private DataSet convertBeanToDataSet(Object source, ConvertDefinition definition) throws NexacroConvertException { + + // 지원하는 형식인지 확인 한다. + if(!NexacroConverterHelper.isSupportedBean(source.getClass())) { + throw new NexacroConvertException("unsupported source type. type="+source.getClass()); + } + + DataSet ds = null; + if(definition.getSchemaDataSet() != null) { + // set schema dataSet + ds = definition.getSchemaDataSet(); + + // map과 달리 bean은 이미 정의가 되어 있기 때문에 row를 추가할때 컬럼을 추가하지 않고, 미리 설정한다. + if(!definition.isDisallowChangeStructure()) { + addColumnIntoDataSet(ds, source); + } + + } else { + ds = new DataSet(definition.getName()); + addColumnIntoDataSet(ds, source); + } + + addRowIntoDataSet(ds, source); + return ds; + } + + private DataSet convertMapToDataSet(Map source, ConvertDefinition definition) throws NexacroConvertException { + DataSet ds = null; + if(definition.getSchemaDataSet() != null) { + // set schema dataSet + ds = definition.getSchemaDataSet(); + } else { + ds = new DataSet(definition.getName()); + addColumnIntoDataSet(ds, source); + } + + addRowIntoDataSet(ds, source, definition.isDisallowChangeStructure()); + return ds; + } + + @Override + public void addColumnIntoDataSet(DataSet ds, Map source) throws NexacroConvertException { + super.addColumnIntoDataSet(ds, source); + } + + @Override + public void addColumnIntoDataSet(DataSet ds, Object source) { + super.addColumnIntoDataSet(ds, source); + } + + @Override + public void addRowIntoDataSet(DataSet ds, Map source, boolean disallowChangeStructure) throws NexacroConvertException { + super.addRowIntoDataSet(ds, source, disallowChangeStructure); + } + + @Override + public void addRowIntoDataSet(DataSet ds, Object source) { + super.addRowIntoDataSet(ds, source); + } + +} diff --git a/src/test/java/com/nexacro/spring/data/support/ListToDataSetConverterTest.java b/src/test/java/com/nexacro/spring/data/support/ListToDataSetConverterTest.java index d3f41e3..397a305 100644 --- a/src/test/java/com/nexacro/spring/data/support/ListToDataSetConverterTest.java +++ b/src/test/java/com/nexacro/spring/data/support/ListToDataSetConverterTest.java @@ -20,6 +20,7 @@ import com.nexacro.spring.util.ReflectionUtil; import com.nexacro.xapi.data.ColumnHeader; import com.nexacro.xapi.data.ConstantColumnHeader; import com.nexacro.xapi.data.DataSet; +import com.nexacro.xapi.data.datatype.PlatformDataType; /** * @@ -100,7 +101,7 @@ public class ListToDataSetConverterTest { Assert.fail("converted object must be implemented DataSet"); } - NexacroTestUtil.compareDefaultDataSet((DataSet) ds); + NexacroTestUtil.compareDefaultDataSet((DataSet) ds, 0); } @@ -123,7 +124,7 @@ public class ListToDataSetConverterTest { Assert.fail("converted object must be implemented DataSet"); } - NexacroTestUtil.compareDefaultDataSet((DataSet) ds); + NexacroTestUtil.compareDefaultDataSet((DataSet) ds, 0); } @@ -235,6 +236,207 @@ public class ListToDataSetConverterTest { } + @Test + public void testAllowChangeStructure() { + // v1.0.0에서 Map의 null 컬럼 추가 되는 것을 확인한다. + List> defaultMap = new ArrayList>(); + { + Map map = new HashMap(); + map.put("col1", "value"); + defaultMap.add(map); + } + { + Map map = new HashMap(); + map.put("col2", "value"); + defaultMap.add(map); + } + + ConvertDefinition definition = new ConvertDefinition("ds"); + definition.setDisallowChangeStructure(false); // set allow structure change + + Object dsObj = null; + try { + dsObj = converter.convert(defaultMap, definition); + } catch (NexacroConvertException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + DataSet ds = (DataSet) dsObj; + + Assert.assertTrue(ds.containsColumn("col1")); + Assert.assertTrue(ds.containsColumn("col2")); + + } + + @Test + public void testDisallowChangeStructure() { + + List> defaultMap = new ArrayList>(); + { + Map map = new HashMap(); + map.put("col1", "value"); + defaultMap.add(map); + } + { + Map map = new HashMap(); + map.put("col2", "value"); + defaultMap.add(map); + } + + ConvertDefinition definition = new ConvertDefinition("ds"); + definition.setDisallowChangeStructure(true); // set disallow structure change + + Object dsObj = null; + try { + dsObj = converter.convert(defaultMap, definition); + } catch (NexacroConvertException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + DataSet ds = (DataSet) dsObj; + + Assert.assertTrue(ds.containsColumn("col1")); + Assert.assertFalse(ds.containsColumn("col2")); + + } + + @Test + public void testAllowChangeStructureWithSchemaDataSet() { + + DataSet schemaDataSet = new DataSet("schemaDa"); + schemaDataSet.addColumn("defaultCol1", PlatformDataType.STRING); + + List> defaultMap = new ArrayList>(); + { + Map map = new HashMap(); + map.put("col1", "value"); + defaultMap.add(map); + } + { + Map map = new HashMap(); + map.put("col2", "value"); + defaultMap.add(map); + } + + ConvertDefinition definition = new ConvertDefinition("ds"); + definition.setDisallowChangeStructure(false); // set allow structure change + definition.setSchemaDataSet(schemaDataSet); // set schema dataSet + + Object dsObj = null; + try { + dsObj = converter.convert(defaultMap, definition); + } catch (NexacroConvertException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + DataSet ds = (DataSet) dsObj; + Assert.assertTrue(schemaDataSet.equals(ds)); + + Assert.assertTrue(ds.containsColumn("defaultCol1")); + Assert.assertTrue(ds.containsColumn("col1")); + Assert.assertTrue(ds.containsColumn("col2")); + + } + + @Test + public void testAllowChangeStructureWithSchemaDataSetWithBean() { + + DataSet schemaDataSet = new DataSet("schemaDa"); + schemaDataSet.addColumn("defaultCol1", PlatformDataType.STRING); + + List defaultBean = NexacroTestUtil.createDefaultBeans(); + + ConvertDefinition definition = new ConvertDefinition("ds"); + definition.setDisallowChangeStructure(false); // set allow structure change + definition.setSchemaDataSet(schemaDataSet); // set schema dataSet + + Object dsObj = null; + try { + dsObj = converter.convert(defaultBean, definition); + } catch (NexacroConvertException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + + DataSet ds = (DataSet) dsObj; + Assert.assertTrue(schemaDataSet.equals(ds)); + Assert.assertTrue(ds.containsColumn("defaultCol1")); + NexacroTestUtil.compareDefaultDataSet(ds, 1); // defaultColumn + + } + + @Test + public void testDisallowChangeStructureWithSchemaDataSet() { + + DataSet schemaDataSet = new DataSet("schemaDa"); + schemaDataSet.addColumn("defaultCol1", PlatformDataType.STRING); + + List> defaultMap = new ArrayList>(); + { + Map map = new HashMap(); + map.put("col1", "value"); + defaultMap.add(map); + } + { + Map map = new HashMap(); + map.put("col2", "value"); + defaultMap.add(map); + } + + ConvertDefinition definition = new ConvertDefinition("ds"); + definition.setDisallowChangeStructure(true); // set disallow structure change + definition.setSchemaDataSet(schemaDataSet); // set schema dataSet + + Object dsObj = null; + try { + dsObj = converter.convert(defaultMap, definition); + } catch (NexacroConvertException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + DataSet ds = (DataSet) dsObj; + Assert.assertTrue(schemaDataSet.equals(ds)); + + Assert.assertTrue(ds.containsColumn("defaultCol1")); + Assert.assertFalse(ds.containsColumn("col1")); + Assert.assertFalse(ds.containsColumn("col2")); + + } + + @Test + public void testDisallowChangeStructureWithSchemaDataSetWithBean() { + + DataSet schemaDataSet = new DataSet("schemaDa"); + schemaDataSet.addColumn("defaultCol1", PlatformDataType.STRING); + + List defaultBean = NexacroTestUtil.createDefaultBeans(); + + ConvertDefinition definition = new ConvertDefinition("ds"); + definition.setDisallowChangeStructure(true); // set disallow structure change + definition.setSchemaDataSet(schemaDataSet); // set schema dataSet + + Object dsObj = null; + try { + dsObj = converter.convert(defaultBean, definition); + } catch (NexacroConvertException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + + DataSet ds = (DataSet) dsObj; + Assert.assertTrue(schemaDataSet.equals(ds)); + Assert.assertTrue(ds.containsColumn("defaultCol1")); + + Assert.assertEquals(ds.getColumnCount(), 1); + + } + @Test public void testUpperCase() { diff --git a/src/test/java/com/nexacro/spring/data/support/NexacroTestUtil.java b/src/test/java/com/nexacro/spring/data/support/NexacroTestUtil.java index 33c1148..6ccf060 100644 --- a/src/test/java/com/nexacro/spring/data/support/NexacroTestUtil.java +++ b/src/test/java/com/nexacro/spring/data/support/NexacroTestUtil.java @@ -1,396 +1,396 @@ -package com.nexacro.spring.data.support; - -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.junit.Assert; - -import com.nexacro.spring.data.DataSetRowTypeAccessor; -import com.nexacro.spring.data.DataSetSavedDataAccessor; -import com.nexacro.spring.data.support.bean.DefaultBean; -import com.nexacro.xapi.data.ColumnHeader; -import com.nexacro.xapi.data.DataSet; -import com.nexacro.xapi.data.datatype.DataType; -import com.nexacro.xapi.data.datatype.PlatformDataType; - -/** - * - * @author Park SeongMin - * @since 08.10.2015 - * @version 1.0 - * @see - */ -public abstract class NexacroTestUtil { - - private static String[] dsPropertyNames; - private static List dsValuesList; - private static DataType[] dsDataTypes; - - // private static List> dataMapList; - // private static Map dataTypeMap; - - private static Map supportedClassesValue; - - static { - initSupportedClassesValue(); - initDsPropertyNames(); - initDsValueList(); - initDsDataTypes(); - } - - static Map getSupportedClassesValue() { - return Collections.unmodifiableMap(supportedClassesValue); - } - - private static void initSupportedClassesValue() { - - supportedClassesValue = new HashMap(); - - Calendar instance = null; - Date hireDate = null; - instance = Calendar.getInstance(); - instance.set(2009, Calendar.JANUARY, 1); - hireDate = new Date(instance.getTimeInMillis()); - - supportedClassesValue.put(int.class, 11); - supportedClassesValue.put(Integer.class, new Integer(11)); - supportedClassesValue.put(long.class, 11l); - supportedClassesValue.put(Long.class, new Long(11l)); - supportedClassesValue.put(float.class, 180.1f); - supportedClassesValue.put(Float.class, new Float(180.1f)); - supportedClassesValue.put(double.class, 11.1d); - supportedClassesValue.put(Double.class, new Double(11.1d)); - supportedClassesValue.put(boolean.class, true); - supportedClassesValue.put(Boolean.class, Boolean.TRUE); - supportedClassesValue.put(byte[].class, new byte[] { 1, 1 }); - supportedClassesValue.put(Byte[].class, new Byte[] { 1, 1 }); - supportedClassesValue.put(String.class, "seongmin"); - supportedClassesValue.put(Date.class, hireDate); - supportedClassesValue.put(BigDecimal.class, new BigDecimal("10001")); - supportedClassesValue.put(Object.class, new Object()); - - } - - private static void initDsPropertyNames() { - - dsPropertyNames = new String[] { "employeeId", "access", "height", "commissionPercent", "male", "image", - "firstName", "lastName", "email", "hireDate", "salary", "obj" }; - - } - - private static void initDsValueList() { - - dsValuesList = new ArrayList(); - - Calendar instance = null; - Date hireDate = null; - instance = Calendar.getInstance(); - instance.set(2009, Calendar.JANUARY, 1); - hireDate = new Date(instance.getTimeInMillis()); - - Object[] firstValues = new Object[] { 11, 11l, 180.1f, 11.1d, true, new byte[] { 1, 1 }, "seongmin", "park", - "seongmin@tobesoft.com", hireDate, new BigDecimal("10001"), new Object() }; - dsValuesList.add(firstValues); - - instance = Calendar.getInstance(); - instance.set(2009, Calendar.JANUARY, 2); - hireDate = new Date(instance.getTimeInMillis()); - Object[] secondValues = new Object[] { 12, 12l, 180.2f, 11.2d, false, new byte[] { 1, 2 }, "hyena", "lee", - "hyena@tobesoft.com", hireDate, new BigDecimal("10002"), new Object() }; - dsValuesList.add(secondValues); - - } - - private static void initDsDataTypes() { - dsDataTypes = new DataType[] { PlatformDataType.INT, PlatformDataType.LONG, PlatformDataType.FLOAT, - PlatformDataType.DOUBLE, PlatformDataType.BOOLEAN, PlatformDataType.BLOB, PlatformDataType.STRING, - PlatformDataType.STRING, PlatformDataType.STRING, PlatformDataType.DATE_TIME, - PlatformDataType.BIG_DECIMAL, PlatformDataType.UNDEFINED }; - } - - public static List createDefaultBeans() { - - List beanList = new ArrayList(); - - for (Object[] value : dsValuesList) { - - NexacroBeanWrapper wrapper = NexacroBeanWrapper.createBeanWrapper(DefaultBean.class); - for (int i=0; i> createDefaultMaps() { - - List> mapList = new ArrayList>(); - - for (Object[] value : dsValuesList) { - - Map dataMap = new HashMap(); - - for (int i=0; i beanList) { - - if (beanList == null) { - Assert.fail("bean list is null"); - } - - int expectedSize = dsValuesList.size(); - int actualSize = beanList.size(); - Assert.assertEquals("bean list size does not matched. please check converted columns. expected=" + expectedSize - + ", actual=" + actualSize, expectedSize, actualSize); - - for (int rowIndex = 0; rowIndex < actualSize; rowIndex++) { - DefaultBean defaultBean = beanList.get(rowIndex); - NexacroBeanWrapper beanWrapper = NexacroBeanWrapper.createBeanWrapper(defaultBean); - - NexacroBeanProperty[] properties = beanWrapper.getProperties(); - if (dsPropertyNames.length != properties.length) { - Assert.fail("bean properties does not matched. please check converted columns. expected=" - + dsPropertyNames.length + ", actual=" + properties.length); - } - - Object[] values = dsValuesList.get(rowIndex); - for (int propertyIndex = 0; propertyIndex < dsPropertyNames.length; propertyIndex++) { - NexacroBeanProperty property = beanWrapper.getProperty(dsPropertyNames[propertyIndex]); - - Object expectedValue = values[propertyIndex]; - Object actualValue = beanWrapper.getPropertyValue(dsPropertyNames[propertyIndex]); - - Assert.assertEquals("row(" + rowIndex + ") property(" + dsPropertyNames[propertyIndex] - + ") value does not matched. expected=" + expectedValue + ", actual=" + actualValue, - expectedValue, actualValue); - } - - } - - } - - - - public static void compareDefaultDataSet(DataSet ds) { - - if (ds == null) { - Assert.fail("compare DataSet is null"); - } - - int expectedSize = dsPropertyNames.length; - int actualSize = ds.getColumnCount(); - Assert.assertEquals("DataSet column does not matched. please check converted columns. expected=" + expectedSize - + ", actual=" + actualSize, expectedSize, actualSize); - - // check column - for (int propertyIndex = 0; propertyIndex < dsPropertyNames.length; propertyIndex++) { - - String propertyName = dsPropertyNames[propertyIndex]; - ColumnHeader column = ds.getColumn(propertyName); - - if (!propertyName.equals(column.getName())) { - Assert.fail("column name does not matched. expected=" + dsPropertyNames[propertyIndex] + ", autual=" - + column.getName()); - } - if (!dsDataTypes[propertyIndex].equals(column.getPlatformDataType())) { - Assert.fail(column.getName() + " data type does not matched. expected=" + dsDataTypes[propertyIndex] + ", autual=" - + column.getPlatformDataType()); - } - } - - // check values - int rowCount = ds.getRowCount(); - if (dsValuesList.size() != rowCount) { - Assert.fail("DataSet rows does not matched. expected=" + dsValuesList.size() + ", actual=" + rowCount); - } - - for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { - - Object[] values = dsValuesList.get(rowIndex); - for (int propertyIndex = 0; propertyIndex < dsPropertyNames.length; propertyIndex++) { - String propertyName = dsPropertyNames[propertyIndex]; - - Object expectedValue = values[propertyIndex]; - Object actualValue = ds.getObject(rowIndex, propertyName); - - Assert.assertEquals("row(" + rowIndex + ") property(" + dsPropertyNames[propertyIndex] - + ") value does not matched. expected=" + expectedValue + ", actual=" + actualValue, - expectedValue, actualValue); - } - } - - } - - public static void compareDefaultMaps(List> mapList) { - - if (mapList == null) { - Assert.fail("map list is null"); - } - - int size = mapList.size(); - if (dsValuesList.size() != size) { - Assert.fail("map list size does not matched. expected=" + dsValuesList.size() + ", actual=" + size); - } - - for (int rowIndex = 0; rowIndex < size; rowIndex++) { - Map dataMap = mapList.get(rowIndex); - - Set keySet = dataMap.keySet(); - - // row type이 추가되어 있다. - int expectedSize = dsPropertyNames.length + 1; - int actualSize = keySet.size(); - Assert.assertEquals("map properties does not matched. please check converted columns. expected=" - + expectedSize + ", actual=" + actualSize, expectedSize, actualSize); - - Object[] values = dsValuesList.get(rowIndex); - for (int propertyIndex = 0; propertyIndex < dsPropertyNames.length; propertyIndex++) { - - String propertyName = dsPropertyNames[propertyIndex]; - - Object expectedValue = values[propertyIndex]; - Object actualValue = dataMap.get(propertyName); - - Assert.assertEquals("row(" + rowIndex + ") property(" + dsPropertyNames[propertyIndex] - + ") value does not matched. expected=" + expectedValue + ", actual=" + actualValue, - expectedValue, actualValue); - } - - } - - } - - public static class StaticPropertyBean { - - private String name; - private static double commissionPercent; - - /** - * @return the name - */ - public String getName() { - return name; - } - - /** - * @param name - * the name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * @return the commissionPercent - */ - public static double getCommissionPercent() { - return commissionPercent; - } - - /** - * @param commissionPercent the commissionPercent to set - */ - public static void setCommissionPercent(double commissionPercent) { - StaticPropertyBean.commissionPercent = commissionPercent; - } - - } - - public static class DataSetRowTypeBean implements DataSetRowTypeAccessor { - - private String name; - private int rowType; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public int getRowType() { - return this.rowType; - } - - @Override - public void setRowType(int rowType) { - this.rowType = rowType; - } - - } - - public static class DataSetSavedDataBean implements DataSetSavedDataAccessor, DataSetRowTypeAccessor { - - private String name; - private int rowType; - - private DataSetSavedDataBean savedData; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public DataSetSavedDataBean getData() { - return this.savedData; - } - - @Override - public void setData(DataSetSavedDataBean savedData) { - this.savedData = savedData; - } - - @Override - public int getRowType() { - return this.rowType; - } - - @Override - public void setRowType(int rowType) { - this.rowType = rowType; - } - - } - -} +package com.nexacro.spring.data.support; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Assert; + +import com.nexacro.spring.data.DataSetRowTypeAccessor; +import com.nexacro.spring.data.DataSetSavedDataAccessor; +import com.nexacro.spring.data.support.bean.DefaultBean; +import com.nexacro.xapi.data.ColumnHeader; +import com.nexacro.xapi.data.DataSet; +import com.nexacro.xapi.data.datatype.DataType; +import com.nexacro.xapi.data.datatype.PlatformDataType; + +/** + * + * @author Park SeongMin + * @since 08.10.2015 + * @version 1.0 + * @see + */ +public abstract class NexacroTestUtil { + + private static String[] dsPropertyNames; + private static List dsValuesList; + private static DataType[] dsDataTypes; + + // private static List> dataMapList; + // private static Map dataTypeMap; + + private static Map supportedClassesValue; + + static { + initSupportedClassesValue(); + initDsPropertyNames(); + initDsValueList(); + initDsDataTypes(); + } + + static Map getSupportedClassesValue() { + return Collections.unmodifiableMap(supportedClassesValue); + } + + private static void initSupportedClassesValue() { + + supportedClassesValue = new HashMap(); + + Calendar instance = null; + Date hireDate = null; + instance = Calendar.getInstance(); + instance.set(2009, Calendar.JANUARY, 1); + hireDate = new Date(instance.getTimeInMillis()); + + supportedClassesValue.put(int.class, 11); + supportedClassesValue.put(Integer.class, new Integer(11)); + supportedClassesValue.put(long.class, 11l); + supportedClassesValue.put(Long.class, new Long(11l)); + supportedClassesValue.put(float.class, 180.1f); + supportedClassesValue.put(Float.class, new Float(180.1f)); + supportedClassesValue.put(double.class, 11.1d); + supportedClassesValue.put(Double.class, new Double(11.1d)); + supportedClassesValue.put(boolean.class, true); + supportedClassesValue.put(Boolean.class, Boolean.TRUE); + supportedClassesValue.put(byte[].class, new byte[] { 1, 1 }); + supportedClassesValue.put(Byte[].class, new Byte[] { 1, 1 }); + supportedClassesValue.put(String.class, "seongmin"); + supportedClassesValue.put(Date.class, hireDate); + supportedClassesValue.put(BigDecimal.class, new BigDecimal("10001")); + supportedClassesValue.put(Object.class, new Object()); + + } + + private static void initDsPropertyNames() { + + dsPropertyNames = new String[] { "employeeId", "access", "height", "commissionPercent", "male", "image", + "firstName", "lastName", "email", "hireDate", "salary", "obj" }; + + } + + private static void initDsValueList() { + + dsValuesList = new ArrayList(); + + Calendar instance = null; + Date hireDate = null; + instance = Calendar.getInstance(); + instance.set(2009, Calendar.JANUARY, 1); + hireDate = new Date(instance.getTimeInMillis()); + + Object[] firstValues = new Object[] { 11, 11l, 180.1f, 11.1d, true, new byte[] { 1, 1 }, "seongmin", "park", + "seongmin@tobesoft.com", hireDate, new BigDecimal("10001"), new Object() }; + dsValuesList.add(firstValues); + + instance = Calendar.getInstance(); + instance.set(2009, Calendar.JANUARY, 2); + hireDate = new Date(instance.getTimeInMillis()); + Object[] secondValues = new Object[] { 12, 12l, 180.2f, 11.2d, false, new byte[] { 1, 2 }, "hyena", "lee", + "hyena@tobesoft.com", hireDate, new BigDecimal("10002"), new Object() }; + dsValuesList.add(secondValues); + + } + + private static void initDsDataTypes() { + dsDataTypes = new DataType[] { PlatformDataType.INT, PlatformDataType.LONG, PlatformDataType.FLOAT, + PlatformDataType.DOUBLE, PlatformDataType.BOOLEAN, PlatformDataType.BLOB, PlatformDataType.STRING, + PlatformDataType.STRING, PlatformDataType.STRING, PlatformDataType.DATE_TIME, + PlatformDataType.BIG_DECIMAL, PlatformDataType.UNDEFINED }; + } + + public static List createDefaultBeans() { + + List beanList = new ArrayList(); + + for (Object[] value : dsValuesList) { + + NexacroBeanWrapper wrapper = NexacroBeanWrapper.createBeanWrapper(DefaultBean.class); + for (int i=0; i> createDefaultMaps() { + + List> mapList = new ArrayList>(); + + for (Object[] value : dsValuesList) { + + Map dataMap = new HashMap(); + + for (int i=0; i beanList) { + + if (beanList == null) { + Assert.fail("bean list is null"); + } + + int expectedSize = dsValuesList.size(); + int actualSize = beanList.size(); + Assert.assertEquals("bean list size does not matched. please check converted columns. expected=" + expectedSize + + ", actual=" + actualSize, expectedSize, actualSize); + + for (int rowIndex = 0; rowIndex < actualSize; rowIndex++) { + DefaultBean defaultBean = beanList.get(rowIndex); + NexacroBeanWrapper beanWrapper = NexacroBeanWrapper.createBeanWrapper(defaultBean); + + NexacroBeanProperty[] properties = beanWrapper.getProperties(); + if (dsPropertyNames.length != properties.length) { + Assert.fail("bean properties does not matched. please check converted columns. expected=" + + dsPropertyNames.length + ", actual=" + properties.length); + } + + Object[] values = dsValuesList.get(rowIndex); + for (int propertyIndex = 0; propertyIndex < dsPropertyNames.length; propertyIndex++) { + NexacroBeanProperty property = beanWrapper.getProperty(dsPropertyNames[propertyIndex]); + + Object expectedValue = values[propertyIndex]; + Object actualValue = beanWrapper.getPropertyValue(dsPropertyNames[propertyIndex]); + + Assert.assertEquals("row(" + rowIndex + ") property(" + dsPropertyNames[propertyIndex] + + ") value does not matched. expected=" + expectedValue + ", actual=" + actualValue, + expectedValue, actualValue); + } + + } + + } + + + + public static void compareDefaultDataSet(DataSet ds, int addedColumns) { + + if (ds == null) { + Assert.fail("compare DataSet is null"); + } + + int expectedSize = dsPropertyNames.length + addedColumns; + int actualSize = ds.getColumnCount(); + Assert.assertEquals("DataSet column does not matched. please check converted columns. expected=" + expectedSize + + ", actual=" + actualSize, expectedSize, actualSize); + + // check column + for (int propertyIndex = 0; propertyIndex < dsPropertyNames.length; propertyIndex++) { + + String propertyName = dsPropertyNames[propertyIndex]; + ColumnHeader column = ds.getColumn(propertyName); + + if (!propertyName.equals(column.getName())) { + Assert.fail("column name does not matched. expected=" + dsPropertyNames[propertyIndex] + ", autual=" + + column.getName()); + } + if (!dsDataTypes[propertyIndex].equals(column.getPlatformDataType())) { + Assert.fail(column.getName() + " data type does not matched. expected=" + dsDataTypes[propertyIndex] + ", autual=" + + column.getPlatformDataType()); + } + } + + // check values + int rowCount = ds.getRowCount(); + if (dsValuesList.size() != rowCount) { + Assert.fail("DataSet rows does not matched. expected=" + dsValuesList.size() + ", actual=" + rowCount); + } + + for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { + + Object[] values = dsValuesList.get(rowIndex); + for (int propertyIndex = 0; propertyIndex < dsPropertyNames.length; propertyIndex++) { + String propertyName = dsPropertyNames[propertyIndex]; + + Object expectedValue = values[propertyIndex]; + Object actualValue = ds.getObject(rowIndex, propertyName); + + Assert.assertEquals("row(" + rowIndex + ") property(" + dsPropertyNames[propertyIndex] + + ") value does not matched. expected=" + expectedValue + ", actual=" + actualValue, + expectedValue, actualValue); + } + } + + } + + public static void compareDefaultMaps(List> mapList) { + + if (mapList == null) { + Assert.fail("map list is null"); + } + + int size = mapList.size(); + if (dsValuesList.size() != size) { + Assert.fail("map list size does not matched. expected=" + dsValuesList.size() + ", actual=" + size); + } + + for (int rowIndex = 0; rowIndex < size; rowIndex++) { + Map dataMap = mapList.get(rowIndex); + + Set keySet = dataMap.keySet(); + + // row type이 추가되어 있다. + int expectedSize = dsPropertyNames.length + 1; + int actualSize = keySet.size(); + Assert.assertEquals("map properties does not matched. please check converted columns. expected=" + + expectedSize + ", actual=" + actualSize, expectedSize, actualSize); + + Object[] values = dsValuesList.get(rowIndex); + for (int propertyIndex = 0; propertyIndex < dsPropertyNames.length; propertyIndex++) { + + String propertyName = dsPropertyNames[propertyIndex]; + + Object expectedValue = values[propertyIndex]; + Object actualValue = dataMap.get(propertyName); + + Assert.assertEquals("row(" + rowIndex + ") property(" + dsPropertyNames[propertyIndex] + + ") value does not matched. expected=" + expectedValue + ", actual=" + actualValue, + expectedValue, actualValue); + } + + } + + } + + public static class StaticPropertyBean { + + private String name; + private static double commissionPercent; + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name + * the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the commissionPercent + */ + public static double getCommissionPercent() { + return commissionPercent; + } + + /** + * @param commissionPercent the commissionPercent to set + */ + public static void setCommissionPercent(double commissionPercent) { + StaticPropertyBean.commissionPercent = commissionPercent; + } + + } + + public static class DataSetRowTypeBean implements DataSetRowTypeAccessor { + + private String name; + private int rowType; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int getRowType() { + return this.rowType; + } + + @Override + public void setRowType(int rowType) { + this.rowType = rowType; + } + + } + + public static class DataSetSavedDataBean implements DataSetSavedDataAccessor, DataSetRowTypeAccessor { + + private String name; + private int rowType; + + private DataSetSavedDataBean savedData; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public DataSetSavedDataBean getData() { + return this.savedData; + } + + @Override + public void setData(DataSetSavedDataBean savedData) { + this.savedData = savedData; + } + + @Override + public int getRowType() { + return this.rowType; + } + + @Override + public void setRowType(int rowType) { + this.rowType = rowType; + } + + } + +} diff --git a/src/test/java/com/nexacro/spring/data/support/ObjectToDataSetConverterTest.java b/src/test/java/com/nexacro/spring/data/support/ObjectToDataSetConverterTest.java index e2c90c2..388ad14 100644 --- a/src/test/java/com/nexacro/spring/data/support/ObjectToDataSetConverterTest.java +++ b/src/test/java/com/nexacro/spring/data/support/ObjectToDataSetConverterTest.java @@ -19,6 +19,7 @@ import com.nexacro.spring.data.support.bean.UpperCaseBean; import com.nexacro.spring.util.ReflectionUtil; import com.nexacro.xapi.data.ColumnHeader; import com.nexacro.xapi.data.DataSet; +import com.nexacro.xapi.data.datatype.PlatformDataType; public class ObjectToDataSetConverterTest { @@ -113,6 +114,124 @@ public class ObjectToDataSetConverterTest { Assert.assertEquals("ds", dataset.getName()); } + @Test + public void testAllowChangeStructureWithSchemaDataSet() { + + DataSet schemaDataSet = new DataSet("schemaDa"); + schemaDataSet.addColumn("defaultCol1", PlatformDataType.STRING); + + Map map = new HashMap(); + map.put("col1", "value"); + + ConvertDefinition definition = new ConvertDefinition("ds"); + definition.setDisallowChangeStructure(false); // set allow structure change + definition.setSchemaDataSet(schemaDataSet); // set schema dataSet + + Object dsObj = null; + try { + dsObj = converter.convert(map, definition); + } catch (NexacroConvertException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + DataSet ds = (DataSet) dsObj; + Assert.assertTrue(schemaDataSet.equals(ds)); + + Assert.assertTrue(ds.containsColumn("defaultCol1")); + Assert.assertTrue(ds.containsColumn("col1")); + + } + + @Test + public void testAllowChangeStructureWithSchemaDataSetWithBean() { + + DataSet schemaDataSet = new DataSet("schemaDa"); + schemaDataSet.addColumn("defaultCol1", PlatformDataType.STRING); + + DefaultBean defaultBean = new DefaultBean(); + defaultBean.setLastName("Kim"); + + ConvertDefinition definition = new ConvertDefinition("ds"); + definition.setDisallowChangeStructure(false); // set allow structure change + definition.setSchemaDataSet(schemaDataSet); // set schema dataSet + + Object dsObj = null; + try { + dsObj = converter.convert(defaultBean, definition); + } catch (NexacroConvertException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + + DataSet ds = (DataSet) dsObj; + Assert.assertTrue(schemaDataSet.equals(ds)); + Assert.assertTrue(ds.containsColumn("defaultCol1")); + + Assert.assertEquals("Kim", ds.getObject(0, "lastName")); + + } + + @Test + public void testDisallowChangeStructureWithSchemaDataSet() { + + DataSet schemaDataSet = new DataSet("schemaDa"); + schemaDataSet.addColumn("defaultCol1", PlatformDataType.STRING); + + Map map = new HashMap(); + map.put("col1", "value"); + + ConvertDefinition definition = new ConvertDefinition("ds"); + definition.setDisallowChangeStructure(true); // set disallow structure change + definition.setSchemaDataSet(schemaDataSet); // set schema dataSet + + Object dsObj = null; + try { + dsObj = converter.convert(map, definition); + } catch (NexacroConvertException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + DataSet ds = (DataSet) dsObj; + Assert.assertTrue(schemaDataSet.equals(ds)); + + Assert.assertTrue(ds.containsColumn("defaultCol1")); + Assert.assertFalse(ds.containsColumn("col1")); + Assert.assertFalse(ds.containsColumn("col2")); + + } + + @Test + public void testDisallowChangeStructureWithSchemaDataSetWithBean() { + + DataSet schemaDataSet = new DataSet("schemaDa"); + schemaDataSet.addColumn("defaultCol1", PlatformDataType.STRING); + + DefaultBean defaultBean = new DefaultBean(); + defaultBean.setLastName("Kim"); + + ConvertDefinition definition = new ConvertDefinition("ds"); + definition.setDisallowChangeStructure(true); // set disallow structure change + definition.setSchemaDataSet(schemaDataSet); // set schema dataSet + + Object dsObj = null; + try { + dsObj = converter.convert(defaultBean, definition); + } catch (NexacroConvertException e) { + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + DataSet ds = (DataSet) dsObj; + Assert.assertTrue(schemaDataSet.equals(ds)); + Assert.assertTrue(ds.containsColumn("defaultCol1")); + + Assert.assertEquals(ds.getColumnCount(), 1); + + } + @Test public void testUpperCase() {