Closed
Description
Normally it's safe to change type from array to list, but it's not for Resource
.
@Value("${resources:file:/tmp/*.text}")
Resource[] resourceArray; // multiple FileSystemResource
@Value("${resources:file:/tmp/*.text}")
List<Resource> resourceList; // only one FileUrlResource
here is the full test
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import test.ResourceInjectionTest.TestConfiguration;
@RunWith(SpringRunner.class)
@TestPropertySource(properties = { "resources=file:/tmp/*.text" })
@ContextConfiguration(classes = TestConfiguration.class)
public class ResourceInjectionTest {
private static File[] files;
@Autowired
private TestConfiguration testConfiguration;
@BeforeClass
public static void setup() throws IOException {
files = new File[2];
files[0] = new File("/tmp", "a.text");
files[1] = new File("/tmp", "b.text");
for (File f : files)
f.createNewFile();
}
@AfterClass
public static void cleanup() {
for (File f : files)
f.delete();
}
@Test
public void testInjection() {
assertEquals(2, testConfiguration.resourceArray.length); // two FileSystemResource
assertEquals(2, testConfiguration.resourceList.size()); // one FileUrlResource
}
static class TestConfiguration {
@Value("${resources}")
Resource[] resourceArray;
@Value("${resources}")
List<Resource> resourceList;
}
}