Added unit tests for Nfl sample job.

This commit is contained in:
lucasward
2007-09-10 21:19:37 +00:00
parent ef3de138dc
commit b2d06ade6d
8 changed files with 271 additions and 48 deletions

View File

@@ -5,37 +5,43 @@ import org.springframework.batch.sample.domain.NflGame;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.util.Assert;
public class SqlNflGameDao extends JdbcDaoSupport implements OutputSource{
public class SqlNflGameDao extends JdbcDaoSupport implements OutputSource {
private static final String INSERT_GAME = "INSERT into GAMES(player_id,year,team,week,opponent," +
"completes,attempts,passing_yards,passing_td,interceptions,rushes,rush_yards," +
"receptions,receptions_yards,total_td) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
private static final String INSERT_GAME = "INSERT into GAMES(player_id,year,team,week,opponent,"
+ "completes,attempts,passing_yards,passing_td,interceptions,rushes,rush_yards,"
+ "receptions,receptions_yards,total_td) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
public void write(Object output) {
Assert.isTrue(output instanceof NflGame, "Only NflGame objects can be written out" +
"using this Dao");
NflGame game = (NflGame)output;
Object[] args = new Object[]{game.getId(),new Integer(game.getYear()),game.getTeam(),new Integer(game.getWeek()),
game.getOpponent(),new Integer(game.getCompletes()),new Integer(game.getAttempts()),new Integer(game.getPassingYards()),
new Integer(game.getPassingTd()),new Integer(game.getInterceptions()),new Integer(game.getRushes()),new Integer(game.getRushYards()),
new Integer(game.getReceptions()),new Integer(game.getReceptionYards()),new Integer(game.getTotalTd())};
Assert.isTrue(output instanceof NflGame,
"Only NflGame objects can be written out" + "using this Dao");
NflGame game = (NflGame) output;
Object[] args = new Object[] { game.getId(),
new Integer(game.getYear()), game.getTeam(),
new Integer(game.getWeek()), game.getOpponent(),
new Integer(game.getCompletes()),
new Integer(game.getAttempts()),
new Integer(game.getPassingYards()),
new Integer(game.getPassingTd()),
new Integer(game.getInterceptions()),
new Integer(game.getRushes()),
new Integer(game.getRushYards()),
new Integer(game.getReceptions()),
new Integer(game.getReceptionYards()),
new Integer(game.getTotalTd()) };
this.getJdbcTemplate().update(INSERT_GAME, args);
}
public void close() {
// TODO Auto-generated method stub
}
public void open() {
// TODO Auto-generated method stub
}
}

View File

@@ -3,51 +3,28 @@
*/
package org.springframework.batch.sample.dao;
import javax.sql.DataSource;
import org.springframework.batch.sample.domain.NflPlayer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**
* @author venu.valmeti
* @author Lucas Ward
*
*/
public class SqlNflPlayerDao implements NflPlayerDao, InitializingBean {
public class SqlNflPlayerDao extends JdbcDaoSupport implements NflPlayerDao {
public static final String INSERT_PLAYER = "INSERT into players(player_id, " +
"last_name, first_name, pos, year_of_birth, year_drafted)" +
" values (?,?,?,?,?,?)";
DataSource dataSource;
JdbcTemplate jdbcTemplate;
public void afterPropertiesSet() throws Exception {
if(dataSource == null){
throw new IllegalStateException("DataSource must not be null.");
}
}
public SqlNflPlayerDao(DataSource dataSource){
this.dataSource = dataSource;
jdbcTemplate = new JdbcTemplate(dataSource);
}
/* (non-Javadoc)
* @see com.nfl.NflPlayerDao#savePlayer(com.nfl.NflPlayer)
*/
public void savePlayer(NflPlayer nflPlayer) {
jdbcTemplate.update(INSERT_PLAYER,
getJdbcTemplate().update(INSERT_PLAYER,
new Object[]{nflPlayer.getID(),nflPlayer.getLastName(),
nflPlayer.getFirstName(), nflPlayer.getPosition(),
new Integer(nflPlayer.getBirthYear()),
new Integer(nflPlayer.getDebutYear())});
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}

View File

@@ -1,5 +1,7 @@
package org.springframework.batch.sample.domain;
import org.apache.commons.lang.builder.EqualsBuilder;
public class NflGame {
private String id;
@@ -204,4 +206,8 @@ public class NflGame {
return "NFL Game: ID=" + id + " " + team + " vs. " + opponent +
" - " + year;
}
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
}

View File

@@ -1,5 +1,7 @@
package org.springframework.batch.sample.domain;
import org.apache.commons.lang.builder.EqualsBuilder;
/**
* Domain object representing the summary of a given Nfl Player's
* year.
@@ -99,4 +101,8 @@ public class NflPlayerSummary {
public String toString() {
return "NFL Player Summary: ID=" + id + " Year=" + year;
}
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
}

View File

@@ -42,8 +42,7 @@
<property name="nflPlayerDao">
<bean
class="org.springframework.batch.sample.dao.SqlNflPlayerDao">
<constructor-arg
ref="dataSource" />
<property name="dataSource" ref="dataSource" />
</bean>
</property>
</bean>

View File

@@ -0,0 +1,98 @@
/*
* 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.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.batch.sample.domain.NflGame;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
/**
* @author Lucas Ward
*
*/
public class SqlNflGameDaoIntegrationTests extends
AbstractTransactionalDataSourceSpringContextTests {
SqlNflGameDao gameDao;
NflGame game = new NflGame();
protected String[] getConfigLocations() {
return new String[]{"data-source-context.xml"};
}
protected void onSetUpBeforeTransaction() throws Exception {
super.onSetUpBeforeTransaction();
gameDao = new SqlNflGameDao();
gameDao.setJdbcTemplate(getJdbcTemplate());
game.setId("AbduKa00");
game.setYear(1996);
game.setTeam("mia");
game.setWeek(10);
game.setOpponent("nwe");
game.setAttempts(0);
game.setCompletes(0);
game.setPassingYards(0);
game.setPassingTd(0);
game.setInterceptions(0);
game.setRushes(29);
game.setRushYards(109);
game.setReceptions(1);
game.setReceptionYards(16);
game.setTotalTd(2);
}
public void testWrite(){
gameDao.write(game);
NflGame tempGame = (NflGame)getJdbcTemplate().queryForObject("SELECT * FROM GAMES", new NflGameRowMapper());
assertEquals(tempGame, game);
}
public class NflGameRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int arg1) throws SQLException{
if(rs == null){
return null;
}
NflGame nflGame = new NflGame();
nflGame.setId(rs.getString("PLAYER_ID"));
nflGame.setYear(rs.getInt("year"));
nflGame.setTeam(rs.getString("team"));
nflGame.setWeek(rs.getInt("week"));
nflGame.setOpponent(rs.getString("opponent"));
nflGame.setCompletes(rs.getInt("completes"));
nflGame.setAttempts(rs.getInt("attempts"));
nflGame.setPassingYards(rs.getInt("passing_Yards"));
nflGame.setPassingTd(rs.getInt("passing_Td"));
nflGame.setInterceptions(rs.getInt("interceptions"));
nflGame.setRushes(rs.getInt("rushes"));
nflGame.setRushYards(rs.getInt("rush_Yards"));
nflGame.setReceptions(rs.getInt("receptions"));
nflGame.setReceptionYards(rs.getInt("receptions_Yards"));
nflGame.setTotalTd(rs.getInt("total_Td"));
return nflGame;
}
}
}

View File

@@ -0,0 +1,61 @@
/**
*
*/
package org.springframework.batch.sample.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.batch.sample.domain.NflPlayer;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
/**
* @author Lucas Ward
*
*/
public class SqlNflPlayerDaoIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
private SqlNflPlayerDao playerDao;
private NflPlayer player;
private static final String GET_PLAYER = "SELECT * from PLAYERS";
protected String[] getConfigLocations() {
// TODO Auto-generated method stub
return new String[] {"data-source-context.xml"};
}
protected void onSetUpBeforeTransaction() throws Exception {
// TODO Auto-generated method stub
super.onSetUpBeforeTransaction();
playerDao = new SqlNflPlayerDao();
playerDao.setJdbcTemplate(this.jdbcTemplate);
player = new NflPlayer();
player.setID("AKFJDL00");
player.setFirstName("John");
player.setLastName("Doe");
player.setPosition("QB");
player.setBirthYear(1975);
player.setDebutYear(1998);
}
public void testSavePlayer(){
playerDao.savePlayer(player);
getJdbcTemplate().query(GET_PLAYER, new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException {
assertEquals(rs.getString("PLAYER_ID"), "AKFJDL00");
assertEquals(rs.getString("LAST_NAME"), "Doe");
assertEquals(rs.getString("FIRST_NAME"), "John");
assertEquals(rs.getString("POS"), "QB");
assertEquals(rs.getInt("YEAR_OF_BIRTH"), 1975);
assertEquals(rs.getInt("YEAR_DRAFTED"), 1998);
}
});
}
}

