1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.neo4j.examples;
20
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.neo4j.graphdb.GraphDatabaseService;
25 import org.neo4j.graphdb.Node;
26 import org.neo4j.graphdb.Transaction;
27 import org.neo4j.kernel.EmbeddedGraphDatabase;
28
29 import java.io.File;
30
31 import static org.hamcrest.Matchers.greaterThan;
32 import static org.hamcrest.Matchers.is;
33 import static org.junit.Assert.assertThat;
34
35
36
37
38 public class Neo4jBasicTest
39 {
40
41
42
43 protected File testDirectory = new File( "target/var" );
44
45
46
47
48 protected File testDatabasePath = new File( testDirectory, "testdb" );
49 protected GraphDatabaseService graphDb;
50
51
52
53
54
55
56
57 @Before
58 public void prepareTestDatabase()
59 {
60
61 deleteFileOrDirectory( testDatabasePath );
62 graphDb = new EmbeddedGraphDatabase( testDatabasePath.getAbsolutePath() );
63
64 }
65
66
67
68
69 @After
70 public void destroyTestDatabase()
71 {
72
73 graphDb.shutdown();
74
75 }
76
77 protected void deleteFileOrDirectory( File path )
78 {
79 if ( path.exists() )
80 {
81 if ( path.isDirectory() )
82 {
83 for ( File child : path.listFiles() )
84 {
85 deleteFileOrDirectory( child );
86 }
87 }
88 path.delete();
89 }
90 }
91
92
93 @Test
94 public void shouldCreateNode()
95 {
96
97 Transaction tx = graphDb.beginTx();
98
99 Node n = null;
100 try
101 {
102 n = graphDb.createNode();
103 n.setProperty( "name", "Nancy" );
104 tx.success();
105 } catch ( Exception e )
106 {
107 tx.failure();
108 } finally
109 {
110 tx.finish();
111 }
112
113
114 assertThat( n.getId(), is( greaterThan( 0l ) ) );
115
116
117 Node foundNode = graphDb.getNodeById( n.getId() );
118 assertThat( foundNode.getId(), is( n.getId() ) );
119 assertThat( (String) foundNode.getProperty( "name" ), is( "Nancy" ) );
120
121
122 }
123
124 }