Skip to content

Commit 2e19061

Browse files
committed
DriverDocIT back
1 parent 64139b7 commit 2e19061

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

driver/src/main/java/org/neo4j/driver/v1/Driver.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,45 @@
2626
/**
2727
* A Neo4j database driver, through which you can create {@link Session sessions} to run statements against the database.
2828
* <p>
29+
* An example:
30+
* <pre class="doctest:DriverDocIT#exampleUsage">
31+
* {@code
32+
* // Create a driver with default configuration
33+
* Driver driver = GraphDatabase.driver( "bolt://localhost:7687" );
34+
*
35+
* // Establish a session
36+
* Session session = driver.session();
37+
*
38+
* // Running a simple statement can be done like this
39+
* session.run( "CREATE (n {name:'Bob'})" );
40+
*
41+
* // Or, run multiple statements together in an atomic transaction:
42+
* try( Transaction tx = session.beginTransaction() )
43+
* {
44+
* tx.run( "CREATE (n {name:'Alice'})" );
45+
* tx.run( "CREATE (n {name:'Tina'})" );
46+
* tx.success();
47+
* }
48+
*
49+
* // Retrieve results
50+
* StatementResult result = session.run( "MATCH (n) RETURN n.name" );
51+
* List<String> names = new LinkedList<>();
52+
* while( result.hasNext() )
53+
* {
54+
* names.add( result.next().get("n.name").asString() );
55+
* }
56+
*
57+
* // Sessions are pooled, to avoid the overhead of creating new connections - this means
58+
* // it is very important to close your session when you are done with it, otherwise you will
59+
* // run out of sessions.
60+
* session.close();
61+
*
62+
* // And, to clean up resources, always close the driver when your application is done
63+
* driver.close();
64+
* }
65+
* </pre>
66+
* <p>
67+
*
2968
* A driver maintains a connection pool for each Neo4j instance. For resource efficiency reasons you are encouraged
3069
* to use the same driver instance across your application. You can control the connection pooling behavior when you
3170
* create the driver using the {@link Config} you pass into {@link GraphDatabase#driver(URI, Config)}.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) 2002-2016 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.v1;
20+
21+
import javadoctest.DocSnippet;
22+
import javadoctest.DocTestRunner;
23+
import org.junit.Rule;
24+
import org.junit.runner.RunWith;
25+
26+
import java.util.LinkedList;
27+
import java.util.List;
28+
29+
import org.neo4j.driver.v1.util.TestNeo4jSession;
30+
31+
import static org.hamcrest.MatcherAssert.assertThat;
32+
import static org.hamcrest.Matchers.containsInAnyOrder;
33+
import static org.junit.Assert.assertEquals;
34+
35+
@RunWith( DocTestRunner.class )
36+
public class DriverDocIT
37+
{
38+
@Rule
39+
public TestNeo4jSession session = new TestNeo4jSession();
40+
41+
/** @see Driver */
42+
@SuppressWarnings("unchecked")
43+
public void exampleUsage( DocSnippet snippet )
44+
{
45+
// given
46+
snippet.addImport( List.class );
47+
snippet.addImport( LinkedList.class );
48+
session.run( "MATCH (n) DETACH DELETE (n)" );
49+
50+
// when
51+
snippet.run();
52+
53+
// then it should've created a bunch of data
54+
StatementResult result = session.run( "MATCH (n) RETURN count(n)" );
55+
assertEquals( 3, result.single().get( 0 ).asInt() );
56+
assertThat( (List<String>)snippet.get( "names" ), containsInAnyOrder( "Bob", "Alice", "Tina" ) );
57+
}
58+
}

0 commit comments

Comments
 (0)