Skip to content

Commit

Permalink
ARQ-1107 Add Persistence Extension Spring integration Test Case
Browse files Browse the repository at this point in the history
Re-Enable disabled test case from ARQ-1806 as a Persistence Extension
test.
  • Loading branch information
aslakknutsen committed Jun 26, 2014
1 parent baafbe2 commit 7738081
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 66 deletions.
18 changes: 18 additions & 0 deletions arquillian-service-integration-spring-3-int-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-spring</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-dbunit</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-transaction-jta</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.persistence.Cleanup;
import org.jboss.arquillian.persistence.CleanupStrategy;
import org.jboss.arquillian.persistence.DataSource;
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
import org.jboss.arquillian.persistence.UsingDataSet;
import org.jboss.arquillian.spring.integration.test.annotation.SpringConfiguration;
import org.jboss.arquillian.spring.testsuite.beans.model.Employee;
import org.jboss.arquillian.spring.testsuite.beans.repository.EmployeeRepository;
import org.jboss.arquillian.transaction.api.annotation.Transactional;
import org.jboss.shrinkwrap.api.Archive;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -47,10 +43,10 @@
*
* @author <a href="mailto:[email protected]">Jakub Narloch</a>
*/
@Ignore // ARQ-1805 - temp
@RunWith(Arquillian.class)
@Transactional(manager = "txManager")
@SpringConfiguration("applicationContext-jpa.xml")
@DataSource("springDataSource")
public class JpaEmployeeRepositoryTestCase {

/**
Expand All @@ -71,27 +67,12 @@ public static Archive createTestArchive() {
@Qualifier("jpaEmployeeRepository")
private EmployeeRepository employeeRepository;

/**
* <p>{@link javax.persistence.EntityManager} instance used by tests.</p>
*/
@PersistenceContext
private EntityManager entityManager;

/**
* <p>Tears down the test environment.</p>
*/
@After
public void tearDown() {

entityManager.createNativeQuery("delete from Employee").executeUpdate();
}

/**
* <p>Tests the {@link org.jboss.arquillian.spring.testsuite.beans.repository.EmployeeRepository#save(org.jboss.arquillian.spring.testsuite.beans.model.Employee)}
* method.</p>
*/
@Test
@SuppressWarnings("unchecked")
@ShouldMatchDataSet(value = "employee.yml", excludeColumns = "id")
public void testSave() {

Employee employee = new Employee();
Expand All @@ -103,11 +84,6 @@ public void testSave() {
employee.setName("New employee");

employeeRepository.save(employee);

List<Employee> result = entityManager.createQuery("from Employee").getResultList();

assertNotNull("Method returned null list as result.", result);
assertEquals("Two employees were expected.", 2, result.size());
}

/**
Expand All @@ -117,46 +93,13 @@ public void testSave() {
* @throws Exception if any error occurs
*/
@Test
@UsingDataSet("employee.yml")
@Cleanup(strategy = CleanupStrategy.USED_TABLES_ONLY)
public void testGetEmployees() throws Exception {

runScript(entityManager, "insert.sql");

List<Employee> result = employeeRepository.getEmployees();

assertNotNull("Method returned null list as result.", result);
assertEquals("Two employees were expected.", 2, result.size());
}

/**
* <p>Executes a sql script.</p>
*
* @param entityManager the entity manager
* @param fileName the file name
*
* @throws java.io.IOException if any error occurs
*/
public static void runScript(EntityManager entityManager, String fileName) throws IOException {

// retrieves the resource from class path
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);

BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));

// loads the entire file
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = inputReader.readLine()) != null) {

if (!line.startsWith("--")) {
stringBuilder.append(line);
}
}

// splits the commands by semicolon
String[] commands = stringBuilder.toString().split(";");
for (final String command : commands) {

entityManager.createNativeQuery(command).executeUpdate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<!-- Lookups the entity manager factory -->
<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/ArquillianTestUnit"/>
<jee:jndi-lookup id="springDataSource" jndi-name="jdbc/ArquillianSpringDataSource"/>

<!-- Enables the declarative transaction support -->
<tx:annotation-driven transaction-manager="txManager"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
employee:
- id: 1
name: 'Test employee'
- id: 2
name: 'New employee'

0 comments on commit 7738081

Please sign in to comment.