Skip to content

Commit 443162d

Browse files
committed
[ARQ-1299] Fixed handling of empty tables in YAML.
1 parent edf9be5 commit 443162d

File tree

8 files changed

+199
-46
lines changed

8 files changed

+199
-46
lines changed

impl/src/main/java/org/jboss/arquillian/persistence/dbunit/dataset/yaml/YamlDataSetProducer.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.HashSet;
2424
import java.util.List;
2525
import java.util.Map;
26-
import java.util.Set;
2726

2827
import org.dbunit.dataset.Column;
2928
import org.dbunit.dataset.DataSetException;
@@ -61,7 +60,7 @@ public class YamlDataSetProducer implements IDataSetProducer
6160

6261
public YamlDataSetProducer(InputStream inputStream)
6362
{
64-
this.input = inputStream;
63+
input = inputStream;
6564
}
6665

6766
@Override
@@ -152,7 +151,12 @@ private List<Table> createTables(Map<String, List<Map<String, String>>> yamlStru
152151

153152
private Collection<Row> extractRows(List<Map<String, String>> rows)
154153
{
155-
final List<Row> extractedRows = new ArrayList<Row>();
154+
final Collection<Row> extractedRows = new ArrayList<Row>();
155+
if (rows == null || rows.isEmpty())
156+
{
157+
return extractedRows;
158+
}
159+
156160
for (Map<String, String> row : rows)
157161
{
158162
extractedRows.add(new Row(row));
@@ -162,7 +166,12 @@ private Collection<Row> extractRows(List<Map<String, String>> rows)
162166

163167
private Collection<String> extractColumns(List<Map<String, String>> rows)
164168
{
165-
final Set<String> columns = new HashSet<String>();
169+
final Collection<String> columns = new HashSet<String>();
170+
if (rows == null || rows.isEmpty())
171+
{
172+
return columns;
173+
}
174+
166175
for (Map<String, String> row : rows)
167176
{
168177
columns.addAll(row.keySet());

impl/src/main/java/org/jboss/arquillian/persistence/script/ScriptExecutor.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,41 +70,39 @@ public class ScriptExecutor
7070

7171
private boolean fullLineDelimiter = false;
7272

73-
private List<String> statements = new ArrayList<String>();
74-
7573
public ScriptExecutor(final Connection connection)
7674
{
7775
this.connection = connection;
7876
}
7977

8078
public void execute(String script)
8179
{
80+
final List<String> statements = new ArrayList<String>();
8281
try
8382
{
8483
script = removeComments(script);
8584
final BufferedReader lineReader = new BufferedReader(new StringReader(script));
8685
final StringBuilder readSqlStatement = new StringBuilder();
8786
String line = null;
88-
statements.clear();
8987
while ((line = lineReader.readLine()) != null)
9088
{
9189
boolean isFullCommand = parseLine(line, readSqlStatement);
9290
if (isFullCommand)
9391
{
9492
if(multipleInlineStatements(line))
9593
{
96-
splitInlineStatements(line);
94+
statements.addAll(splitInlineStatements(line));
9795
}
9896
else
9997
{
100-
addStatement(readSqlStatement.toString());
98+
statements.add(readSqlStatement.toString().trim());
10199
}
102100
readSqlStatement.setLength(0);
103101
}
104102
}
105103
if (shouldExecuteRemainingStatements(readSqlStatement))
106104
{
107-
addStatement(readSqlStatement.toString());
105+
statements.add(readSqlStatement.toString().trim());
108106
}
109107
}
110108
catch (Exception e)
@@ -176,18 +174,15 @@ private boolean isNewLineStatementDelimiter()
176174
return ScriptingConfiguration.NEW_LINE_SYMBOL.equals(getStatementDelimiter());
177175
}
178176

179-
private void splitInlineStatements(String line)
177+
private List<String> splitInlineStatements(String line)
180178
{
179+
final List<String> statements = new ArrayList<String>();
181180
final StringTokenizer sqlStatements = new StringTokenizer(line, getStatementDelimiter());
182181
while (sqlStatements.hasMoreElements())
183182
{
184-
addStatement(sqlStatements.nextToken());
183+
statements.add(sqlStatements.nextToken().trim());
185184
}
186-
}
187-
188-
private void addStatement(String statement)
189-
{
190-
statements.add(statement.trim());
185+
return statements;
191186
}
192187

193188
private boolean multipleInlineStatements(String line)

int-tests/src/test/java/org/jboss/arquillian/integration/persistence/test/boundary/EmptyDataSetsTest.java

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717
*/
1818
package org.jboss.arquillian.integration.persistence.test.boundary;
1919

20-
import static org.fest.assertions.Assertions.assertThat;
21-
22-
import java.util.List;
23-
2420
import javax.persistence.EntityManager;
2521
import javax.persistence.PersistenceContext;
2622

2723
import org.jboss.arquillian.container.test.api.Deployment;
2824
import org.jboss.arquillian.integration.persistence.example.UserAccount;
2925
import org.jboss.arquillian.integration.persistence.util.Query;
26+
import org.jboss.arquillian.integration.persistence.util.UserPersistenceAssertion;
3027
import org.jboss.arquillian.junit.Arquillian;
3128
import org.jboss.arquillian.persistence.Cleanup;
3229
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
@@ -56,7 +53,7 @@ public static Archive<?> createDeploymentPackage()
5653
{
5754
return ShrinkWrap.create(WebArchive.class, "test.war")
5855
.addPackage(UserAccount.class.getPackage())
59-
.addClass(Query.class)
56+
.addClasses(Query.class, UserPersistenceAssertion.class)
6057
// required for remote containers in order to run tests with FEST-Asserts
6158
.addPackages(true, "org.fest")
6259
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
@@ -70,76 +67,67 @@ public static Archive<?> createDeploymentPackage()
7067
@UsingDataSet("empty/empty.yml")
7168
public void should_skip_empty_yaml_data_set() throws Exception
7269
{
73-
assertNoUserAccountsStored();
70+
new UserPersistenceAssertion(em).assertNoUserAccountsStored();
7471
}
7572

7673
@Test
7774
@UsingDataSet("empty/empty.json")
7875
public void should_skip_empty_json_data_set() throws Exception
7976
{
80-
assertNoUserAccountsStored();
77+
new UserPersistenceAssertion(em).assertNoUserAccountsStored();
8178
}
8279

8380
@Test
8481
@UsingDataSet("empty/empty.xml")
8582
public void should_skip_empty_xml_data_set() throws Exception
8683
{
87-
assertNoUserAccountsStored();
84+
new UserPersistenceAssertion(em).assertNoUserAccountsStored();
8885
}
8986

9087
@Test
9188
@UsingDataSet("empty/empty.xls")
9289
public void should_skip_empty_xls_data_set() throws Exception
9390
{
94-
assertNoUserAccountsStored();
91+
new UserPersistenceAssertion(em).assertNoUserAccountsStored();
92+
}
93+
94+
@Test
95+
@UsingDataSet("empty/empty-tables.yml")
96+
public void should_clean_when_yaml_with_empty_tables_provided() throws Exception
97+
{
98+
new UserPersistenceAssertion(em).assertNoUserAccountsStored();
9599
}
96100

97101
@Test(expected = AssertionError.class)
98102
@UsingDataSet("users.json")
99103
@ShouldMatchDataSet("empty/empty.yml")
100104
public void should_fail_when_empty_set_yaml_used_for_verifying_content_of_non_empty_database()
101105
{
102-
assertUserAccountsStored();
106+
new UserPersistenceAssertion(em).assertUserAccountsStored();
103107
}
104108

105109
@Test(expected = AssertionError.class)
106110
@UsingDataSet("users.yml")
107111
@ShouldMatchDataSet("empty/empty.json")
108112
public void should_fail_when_empty_set_json_used_for_verifying_content_of_non_empty_database() throws Exception
109113
{
110-
assertUserAccountsStored();
114+
new UserPersistenceAssertion(em).assertUserAccountsStored();
111115
}
112116

113117
@Test(expected = AssertionError.class)
114118
@UsingDataSet("users.xml")
115119
@ShouldMatchDataSet("empty/empty.xls")
116120
public void should_fail_when_empty_set_xls_used_for_verifying_content_of_non_empty_database() throws Exception
117121
{
118-
assertUserAccountsStored();
122+
new UserPersistenceAssertion(em).assertUserAccountsStored();
119123
}
120124

121125
@Test(expected = AssertionError.class)
122126
@UsingDataSet("users.xml")
123127
@ShouldMatchDataSet("empty/empty.xml")
124128
public void should_fail_when_empty_set_xml_used_for_verifying_content_of_non_empty_database() throws Exception
125129
{
126-
assertUserAccountsStored();
127-
}
128-
129-
// Private helper methods
130-
131-
private void assertUserAccountsStored()
132-
{
133-
@SuppressWarnings("unchecked")
134-
List<UserAccount> savedUserAccounts = em.createQuery(Query.selectAllInJPQL(UserAccount.class)).getResultList();
135-
assertThat(savedUserAccounts).isNotEmpty();
136-
}
137-
138-
private void assertNoUserAccountsStored()
139-
{
140-
@SuppressWarnings("unchecked")
141-
List<UserAccount> savedUserAccounts = em.createQuery(Query.selectAllInJPQL(UserAccount.class)).getResultList();
142-
assertThat(savedUserAccounts).isEmpty();
130+
new UserPersistenceAssertion(em).assertUserAccountsStored();
143131
}
144132

145133
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.jboss.arquillian.integration.persistence.test.boundary;
2+
3+
import javax.persistence.EntityManager;
4+
import javax.persistence.PersistenceContext;
5+
6+
import org.jboss.arquillian.container.test.api.Deployment;
7+
import org.jboss.arquillian.integration.persistence.example.UserAccount;
8+
import org.jboss.arquillian.integration.persistence.util.Query;
9+
import org.jboss.arquillian.integration.persistence.util.UserPersistenceAssertion;
10+
import org.jboss.arquillian.junit.Arquillian;
11+
import org.jboss.arquillian.junit.InSequence;
12+
import org.jboss.arquillian.persistence.Cleanup;
13+
import org.jboss.arquillian.persistence.TestExecutionPhase;
14+
import org.jboss.arquillian.persistence.UsingDataSet;
15+
import org.jboss.shrinkwrap.api.Archive;
16+
import org.jboss.shrinkwrap.api.ShrinkWrap;
17+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
18+
import org.jboss.shrinkwrap.api.spec.WebArchive;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
@RunWith(Arquillian.class)
23+
@Cleanup(phase = TestExecutionPhase.BEFORE)
24+
public class EmptyTablesJsonDataSetTest
25+
{
26+
27+
@Deployment
28+
public static Archive<?> createDeploymentPackage()
29+
{
30+
return ShrinkWrap.create(WebArchive.class, "test.war")
31+
.addPackage(UserAccount.class.getPackage())
32+
.addClasses(Query.class, UserPersistenceAssertion.class)
33+
// required for remote containers in order to run tests with FEST-Asserts
34+
.addPackages(true, "org.fest")
35+
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
36+
.addAsResource("test-persistence.xml", "META-INF/persistence.xml");
37+
}
38+
39+
@PersistenceContext
40+
EntityManager em;
41+
42+
@Test @InSequence(1)
43+
@UsingDataSet("users.json")
44+
public void should_fail_when_empty_set_yaml_used_for_verifying_content_of_non_empty_database()
45+
{
46+
new UserPersistenceAssertion(em).assertUserAccountsStored();
47+
}
48+
49+
@Test @InSequence(2)
50+
@UsingDataSet("empty/empty-tables.json")
51+
public void should_clean_when_yaml_with_empty_tables_provided() throws Exception
52+
{
53+
new UserPersistenceAssertion(em).assertNoUserAccountsStored();
54+
}
55+
56+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.jboss.arquillian.integration.persistence.test.boundary;
2+
3+
import javax.persistence.EntityManager;
4+
import javax.persistence.PersistenceContext;
5+
6+
import org.jboss.arquillian.container.test.api.Deployment;
7+
import org.jboss.arquillian.integration.persistence.example.UserAccount;
8+
import org.jboss.arquillian.integration.persistence.util.Query;
9+
import org.jboss.arquillian.integration.persistence.util.UserPersistenceAssertion;
10+
import org.jboss.arquillian.junit.Arquillian;
11+
import org.jboss.arquillian.junit.InSequence;
12+
import org.jboss.arquillian.persistence.Cleanup;
13+
import org.jboss.arquillian.persistence.CleanupStrategy;
14+
import org.jboss.arquillian.persistence.TestExecutionPhase;
15+
import org.jboss.arquillian.persistence.UsingDataSet;
16+
import org.jboss.shrinkwrap.api.Archive;
17+
import org.jboss.shrinkwrap.api.ShrinkWrap;
18+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
19+
import org.jboss.shrinkwrap.api.spec.WebArchive;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
23+
@RunWith(Arquillian.class)
24+
@Cleanup(phase = TestExecutionPhase.BEFORE)
25+
public class EmptyTablesYamlDataSetTest
26+
{
27+
28+
@Deployment
29+
public static Archive<?> createDeploymentPackage()
30+
{
31+
return ShrinkWrap.create(WebArchive.class, "test.war")
32+
.addPackage(UserAccount.class.getPackage())
33+
.addClasses(Query.class, UserPersistenceAssertion.class)
34+
// required for remote containers in order to run tests with FEST-Asserts
35+
.addPackages(true, "org.fest")
36+
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
37+
.addAsResource("test-persistence.xml", "META-INF/persistence.xml");
38+
}
39+
40+
@PersistenceContext
41+
EntityManager em;
42+
43+
@Test @InSequence(1)
44+
@UsingDataSet("users.json")
45+
public void should_fail_when_empty_set_yaml_used_for_verifying_content_of_non_empty_database()
46+
{
47+
new UserPersistenceAssertion(em).assertUserAccountsStored();
48+
}
49+
50+
@Test @InSequence(2)
51+
@UsingDataSet("empty/empty-tables.yml")
52+
@Cleanup(phase = TestExecutionPhase.BEFORE, strategy = CleanupStrategy.USED_TABLES_ONLY)
53+
public void should_clean_when_yaml_with_empty_tables_provided() throws Exception
54+
{
55+
new UserPersistenceAssertion(em).assertNoUserAccountsStored();
56+
}
57+
58+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.jboss.arquillian.integration.persistence.util;
2+
3+
import static org.fest.assertions.Assertions.assertThat;
4+
5+
import java.util.List;
6+
7+
import javax.persistence.EntityManager;
8+
9+
import org.jboss.arquillian.integration.persistence.example.UserAccount;
10+
11+
public class UserPersistenceAssertion
12+
{
13+
14+
private final EntityManager em;
15+
16+
public UserPersistenceAssertion(EntityManager em)
17+
{
18+
this.em = em;
19+
this.em.clear();
20+
}
21+
22+
public void assertUserAccountsStored()
23+
{
24+
@SuppressWarnings("unchecked")
25+
List<UserAccount> savedUserAccounts = em.createQuery(Query.selectAllInJPQL(UserAccount.class)).getResultList();
26+
assertThat(savedUserAccounts).isNotEmpty();
27+
}
28+
29+
public void assertNoUserAccountsStored()
30+
{
31+
@SuppressWarnings("unchecked")
32+
List<UserAccount> savedUserAccounts = em.createQuery(Query.selectAllInJPQL(UserAccount.class)).getResultList();
33+
assertThat(savedUserAccounts).isEmpty();
34+
}
35+
36+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"useraccount": [],
3+
"address": [],
4+
"useraccount_address": []
5+
}
6+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
useraccount:
2+
3+
address:
4+
5+
useraccount_address:

0 commit comments

Comments
 (0)