CollectionTranslator { public Object translate( Object value, CollectionProperty property, TranslationSource cache ) { if ( property.shouldItemsBeTranslated() ) { Object target = createTargetCollection( value, property, cache ); translateToTargetCollection( value, target, property, cache ); return target; } else if ( shouldCopy( value, property ) ) { Object target = createTargetCollection( value, property, cache ); copyToTargetCollection( value, target, property ); return target; } else { return value; } } CollectionTranslator( Configuration configuration, SourcePropertyFactory sourcePropertyFactory ); Object translate( Object value, CollectionProperty property, TranslationSource cache ); @SuppressWarnings( "unchecked" ) void updateCollection( Object source, Collection<Object> destinationCollection, TranslationSource translationSource,
			CollectionProperty property );  }
@SuppressWarnings( "unchecked" ) @Test public void testCollectionTranslatorPerformsDefensiveCopyByDefault() { Set<String> rhyme = new HashSet<String>(); rhyme.add( "one" ); rhyme.add( "two" ); rhyme.add( "buckle my shoe" ); Mockito.doReturn( Set.class ).when( property ).getType(); Set<String> translated = (Set<String>)translator.translate( rhyme, property, session ); Assert.assertNotNull( translated ); Assert.assertEquals( rhyme, translated ); Assert.assertNotSame( rhyme, translated ); }
@SuppressWarnings( "unchecked" ) @Test public void testCollectionTranslatorRetainsSortWhenTranslating() { SortedSet<String> rhyme = new TreeSet<String>(); rhyme.add( "Zed" ); rhyme.add( "Ay" ); rhyme.add( "Pee" ); assertOrder( rhyme, "Ay", "Pee", "Zed" ); Mockito.doReturn( SortedSet.class ).when( property ).getType(); SortedSet<String> translated = (SortedSet<String>)translator.translate( rhyme, property, session ); Assert.assertNotNull( translated ); Assert.assertEquals( rhyme, translated ); Assert.assertNotSame( rhyme, translated ); assertOrder( translated, "Ay", "Pee", "Zed" ); translated.add( "Cee" ); assertOrder( translated, "Ay", "Cee", "Pee", "Zed" ); }
@SuppressWarnings( "unchecked" ) @Test public void testCollectionTranslatorCanBeConfiguredToNotPerformDefensiveCopy() { List<String> rhyme = new ArrayList<String>(); rhyme.add( "one" ); rhyme.add( "two" ); rhyme.add( "buckle my shoe" ); configuration.setPerformingDefensiveCopies( false ); Mockito.doReturn( List.class ).when( property ).getType(); List<String> translated = (List<String>)translator.translate( rhyme, property, session ); Assert.assertNotNull( translated ); Assert.assertEquals( rhyme, translated ); Assert.assertSame( rhyme, translated ); }
@SuppressWarnings( "unchecked" ) @Test public void testCollectionTranslatorTranslatesContentsOfCollection() { Collection<Foo> rhyme = new ArrayList<Foo>(); rhyme.add( new Foo( "this" ) ); rhyme.add( new Foo( "that" ) ); when( property.shouldItemsBeTranslated() ).thenReturn( true ); doReturn( Bar.class ).when( property ).getItemClass(); doReturn( DefaultObjectTargetFactory.class ).when( property ).getItemFactory(); Mockito.doReturn( Collection.class ).when( property ).getType(); Collection<Bar> translated = (Collection<Bar>)translator.translate( rhyme, property, session ); Assert.assertEquals( 2, translated.size() ); Iterator<Bar> iterator = translated.iterator(); Assert.assertEquals( "this", iterator.next().getValue() ); Assert.assertEquals( "that", iterator.next().getValue() ); }