View File

@@ -0,0 +1,70 @@
/*
* 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.dao;
import org.springframework.batch.sample.domain.NflPlayerSummary;
import org.springframework.batch.sample.mapping.NflPlayerSummaryMapper;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
/**
* @author Lucas Ward
*
*/
public class SqlNflPlayerSummaryDaoIntegrationTests extends
AbstractTransactionalDataSourceSpringContextTests {
SqlNflPlayerSummaryDao playerSummaryDao;
NflPlayerSummary summary;
protected String[] getConfigLocations() {
return new String[] { "data-source-context.xml" };
}
protected void onSetUpBeforeTransaction() throws Exception {
super.onSetUpBeforeTransaction();
playerSummaryDao = new SqlNflPlayerSummaryDao();
playerSummaryDao.setJdbcTemplate(getJdbcTemplate());
summary = new NflPlayerSummary();
summary.setId("AikmTr00");
summary.setYear(1997);
summary.setCompletes(294);
summary.setAttempts(517);
summary.setPassingYards(3283);
summary.setPassingTd(19);
summary.setInterceptions(12);
summary.setRushes(25);
summary.setRushYards(79);
summary.setReceptions(0);
summary.setReceptionYards(0);
summary.setTotalTd(0);
}
public void testWrite() {
playerSummaryDao.write(summary);
NflPlayerSummary testSummary = (NflPlayerSummary) getJdbcTemplate()
.queryForObject("SELECT * FROM PLAYER_SUMMARY",
new NflPlayerSummaryMapper());
assertEquals(testSummary, summary);
}
}