package test.com.fp.json; import java.io.Serializable; import java.math.BigDecimal; import java.sql.Date; import java.sql.Timestamp; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import junit.framework.TestCase; import com.fp.dto.fin.FinancialRequest; import com.fp.dto.json.Serializer; import com.fp.dto.save.SaveRequest; public class SerializerTest extends TestCase { @SuppressWarnings("unchecked") public void compareList(List pExtected, List pData) { TestCase.assertEquals(pExtected.size(), pData.size()); for (int i = 0; i < pExtected.size(); i++) { this.compareTestingBean((TestingBean) pExtected.get(i), (TestingBean) pData.get(i)); } } public void compareTestingBean(TestingBean pExtected, TestingBean pData) { TestCase.assertEquals(pExtected.getEntero(), pData.getEntero()); TestCase.assertEquals(pExtected.getString(), pData.getString()); TestCase.assertEquals(pExtected.getAmount(), pData.getAmount()); TestCase.assertEquals(pExtected.getDate(), pData.getDate()); TestCase.assertEquals(pExtected.getTimestamp(), pData.getTimestamp()); TestCase.assertEquals(pExtected.isFlag(), pData.isFlag()); TestCase.assertEquals(pExtected.getTransportBeanClass(), pData.getTransportBeanClass()); } public void testBean2JSON() throws Exception { long time = 1228088753000L; long date = 1228021200000L; TestingBean bean = new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true); Serializer ser = new Serializer(bean); System.out.println(ser.toJSON()); } public void testBean2JSON1() throws Exception { long time = 1228088753000L; long date = 1228021200000L; TestingBean bean = new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true); bean.setPk(new TestingBeanKey("Prueba", 21L)); Serializer ser = new Serializer(bean); System.out.println(">>>>>>>>>>>>>>>>>>>" + ser.toJSON()); System.out.println(">>>>>>>>>>>>>>>>>>>" + ser.toXML()); } public void testJson2Bean1() throws Exception { String json = "{\"amount\":12.34,\"timestamp\":\"2008-11-30 18:45:53\",\"flag\":true,\"pk_campo2\":21,\"string\":\"Value\",\"pk_campo1\":\"Prueba\",\"entero\":10,\"transportBeanClass\":\"test.com.fp.json.TestingBean\",\"date\":\"2008-11-30\",\"pk_transportBeanClass\":\"test.com.fp.json.TestingBeanKey\",\"modified\":false}"; Serializer ser = new Serializer(json); TestingBean b = (TestingBean) ser.toObject(TestingBean.class); System.out.println(b); } public void testQueryRequest() throws Exception { FinancialRequest fr = new FinancialRequest(); fr.setBranchCode(1); fr.setUser("gf"); fr.setCreditAccount("aaaaa"); Serializer ser = new Serializer(fr); System.out.println(">>>>>>>>>>>>>>>>>>>" + ser.toJSON()); } public void testBean2JSONAdditional() throws Exception { long time = 1228088753000L; long date = 1228021200000L; TestingBean bean = new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true); bean.setPk(new TestingBeanKey("Prueba", 21L)); bean.addAddtionalInfo("add1", "Des1"); bean.addAddtionalInfo("add2", "Des2"); Serializer ser = new Serializer(bean); System.out.println(">>>>>>>>>>>>>>>>>>>" + ser.toJSON()); System.out.println(">>>>>>>>>>>>>>>>>>>" + ser.toXML()); ser = new Serializer(new TestingBean1("Prueba", 12, 13)); System.out.println(">>>>>>>>>>>>>>>>>>>" + ser.toJSON()); System.out.println(">>>>>>>>>>>>>>>>>>>" + ser.toXML()); } public void testBean2XML() throws Exception { long time = 1228088753000L; long date = 1228021200000L; TestingBean bean = new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true); Serializer ser = new Serializer(bean); System.out.println("Bean a XML"); System.out.println(ser.toXML()); } public void testJSON2Bean() throws Exception { long time = 1228088753000L; long date = 1228021200000L; TestingBean expected = new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true); String data = "{\"amount\":12.34,\"date\":\"2008-11-30\",\"entero\":10,\"flag\":true,\"string\":\"Value\",\"timestamp\":\"2008-11-30 18:45:53\",\"transportBeanClass\":\"test.com.fp.json.TestingBean\"}"; Serializer ser = new Serializer(data); TestingBean json = (TestingBean) ser.toObject(TestingBean.class); this.compareTestingBean(expected, json); } public void testJSON2Bean1() throws Exception { long time = 1228088753000L; long date = 1228021200000L; TestingBean expected = new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true); expected.setPk(new TestingBeanKey("Prueba", 21L)); String data = "{\"amount\":12.34,\"date\":\"2008-11-30\",\"deleted\":false,\"entero\":10,\"flag\":true,\"modified\":false,\"pk\":{\"campo1\":\"Prueba\",\"campo2\":21},\"string\":\"Value\",\"timestamp\":\"2008-11-30 18:45:53\",\"transportBeanClass\":\"test.com.fp.json.TestingBean\"}"; Serializer ser = new Serializer(data); TestingBean json = (TestingBean) ser.toObject(TestingBean.class); this.compareTestingBean(expected, json); } @SuppressWarnings("unchecked") public void testJSON2Map() throws Exception { Map expected = new HashMap(); expected.put("booleano", true); expected.put("int", 10); expected.put("string", "Valor"); expected.put("monto", new BigDecimal("1200.43")); long time = 1228088753000L; long date = 1228021200000L; expected.put("date", new Date(date)); expected.put("timestamp", new Timestamp(time)); String data = "{\"monto\":1200.43,\"string\":\"Valor\",\"int\":10,\"timestamp\":\"2008-11-30 18:45:53\",\"date\":\"2008-11-30\",\"booleano\":true}"; Serializer ser = new Serializer(data); Map json = (Map) ser.toObject(Map.class); Set keys = expected.keySet(); for (String key : keys) { Object ex = expected.get(key); Object obj = json.get(key); TestCase.assertEquals(ex.getClass(), obj.getClass()); TestCase.assertEquals(ex, obj); } } @SuppressWarnings("unchecked") public void testJSON2MapBean() throws Exception { Map expected = new HashMap(); expected.put("booleano", true); expected.put("int", 10); expected.put("string", "Valor"); expected.put("monto", new BigDecimal("1200.43")); long time = 1228088753000L; long date = 1228021200000L; expected.put("date", new Date(date)); expected.put("timestamp", new Timestamp(time)); expected.put("bean", new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true)); String data = "{\"monto\":1200.43,\"string\":\"Valor\",\"monto1\":1200,\"int\":10,\"timestamp\":\"2008-11-30 18:45:53\",\"date\":\"2008-11-30\",\"bean\":{\"amount\":12.34,\"date\":\"2008-11-30\",\"entero\":10,\"flag\":true,\"string\":\"Value\",\"timestamp\":\"2008-11-30 18:45:53\",\"transportBeanClass\":\"test.com.fp.json.TestingBean\"},\"booleano\":true}"; Serializer ser = new Serializer(data); Map json = (Map) ser.toObject(Map.class); Set keys = expected.keySet(); for (String key : keys) { Object ex = expected.get(key); Object obj = json.get(key); TestCase.assertEquals(ex.getClass(), obj.getClass()); if (ex instanceof TestingBean) { this.compareTestingBean((TestingBean) ex, (TestingBean) obj); } else { TestCase.assertEquals(ex, obj); } } } @SuppressWarnings("unchecked") public void testJSON2MapBeanArray() throws Exception { Map expected = new HashMap(); expected.put("booleano", true); expected.put("int", 10); expected.put("string", "Valor"); expected.put("monto", new BigDecimal("1200.43")); long time = 1228088753000L; long date = 1228021200000L; expected.put("date", new Date(date)); expected.put("timestamp", new Timestamp(time)); List arr = new ArrayList(); arr.add(new TestingBean(new Date(date), new Timestamp(time), "Value1", 10, new BigDecimal("12.34"), true)); arr.add(new TestingBean(new Date(date), new Timestamp(time), "Value1", 10, new BigDecimal("111.34"), false)); expected.put("bean", arr); String data = "{\"monto\":1200.43,\"string\":\"Valor\",\"int\":10,\"timestamp\":\"2008-11-30 18:45:53\",\"date\":\"2008-11-30\",\"bean\":[{\"amount\":12.34,\"date\":\"2008-11-30\",\"entero\":10,\"flag\":true,\"string\":\"Value1\",\"timestamp\":\"2008-11-30 18:45:53\",\"transportBeanClass\":\"test.com.fp.json.TestingBean\"},{\"amount\":111.34,\"date\":\"2008-11-30\",\"entero\":10,\"flag\":false,\"string\":\"Value1\",\"timestamp\":\"2008-11-30 18:45:53\",\"transportBeanClass\":\"test.com.fp.json.TestingBean\"}],\"booleano\":true}"; Serializer ser = new Serializer(data); Map json = (Map) ser.toObject(Map.class); Set keys = expected.keySet(); for (String key : keys) { Object ex = expected.get(key); Object obj = json.get(key); if (ex instanceof List) { compareList((List) ex, (List) obj); } else { TestCase.assertEquals("" + ex + "<-->" + obj, ex.getClass(), obj.getClass()); TestCase.assertEquals("" + ex.getClass().getName() + "<-->" + obj.getClass().getName(), ex, obj); } } } public void testMap2JSON() throws Exception { Map data = new HashMap(); data.put("booleano", true); data.put("int", 10); data.put("string", "Valor"); data.put("monto", new BigDecimal("1200.43")); data.put("monto1", new BigDecimal("1200")); long time = 1228088753000L; long date = 1228021200000L; data.put("date", new Date(date)); data.put("timestamp", new Timestamp(time)); Serializable sData = (Serializable) data; Serializer ser = new Serializer(sData); System.out.println(ser.toJSON()); } public void testMap2XML() throws Exception { Map data = new HashMap(); data.put("booleano", true); data.put("int", 10); data.put("string", "Valor"); data.put("monto", new BigDecimal("1200.43")); data.put("monto1", new BigDecimal("1200")); long time = 1228088753000L; long date = 1228021200000L; data.put("date", new Date(date)); data.put("timestamp", new Timestamp(time)); Serializable sData = (Serializable) data; Serializer ser = new Serializer(sData); System.out.println("MAP a XML"); System.out.println(ser.toXML()); // String expected = // "\ntrue2008-11-30101200.431200Valor2008-11-30 18:45:53"; } public void testMapBean2JSON() throws Exception { Map data = new HashMap(); data.put("booleano", true); data.put("int", 10); data.put("string", "Valor"); data.put("monto", new BigDecimal("1200.43")); data.put("monto1", new BigDecimal("1200")); long time = 1228088753000L; long date = 1228021200000L; data.put("date", new Date(date)); data.put("timestamp", new Timestamp(time)); data.put("bean", new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true)); Serializable sData = (Serializable) data; Serializer ser = new Serializer(sData); System.out.println(ser.toJSON()); } public void testMapBean2XML() throws Exception { Map data = new HashMap(); data.put("booleano", true); data.put("int", 10); data.put("string", "Valor"); data.put("monto", new BigDecimal("1200.43")); data.put("monto1", new BigDecimal("1200")); long time = 1228088753000L; long date = 1228021200000L; data.put("date", new Date(date)); data.put("timestamp", new Timestamp(time)); data.put("bean", new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true)); Serializable sData = (Serializable) data; Serializer ser = new Serializer(sData); System.out.println("Map Bean a XML"); System.out.println(ser.toXML()); } public void testMapBeanArray2JSON() throws Exception { Map data = new HashMap(); data.put("booleano", true); data.put("int", 10); data.put("string", "Valor"); data.put("monto", new BigDecimal("1200.43")); data.put("monto1", new BigDecimal("1200")); long time = 1228088753000L; long date = 1228021200000L; data.put("date", new Date(date)); data.put("timestamp", new Timestamp(time)); List arr = new ArrayList(); arr.add(new TestingBean(new Date(date), new Timestamp(time), "Value1", 10, new BigDecimal("12.34"), true)); arr.add(new TestingBean(new Date(date), new Timestamp(time), "Value1", 10, new BigDecimal("111.34"), false)); data.put("bean", arr); Serializable sData = (Serializable) data; Serializer ser = new Serializer(sData); System.out.println(ser.toJSON()); } public void testMapBeanArray2XML() throws Exception { Map data = new HashMap(); data.put("booleano", true); data.put("int", 10); data.put("string", "Valor"); data.put("monto", new BigDecimal("1200.43")); data.put("monto1", new BigDecimal("1200")); long time = 1228088753000L; long date = 1228021200000L; data.put("date", new Date(date)); data.put("timestamp", new Timestamp(time)); List arr = new ArrayList(); arr.add(new TestingBean(new Date(date), new Timestamp(time), "Value1", 10, new BigDecimal("12.34"), true)); arr.add(new TestingBean(new Date(date), new Timestamp(time), "Value1", 10, new BigDecimal("111.34"), false)); data.put("bean", arr); Serializable sData = (Serializable) data; Serializer ser = new Serializer(sData); System.out.println("MAP ARRAY"); System.out.println(ser.toXML()); } public void testXML2Bean() throws Exception { long time = 1228088753000L; long date = 1228021200000L; TestingBean expected = new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true); String data = "12.342008-11-3010trueValue2008-11-30 18:45:53test.com.fp.json.TestingBean"; Serializer ser = new Serializer(data, true); TestingBean json = (TestingBean) ser.toObject(TestingBean.class); this.compareTestingBean(expected, json); } public void testXML2Bean1() throws Exception { long time = 1228088753000L; long date = 1228021200000L; TestingBean expected = new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true); expected.setPk(new TestingBeanKey("Prueba", 21L)); String data = " 12.342008-11-30false10truefalsePrueba21Value2008-11-30 18:45:53test.com.fp.json.TestingBean"; Serializer ser = new Serializer(data, true); TestingBean json = (TestingBean) ser.toObject(TestingBean.class); this.compareTestingBean(expected, json); } @SuppressWarnings("unchecked") public void testXML2Map() throws Exception { Map expected = new HashMap(); expected.put("booleano", true); expected.put("int", 10); expected.put("string", "Valor"); expected.put("monto", new BigDecimal("1200.43")); long time = 1228088753000L; long date = 1228021200000L; expected.put("date", new Date(date)); expected.put("timestamp", new Timestamp(time)); String data = "\ntrue2008-11-30101200.431200Valor2008-11-30 18:45:53"; Serializer ser = new Serializer(data, true); Map json = (Map) ser.toObject(Map.class); Set keys = expected.keySet(); for (String key : keys) { Object ex = expected.get(key); Object obj = json.get(key); TestCase.assertEquals(ex.getClass(), obj.getClass()); TestCase.assertEquals(ex, obj); } } @SuppressWarnings("unchecked") public void testXML2MapBean() throws Exception { Map expected = new HashMap(); expected.put("booleano", true); expected.put("int", 10); expected.put("string", "Valor"); expected.put("monto", new BigDecimal("1200.43")); long time = 1228088753000L; long date = 1228021200000L; expected.put("date", new Date(date)); expected.put("timestamp", new Timestamp(time)); expected.put("bean", new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true)); String data = "12.342008-11-3010trueValue2008-11-30 18:45:53test.com.fp.json.TestingBeantrue2008-11-30101200.431200Valor2008-11-30 18:45:53"; Serializer ser = new Serializer(data, true); Map json = (Map) ser.toObject(Map.class); Set keys = expected.keySet(); for (String key : keys) { Object ex = expected.get(key); Object obj = json.get(key); TestCase.assertEquals(ex.getClass(), obj.getClass()); if (ex instanceof TestingBean) { this.compareTestingBean((TestingBean) ex, (TestingBean) obj); } else { TestCase.assertEquals(ex, obj); } } } /* * public void testFinancial() throws Exception{ try{ String * xml=StreamHelper.readFile("/home/gfiallos/temp/flip/a.xml"); Serializer ser=new Serializer(xml, true); * FinancialRequest fr=(FinancialRequest)ser.toObject(FinancialRequest.class); Serializer ser1=new Serializer(fr); * System.out.print(">>>"+ser1.toJSON()); }catch(Exception e){ e.printStackTrace(); } } */ /* * private void testBeanPersistenteToJSON() throws Exception{ SaveRequest sq = new SaveRequest(); long time = * 1228088753000L; long date = 1228021200000L; TestingBean expected = new TestingBean(new Date(date), new * Timestamp(time), "Value", 10, new BigDecimal("12.34"), true); sq.putSaveBean("A", expected); Serializer ser = new * Serializer(sq); String dataJSON=ser.toJSON(); String dataExpected= * "{\"officeCode\":null,\"journalId\":null,\"channelCode\":null,\"authorizedUser\":null,\"A\":{\"deletedObject\":null,\"beanName\":\"A\",\"automatic\":true,\"modifiedObject\":{\"amount\":12.34,\"timestamp\":\"2008-11-30 18:45:53\",\"flag\":true,\"string\":\"Value\",\"entero\":10,\"transportBeanClass\":\"test.com.fp.json.TestingBean\",\"date\":\"2008-11-30\",\"modified\":false,\"pk\":null},\"modifiedRecords\":[],\"transportBeanClass\":\"com.fp.dto.save.SaveBean\",\"deletedRecords\":[],\"modified\":false},\"transactionCode\":null,\"transactionVersion\":null,\"internalUserCode\":null,\"branchCode\":null,\"lOrderPersistentBeans\":[\"A\"],\"modified\":false,\"terminalCode\":null,\"company\":null,\"transactionModule\":null,\"language\":null,\"transportBeanClass\":\"com.fp.dto.save.SaveRequest\",\"user\":null,\"profile\":null}" * ; System.out.println("BeanPersistenteToJSON "+dataJSON); TestCase.assertEquals(dataExpected, dataJSON); * * } */ public void testBeanPersistente() throws Exception { String json = "{\"officeCode\":null,\"journalId\":null,\"channelCode\":null,\"authorizedUser\":null,\"A\":{\"deletedObject\":null,\"beanName\":\"A\",\"automatic\":true,\"modifiedObject\":{\"amount\":12.34,\"timestamp\":\"2008-11-30 18:45:53\",\"flag\":true,\"string\":\"Value\",\"entero\":10,\"transportBeanClass\":\"test.com.fp.json.TestingBean\",\"date\":\"2008-11-30\",\"pk\":null},\"modifiedRecords\":[],\"transportBeanClass\":\"com.fp.dto.save.SaveBean\",\"deletedRecords\":[]},\"transactionCode\":null,\"transactionVersion\":null,\"internalUserCode\":null,\"branchCode\":null,\"lOrderPersistentBeans\":[\"A\"],\"terminalCode\":null,\"company\":null,\"transactionModule\":null,\"language\":null,\"transportBeanClass\":\"com.fp.dto.save.SaveRequest\",\"user\":null,\"profile\":null}"; SaveRequest sq = new SaveRequest(); long time = 1228088753000L; long date = 1228021200000L; TestingBean expected = new TestingBean(new Date(date), new Timestamp(time), "Value", 10, new BigDecimal("12.34"), true); sq.putSaveBean("A", expected); Serializer ser = new Serializer(json); SaveRequest srData = (SaveRequest) ser.toObject(SaveRequest.class); TestingBean val = (TestingBean) srData.getSaveBeanModifiedObject("A"); TestCase.assertEquals(expected.toString(), val.toString()); } public void testFinancial1() throws Exception { try { String json = "{\"accountingVoucher\":false,\"newAccountOffice\":null,\"officeCode\":1,\"journalId\":null,\"authorizedUser\":null,\"transactionCode\":200,\"transactionVersion\":1,\"newSubProduct\":null,\"onLine\":true,\"document\":\"ABC\",\"area\":\"1\",\"terminalCode\":\"ter-jvc\",\"commentary\":null,\"originalTransactionModule\":null,\"transactionModule\":\"11\",\"accountingDate\":null,\"processDate\":null,\"profile\":1,\"reverse\":\"N\",\"journalIdReverse\":null,\"valueDate\":null,\"creditQuotaNumber\":null,\"channelCode\":\"PC\",\"depositCode\":null,\"financialItems\":[{\"account\":\"[]\",\"accountStatus\":\"[]\",\"accountingCode\":\"[]\",\"amount\":100,\"companyAccount\":0,\"creditType\":\"[]\",\"currecy\":\"USD\",\"destinyBranch\":0,\"destinyOffice\":0,\"expirationDate\":\"\",\"itemCode\":1,\"overdrawCatalog\":\"[]\",\"overdrawCatalogCode\":\"[]\",\"quotaNumber\":0,\"repeating\":false}],\"creditAccount\":null,\"settlement\":null,\"transactionMode\":\"N\",\"branchCode\":1,\"newAccountBranch\":null,\"modified\":false,\"debitAccount\":\"1110000001\",\"originalTransactionVersion\":null,\"company\":1,\"debitQuotaNumber\":null,\"workingDate\":null,\"language\":\"ES\",\"transportBeanClass\":\"com.fp.dto.fin.FinancialRequest\",\"originalTransactionCode\":null,\"user\":\"jvaca\"}"; Serializer ser = new Serializer(json); FinancialRequest fr = (FinancialRequest) ser.toObject(FinancialRequest.class); System.out.print(fr); } catch (Exception e) { e.printStackTrace(); } } }