001: package org.directwebremoting.convert;
002:
003: import java.lang.reflect.Method;
004: import java.math.BigDecimal;
005: import java.math.BigInteger;
006: import java.text.DateFormat;
007: import java.text.SimpleDateFormat;
008: import java.util.Date;
009: import java.util.Set;
010:
011: import javax.servlet.ServletContext;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015: import org.directwebremoting.AjaxFilterChain;
016: import org.directwebremoting.WebContextFactory;
017: import org.directwebremoting.convert.mapped.BeanEx;
018: import org.directwebremoting.convert.mapped.Hibernate2Ex;
019: import org.directwebremoting.convert.mapped.Hibernate3Ex;
020: import org.directwebremoting.convert.mapped.Hibernate3NestEx;
021: import org.directwebremoting.convert.mapped.Hibernate3sEx;
022: import org.directwebremoting.convert.mapped.ObjectEx;
023: import org.directwebremoting.convert.mapped.ObjectForceEx;
024: import org.directwebremoting.extend.ConverterManager;
025: import org.directwebremoting.extend.InboundContext;
026: import org.directwebremoting.extend.InboundVariable;
027: import org.directwebremoting.extend.MarshallException;
028: import org.directwebremoting.extend.OutboundContext;
029: import org.directwebremoting.extend.OutboundVariable;
030: import org.directwebremoting.hibernate.Database;
031: import org.directwebremoting.hibernate.H3SessionAjaxFilter;
032: import org.directwebremoting.util.SingletonContainer;
033: import org.hibernate.Session;
034: import org.hibernate.SessionFactory;
035: import org.hibernate.Transaction;
036: import org.hibernate.cfg.Configuration;
037: import org.junit.AfterClass;
038: import org.junit.Assert;
039: import org.junit.BeforeClass;
040: import org.junit.Ignore;
041: import org.junit.Test;
042:
043: /**
044: * The tests for the <code>PrimitiveConverter</code> class.
045: * @see PrimitiveConverter
046: * @author Joe Walker [joe at getahead dot ltd dot uk]
047: */
048: public class AllConverterTest {
049: public AllConverterTest() throws Exception {
050: }
051:
052: @BeforeClass
053: public static void setUp() throws Exception {
054: singletonContainer.engageThread();
055: }
056:
057: @AfterClass
058: public static void tearDown() throws Exception {
059: singletonContainer.disengageThread();
060: }
061:
062: @Test
063: public void nullConvert() throws Exception {
064: assertOutboundConversion(null, "null");
065: }
066:
067: @Test
068: public void booleanConvert() throws Exception {
069: assertInboundConversion("true", Boolean.class, Boolean.TRUE);
070: assertInboundConversion("tRuE", Boolean.class, Boolean.TRUE);
071: assertInboundConversion("false", Boolean.class, Boolean.FALSE);
072: assertInboundConversion("FALSE", Boolean.class, Boolean.FALSE);
073: assertInboundConversion("yes", Boolean.class, Boolean.FALSE);
074: assertInboundConversion("null", Boolean.class, Boolean.FALSE);
075: assertInboundConversion("", Boolean.class, null);
076: assertInboundConversion("true", Boolean.TYPE, Boolean.TRUE);
077: assertInboundConversion("tRuE", Boolean.TYPE, Boolean.TRUE);
078: assertInboundConversion("false", Boolean.TYPE, Boolean.FALSE);
079: assertInboundConversion("FALSE", Boolean.TYPE, Boolean.FALSE);
080: assertInboundConversion("yes", Boolean.TYPE, Boolean.FALSE);
081: assertInboundConversion("null", Boolean.TYPE, Boolean.FALSE);
082: assertInboundConversion("", Boolean.TYPE, Boolean.FALSE);
083:
084: assertOutboundConversion(Boolean.TRUE, "true");
085: assertOutboundConversion(Boolean.FALSE, "false");
086: }
087:
088: @Test
089: public void byteConvert() throws Exception {
090: assertInboundConversion("127", Byte.class, new Byte("127"));
091: assertInboundConversion("-128", Byte.class, new Byte("-128"));
092: assertInboundConversion("0", Byte.class, new Byte("0"));
093: assertInboundConversion("", Byte.class, null);
094: assertInboundConversion("127", Byte.TYPE, new Byte("127"));
095: assertInboundConversion("-128", Byte.TYPE, new Byte("-128"));
096: assertInboundConversion("0", Byte.TYPE, new Byte("0"));
097: assertInboundConversion("", Byte.TYPE, new Byte("0"));
098:
099: assertOutboundConversion(new Byte("127"), "127");
100: assertOutboundConversion(new Byte("-128"), "-128");
101: assertOutboundConversion(new Byte("0"), "0");
102:
103: assertInboundConversionFailure("128", Byte.class);
104: assertInboundConversionFailure("-129", Byte.class);
105: assertInboundConversionFailure("null", Byte.class);
106: assertInboundConversionFailure("128", Byte.TYPE);
107: assertInboundConversionFailure("-129", Byte.TYPE);
108: assertInboundConversionFailure("null", Byte.TYPE);
109: }
110:
111: @Test
112: public void shortConvert() throws Exception {
113: assertInboundConversion("-128", Short.class, new Short("-128"));
114: assertInboundConversion("0", Short.class, new Short("0"));
115: assertInboundConversion("", Short.class, null);
116: assertInboundConversion("-128", Short.TYPE, new Short("-128"));
117: assertInboundConversion("0", Short.TYPE, new Short("0"));
118: assertInboundConversion("", Short.TYPE, new Short("0"));
119:
120: assertOutboundConversion(new Short("-128"), "-128");
121: assertOutboundConversion(new Short("0"), "0");
122:
123: assertInboundConversionFailure("null", Short.class);
124: assertInboundConversionFailure("null", Short.TYPE);
125: }
126:
127: @Test
128: public void testIntegerConvert() throws Exception {
129: assertInboundConversion("-128", Integer.class, new Integer(
130: "-128"));
131: assertInboundConversion("0", Integer.class, new Integer("0"));
132: assertInboundConversion("", Integer.class, null);
133: assertInboundConversion("-128", Integer.TYPE, new Integer(
134: "-128"));
135: assertInboundConversion("0", Integer.TYPE, new Integer("0"));
136: assertInboundConversion("", Integer.TYPE, new Integer("0"));
137:
138: assertOutboundConversion(new Integer("-128"), "-128");
139: assertOutboundConversion(new Integer("0"), "0");
140:
141: assertInboundConversionFailure("null", Integer.class);
142: assertInboundConversionFailure("null", Integer.TYPE);
143: }
144:
145: @Test
146: public void longConvert() throws Exception {
147: assertInboundConversion("-128", Long.class, new Long("-128"));
148: assertInboundConversion("0", Long.class, new Long("0"));
149: assertInboundConversion("", Long.class, null);
150: assertInboundConversion("-128", Long.TYPE, new Long("-128"));
151: assertInboundConversion("0", Long.TYPE, new Long("0"));
152: assertInboundConversion("", Long.TYPE, new Long("0"));
153:
154: assertOutboundConversion(new Long("-128"), "-128");
155: assertOutboundConversion(new Long("0"), "0");
156:
157: assertInboundConversionFailure("null", Long.class);
158: assertInboundConversionFailure("null", Long.TYPE);
159: }
160:
161: @Test
162: public void floatConvert() throws Exception {
163: assertInboundConversion("-12.8", Float.class,
164: new Float("-12.8"));
165: assertInboundConversion("0", Float.class, new Float("0"));
166: assertInboundConversion("", Float.class, null);
167: assertInboundConversion("-12.8", Float.TYPE, new Float("-12.8"));
168: assertInboundConversion("0", Float.TYPE, new Float("0"));
169: assertInboundConversion("", Float.TYPE, new Float("0"));
170:
171: assertOutboundConversion(new Float("-12.8"), "-12.8");
172: assertOutboundConversion(new Float("0"), "0.0");
173:
174: assertInboundConversionFailure("null", Float.class);
175: assertInboundConversionFailure("null", Float.TYPE);
176: }
177:
178: @Test
179: public void doubleConvert() throws Exception {
180: assertInboundConversion("-12.8", Double.class, new Double(
181: "-12.8"));
182: assertInboundConversion("0", Double.class, new Double("0"));
183: assertInboundConversion("", Double.class, null);
184: assertInboundConversion("-12.8", Double.TYPE, new Double(
185: "-12.8"));
186: assertInboundConversion("0", Double.TYPE, new Double("0"));
187: assertInboundConversion("", Double.TYPE, new Double("0"));
188:
189: assertOutboundConversion(new Double("-12.8"), "-12.8");
190: assertOutboundConversion(new Double("0"), "0.0");
191:
192: assertInboundConversionFailure("null", Double.class);
193: assertInboundConversionFailure("null", Double.TYPE);
194: }
195:
196: @Test
197: public void characterConvert() throws Exception {
198: assertInboundConversion("-", Character.class,
199: new Character('-'));
200: assertInboundConversion("0", Character.class,
201: new Character('0'));
202: assertInboundConversion("\"", Character.class, new Character(
203: '\"'));
204: assertInboundConversion("\'", Character.class, new Character(
205: '\''));
206: assertInboundConversion("\u0394", Character.class,
207: new Character('\u0394'));
208: assertInboundConversion("-", Character.TYPE, new Character('-'));
209: assertInboundConversion("0", Character.TYPE, new Character('0'));
210: assertInboundConversion("\"", Character.TYPE, new Character(
211: '\"'));
212: assertInboundConversion("\'", Character.TYPE, new Character(
213: '\''));
214: assertInboundConversion("\u0394", Character.TYPE,
215: new Character('\u0394'));
216:
217: assertOutboundConversion(new Character('-'), "\"-\"");
218: assertOutboundConversion(new Character('0'), "\"0\"");
219: assertOutboundConversion(new Character('\"'), "\"\\\"\"");
220: assertOutboundConversion(new Character('\''), "\"\\'\"");
221: assertOutboundConversion(new Character('\u0394'), "\"\\u0394\"");
222: assertOutboundConversion(new Character('-'), "\"-\"");
223: assertOutboundConversion(new Character('0'), "\"0\"");
224: assertOutboundConversion(new Character('\"'), "\"\\\"\"");
225: assertOutboundConversion(new Character('\''), "\"\\'\"");
226: assertOutboundConversion(new Character('\u0394'), "\"\\u0394\"");
227:
228: assertInboundConversionFailure("", Character.class);
229: assertInboundConversionFailure("", Character.TYPE);
230: assertInboundConversionFailure("null", Character.class);
231: assertInboundConversionFailure("null", Character.TYPE);
232: }
233:
234: @Test
235: public void stringConvert() throws Exception {
236: assertInboundConversion("-", String.class, "-");
237: assertInboundConversion("0", String.class, "0");
238: assertInboundConversion("\"", String.class, "\"");
239: assertInboundConversion("\'", String.class, "\'");
240: assertInboundConversion("\u0394", String.class, "\u0394");
241: assertInboundConversion("", String.class, "");
242: assertInboundConversion("null", String.class, "null");
243:
244: assertOutboundConversion("-", "\"-\"");
245: assertOutboundConversion("0", "\"0\"");
246: assertOutboundConversion("\"", "\"\\\"\"");
247: assertOutboundConversion("\'", "\"\\'\"");
248: assertOutboundConversion("\u0394", "\"\\u0394\"");
249: assertOutboundConversion("", "\"\"");
250: assertOutboundConversion("null", "\"null\"");
251: }
252:
253: @Test
254: public void bigIntegerConvert() throws Exception {
255: assertInboundConversion("-12", BigInteger.class,
256: new BigInteger("-12"));
257: assertInboundConversion("0", BigInteger.class, new BigInteger(
258: "0"));
259: assertInboundConversion("", BigInteger.class, null);
260:
261: assertOutboundConversion(new BigInteger("-12"), "-12");
262: assertOutboundConversion(new BigInteger("0"), "0");
263:
264: assertInboundConversionFailure("null", BigInteger.class);
265: }
266:
267: @Test
268: public void bigDecimalConvert() throws Exception {
269: assertInboundConversion("-12", BigDecimal.class,
270: new BigDecimal("-12"));
271: assertInboundConversion("0", BigDecimal.class, new BigDecimal(
272: "0"));
273: assertInboundConversion("", BigDecimal.class, null);
274:
275: assertOutboundConversion(new BigDecimal("-12"), "-12");
276: assertOutboundConversion(new BigDecimal("0"), "0");
277:
278: assertInboundConversionFailure("null", BigDecimal.class);
279: }
280:
281: @Test
282: public void dateConvert() throws Exception {
283: assertInboundConversion("1104537600000", Date.class, testDate);
284: assertInboundConversion("null", Date.class, null);
285: assertInboundConversion("1104537600000", java.sql.Date.class,
286: testDate);
287: assertInboundConversion("null", java.sql.Date.class, null);
288:
289: assertOutboundConversion(testDate, "new Date(1104537600000)");
290: }
291:
292: @Test
293: public void beanConvert() throws Exception {
294: assertInboundConversion("null", BeanEx.class, null);
295: assertInboundConversion("{ }", BeanEx.class, new BeanEx());
296: assertInboundConversion("{ name:string:fred }", BeanEx.class,
297: new BeanEx("fred"));
298: assertInboundConversion("{ wrong:string:fred }", BeanEx.class,
299: new BeanEx());
300:
301: assertOutboundConversion(new BeanEx(), "{name:null}");
302: assertOutboundConversion(new BeanEx("fred"), "{name:\"fred\"}");
303: assertOutboundConversion(new BeanEx(), "{name:null}");
304: }
305:
306: @Test
307: public void objectConvert() throws Exception {
308: assertInboundConversion("null", ObjectEx.class, null);
309: assertInboundConversion("{ }", ObjectEx.class, new ObjectEx());
310: assertInboundConversion("{ name:string:fred }", ObjectEx.class,
311: new ObjectEx("fred"));
312: assertInboundConversion("{ wrong:string:fred }",
313: ObjectEx.class, new ObjectEx());
314: assertInboundConversion("{ hidden:string:fred }",
315: ObjectEx.class, new ObjectEx());
316: assertInboundConversion("null", ObjectForceEx.class, null);
317: assertInboundConversion("{ }", ObjectForceEx.class,
318: new ObjectForceEx());
319: assertInboundConversion("{ name:string:fred }",
320: ObjectForceEx.class, new ObjectForceEx("fred"));
321: assertInboundConversion("{ wrong:string:fred }",
322: ObjectForceEx.class, new ObjectForceEx());
323:
324: assertOutboundConversion(new ObjectEx(), "{name:null}");
325: assertOutboundConversion(new ObjectEx("fred"),
326: "{name:\"fred\"}");
327: assertOutboundConversion(new ObjectEx(), "{name:null}");
328: assertOutboundConversion(new ObjectEx(), "{name:null}");
329: assertOutboundConversion(new ObjectForceEx(), "{name:null}");
330: assertOutboundConversion(new ObjectForceEx("fred"),
331: "{name:\"fred\"}");
332: assertOutboundConversion(new ObjectForceEx(), "{name:null}");
333: }
334:
335: @Test
336: public void hibernateInit() throws Exception {
337: Database.init();
338:
339: Configuration config = new Configuration();
340: config.configure("hibernate3.cfg.xml");
341: SessionFactory sessionFactory = config.buildSessionFactory();
342: Session session = sessionFactory.getCurrentSession();
343: Transaction transaction = session.beginTransaction();
344:
345: // Filter-time code (H3SessionAjaxFilter)
346:
347: // Run-time code. Probably won't use HibernateUtil2
348: Hibernate3Ex parent = (Hibernate3Ex) session.load(
349: Hibernate3Ex.class, 1);
350:
351: // Some random checks
352: Assert.assertEquals("fred", parent.getName());
353: Set<Hibernate3NestEx> children = parent.getChildren();
354: Assert.assertEquals(1, children.size());
355: Hibernate3NestEx child = children.iterator().next();
356: Assert.assertEquals("jim", child.getName());
357: Assert.assertEquals(parent, child.getOwner());
358:
359: // Filter-time code (H3SessionAjaxFilter)
360: transaction.commit();
361:
362: // Shutdown code, when do we need to do this?
363: sessionFactory.close();
364: }
365:
366: @Test
367: public void hibernateBasicsConvert() throws Exception {
368: // Checks that do not need DB access
369: assertInboundConversion("null", Hibernate3Ex.class, null);
370: assertInboundConversion("{ }", Hibernate3Ex.class,
371: new Hibernate3Ex());
372: assertInboundConversion("{ id:int:1,name:string:fred }",
373: Hibernate3Ex.class, new Hibernate3Ex(1, "fred"));
374:
375: assertOutboundConversion(new Hibernate3Ex(),
376: "var s0=[];{children:s0,id:null,name:null}");
377: }
378:
379: @Test
380: public void hibernate3sConvert() throws Exception {
381: Database.init();
382:
383: // Hibernate 3 setup, keep the session open and use the bean converter
384: Configuration config = new Configuration();
385: config.configure("hibernate3.cfg.xml");
386: SessionFactory sessionFactory = config.buildSessionFactory();
387: Session session = sessionFactory.getCurrentSession();
388:
389: ServletContext servletContext = WebContextFactory.get()
390: .getServletContext();
391: H3SessionAjaxFilter.setSessionFactory(servletContext,
392: sessionFactory);
393:
394: Transaction transaction = session.beginTransaction();
395: final Hibernate3sEx parent = (Hibernate3sEx) session.load(
396: Hibernate3sEx.class, 1);
397: parent.getId();
398: parent.getName();
399: //parent.getChildren();
400:
401: transaction.commit();
402:
403: H3SessionAjaxFilter filter = new H3SessionAjaxFilter();
404: filter.doFilter(null, null, null, new AjaxFilterChain() {
405: public Object doFilter(Object obj, Method method,
406: Object[] params) throws Exception {
407: // This is way to fragile, but it will do for now
408: assertOutboundConversion(
409: parent,
410: "var s0=[];var s1={};s0[0]=s1;s1.id=2;s1.name=\"jim\";s1.owner=null;{children:s0,id:1,name:\"fred\"}");
411: return null;
412: }
413: });
414:
415: sessionFactory.close();
416: }
417:
418: @Test
419: public void hibernate3Convert() throws Exception {
420: Database.init();
421:
422: // Hibernate 3 setup, close the session and use the h3 converter
423: Configuration config = new Configuration();
424: config.configure("hibernate3.cfg.xml");
425: SessionFactory sessionFactory = config.buildSessionFactory();
426: Session session = sessionFactory.getCurrentSession();
427: Transaction transaction = session.beginTransaction();
428:
429: Hibernate3Ex parent = (Hibernate3Ex) session.load(
430: Hibernate3Ex.class, 1);
431: parent.getId();
432: parent.getName();
433: //parent.getChildren();
434:
435: transaction.commit();
436: sessionFactory.close();
437:
438: // This is way to fragile, but it will do for now
439: assertOutboundConversion(parent,
440: "{children:null,id:1,name:\"fred\"}");
441: }
442:
443: @Ignore("Hibernate 2 appears broken")
444: @Test
445: public void hibernate2Convert() throws Exception {
446: Database.init();
447:
448: // Hibernate 2 setup, close the session and use the h2 converter
449: net.sf.hibernate.cfg.Configuration config = new net.sf.hibernate.cfg.Configuration();
450: config.configure(getClass().getResource("/hibernate2.cfg.xml"));
451: net.sf.hibernate.SessionFactory sessionFactory = config
452: .buildSessionFactory();
453: net.sf.hibernate.Session session = sessionFactory.openSession();
454: net.sf.hibernate.Transaction transaction = session
455: .beginTransaction();
456:
457: Hibernate2Ex parent = (Hibernate2Ex) session.load(
458: Hibernate2Ex.class, 1);
459: // This is way to fragile, but it will do for now
460: assertOutboundConversion(
461: parent,
462: "var s0={};var s1=[];var s2={};s0.children=s1;s0.id=1;s0.name=\"fred\";s1[0]=s2;s2.id=2;s2.name=\"jim\";s2.owner=s0;s0");
463:
464: transaction.commit();
465: sessionFactory.close();
466: }
467:
468: /*
469: @Test
470: public void convert() throws Exception
471: {
472: }
473: */
474:
475: protected void assertInboundConversion(String input,
476: Class<?> convertTo, Object expected) {
477: ConverterManager converterManager = singletonContainer
478: .getConverterManager();
479: InboundContext ctx = new InboundContext();
480:
481: String explanation = "Convert \"" + input + "\" to "
482: + convertTo.getName();
483:
484: boolean isException = expected instanceof Class
485: && (Exception.class
486: .isAssignableFrom((Class<?>) expected));
487:
488: if (isException) {
489: try {
490: new InboundVariable(ctx, null, "type", input);
491: Assert.fail();
492: } catch (Exception ex) {
493: Assert.assertEquals(explanation, ex.getClass(),
494: expected);
495: }
496: } else {
497: try {
498: InboundVariable iv = new InboundVariable(ctx, null,
499: "type", input);
500: Object result = converterManager.convertInbound(
501: convertTo, iv, ctx, null);
502: Assert.assertEquals(explanation, result, expected);
503: } catch (Exception ex) {
504: log.error(explanation, ex);
505: Assert.fail(explanation);
506: }
507: }
508: }
509:
510: protected void assertInboundConversionFailure(String input,
511: Class<?> convertTo) {
512: ConverterManager converterManager = singletonContainer
513: .getConverterManager();
514: InboundContext ctx = new InboundContext();
515:
516: String explanation = "Convert \"" + input + "\" to "
517: + convertTo.getSimpleName();
518:
519: try {
520: InboundVariable iv = new InboundVariable(ctx, null, "type",
521: input);
522: converterManager.convertInbound(convertTo, iv, ctx, null);
523: Assert.fail();
524: } catch (Exception ex) {
525: Assert.assertEquals(explanation, ex.getClass(),
526: MarshallException.class);
527: }
528: }
529:
530: protected void assertOutboundConversion(Object input,
531: String expected) throws MarshallException {
532: ConverterManager converterManager = singletonContainer
533: .getConverterManager();
534: OutboundContext ctx = new OutboundContext(false);
535:
536: OutboundVariable result = converterManager.convertOutbound(
537: input, ctx);
538:
539: Assert.assertNotNull(result);
540:
541: String script = result.getDeclareCode() + result.getBuildCode()
542: + result.getAssignCode();
543: script = script.replace("\r", "");
544: script = script.replace("\n", "");
545:
546: Assert.assertEquals(expected, script);
547: }
548:
549: private static final Log log = LogFactory
550: .getLog(AllConverterTest.class);
551:
552: private static SingletonContainer singletonContainer;
553: private static final DateFormat format = new SimpleDateFormat(
554: "dd-MM-yyyy");
555: private static Date testDate;
556: static {
557: try {
558: testDate = format.parse("01-01-2005");
559: singletonContainer = new SingletonContainer();
560: } catch (Exception ex) {
561: log.error("init failure", ex);
562: }
563: }
564: }
|