/
StoredIndexTest.java
182 lines (167 loc) · 5.84 KB
/
StoredIndexTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2012 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.infinispan;
import java.util.List;
import junit.framework.Assert;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.hibernate.Transaction;
import org.hibernate.cfg.Environment;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.test.util.FullTextSessionBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Verifies we're able to start from an existing index in Infinispan,
* stored in a CacheLoader. Requires a persistent database so that
* we can shutdown the SessionFactory and start over again
* (simulated via a custom H2 service)
*
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2012 Red Hat Inc.
*/
public class StoredIndexTest {
private FullTextSessionBuilder node;
@Test
public void testRestartingNode() {
// Run 1 of application:
startNode( true );
try {
storeEmail( "there are some problems on this planet!" );
assertEmailsFound( "some", 1 );
}
finally {
//shutdown
stopNode();
}
// Restart same application:
startNode( false );
try {
assertEmailsFound( "some", 1 );
storeEmail( "stored stuff should not vanish on this planet" );
assertEmailsFound( "stuff", 1 );
assertEmailsFound( "some", 1 );
assertEmailsFound( "planet", 2 );
}
finally {
cleanupStoredIndex();
stopNode();
}
}
/**
* Verifies a query on a specific term returns an expected amount of results.
* We do actually load entities from database, so both database and index are tested.
*
* @param termMatch
* @param expectedMatches
*/
private void assertEmailsFound(String termMatch, int expectedMatches) {
FullTextSession fullTextSession = node.openFullTextSession();
try {
TermQuery termQuery = new TermQuery( new Term( "message", termMatch ) );
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( termQuery, SimpleEmail.class );
List<SimpleEmail> list = fullTextQuery.list();
Assert.assertEquals( expectedMatches, list.size() );
if ( expectedMatches != 0 ) {
Assert.assertEquals( "complaints-office@world.com", list.get( 0 ).to );
}
}
finally {
fullTextSession.close();
}
}
/**
* Saves a new test email
*/
private void storeEmail(String content) {
SimpleEmail email = new SimpleEmail();
email.to = "complaints-office@world.com";
email.message = content;
FullTextSession fullTextSession = node.openFullTextSession();
Transaction transaction = fullTextSession.beginTransaction();
fullTextSession.save( email );
transaction.commit();
fullTextSession.close();
}
/**
* Creates a new SessionFactory using a shared H2 connection pool, and running
* an Infinispan Directory storing the index in memory and write-through filesystem.
* @param createSchema set to false to not drop an existing schema
*/
private void startNode(boolean createSchema) {
node = new FullTextSessionBuilder()
.setProperty( "hibernate.search.default.directory_provider", "infinispan" )
.setProperty( CacheManagerServiceProvider.INFINISPAN_CONFIGURATION_RESOURCENAME, "filesystem-loading-infinispan.xml" )
// avoid killing the schema when you still have to run the second node:
.setProperty( Environment.HBM2DDL_AUTO, createSchema ? "create" : "validate" )
// share the same in-memory database connection pool
.setProperty(
Environment.CONNECTION_PROVIDER,
org.hibernate.search.infinispan.ClusterSharedConnectionProvider.class.getName()
)
.addAnnotatedClass( SimpleEmail.class )
.build();
}
/**
* Closes the SessionFactory, SearchFactory and Infinispan CacheManagers.
* Only service to survive is the H2 in memory database, and the data stored by the
* CacheLoader enabled in the Infinispan configuration.
*/
public void stopNode() {
if ( node != null ) {
node.close();
node = null;
}
}
/**
* This test uses and Infinispan CacheLoader writing in $tmp directory.
* Make sure we at least clear the index so that subsequent runs of the same
* test won't fail.
*/
private void cleanupStoredIndex() {
FullTextSession fullTextSession = node.openFullTextSession();
try {
Transaction transaction = fullTextSession.beginTransaction();
fullTextSession.purgeAll( SimpleEmail.class );
transaction.commit();
}
finally {
fullTextSession.close();
}
}
/**
* We need to use the custom H2 connector pool to make sure that when shutting
* down the first node we don't kill the database with all data we need
* for the second phase of the test.
*/
@BeforeClass
public static void prepareConnectionPool() {
ClusterSharedConnectionProvider.realStart();
}
/**
* Kills the static connection pool of H2 started by {@link #prepareConnectionPool()}
*/
@AfterClass
public static void shutdownConnectionPool() {
ClusterSharedConnectionProvider.realStop();
}
}