Created
March 8, 2013 16:42
-
-
Save philwebb/5117828 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2002-2013 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.support; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import java.util.Set; | |
import org.junit.Test; | |
import org.springframework.core.convert.TypeDescriptor; | |
import org.springframework.core.convert.converter.Converter; | |
import static org.hamcrest.Matchers.*; | |
import static org.junit.Assert.*; | |
public class MapToMapTests { | |
public Map<String, Map<String, List<Map<String, String>>>> source; | |
public Map<String, Map<String, Set<ConfigurationDescriptor>>> target; | |
@SuppressWarnings("unchecked") | |
@Test | |
public void mapToMap() throws Exception { | |
DefaultConversionService conversionService = new DefaultConversionService(); | |
conversionService.addConverter(new MapToConfigurationDescriptorConverter()); | |
TypeDescriptor sourceType = new TypeDescriptor(getClass().getDeclaredField("source")); | |
TypeDescriptor targetType = new TypeDescriptor(getClass().getDeclaredField("target")); | |
Map<String, List<Map<String, String>>> entry = new HashMap<String, List<Map<String, String>>>(); | |
entry.put("b", Arrays.asList(Collections.singletonMap("c", "d"))); | |
this.source = new HashMap<String, Map<String, List<Map<String, String>>>>(); | |
this.source.put("a", entry); | |
this.target = (Map<String, Map<String, Set<ConfigurationDescriptor>>>) conversionService.convert(this.source, targetType); | |
assertThat(sourceType.isAssignableTo(targetType), equalTo(false)); | |
assertThat(conversionService.canConvert(sourceType, targetType), equalTo(true)); | |
assertThat(target.get("a").get("b"), instanceOf(Set.class)); | |
assertThat(target.get("a").get("b").iterator().next().toString(), equalTo("[c=d]")); | |
} | |
private static class MapToConfigurationDescriptorConverter implements Converter<Map<String, String>, ConfigurationDescriptor> { | |
@Override | |
public ConfigurationDescriptor convert(Map<String, String> source) { | |
return new ConfigurationDescriptor(source.entrySet()); | |
} | |
} | |
public static class ConfigurationDescriptor { | |
private String s; | |
public ConfigurationDescriptor(Set<Entry<String, String>> entrySet) { | |
this.s = entrySet.toString(); | |
} | |
@Override | |
public String toString() { | |
return this.s; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment