RESOLVED - BATCH-708: merge MultiResourceItemReader and SortedMultiResourceItemReader
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package org.springframework.batch.item.file;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.springframework.batch.item.CommonItemStreamItemReaderTests;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
@@ -35,6 +37,12 @@ public class MultiResourceItemReaderFlatFileTests extends
|
||||
|
||||
multiReader.setResources(new Resource[] { r1, r2, r3, r4 });
|
||||
multiReader.setSaveState(true);
|
||||
multiReader.setComparator(new Comparator() {
|
||||
public int compare(Object arg0, Object arg1) {
|
||||
return 0; // preserve original ordering
|
||||
}
|
||||
|
||||
});
|
||||
multiReader.afterPropertiesSet();
|
||||
|
||||
return multiReader;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.springframework.batch.item.file;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
@@ -38,6 +40,10 @@ public class MultiResourceItemReaderIntegrationTests extends TestCase {
|
||||
itemReader.setFieldSetMapper(new PassThroughFieldSetMapper());
|
||||
|
||||
tested.setDelegate(itemReader);
|
||||
tested.setComparator(new Comparator() {
|
||||
public int compare(Object o1, Object o2) {
|
||||
return 0; // do not change ordering
|
||||
}});
|
||||
tested.setResources(new Resource[] { r1, r2, r3, r4, r5 });
|
||||
tested.afterPropertiesSet();
|
||||
}
|
||||
@@ -137,6 +143,40 @@ public class MultiResourceItemReaderIntegrationTests extends TestCase {
|
||||
assertEquals(null, readItem());
|
||||
}
|
||||
|
||||
/**
|
||||
* Resources are ordered according to injected comparator.
|
||||
*/
|
||||
public void testResourceOrderingWithCustomComparator() {
|
||||
|
||||
Resource r1 = new ByteArrayResource("".getBytes(), "b");
|
||||
Resource r2 = new ByteArrayResource("".getBytes(), "a");
|
||||
Resource r3 = new ByteArrayResource("".getBytes(), "c");
|
||||
|
||||
|
||||
Resource[] resources = new Resource[] {r1, r2, r3};
|
||||
|
||||
Comparator comp = new Comparator() {
|
||||
|
||||
/**
|
||||
* Reversed ordering by filename.
|
||||
*/
|
||||
public int compare(Object o1, Object o2) {
|
||||
Resource r1 = (Resource) o1;
|
||||
Resource r2 = (Resource) o2;
|
||||
return -r1.getDescription().compareTo(r2.getDescription());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
tested.setComparator(comp);
|
||||
tested.setResources(resources);
|
||||
tested.open(ctx);
|
||||
|
||||
assertSame(r3, resources[0]);
|
||||
assertSame(r1, resources[1]);
|
||||
assertSame(r2, resources[2]);
|
||||
}
|
||||
|
||||
private String readItem() throws Exception {
|
||||
Object result = tested.read();
|
||||
return result == null ? null : ((FieldSet) result).readString(0);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.springframework.batch.item.file;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.events.Attribute;
|
||||
@@ -49,6 +51,11 @@ public class MultiResourceItemReaderXmlTests extends CommonItemStreamItemReaderT
|
||||
multiReader.setDelegate(reader);
|
||||
multiReader.setResources(new Resource[] { r1, r2, r3, r4 });
|
||||
multiReader.setSaveState(true);
|
||||
multiReader.setComparator(new Comparator() {
|
||||
public int compare(Object arg0, Object arg1) {
|
||||
return 0; // preserve original ordering
|
||||
}
|
||||
});
|
||||
multiReader.afterPropertiesSet();
|
||||
|
||||
return multiReader;
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
package org.springframework.batch.item.file;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Tests for {@link SortedMultiResourceItemReader}.
|
||||
*/
|
||||
public class SortedMultiResourceItemReaderTests extends TestCase {
|
||||
|
||||
private SortedMultiResourceItemReader tested = new SortedMultiResourceItemReader();
|
||||
|
||||
private Resource r1 = new FileSystemResource("b");
|
||||
|
||||
private Resource r2 = new FileSystemResource("a");
|
||||
|
||||
private Resource r3 = new FileSystemResource("c");
|
||||
|
||||
private Resource[] resources = { r1, r2, r3 };
|
||||
|
||||
/**
|
||||
* Resources are ordered according to filename by default.
|
||||
*/
|
||||
public void testResourceOrdering() {
|
||||
|
||||
tested.setResources(resources);
|
||||
|
||||
assertSame(r2, resources[0]);
|
||||
assertSame(r1, resources[1]);
|
||||
assertSame(r3, resources[2]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Resources are ordered according to injected comparator.
|
||||
*/
|
||||
public void testResourceOrderingWithCustomComparator() {
|
||||
Comparator comp = new Comparator() {
|
||||
|
||||
/**
|
||||
* Reversed ordering by filename.
|
||||
*/
|
||||
public int compare(Object o1, Object o2) {
|
||||
Resource r1 = (Resource) o1;
|
||||
Resource r2 = (Resource) o2;
|
||||
return -r1.getFilename().compareTo(r2.getFilename());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
tested.setComparator(comp);
|
||||
tested.setResources(resources);
|
||||
|
||||
assertSame(r3, resources[0]);
|
||||
assertSame(r1, resources[1]);
|
||||
assertSame(r2, resources[2]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user