IN PROGRESS - BATCH-971: add database writers to iosample

cleanup
This commit is contained in:
robokaso
2008-12-17 14:07:44 +00:00
parent f05be50863
commit c98903793a
7 changed files with 88 additions and 381 deletions

View File

@@ -1,113 +0,0 @@
package org.springframework.batch.sample;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.sql.DataSource;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.LineTokenizer;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
public class MultiResourceJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
// expected line length in input file (sum of pattern lengths + 2, because
// the counter is appended twice)
private static final int LINE_LENGTH = 29;
// auto-injected attributes
private SimpleJdbcTemplate simpleJdbcTemplate;
private Resource fileLocator;
protected FlatFileItemReader<Trade> itemReader;
private LineTokenizer lineTokenizer;
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@Autowired
public void setLineTokenizer(LineTokenizer lineTokenizer) {
this.lineTokenizer = lineTokenizer;
}
@Before
public void onSetUp() throws Exception {
simpleJdbcTemplate.update("delete from TRADE");
fileLocator = new ClassPathResource(
"data/multiResourceJob/input/20070122.teststream.ImportTradeDataStep.txt");
itemReader = new FlatFileItemReader<Trade>();
FieldSetMapper<Trade> mapper = new TradeFieldSetMapper();
DefaultLineMapper<Trade> lineMapper = new DefaultLineMapper<Trade>();
lineMapper.setLineTokenizer(lineTokenizer);
lineMapper.setFieldSetMapper(mapper);
itemReader.setLineMapper(lineMapper);
itemReader.setResource(fileLocator);
itemReader.open(new ExecutionContext());
}
/*
* fixed-length file is expected on input
*/
protected void validatePreConditions() throws Exception {
BufferedReader reader;
reader = new BufferedReader(new FileReader(fileLocator.getFile()));
String line;
while ((line = reader.readLine()) != null) {
assertEquals(LINE_LENGTH, line.length());
}
}
/**
* Context: 5 items overall, min. 2 items per output file, commitInterval=3, =>
* two files created, with 3 items in the first and two in second.
*/
@Override
protected void validatePostConditions() throws Exception {
File file1 = new File("target/test-outputs/multiResourceOutput.txt.1");
File file2 = new File("target/test-outputs/multiResourceOutput.txt.2");
assertTrue(file1.exists());
assertTrue(file2.exists());
BufferedReader reader1 = new BufferedReader(new FileReader(file1));
for (int i = 1; i <= 3; i++) {
assertEquals(itemReader.read().toString(), reader1.readLine());
}
assertNull(reader1.readLine());
BufferedReader reader2 = new BufferedReader(new FileReader(file2));
for (int i = 1; i <= 2; i++) {
assertEquals(itemReader.read().toString(), reader2.readLine());
}
assertNull(reader2.readLine());
}
@Autowired
public void setJob(@Qualifier("multiResourceJob")
Job job) {
super.setJob(job);
}
}

View File

@@ -1,43 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.sample;
import java.io.FileReader;
import org.custommonkey.xmlunit.XMLAssert;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
public class XmlStaxJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
private static final String OUTPUT_FILE = "target/test-outputs/20070918.testStream.xmlFileStep.output.xml";
private static final String EXPECTED_OUTPUT_FILE = "src/main/resources/data/staxJob/output/expected-output.xml";
/**
* Output should be the same as input
*/
protected void validatePostConditions() throws Exception {
XMLUnit.setIgnoreWhitespace(true);
XMLAssert.assertXMLEqual(new FileReader(EXPECTED_OUTPUT_FILE), new FileReader(OUTPUT_FILE));
}
}

View File

@@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="classpath:/simple-job-launcher-context.xml" />
<import resource="classpath:/jobs/multiResourceJob.xml" />
</beans>

View File

@@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="classpath:/simple-job-launcher-context.xml" />
<import resource="classpath:/jobs/xmlStaxJob.xml" />
</beans>