-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AbstractLuceneIndexTest.java
193 lines (168 loc) · 5.26 KB
/
AbstractLuceneIndexTest.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
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.index.impl.lucene;
import java.util.Map;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.RelationshipIndex;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.test.TestGraphDatabaseFactory;
public abstract class AbstractLuceneIndexTest
{
@Rule
public final TestName testname = new TestName();
protected static GraphDatabaseService graphDb;
protected Transaction tx;
@BeforeClass
public static void setUpStuff()
{
graphDb = new TestGraphDatabaseFactory().newImpermanentDatabase();
}
@AfterClass
public static void tearDownStuff()
{
graphDb.shutdown();
}
@After
public void commitTx()
{
finishTx( true );
}
public void rollbackTx()
{
finishTx( false );
}
public void finishTx( boolean success )
{
if ( tx != null )
{
if ( success )
{
tx.success();
}
tx.close();
tx = null;
}
}
@Before
public void beginTx()
{
if ( tx == null )
{
tx = graphDb.beginTx();
}
}
void restartTx()
{
commitTx();
beginTx();
}
protected static abstract interface EntityCreator<T extends PropertyContainer>
{
T create( Object... properties );
void delete( T entity );
}
private static final RelationshipType TEST_TYPE =
RelationshipType.withName( "TEST_TYPE" );
protected static final EntityCreator<Node> NODE_CREATOR = new EntityCreator<Node>()
{
public Node create( Object... properties )
{
Node node = graphDb.createNode();
setProperties( node, properties );
return node;
}
public void delete( Node entity )
{
entity.delete();
}
};
protected static final EntityCreator<Relationship> RELATIONSHIP_CREATOR =
new EntityCreator<Relationship>()
{
public Relationship create( Object... properties )
{
Relationship rel = graphDb.createNode().createRelationshipTo( graphDb.createNode(), TEST_TYPE );
setProperties( rel, properties );
return rel;
}
public void delete( Relationship entity )
{
entity.delete();
}
};
static class FastRelationshipCreator implements EntityCreator<Relationship>
{
private Node node, otherNode;
public Relationship create( Object... properties )
{
if ( node == null )
{
node = graphDb.createNode();
otherNode = graphDb.createNode();
}
Relationship rel = node.createRelationshipTo( otherNode, TEST_TYPE );
setProperties( rel, properties );
return rel;
}
public void delete( Relationship entity )
{
entity.delete();
}
}
private static void setProperties( PropertyContainer entity, Object... properties )
{
for ( Map.Entry<String, Object> entry : MapUtil.map( properties ).entrySet() )
{
entity.setProperty( entry.getKey(), entry.getValue() );
}
}
protected Index<Node> nodeIndex( Map<String, String> config )
{
return nodeIndex( currentIndexName(), config );
}
protected Index<Node> nodeIndex( String name, Map<String, String> config )
{
return graphDb.index().forNodes( name, config );
}
protected RelationshipIndex relationshipIndex( Map<String, String> config )
{
return relationshipIndex( currentIndexName(), config );
}
protected RelationshipIndex relationshipIndex( String name, Map<String, String> config )
{
return graphDb.index().forRelationships( name, config );
}
protected String currentIndexName()
{
return testname.getMethodName();
}
}