package com.tulliuswalden.infinispantest; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import java.lang.annotation.ElementType; import java.util.Properties; import org.apache.lucene.search.Query; import org.hibernate.search.Environment; import org.hibernate.search.SearchException; import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.ProvidedId; import org.hibernate.search.cfg.SearchMapping; import org.hibernate.search.query.dsl.QueryBuilder; import org.infinispan.Cache; import org.infinispan.configuration.cache.Configuration; import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.manager.DefaultCacheManager; import org.infinispan.query.CacheQuery; import org.infinispan.query.Search; import org.infinispan.query.SearchManager; import org.junit.Test; public class ISPN1820Test { /** * Here we use SearchMapping to have the ability to add Objects to the cache where Annotations are not possible. */ @Test public void testSearchMapping() { final SearchMapping mapping = new SearchMapping(); mapping .entity(BondPVO.class) .indexed() .providedId() .property("id", ElementType.METHOD) .property("name", ElementType.METHOD) .property("isin", ElementType.METHOD) ; final Properties properties = new Properties(); properties.put("hibernate.search.default.directory_provider", "ram"); properties.put(Environment.MODEL_MAPPING, mapping); final Configuration config = new ConfigurationBuilder() .indexing() .enable() .indexLocalOnly(true) .withProperties(properties) .build(); final DefaultCacheManager cacheManager = new DefaultCacheManager(config); final Cache cache = cacheManager.getCache(); final SearchManager sm = Search.getSearchManager(cache); final BondPVO bond = new BondPVO(1, "Test", "DE000123"); try { cache.put(bond.getId(), bond); } catch (final SearchException e) { fail("SearchMapping not working: " + e.getMessage()); } final QueryBuilder qb = sm.buildQueryBuilderForClass(BondPVO.class).get(); final Query q = qb.keyword().onField("name").matching("Test").createQuery(); final CacheQuery cq = sm.getQuery(q, BondPVO.class); assertEquals(1, cq.getResultSize()); } public static final class BondPVO { private long id; private String name; private String isin; public BondPVO(long id, String name, String isin) { super(); this.id = id; this.name = name; this.isin = isin; } 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 getIsin() { return isin; } public void setIsin(String isin) { this.isin = isin; } } /** * Here we show that the first test could work. */ @Test public void testWithoutSearchMapping() { final Properties properties = new Properties(); properties.put("hibernate.search.default.directory_provider", "ram"); final Configuration config = new ConfigurationBuilder() .indexing() .enable() .indexLocalOnly(true) .withProperties(properties) .build(); final DefaultCacheManager cacheManager = new DefaultCacheManager(config); final Cache cache = cacheManager.getCache(); final SearchManager sm = Search.getSearchManager(cache); final BondPVO2 bond = new BondPVO2(1, "Test", "DE000123"); cache.put(bond.getId(), bond); final QueryBuilder qb = sm.buildQueryBuilderForClass(BondPVO2.class).get(); final Query q = qb.keyword().onField("name").matching("Test").createQuery(); final CacheQuery cq = sm.getQuery(q, BondPVO2.class); assertEquals(1, cq.getResultSize()); } @Indexed @ProvidedId public static final class BondPVO2 { @Field private long id; @Field private String name; @Field private String isin; public BondPVO2(long id, String name, String isin) { super(); this.id = id; this.name = name; this.isin = isin; } 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 getIsin() { return isin; } public void setIsin(String isin) { this.isin = isin; } } }