Skip to content

Commit

Permalink
HSEARCH-940 Add Arquillian + JBossAS 7 test case
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD authored and Sanne committed Oct 11, 2011
1 parent 121fd59 commit 8a4c069
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat, Inc. and/or its affiliates or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat, Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.search.test.integration.jbossas7;

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

import java.util.List;

import javax.inject.Inject;

import org.hibernate.search.Version;
import org.hibernate.search.test.integration.jbossas7.controller.MemberRegistration;
import org.hibernate.search.test.integration.jbossas7.model.Member;
import org.hibernate.search.test.integration.jbossas7.util.Resources;
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.Asset;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence.PersistenceDescriptor;
import org.jboss.shrinkwrap.resolver.api.DependencyResolvers;
import org.jboss.shrinkwrap.resolver.api.maven.MavenDependencyResolver;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Example of an integration test using JBoss AS 7 and Arquillian.
*
* @author Davide D'Alto
* @author Sanne Grinovero
*/
@RunWith(Arquillian.class)
public class MemberRegistrationIT {

@Deployment
public static Archive<?> createTestArchive() {
String currentVersion = Version.getVersionString();
return ShrinkWrap
.create( WebArchive.class, MemberRegistrationIT.class.getSimpleName() + ".war" )
.addClasses( Member.class, MemberRegistration.class, Resources.class )
.addAsResource( persistenceXml(), "META-INF/persistence.xml" )
.addAsLibraries(
DependencyResolvers.use( MavenDependencyResolver.class )
.artifact( "org.hibernate:hibernate-search-orm:" + currentVersion )
.exclusion( "org.hibernate:hibernate-entitymanager" )
.exclusion( "org.hibernate:hibernate-core" )
.exclusion( "org.hibernate:hibernate-search-analyzers" )
.exclusion( "org.hibernate.common:hibernate-commons-annotations" )
.exclusion( "org.jboss.logging:jboss-logging" )
.resolveAs( JavaArchive.class ) )
.addAsWebInfResource( EmptyAsset.INSTANCE, "beans.xml" );
}

private static Asset persistenceXml() {
String persistenceXml = Descriptors.create( PersistenceDescriptor.class )
.version( "2.0" )
.persistenceUnit( "primary" )
.jtaDataSource( "java:jboss/datasources/ExampleDS" )
.property( "hibernate.hbm2ddl.auto", "create-drop" )
.property( "hibernate.search.default.directory_provider", "ram" )
.property( "hibernate.search.lucene_version", "LUCENE_CURRENT")
.exportAsString();
return new StringAsset( persistenceXml );
}

@Inject
MemberRegistration memberRegistration;

@Test
public void testRegister() throws Exception {
Member newMember = memberRegistration.getNewMember();
newMember.setName( "Davide D'Alto" );
newMember.setEmail( "davide@mailinator.com" );
newMember.setPhoneNumber( "2125551234" );
memberRegistration.register();

assertNotNull( newMember.getId() );
}

@Test
public void testNewMemberSearch() throws Exception {
Member newMember = memberRegistration.getNewMember();
newMember.setName( "Peter O'Tall" );
newMember.setEmail( "peter@mailinator.com" );
newMember.setPhoneNumber( "4643646643" );
memberRegistration.register();

List<Member> search = memberRegistration.search( "Peter" );

assertFalse( "Expected at least one result after the indexing", search.isEmpty() );
assertEquals( "Search hasn't found a new member", newMember.getName(), search.get( 0 ).getName() );
}

@Test
public void testUnexistingMember() throws Exception {
List<Member> search = memberRegistration.search( "TotallyInventedName" );

assertNotNull( "Search should never return null", search );
assertTrue( "Search results should be empty", search.isEmpty() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat, Inc. and/or its affiliates or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat, Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.search.test.integration.jbossas7.controller;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
import javax.enterprise.inject.Model;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.lucene.search.Query;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.test.integration.jbossas7.model.Member;

@Stateful
@Model
public class MemberRegistration {

@Inject
private FullTextEntityManager em;

private Member newMember;

@Produces
@Named
public Member getNewMember() {
return newMember;
}

public void register() throws Exception {
em.persist( newMember );
initNewMember();
}

@SuppressWarnings("unchecked")
public List<Member> search(String name) {
Query luceneQuery = em.getSearchFactory().buildQueryBuilder()
.forEntity( Member.class ).get().keyword()
.onField( "name" ).matching( name )
.createQuery();

return em.createFullTextQuery( luceneQuery ).getResultList();
}

@PostConstruct
public void initNewMember() {
newMember = new Member();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat, Inc. and/or its affiliates or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat, Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.search.test.integration.jbossas7.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

@Entity
@Indexed
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class Member implements Serializable {

/** Default value included to remove warning. Remove or modify at will. **/
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Long id;

@Field(index = Index.YES, store = Store.NO)
private String name;

@Field(index = Index.YES, store = Store.NO)
private String email;

@Column(name = "phone_number")
private String phoneNumber;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat, Inc. and/or its affiliates or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat, Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.search.test.integration.jbossas7.util;

import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;

/**
* This class uses CDI to alias Java EE resources, such as the persistence context, to CDI beans
*
* <p>
* Example injection on a managed bean field:
* </p>
*
* <pre>
* &#064;Inject
* private EntityManager em;
* </pre>
*/
public class Resources {

@Produces
@PersistenceContext
private EntityManager em;

@SuppressWarnings("unused")
@Produces
private FullTextEntityManager getFullTextEntityManager() {
return Search.getFullTextEntityManager( em );
}
}
20 changes: 20 additions & 0 deletions hibernate-search-integrationtest/src/test/resources/arquillian.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

<engine>
<property name="deploymentExportPath">target/</property>
</engine>

<container qualifier="jboss" default="true">
<protocol type="jmx-as7">
<property name="executionType">REMOTE</property>
</protocol>
<configuration>
<property name="jbossHome">target/jboss-as-7.0.2.Final</property>
</configuration>
</container>

</arquillian>

0 comments on commit 8a4c069

Please sign in to comment.