Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@
<version>4.0.1.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
<scope>test</scope> <!-- for standalone tests -->
</dependency>

<!-- Annotation processor to generate the JPA 2.0 metamodel classes
for typesafe criteria queries -->
Expand Down Expand Up @@ -192,6 +199,14 @@
<version>${version.junit}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.170</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class PersonRepository {

@PersistenceContext
private EntityManager em;
EntityManager em;

public Person createOrUpdatePerson(Person person) {
return em.merge(person);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static Archive<?> jar() {
return ShrinkWrap.create(WebArchive.class)
.addPackage(Person.class.getPackage())
.addPackage(PersonRepository.class.getPackage())
.addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
.addAsResource("META-INF/arquillian-persistence.xml","META-INF/persistence.xml")
.addAsWebInfResource("jbossas-ds.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package fr.ippon.companyfight.repository;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import fr.ippon.companyfight.model.Person;

public class PersonRepositoryStandaloneIT {

private PersonRepository personRepository;

private EntityManager em;
private static EntityManagerFactory entityManagerFactory;


@BeforeClass
public static void createEMF() throws Exception {
entityManagerFactory = Persistence.createEntityManagerFactory("primary");
}
@AfterClass
public static void destroyEMF() throws Exception {
entityManagerFactory.close();
}

@Before
public void preparePersistenceTest() throws Exception {
em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();

personRepository = new PersonRepository();
personRepository.em = em;
}

@After
public void rollbackTransaction() throws Exception {
em.getTransaction().rollback();
em = null;
}

@Test
public void shouldCreateAPerson() {
Person person = new Person();
person.setLogin("test-login");
person.setFollowersCount(10);
person.setAvatarUrl("http://avatar.url");

personRepository.createOrUpdatePerson(person);

Person testPerson = personRepository.findPerson("test-login");
assertNotNull(testPerson);
assertEquals(10, testPerson.getFollowersCount());
assertEquals("http://avatar.url", testPerson.getAvatarUrl());
assertEquals(0, testPerson.getRepositories().size());
}

@Test
public void shouldGetAllPersons() {
List<Person> persons = personRepository.findAll();
assertEquals(0, persons.size());

create10Persons();

persons = personRepository.findAll();
assertEquals(10, persons.size());
}

@Test
public void shouldGetPersonsOrderedByFollowersCount() {
List<Person> persons = personRepository.findPersonsOrderedByFollowersCount();
assertEquals(0, persons.size());

create10Persons();

Person person = new Person();
person.setLogin("test-login-11");
person.setFollowersCount(10);
person.setAvatarUrl("http://avatar.url");
personRepository.createOrUpdatePerson(person);

person = new Person();
person.setLogin("test-login-12");
person.setFollowersCount(0);
person.setAvatarUrl("http://avatar.url");
personRepository.createOrUpdatePerson(person);

persons = personRepository.findPersonsOrderedByFollowersCount();
assertEquals(12, persons.size());
// test-login-11 should be the first user
assertEquals("test-login-11", persons.iterator().next().getLogin());

// test-login-12 should be the last user
assertEquals("test-login-12", persons.get(persons.size() - 1).getLogin());
}

private void create10Persons() {
// Create 10 persons
for (int i = 0; i < 10; i++) {
Person person = new Person();
person.setLogin("test-login-"+i);
person.setFollowersCount(5);
person.setAvatarUrl("http://avatar.url");
personRepository.createOrUpdatePerson(person);
}
}
}
18 changes: 18 additions & 0 deletions src/test/resources/META-INF/arquillian-persistence.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<jta-data-source>java:jboss/datasources/test/CompanyFight</jta-data-source>
<properties>
<!-- Properties for Hibernate-->
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
23 changes: 20 additions & 3 deletions src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<!-- Only for testing purpose outside of the application server ... -->

<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<jta-data-source>java:jboss/datasources/test/CompanyFight</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<!-- note : in Java SE, the module is normally not scanned for entities classes -->
<!-- That being said, Hibernate do scan the directory where it founds persistence.xml in Java SE env
but as test ressources are not in the same directory than the entities, he can't find them for the tests
So we have to list the entities here : -->
<class>fr.ippon.companyfight.model.Person</class>
<class>fr.ippon.companyfight.model.Organization</class>
<class>fr.ippon.companyfight.model.Fight</class>
<class>fr.ippon.companyfight.model.Repository</class>
<class>fr.ippon.companyfight.model.Score</class>

<properties>
<!-- Properties for Hibernate-->
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:mem:junit-test;DB_CLOSE_DELAY=-1" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="sa" />

<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
Expand Down