16 changed files with 253 additions and 52 deletions
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
/* |
||||
* Copyright 2004-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.core.convert.service; |
||||
|
||||
import org.springframework.core.convert.ConversionExecutionException; |
||||
import org.springframework.core.convert.ConversionExecutor; |
||||
|
||||
class NoOpConversionExecutor implements ConversionExecutor { |
||||
|
||||
public static final ConversionExecutor INSTANCE = new NoOpConversionExecutor(); |
||||
|
||||
private NoOpConversionExecutor() { |
||||
|
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionExecutionException { |
||||
return source; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
package org.springframework.core.convert.service; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
public class ArrayToArrayTests { |
||||
|
||||
@Test |
||||
public void testArrayToArrayConversion() { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToArray c = new ArrayToArray(TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(Integer[].class), service); |
||||
Integer[] result = (Integer[]) c.execute(new String[] { "1", "2", "3" }); |
||||
assertEquals(new Integer(1), result[0]); |
||||
assertEquals(new Integer(2), result[1]); |
||||
assertEquals(new Integer(3), result[2]); |
||||
} |
||||
} |
||||
@ -0,0 +1,65 @@
@@ -0,0 +1,65 @@
|
||||
package org.springframework.core.convert.service; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
import java.util.SortedSet; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
public class ArrayToCollectionTests { |
||||
|
||||
@Test |
||||
public void testArrayToCollectionConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("bindTarget")), service); |
||||
List result = (List) c.execute(new String[] { "1", "2", "3" }); |
||||
assertEquals(new Integer(1), result.get(0)); |
||||
assertEquals(new Integer(2), result.get(1)); |
||||
assertEquals(new Integer(3), result.get(2)); |
||||
} |
||||
|
||||
@Test |
||||
public void testArrayToSetConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("setTarget")), service); |
||||
Set result = (Set) c.execute(new String[] { "1" }); |
||||
assertEquals("1", result.iterator().next()); |
||||
} |
||||
|
||||
@Test |
||||
public void testArrayToSortedSetConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("sortedSetTarget")), service); |
||||
SortedSet result = (SortedSet) c.execute(new String[] { "1" }); |
||||
assertEquals(new Integer(1), result.iterator().next()); |
||||
} |
||||
|
||||
@Test |
||||
public void testArrayToCollectionImplConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("implTarget")), service); |
||||
LinkedList result = (LinkedList) c.execute(new String[] { "1" }); |
||||
assertEquals("1", result.iterator().next()); |
||||
} |
||||
|
||||
@Test |
||||
public void testArrayToNonGenericCollectionConversionNullElement() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("listTarget")), service); |
||||
List result = (List) c.execute(new Integer[] { null, new Integer(1) }); |
||||
assertEquals(null, result.get(0)); |
||||
assertEquals(new Integer(1), result.get(1)); |
||||
} |
||||
|
||||
public Collection<Integer> bindTarget; |
||||
public List listTarget; |
||||
public Set setTarget; |
||||
public SortedSet<Integer> sortedSetTarget; |
||||
public LinkedList<String> implTarget; |
||||
|
||||
} |
||||
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
package org.springframework.core.convert.service; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
|
||||
public class CollectionToArrayTests { |
||||
|
||||
@Test |
||||
public void testCollectionToArrayConversion() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToArray c = new CollectionToArray(new TypeDescriptor(getClass().getField("bindTarget")), |
||||
TypeDescriptor.valueOf(Integer[].class), service); |
||||
bindTarget.add("1"); |
||||
bindTarget.add("2"); |
||||
bindTarget.add("3"); |
||||
Integer[] result = (Integer[]) c.execute(bindTarget); |
||||
assertEquals(new Integer(1), result[0]); |
||||
assertEquals(new Integer(2), result[1]); |
||||
assertEquals(new Integer(3), result[2]); |
||||
} |
||||
|
||||
@Test |
||||
public void testCollectionToArrayConversionNoGenericInfo() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor |
||||
.valueOf(Integer[].class), service); |
||||
bindTarget.add("1"); |
||||
bindTarget.add("2"); |
||||
bindTarget.add("3"); |
||||
Integer[] result = (Integer[]) c.execute(bindTarget); |
||||
assertEquals(new Integer(1), result[0]); |
||||
assertEquals(new Integer(2), result[1]); |
||||
assertEquals(new Integer(3), result[2]); |
||||
} |
||||
|
||||
@Test |
||||
public void testCollectionToArrayConversionNoGenericInfoNullElement() throws Exception { |
||||
DefaultConversionService service = new DefaultConversionService(); |
||||
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor |
||||
.valueOf(Integer[].class), service); |
||||
bindTarget.add(null); |
||||
bindTarget.add("1"); |
||||
bindTarget.add("2"); |
||||
bindTarget.add("3"); |
||||
Integer[] result = (Integer[]) c.execute(bindTarget); |
||||
assertEquals(null, result[0]); |
||||
assertEquals(new Integer(1), result[1]); |
||||
assertEquals(new Integer(2), result[2]); |
||||
assertEquals(new Integer(3), result[3]); |
||||
} |
||||
|
||||
public Collection<String> bindTarget = new ArrayList<String>(); |
||||
|
||||
|
||||
} |
||||
Loading…
Reference in new issue