1. opensymphony
  2. xwork

Commits

tmjee  committed 435a38a

Issue number: XW-372
Obtained from:
Submitted by:
Reviewed by:

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1010e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits c546f74
  • Branches master

Comments (0)

Files changed (2)

File src/java/com/opensymphony/xwork/util/XWorkBasicConverter.java

View file
  • Ignore whitespace
     private Class doConvertToClass(Object value) {
         Class clazz = null;
 
-        if (value instanceof String) {
+        if (value instanceof String && value != null && ((String)value).length() > 0) {
             try {
                 clazz = Class.forName((String) value);
             } catch (ClassNotFoundException e) {
     private Object doConvertToDate(Map context, Object value, Class toType) {
         Date result = null;
 
-        if (value instanceof String) {
+        if (value instanceof String && value != null && ((String)value).length() > 0) {
             String sa = (String) value;
             Locale locale = getLocale(context);
 
         } else if (Date.class.isAssignableFrom(value.getClass())) {
             result = (Date) value;
         }
-
         return result;
     }
 

File src/test/com/opensymphony/xwork/util/XWorkBasicConverterTest.java

View file
  • Ignore whitespace
+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.util;
+
+import java.util.Date;
+import java.util.HashMap;
+
+import com.opensymphony.xwork.XworkException;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for XWorkBasicConverter
+ * 
+ * @author tm_jee
+ * @version $Date$ $Id$
+ */
+public class XWorkBasicConverterTest extends TestCase {
+
+	public void test() {
+		// TODO: test for every possible conversion
+		// take into account of empty string
+		// primitive -> conversion error when empty string is passed
+		// object -> return null when empty string is passed
+	}
+	
+	
+	// TODO: more test will come soon !!!
+	
+	// TEST DATE CONVERSION:-
+	public void testDateConversionWithEmptyValue() {
+		XWorkBasicConverter basicConverter = new XWorkBasicConverter();
+		Object convertedObject = basicConverter.convertValue(new HashMap(), null, null, null, "", Date.class);
+		// we must not get XWorkException as that will caused a conversion error
+		assertNull(convertedObject); 
+	}
+	
+	public void testDateConversionWithInvalidValue() {
+		XWorkBasicConverter basicConverter = new XWorkBasicConverter();
+		try {
+			Object convertedObject = basicConverter.convertValue(new HashMap(), null, null, null, "asdsd", Date.class);
+		}
+		catch(XworkException e) {
+			// we MUST get this exception as this is a conversion error
+			assertTrue(true);
+			return;
+		}
+		fail("XWorkException expected - conversion error occurred");
+	}
+}