JobParamsAdapter extends XmlAdapter<JobParams, Map<String, Serializable>> { public Map<String, Serializable> unmarshal( JobParams v ) throws Exception { HashMap<String, ArrayList<Serializable>> draftParamMap = new HashMap<String, ArrayList<Serializable>>(); for ( JobParam jobParam : v.jobParams ) { ArrayList<Serializable> p = draftParamMap.get( jobParam.name ); if ( p == null ) { p = new ArrayList<Serializable>(); draftParamMap.put( jobParam.name, p ); } p.add( jobParam.value ); } HashMap<String, Serializable> paramMap = new HashMap<String, Serializable>(); for ( String paramName : draftParamMap.keySet() ) { ArrayList<Serializable> p = draftParamMap.get( paramName ); if ( p.size() == 1 ) { paramMap.put( paramName, p.get( 0 ) ); } else { paramMap.put( paramName, p ); } } return paramMap; }  JobParams marshal( Map<String, Serializable> v ); Map<String, Serializable> unmarshal( JobParams v );  }
@Test public void testUnmarshal() throws Exception { JobParamsAdapter adapter = new JobParamsAdapter(); Map<String, Serializable> expectedDataMap = new HashMap<String, Serializable>(); expectedDataMap.put( "a", "A" ); expectedDataMap.put( "bb", "[B]" ); expectedDataMap.put( "ccc", "[C].[CCC]" ); expectedDataMap.put( "dddd", "[D].[DDD,ddd]" ); JobParams dataJobParams = createJobParams( new JobParam[]{ createJobParam( "a", "A" ), createJobParam( "bb", "[B]" ), createJobParam( "ccc", "[C].[CCC]" ), createJobParam( "dddd", "[D].[DDD,ddd]" ) } ); Map<String, Serializable> resultMap = adapter.unmarshal( dataJobParams ); Assert.assertNotNull( "resultMap", resultMap ); Assert.assertEquals( "resultMap.size", expectedDataMap.size(), resultMap.size() ); for ( String key : expectedDataMap.keySet() ) { assertEquals( "resultMap[" + key + "]", expectedDataMap.get( key ), resultMap.get( key ) ); } }
@Test public void testUnmarshalMultiValue() throws Exception { JobParamsAdapter adapter = new JobParamsAdapter(); Map<String, Serializable> expectedDataMap = new HashMap<String, Serializable>(); expectedDataMap.put( "a", "A" ); expectedDataMap.put( "bb", "[B]" ); final ArrayList<String> cValue = castAsArrayList( new String[] { "[C].[CCC]", "[D].[DDD,ddd]" } ); expectedDataMap.put( "ccc", cValue ); JobParams dataJobParams = createJobParams( new JobParam[]{ createJobParam( "a", "A" ), createJobParam( "bb", "[B]" ), createJobParam( "ccc", "[C].[CCC]" ), createJobParam( "ccc", "[D].[DDD,ddd]" ) } ); Map<String, Serializable> resultMap = adapter.unmarshal( dataJobParams ); Assert.assertNotNull( "resultMap", resultMap ); Assert.assertEquals( "resultMap.size", expectedDataMap.size(), resultMap.size() ); for ( String key : new String[] { "a", "bb" } ) { assertEquals( "resultMap[" + key + "]", expectedDataMap.get( key ), resultMap.get( key ) ); } String key = "ccc"; assertTrue( "resultMap[" + key + "] is collection", resultMap.get( key ) instanceof Collection ); Collection<?> actualCValue = (Collection<?>) resultMap.get( key ); assertEquals( "resultMap[" + key + "].size", cValue.size(), actualCValue.size() ); assertTrue( "resultMap[" + key + "] all expected values", cValue.containsAll( actualCValue ) ); }