-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rmpestano
committed
Feb 27, 2016
1 parent
ac8a9ba
commit 4151fda
Showing
8 changed files
with
189 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.github.dbunit.rules; | ||
|
||
import com.github.dbunit.rules.api.dataset.DataSet; | ||
import com.github.dbunit.rules.api.dataset.SeedStrategy; | ||
import com.github.dbunit.rules.model.User; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import javax.persistence.EntityManager; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
|
||
/** | ||
* Created by pestano on 27/02/16. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class ScriptsIt { | ||
|
||
@Rule | ||
public EntityManagerProvider emProvider = EntityManagerProvider.instance("rules-it"); | ||
|
||
@Rule | ||
public DBUnitRule dbUnitRule = DBUnitRule.instance(emProvider.getConnection()); | ||
|
||
@BeforeClass | ||
public static void before() { | ||
EntityManager em = EntityManagerProvider.instance("rules-it").em(); | ||
em.getTransaction().begin(); | ||
em.createNativeQuery("INSERT INTO USER VALUES (6,'user6')").executeUpdate(); | ||
em.flush(); | ||
em.getTransaction().commit(); | ||
List<User> users = em.createQuery("select u from User u").getResultList(); | ||
assertThat(users).isNotNull().hasSize(1); | ||
} | ||
|
||
@Test | ||
@DataSet(value = "yml/users.yml", executeScriptsBefore = {"users.sql","tweets.sql"}, | ||
executeScriptsAfter = "after.sql", strategy = SeedStrategy.INSERT)//NEED to be INSERT because clean will delete users inserted in script | ||
public void shouldExecuteScriptsBefore() { | ||
User userFromSqlScript = new User(10); | ||
List<User> users = listUsers("select u from User u where u.id = 6"); | ||
assertThat(users).isNotNull().hasSize(0);//user insert in @Before was deleted by users.sql script | ||
users = listUsers("select u from User u"); | ||
assertThat(users).isNotNull().hasSize(3)// two from users.yaml dataset and one from users.sql script | ||
.contains(userFromSqlScript); | ||
} | ||
|
||
private List<User> listUsers(String sql) { | ||
return EntityManagerProvider.instance("rules-it").em().createQuery(sql).getResultList(); | ||
} | ||
|
||
@AfterClass | ||
public static void after() { | ||
List<User> users = EntityManagerProvider.instance("rules-it").em().createQuery("select u from User u").getResultList(); | ||
if (users == null || users.size() != 1) { | ||
fail("We should have 1 user after test execution"); | ||
} | ||
User user = users.get(0);//after script deletes all users and insert one | ||
assertThat(user.getName()).isNotNull().isEqualTo("user-after"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
DELETE FROM Follower; | ||
DELETE FROM Tweet; | ||
DELETE FROM User; | ||
INSERT INTO USER VALUES (99,'user-after'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
INSERT INTO Tweet (id,content,user_id) VALUES (10,'tweet10',10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DELETE FROM User; | ||
INSERT INTO USER VALUES (10,'user10'); |
Any reason to use context classloader instead of current class loader ? (
getClass().getResource(...)
)