Created
May 27, 2012 09:45
-
-
Save koduki/2803105 to your computer and use it in GitHub Desktop.
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
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package cn.orz.pascal.blog.dao; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
import javax.inject.Inject; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import javax.transaction.UserTransaction; | |
/** | |
* | |
* @author hiro | |
*/ | |
public class AbstractJPATest { | |
public AbstractJPATest() { | |
} | |
@PersistenceContext | |
EntityManager em; | |
@Inject | |
UserTransaction utx; | |
/** | |
* delete entites record. | |
* | |
* @param <T> entity type. | |
* @param entity entity type class. | |
* @throws Exception | |
*/ | |
protected <T> void clearData(Class<T> entity) throws Exception { | |
utx.begin(); | |
em.joinTransaction(); | |
System.out.println("Dumping old records..."); | |
em.createQuery("delete from " + entity.getSimpleName()).executeUpdate(); | |
utx.commit(); | |
} | |
/** | |
* simle list sort by property. | |
* | |
* @param <T> entity type. | |
* @param list base list. | |
* @param property sort key property name. | |
* @return sorted new list. | |
*/ | |
protected <T> List<T> simpleSort(List<T> list, final String property) { | |
List<T> result = new ArrayList<T>(list); | |
Collections.sort(result, new Comparator<T>() { | |
@Override | |
public int compare(T o1, T o2) { | |
try { | |
Method m = o1.getClass().getMethod("get" + property, null); | |
Comparable value1 = (Comparable) m.invoke(o1, null); | |
Comparable value2 = (Comparable) m.invoke(o2, null); | |
return value1.compareTo(value2); | |
} catch (NoSuchMethodException ex) { | |
throw new RuntimeException(ex); | |
} catch (SecurityException ex) { | |
throw new RuntimeException(ex); | |
} catch (IllegalAccessException ex) { | |
throw new RuntimeException(ex); | |
} catch (IllegalArgumentException ex) { | |
throw new RuntimeException(ex); | |
} catch (InvocationTargetException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
}); | |
return result; | |
} | |
} |
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
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package cn.orz.pascal.blog.dao; | |
import cn.orz.pascal.blog.entity.Article; | |
import static org.junit.Assert.*; | |
import static org.hamcrest.CoreMatchers.*; | |
import static org.hamcrest.Matchers.greaterThan; | |
import org.junit.*; | |
import org.jboss.arquillian.container.test.api.Deployment; | |
import org.jboss.arquillian.junit.Arquillian; | |
import org.jboss.shrinkwrap.api.Archive; | |
import org.jboss.shrinkwrap.api.ShrinkWrap; | |
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | |
import org.jboss.shrinkwrap.api.spec.WebArchive; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import javax.ejb.EJB; | |
import javax.inject.Inject; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import javax.transaction.UserTransaction; | |
@RunWith(Arquillian.class) | |
public class ArticleFacadeTest { | |
@PersistenceContext | |
EntityManager em; | |
@Inject | |
UserTransaction utx; | |
public ArticleFacadeTest() { | |
} | |
@Deployment | |
public static Archive<?> createDeployment() { | |
return ShrinkWrap.create(WebArchive.class, "test.war"). | |
addPackage(Article.class.getPackage()). | |
addPackage(ArticleFacade.class.getPackage()). | |
addAsResource("META-INF/persistence.xml"). | |
addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); | |
} | |
@EJB | |
ArticleFacade articleFacade; | |
@Test | |
public void count0_Test() throws Exception { | |
assertThat(articleFacade.count(), is(0)); | |
} | |
} |
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>cn.orz.pascal</groupId> | |
<artifactId>Blog</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>war</packaging> | |
<name>Blog</name> | |
<properties> | |
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.jboss.arquillian</groupId> | |
<artifactId>arquillian-bom</artifactId> | |
<version>1.0.0.Final</version> | |
<scope>import</scope> | |
<type>pom</type> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<repositories> | |
<repository> | |
<id>jboss-deprecated-repository</id> | |
<name>JBoss Deprecated Maven Repository</name> | |
<url>https://repository.jboss.org/nexus/content/repositories/deprecated/</url> | |
<layout>default</layout> | |
<releases> | |
<enabled>true</enabled> | |
<updatePolicy>never</updatePolicy> | |
</releases> | |
<snapshots> | |
<enabled>false</enabled> | |
<updatePolicy>never</updatePolicy> | |
</snapshots> | |
</repository> | |
<repository> | |
<id>JBOSS_NEXUS</id> | |
<url>http://repository.jboss.org/nexus/content/groups/public</url> | |
</repository> | |
<repository> | |
<url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url> | |
<id>eclipselink</id> | |
<layout>default</layout> | |
<name>Repository for library Library[eclipselink]</name> | |
</repository> | |
</repositories> | |
<dependencies> | |
<dependency> | |
<groupId>org.eclipse.persistence</groupId> | |
<artifactId>eclipselink</artifactId> | |
<version>2.3.0</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.persistence</groupId> | |
<artifactId>javax.persistence</artifactId> | |
<version>2.0.3</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.persistence</groupId> | |
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId> | |
<version>2.3.0</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.main.extras</groupId> | |
<artifactId>glassfish-embedded-all</artifactId> | |
<version>3.1.2</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>javax</groupId> | |
<artifactId>javaee-web-api</artifactId> | |
<version>6.0</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.10</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.hamcrest</groupId> | |
<artifactId>hamcrest-all</artifactId> | |
<version>1.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.arquillian.junit</groupId> | |
<artifactId>arquillian-junit-container</artifactId> | |
<version>1.0.0.Final</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.arquillian.container</groupId> | |
<artifactId>arquillian-glassfish-embedded-3.1</artifactId> | |
<version>1.0.0.Final-SNAPSHOT</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.2</version> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
<compilerArguments> | |
<endorseddirs>${endorsed.dir}</endorseddirs> | |
</compilerArguments> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-war-plugin</artifactId> | |
<version>2.1.1</version> | |
<configuration> | |
<failOnMissingWebXml>false</failOnMissingWebXml> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-dependency-plugin</artifactId> | |
<version>2.1</version> | |
<executions> | |
<execution> | |
<phase>validate</phase> | |
<goals> | |
<goal>copy</goal> | |
</goals> | |
<configuration> | |
<outputDirectory>${endorsed.dir}</outputDirectory> | |
<silent>true</silent> | |
<artifactItems> | |
<artifactItem> | |
<groupId>javax</groupId> | |
<artifactId>javaee-endorsed-api</artifactId> | |
<version>6.0</version> | |
<type>jar</type> | |
</artifactItem> | |
</artifactItems> | |
</configuration> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment