1. opensymphony
  2. xwork

Commits

rainerh  committed 1497873

XWork Annotation bugs
o patches applied with minor changes

Issue Number: XW-421
Submitted by: Jonathan Gerrish
Reviewed by: Rainer Hermanns

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

  • Participants
  • Parent commits 17e1062
  • Branches master

Comments (0)

Files changed (8)

File src/java/com/opensymphony/xwork2/util/AnnotationXWorkConverter.java

View file
  */
 package com.opensymphony.xwork2.util;
 
-import com.opensymphony.xwork2.util.FileManager;
 import com.opensymphony.xwork2.conversion.annotations.Conversion;
 import com.opensymphony.xwork2.conversion.annotations.ConversionRule;
 import com.opensymphony.xwork2.conversion.annotations.ConversionType;
 
                     if (key != null) {
                         try {
-                            if (tc.rule().equals(ConversionType.APPLICATION)) {
+                        	if (tc.type()  == ConversionType.APPLICATION) {
                                 defaultMappings.put(key, createTypeConverter(tc.converter()));
                             } else {
                                 if (tc.rule().toString().equals(ConversionRule.KEY_PROPERTY) || tc.rule().toString().equals(ConversionRule.CREATE_IF_NULL)) {
 
                     if (key != null) {
                         try {
-                            if (tc.rule().equals(ConversionType.APPLICATION)) {
+                        	if (tc.type() == ConversionType.APPLICATION) {
                                 defaultMappings.put(key, createTypeConverter(tc.converter()));
                             } else {
                                 if (tc.rule().toString().equals(ConversionRule.KEY_PROPERTY)) {

File src/java/com/opensymphony/xwork2/util/Element.java

View file
 
 package com.opensymphony.xwork2.util;
 
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
 /**
  * <!-- START SNIPPET: description -->
  * <p/>Sets the Element for type conversion.
  * @author Rainer Hermanns
  * @version $Id$
  */
+@Target({ ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
 public @interface Element {
 
     /**

File src/test/com/opensymphony/xwork2/test/annotations/Address.java

View file
+package com.opensymphony.xwork2.test.annotations;
+
+public class Address {
+	private String line1;
+	private String line2;
+	private String city;
+	private String country;
+	public String getCity() {
+		return city;
+	}
+	public void setCity(String city) {
+		this.city = city;
+	}
+	public String getCountry() {
+		return country;
+	}
+	public void setCountry(String country) {
+		this.country = country;
+	}
+	public String getLine1() {
+		return line1;
+	}
+	public void setLine1(String line1) {
+		this.line1 = line1;
+	}
+	public String getLine2() {
+		return line2;
+	}
+	public void setLine2(String line2) {
+		this.line2 = line2;
+	}
+}

File src/test/com/opensymphony/xwork2/test/annotations/AddressTypeConverter.java

View file
+package com.opensymphony.xwork2.test.annotations;
+
+import java.util.Map;
+
+import ognl.DefaultTypeConverter;
+
+public class AddressTypeConverter extends DefaultTypeConverter {
+	public Object convertValue(Map context, Object value, Class toType) {
+		if(value instanceof String) {
+			return decodeAddress((String)value);
+		} else if(value instanceof String && value.getClass().isArray()) {
+			return decodeAddress(((String[])value)[0]);
+		} else {
+			Address address = (Address)value;
+			return address.getLine1() + ":" + address.getLine2() + ":" +
+			       address.getCity() + ":" + address.getCountry();
+		}
+	}
+
+	private Address decodeAddress(String encodedAddress) {
+		String[] parts = ((String)encodedAddress).split(":");
+		Address address = new Address();
+		address.setLine1(parts[0]);
+		address.setLine2(parts[1]);
+		address.setCity(parts[2]);
+		address.setCountry(parts[3]);
+		return address;
+	}
+}

File src/test/com/opensymphony/xwork2/test/annotations/Person.java

View file
+package com.opensymphony.xwork2.test.annotations;
+
+public class Person {
+	private String firstName;
+	private String lastName;
+	
+	public String getFirstName() {
+		return firstName;
+	}
+	
+	public void setFirstName(String firstName) {
+		this.firstName = firstName;
+	}
+	
+	public String getLastName() {
+		return lastName;
+	}
+	
+	public void setLastName(String lastName) {
+		this.lastName = lastName;
+	}
+}

File src/test/com/opensymphony/xwork2/test/annotations/PersonAction.java

View file
+package com.opensymphony.xwork2.test.annotations;
+
+import java.util.List;
+
+import com.opensymphony.xwork2.conversion.annotations.Conversion;
+import com.opensymphony.xwork2.conversion.annotations.ConversionType;
+import com.opensymphony.xwork2.conversion.annotations.TypeConversion;
+import com.opensymphony.xwork2.util.Element;
+
+@Conversion(
+	conversions={
+		@TypeConversion(type=ConversionType.APPLICATION,
+						key="com.opensymphony.xwork2.test.annotations.Address",
+						converter="com.opensymphony.xwork2.test.annotations.AddressTypeConverter"),
+		@TypeConversion(type=ConversionType.APPLICATION,
+						key="com.opensymphony.xwork2.test.annotations.Person",
+						converter="com.opensymphony.xwork2.test.annotations.PersonTypeConverter")})
+public class PersonAction {
+	List<Person> users;
+	private List<Address> address;
+	@Element(com.opensymphony.xwork2.test.annotations.Address.class)
+	private List addressesNoGenericElementAnnotation;
+
+	public List<Person> getUsers() {
+		return users;
+	}
+
+	public void setUsers(List<Person> users) {
+		this.users = users;
+	}
+
+	public void setAddress(List<Address> address) {
+		this.address = address;
+	}
+
+	public List<Address> getAddress() {
+		return address;
+	}
+
+	public void setAddressesNoGenericElementAnnotation(List addressesNoGenericElementAnnotation) {
+		this.addressesNoGenericElementAnnotation = addressesNoGenericElementAnnotation;
+	}
+
+	public List getAddressesNoGenericElementAnnotation() {
+		return addressesNoGenericElementAnnotation;
+	}
+}

File src/test/com/opensymphony/xwork2/test/annotations/PersonActionTest.java

View file
+package com.opensymphony.xwork2.test.annotations;
+
+import java.util.Map;
+
+import com.opensymphony.xwork2.util.*;
+import com.opensymphony.xwork2.XWorkTestCase;
+
+
+public class PersonActionTest extends XWorkTestCase {
+	public void testAddPerson() {
+        ValueStack stack = ValueStackFactory.getFactory().createValueStack();
+
+        Map stackContext = stack.getContext();
+        stackContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
+        stackContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.TRUE);
+        stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
+
+        PersonAction action = new PersonAction();
+        stack.push(action);
+
+        stack.setValue("users", "jonathan:gerrish");
+        assertNotNull(action.getUsers());
+        assertEquals(1, action.getUsers().size());
+        
+        for(Object person : action.getUsers()) {
+        	System.out.println("Person: " + person);
+        }
+        
+        assertEquals(Person.class, action.getUsers().get(0).getClass());
+        assertEquals("jonathan", action.getUsers().get(0).getFirstName());
+        assertEquals("gerrish", action.getUsers().get(0).getLastName());
+	}
+	
+	public void testAddAddress() {
+        ValueStack stack = ValueStackFactory.getFactory().createValueStack();
+		Map stackContext = stack.getContext();
+		stackContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
+		stackContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.TRUE);
+		stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
+
+		PersonAction action = new PersonAction();
+		stack.push(action);
+
+		stack.setValue("address", "2 Chandos Court:61 Haverstock Hill:London:England");
+		assertNotNull(action.getAddress());
+		assertEquals(1, action.getAddress().size());
+
+		for(Object address : action.getAddress()) {
+			System.out.println("Address: " + address);
+		}
+
+		assertEquals(Address.class, action.getAddress().get(0).getClass());
+		assertEquals("2 Chandos Court", action.getAddress().get(0).getLine1());
+		assertEquals("61 Haverstock Hill", action.getAddress().get(0).getLine2());
+		assertEquals("London", action.getAddress().get(0).getCity());
+		assertEquals("England", action.getAddress().get(0).getCountry());
+	}
+	
+	public void testAddAddressesNoGenericElementAnnotation() {
+        ValueStack stack = ValueStackFactory.getFactory().createValueStack();
+		Map stackContext = stack.getContext();
+		stackContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
+		stackContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.TRUE);
+		stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
+
+		PersonAction action = new PersonAction();
+		stack.push(action);
+
+		stack.setValue("addressesNoGenericElementAnnotation", "2 Chandos Court:61 Haverstock Hill:London:England");
+		assertNotNull(action.getAddressesNoGenericElementAnnotation());
+		assertEquals(1, action.getAddressesNoGenericElementAnnotation().size());
+
+		for(Object address : action.getAddressesNoGenericElementAnnotation()) {
+			System.out.println("Address: " + address);
+		}
+
+		assertEquals(Address.class, action.getAddressesNoGenericElementAnnotation().get(0).getClass());
+		assertEquals("2 Chandos Court", ((Address)action.getAddressesNoGenericElementAnnotation().get(0)).getLine1());
+		assertEquals("61 Haverstock Hill", ((Address)action.getAddressesNoGenericElementAnnotation().get(0)).getLine2());
+		assertEquals("London", ((Address)action.getAddressesNoGenericElementAnnotation().get(0)).getCity());
+		assertEquals("England", ((Address)action.getAddressesNoGenericElementAnnotation().get(0)).getCountry());
+	}
+}

File src/test/com/opensymphony/xwork2/test/annotations/PersonTypeConverter.java

View file
+package com.opensymphony.xwork2.test.annotations;
+
+import java.util.Map;
+
+import ognl.DefaultTypeConverter;
+
+public class PersonTypeConverter extends DefaultTypeConverter {
+	public Object convertValue(Map context, Object value, Class toType) {
+		if(value instanceof String) {
+			return decodePerson((String)value);
+		} else if(value instanceof String && value.getClass().isArray()) {
+			return decodePerson(((String[])value)[0]);
+		} else {
+			Person person = (Person)value;
+			return person.getFirstName() + ":" + person.getLastName();
+		}
+	}
+
+	private Person decodePerson(String encodedPerson) {
+		String[] parts = ((String)encodedPerson).split(":");
+		Person person = new Person();
+		person.setFirstName(parts[0]);
+		person.setLastName(parts[1]);
+		return person;
+	}
+}