Skip to content

Commit

Permalink
HSEARCH-1332 Add integration test for Solr JBoss AS module
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed May 23, 2013
1 parent 95212c6 commit a188213
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 1 deletion.
@@ -0,0 +1,154 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* 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,
* v.2.1 along with this distribution; if not, write to the 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 junit.framework.Assert;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.hibernate.search.jpa.FullTextEntityManager;
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.model.SolrMember;
import org.hibernate.search.test.integration.jbossas7.util.Resources;
import org.hibernate.search.util.AnalyzerUtils;
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.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.persistence20.PersistenceDescriptor;
import org.jboss.shrinkwrap.descriptor.api.spec.se.manifest.ManifestDescriptor;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Test the Solr JBoss AS module.
*
* @author Davide D'Alto
* @author Sanne Grinovero
*/
@RunWith(Arquillian.class)
public class SolrModuleMemberRegistrationIT {

@Deployment
public static Archive<?> createTestArchive() {
WebArchive archive = ShrinkWrap
.create( WebArchive.class, SolrModuleMemberRegistrationIT.class.getSimpleName() + ".war" )
.addClasses( Member.class, SolrMember.class, MemberRegistration.class, Resources.class, AnalyzerUtils.class )
.addAsResource( persistenceXml(), "META-INF/persistence.xml" )
.add( manifest(), "META-INF/MANIFEST.MF" )
.addAsWebInfResource( EmptyAsset.INSTANCE, "beans.xml" );
return archive;
}

private static Asset manifest() {
String manifest = Descriptors.create( ManifestDescriptor.class )
.attribute( "Dependencies", "org.hibernate.search.orm services, org.apache.solr:3.6.2" )
.exportAsString();
return new StringAsset( manifest );
}

private static Asset persistenceXml() {
String persistenceXml = Descriptors.create( PersistenceDescriptor.class )
.version( "2.0" )
.createPersistenceUnit()
.name( "primary" )
.jtaDataSource( "java:jboss/datasources/ExampleDS" )
.getOrCreateProperties()
.createProperty().name( "hibernate.hbm2ddl.auto" ).value( "create-drop" ).up()
.createProperty().name( "hibernate.search.default.lucene_version" ).value( "LUCENE_CURRENT" ).up()
.createProperty().name( "hibernate.search.default.directory_provider" ).value( "ram" ).up()
.createProperty().name( "hibernate.search.autoregister_listeners" ).value( "true" ).up()
.up().up()
.exportAsString();
return new StringAsset( persistenceXml );
}

@Inject
MemberRegistration memberRegistration;

@Inject
FullTextEntityManager em;

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

assertNotNull( newMember.getId() );
}

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

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() );
}

@Test
public void testCustomAnalyzerExists() throws Exception {
Analyzer analyzer = em.getSearchFactory().getAnalyzer( "customanalyzer" );
String text = "This iS just FOOBAR's";
Token[] tokens = AnalyzerUtils.tokensFromAnalysis( analyzer, "name", text );
assertTokensEqual( tokens, new String[] { "this", "is", "just", "foobar's" } );
}

private static void assertTokensEqual(Token[] tokens, String[] strings) {
Assert.assertEquals( strings.length, tokens.length );

for ( int i = 0; i < tokens.length; i++ ) {
Assert.assertEquals( "index " + i, strings[i], AnalyzerUtils.getTermText( tokens[i] ) );
}
}

}
Expand Up @@ -52,7 +52,11 @@ public Member getNewMember() {
}

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

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

Expand Down
@@ -0,0 +1,46 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* 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,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.hibernate.search.test.integration.jbossas7.model;

import static javax.persistence.InheritanceType.TABLE_PER_CLASS;

import javax.persistence.Entity;
import javax.persistence.Inheritance;

import org.apache.solr.analysis.LowerCaseFilterFactory;
import org.apache.solr.analysis.StandardTokenizerFactory;
import org.hibernate.search.annotations.AnalyzerDef;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.TokenFilterDef;
import org.hibernate.search.annotations.TokenizerDef;

@Entity
@Indexed
@Inheritance(strategy = TABLE_PER_CLASS)
@AnalyzerDef(
name = "customanalyzer",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = { @TokenFilterDef(factory = LowerCaseFilterFactory.class) })
public class SolrMember extends Member {

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

0 comments on commit a188213

Please sign in to comment.