-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NodeTest.java
487 lines (444 loc) · 14.5 KB
/
NodeTest.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
* 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.kernel.impl.core;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.lang.Thread.State;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.neo4j.graphdb.ConstraintViolationException;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.test.DatabaseRule;
import org.neo4j.test.GraphTransactionRule;
import org.neo4j.test.ImpermanentDatabaseRule;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.neo4j.helpers.Exceptions.launderedException;
public class NodeTest
{
@ClassRule
public static DatabaseRule db = new ImpermanentDatabaseRule();
@Rule
public GraphTransactionRule tx = new GraphTransactionRule( db );
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void shouldGiveHelpfulExceptionWhenDeletingNodeWithRels() throws Exception
{
// Given
Node node;
node = db.createNode();
Node node2 = db.createNode();
node.createRelationshipTo( node2, RelationshipType.withName( "MAYOR_OF" ) );
tx.success();
// And given a transaction deleting just the node
tx.begin();
node.delete();
// Expect
exception.expect( ConstraintViolationException.class );
exception.expectMessage( "Cannot delete node<"+node.getId()+">, because it still has relationships. " +
"To delete this node, you must first delete its relationships." );
// When I commit
tx.success();
}
@Test
public void testNodeCreateAndDelete()
{
Node node = getGraphDb().createNode();
long nodeId = node.getId();
getGraphDb().getNodeById( nodeId );
node.delete();
tx.success();
tx.begin();
try
{
getGraphDb().getNodeById( nodeId );
fail( "Node[" + nodeId + "] should be deleted." );
}
catch ( NotFoundException e )
{
}
}
@Test
public void testDeletedNode()
{
// do some evil stuff
Node node = getGraphDb().createNode();
node.delete();
try
{
node.setProperty( "key1", new Integer( 1 ) );
fail( "Adding stuff to deleted node should throw exception" );
}
catch ( Exception e )
{ // good
}
}
@Test
public void testNodeAddProperty()
{
Node node1 = getGraphDb().createNode();
Node node2 = getGraphDb().createNode();
try
{
node1.setProperty( null, null );
fail( "Null argument should result in exception." );
}
catch ( IllegalArgumentException e )
{
}
String key1 = "key1";
String key2 = "key2";
String key3 = "key3";
Integer int1 = new Integer( 1 );
Integer int2 = new Integer( 2 );
String string1 = new String( "1" );
String string2 = new String( "2" );
// add property
node1.setProperty( key1, int1 );
node2.setProperty( key1, string1 );
node1.setProperty( key2, string2 );
node2.setProperty( key2, int2 );
assertTrue( node1.hasProperty( key1 ) );
assertTrue( node2.hasProperty( key1 ) );
assertTrue( node1.hasProperty( key2 ) );
assertTrue( node2.hasProperty( key2 ) );
assertTrue( !node1.hasProperty( key3 ) );
assertTrue( !node2.hasProperty( key3 ) );
assertEquals( int1, node1.getProperty( key1 ) );
assertEquals( string1, node2.getProperty( key1 ) );
assertEquals( string2, node1.getProperty( key2 ) );
assertEquals( int2, node2.getProperty( key2 ) );
// getTransaction().failure();
}
@Test
public void testNodeRemoveProperty()
{
String key1 = "key1";
String key2 = "key2";
Integer int1 = new Integer( 1 );
Integer int2 = new Integer( 2 );
String string1 = new String( "1" );
String string2 = new String( "2" );
Node node1 = getGraphDb().createNode();
Node node2 = getGraphDb().createNode();
try
{
if ( node1.removeProperty( key1 ) != null )
{
fail( "Remove of non existing property should return null" );
}
}
catch ( NotFoundException e )
{
}
try
{
node1.removeProperty( null );
fail( "Remove null property should throw exception." );
}
catch ( IllegalArgumentException e )
{
}
node1.setProperty( key1, int1 );
node2.setProperty( key1, string1 );
node1.setProperty( key2, string2 );
node2.setProperty( key2, int2 );
try
{
node1.removeProperty( null );
fail( "Null argument should result in exception." );
}
catch ( IllegalArgumentException e )
{
}
// test remove property
assertEquals( int1, node1.removeProperty( key1 ) );
assertEquals( string1, node2.removeProperty( key1 ) );
// test remove of non existing property
try
{
if ( node2.removeProperty( key1 ) != null )
{
fail( "Remove of non existing property return null." );
}
}
catch ( NotFoundException e )
{
// must mark as rollback only
}
// getTransaction().failure();
}
@Test
public void testNodeChangeProperty()
{
String key1 = "key1";
String key2 = "key2";
String key3 = "key3";
Integer int1 = new Integer( 1 );
Integer int2 = new Integer( 2 );
String string1 = new String( "1" );
String string2 = new String( "2" );
Boolean bool1 = new Boolean( true );
Boolean bool2 = new Boolean( false );
Node node1 = getGraphDb().createNode();
Node node2 = getGraphDb().createNode();
node1.setProperty( key1, int1 );
node2.setProperty( key1, string1 );
node1.setProperty( key2, string2 );
node2.setProperty( key2, int2 );
try
{
node1.setProperty( null, null );
fail( "Null argument should result in exception." );
}
catch ( IllegalArgumentException e )
{
}
catch ( NotFoundException e )
{
fail( "wrong exception" );
}
// test change property
node1.setProperty( key1, int2 );
node2.setProperty( key1, string2 );
assertEquals( string2, node2.getProperty( key1 ) );
node1.setProperty( key3, bool1 );
node1.setProperty( key3, bool2 );
assertEquals( string2, node2.getProperty( key1 ) );
node1.delete();
node2.delete();
}
@Test
public void testNodeChangeProperty2()
{
String key1 = "key1";
Integer int1 = new Integer( 1 );
Integer int2 = new Integer( 2 );
String string1 = new String( "1" );
String string2 = new String( "2" );
Boolean bool1 = new Boolean( true );
Boolean bool2 = new Boolean( false );
Node node1 = getGraphDb().createNode();
node1.setProperty( key1, int1 );
node1.setProperty( key1, int2 );
assertEquals( int2, node1.getProperty( key1 ) );
node1.removeProperty( key1 );
node1.setProperty( key1, string1 );
node1.setProperty( key1, string2 );
assertEquals( string2, node1.getProperty( key1 ) );
node1.removeProperty( key1 );
node1.setProperty( key1, bool1 );
node1.setProperty( key1, bool2 );
assertEquals( bool2, node1.getProperty( key1 ) );
node1.removeProperty( key1 );
node1.delete();
}
@Test
public void testNodeGetProperties()
{
String key1 = "key1";
String key2 = "key2";
String key3 = "key3";
Integer int1 = new Integer( 1 );
Integer int2 = new Integer( 2 );
String string = new String( "3" );
Node node1 = getGraphDb().createNode();
try
{
node1.getProperty( key1 );
fail( "get non existing property din't throw exception" );
}
catch ( NotFoundException e )
{
}
try
{
node1.getProperty( null );
fail( "get of null key din't throw exception" );
}
catch ( IllegalArgumentException e )
{
}
assertTrue( !node1.hasProperty( key1 ) );
assertTrue( !node1.hasProperty( null ) );
node1.setProperty( key1, int1 );
node1.setProperty( key2, int2 );
node1.setProperty( key3, string );
Iterator<String> keys = node1.getPropertyKeys().iterator();
keys.next();
keys.next();
keys.next();
Map<String, Object> properties = node1.getAllProperties();
assertTrue( properties.get( key1 ).equals( int1 ) );
assertTrue( properties.get( key2 ).equals( int2 ) );
assertTrue( properties.get( key3 ).equals( string ) );
properties = node1.getProperties( key1, key2 );
assertTrue( properties.get( key1 ).equals( int1 ) );
assertTrue( properties.get( key2 ).equals( int2 ) );
assertFalse( properties.containsKey( key3 ) );
properties = node1.getProperties();
assertTrue( properties.isEmpty() );
try
{
String[] names = null;
node1.getProperties( names );
fail();
}
catch ( NullPointerException e )
{
// Ok
}
try
{
String[] names = new String[]{null};
node1.getProperties( names );
fail();
}
catch ( NullPointerException e )
{
// Ok
}
try
{
node1.removeProperty( key3 );
}
catch ( NotFoundException e )
{
fail( "Remove of property failed." );
}
assertTrue( !node1.hasProperty( key3 ) );
assertTrue( !node1.hasProperty( null ) );
node1.delete();
}
@Test
public void testAddPropertyThenDelete()
{
Node node = getGraphDb().createNode();
node.setProperty( "test", "test" );
tx.success();
tx.begin();
node.setProperty( "test2", "test2" );
node.delete();
tx.success();
}
@Test
public void testChangeProperty()
{
Node node = getGraphDb().createNode();
node.setProperty( "test", "test1" );
tx.success();
tx.begin();
node.setProperty( "test", "test2" );
node.removeProperty( "test" );
node.setProperty( "test", "test3" );
assertEquals( "test3", node.getProperty( "test" ) );
node.removeProperty( "test" );
node.setProperty( "test", "test4" );
tx.success();
tx.begin();
assertEquals( "test4", node.getProperty( "test" ) );
}
@Test
public void testChangeProperty2()
{
Node node = getGraphDb().createNode();
node.setProperty( "test", "test1" );
tx.success();
tx.begin();
node.removeProperty( "test" );
node.setProperty( "test", "test3" );
assertEquals( "test3", node.getProperty( "test" ) );
tx.success();
tx.begin();
assertEquals( "test3", node.getProperty( "test" ) );
node.removeProperty( "test" );
node.setProperty( "test", "test4" );
tx.success();
tx.begin();
assertEquals( "test4", node.getProperty( "test" ) );
}
@Test
public void testNodeLockingProblem() throws InterruptedException
{
testLockProblem( getGraphDb().createNode() );
}
@Test
public void testRelationshipLockingProblem() throws InterruptedException
{
Node node = getGraphDb().createNode();
Node node2 = getGraphDb().createNode();
testLockProblem( node.createRelationshipTo( node2,
RelationshipType.withName( "lock-rel" ) ) );
}
private void testLockProblem( final PropertyContainer entity ) throws InterruptedException
{
entity.setProperty( "key", "value" );
final AtomicBoolean gotTheLock = new AtomicBoolean();
Thread thread = new Thread()
{
@Override
public void run()
{
try( Transaction tx = getGraphDb().beginTx() )
{
tx.acquireWriteLock( entity );
gotTheLock.set( true );
tx.success();
}
catch ( Exception e )
{
e.printStackTrace();
throw launderedException( e );
}
}
};
thread.start();
long endTime = System.currentTimeMillis() + 5000;
while ( thread.getState() != State.TERMINATED )
{
if ( thread.getState() == Thread.State.WAITING || thread.getState() == State.TIMED_WAITING)
{
break;
}
Thread.sleep( 1 );
if ( System.currentTimeMillis() > endTime ) break;
}
boolean gotLock = gotTheLock.get();
tx.success();
tx.begin();
assertFalse( gotLock );
thread.join();
}
private GraphDatabaseService getGraphDb()
{
return db.getGraphDatabaseService();
}
}