BeanListFactoryBean creates defensive copy of beans found.

This is necessary to the sorting applied correctly as it would be dropped through the proxy access otherwise.
This commit is contained in:
Oliver Gierke
2012-10-29 14:52:07 +01:00
parent 30e2b2c2fe
commit 50857dc867
2 changed files with 10 additions and 11 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.plugin.core.support;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@@ -38,7 +39,8 @@ public class BeanListFactoryBean<T> extends AbstractTypeAwareSupport<T> implemen
*/
public List<T> getObject() {
List<T> beans = getBeans();
List<T> beans = new ArrayList<T>();
beans.addAll(getBeans());
Collections.sort(beans, COMPARATOR);
return beans;
@@ -59,4 +61,4 @@ public class BeanListFactoryBean<T> extends AbstractTypeAwareSupport<T> implemen
public boolean isSingleton() {
return true;
}
}
}

View File

@@ -16,11 +16,10 @@
package org.springframework.plugin.core.support;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
@@ -29,7 +28,6 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.plugin.core.support.BeanListFactoryBean;
/**
* Unit test for {@link BeanListFactoryBean}.
@@ -60,11 +58,10 @@ public class BeanListFactoryBeanUnitTest {
Ordered first = getOrdered(5);
Ordered second = getOrdered(0);
Map<String, Ordered> beans = new HashMap<String, Ordered>();
beans.put("first", first);
beans.put("second", second);
when(context.getBeansOfType(Ordered.class, false, false)).thenReturn(beans);
when(context.getBeanNamesForType(Ordered.class, false, false)).thenReturn(new String[] { "first", "second" });
when(context.getType(any(String.class))).thenReturn((Class) Ordered.class);
when(context.getBean("first")).thenReturn(first);
when(context.getBean("second")).thenReturn(second);
Object result = factory.getObject();
assertTrue(result instanceof List<?>);
@@ -78,7 +75,7 @@ public class BeanListFactoryBeanUnitTest {
@Test
public void returnsEmptyListIfNoBeansFound() throws Exception {
when(context.getBeansOfType(Ordered.class)).thenReturn(new HashMap<String, Ordered>());
when(context.getBeanNamesForType(Ordered.class, false, false)).thenReturn(new String[0]);
Object result = factory.getObject();
assertTrue(result instanceof List<?>);