1 /**
2 * Licensed to Neo Technology under one or more contributor
3 * license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright
5 * ownership. Neo Technology licenses this file to you under
6 * the Apache License, Version 2.0 (the "License"); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * An example of unit testing with Neo4j.
37 */
38 public class Neo4jBasicTest
39 {
40 /**
41 * Base directory for temporary database.
42 */
43 protected File testDirectory = new File( "target/var" );
44
45 /**
46 * Full path to the temporary database.
47 */
48 protected File testDatabasePath = new File( testDirectory, "testdb" );
49 protected GraphDatabaseService graphDb;
50
51
52 /**
53 * Create temporary database for each unit test.
54 * <p/>
55 * This will delete any existing database prior to creating a new one.
56 */
57 @Before
58 public void prepareTestDatabase()
59 {
60 // START SNIPPET: beforeTest
61 deleteFileOrDirectory( testDatabasePath );
62 graphDb = new EmbeddedGraphDatabase( testDatabasePath.getAbsolutePath() );
63 // END SNIPPET: beforeTest
64 }
65
66 /**
67 * Shutdown the database.
68 */
69 @After
70 public void destroyTestDatabase()
71 {
72 // START SNIPPET: afterTest
73 graphDb.shutdown();
74 // END SNIPPET: afterTest
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 // START SNIPPET: unitTest
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 // The node should have an id greater than 0, which is the id of the reference node.
114 assertThat( n.getId(), is( greaterThan( 0l ) ) );
115
116 // Retrieve a node by using the id of the created node. The id's and property should match.
117 Node foundNode = graphDb.getNodeById( n.getId() );
118 assertThat( foundNode.getId(), is( n.getId() ) );
119 assertThat( (String) foundNode.getProperty( "name" ), is( "Nancy" ) );
120
121 // END SNIPPET: unitTest
122 }
123
124 }