Add JdbcTestUtils.deleteRowsInTableWhere method

Issue: SPR-10302
This commit is contained in:
Phillip Webb
2013-02-25 11:26:59 -08:00
parent 576be97285
commit 720714b434
2 changed files with 72 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@@ -16,25 +16,38 @@
package org.springframework.test.jdbc;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Unit tests for {@link JdbcTestUtils}.
*
* @author Thomas Risberg
* @author Sam Brannen
* @author Phillip Webb
* @since 2.5.4
*/
@RunWith(MockitoJUnitRunner.class)
public class JdbcTestUtilsTests {
@Mock
private JdbcTemplate jdbcTemplate;
@Test
public void containsDelimiters() {
assertTrue("test with ';' is wrong", !JdbcTestUtils.containsSqlScriptDelimiters("select 1\n select ';'", ';'));
@@ -104,4 +117,26 @@ public class JdbcTestUtilsTests {
assertEquals("statement 4 not split correctly", statement4, statements.get(3));
}
@Test
public void testDeleteNoWhere() throws Exception {
given(jdbcTemplate.update("DELETE FROM person")).willReturn(10);
int deleted = JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "person", null);
assertThat(deleted, equalTo(10));
}
@Test
public void testDeleteWhere() throws Exception {
given(jdbcTemplate.update("DELETE FROM person WHERE name = 'Bob' and age > 25")).willReturn(10);
int deleted = JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "person", "name = 'Bob' and age > 25");
assertThat(deleted, equalTo(10));
}
@Test
public void deleteWhereAndArguments() throws Exception {
given(jdbcTemplate.update("DELETE FROM person WHERE name = ? and age > ?", "Bob", 25)).willReturn(10);
int deleted = JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "person", "name = ? and age > ?", "Bob", 25);
assertThat(deleted, equalTo(10));
}
}