Skip to content

Commit aa3a4d0

Browse files
committed
Merge pull request #98 from jakewins/1.0-examples-again
Add examples for documentation
2 parents 661a4f5 + 9dc7470 commit aa3a4d0

File tree

3 files changed

+277
-0
lines changed

3 files changed

+277
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,16 @@ public static Driver driver( URI url, Config config )
5959
{
6060
return new Driver( url, config );
6161
}
62+
63+
/**
64+
* Return a driver for a Neo4j instance with custom configuration.
65+
*
66+
* @param url the URL to a Neo4j instance
67+
* @param config user defined configuration
68+
* @return a new driver to the database instance specified by the URL
69+
*/
70+
public static Driver driver( String url, Config config )
71+
{
72+
return driver( URI.create( url ), config );
73+
}
6274
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/**
2+
* Copyright (c) 2002-2015 "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.integration;
20+
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
24+
import java.io.File;
25+
26+
import org.neo4j.driver.v1.Config;
27+
import org.neo4j.driver.v1.Driver;
28+
import org.neo4j.driver.v1.GraphDatabase;
29+
import org.neo4j.driver.v1.ResultCursor;
30+
import org.neo4j.driver.v1.Session;
31+
import org.neo4j.driver.v1.Transaction;
32+
import org.neo4j.driver.v1.Values;
33+
import org.neo4j.driver.v1.util.StdIOCapture;
34+
import org.neo4j.driver.v1.util.TestNeo4j;
35+
36+
import static java.util.Arrays.asList;
37+
import static org.hamcrest.Matchers.equalTo;
38+
import static org.junit.Assert.assertThat;
39+
import static org.neo4j.driver.v1.Config.TlsAuthenticationConfig.usingKnownCerts;
40+
import static org.neo4j.driver.v1.Config.TlsAuthenticationConfig.usingTrustedCert;
41+
42+
/**
43+
* The tests below are examples that get pulled into the Driver Manual using the tags inside the tests.
44+
*
45+
* DO NOT add tests to this file that are not for that exact purpose.
46+
* DO NOT modify these tests without ensuring they remain consistent with the equivalent examples in other drivers
47+
*/
48+
public class ExamplesIT
49+
{
50+
@Rule
51+
public TestNeo4j neo4j = new TestNeo4j();
52+
53+
@Test
54+
public void minimumViableSnippet() throws Throwable
55+
{
56+
StdIOCapture stdIO = new StdIOCapture();
57+
try( AutoCloseable captured = stdIO.capture() )
58+
{
59+
// tag::minimum-snippet[]
60+
Driver driver = GraphDatabase.driver( "bolt://localhost" );
61+
Session session = driver.session();
62+
63+
session.run( "CREATE (neo:Person {name:'Neo', age:23})" );
64+
65+
ResultCursor result = session.run( "MATCH (p:Person) WHERE p.name = 'Neo' RETURN p.age" );
66+
while ( result.next() )
67+
{
68+
System.out.println( "Neo is " + result.value( "p.age" ).asInt() + " years old." );
69+
}
70+
71+
session.close();
72+
driver.close();
73+
// end::minimum-snippet[]
74+
}
75+
76+
// Then
77+
assertThat( stdIO.stdout(), equalTo( asList("Neo is 23 years old.") ) );
78+
}
79+
80+
@Test
81+
public void statement() throws Throwable
82+
{
83+
StdIOCapture stdIO = new StdIOCapture();
84+
try( AutoCloseable captured = stdIO.capture();
85+
Driver driver = GraphDatabase.driver( "bolt://localhost" );
86+
Session session = driver.session() )
87+
{
88+
// tag::statement[]
89+
ResultCursor result = session.run( "CREATE (p:Person { name: {name} })", Values.parameters( "name", "The One" ) );
90+
91+
int theOnesCreated = result.summarize().updateStatistics().nodesCreated();
92+
System.out.println("There were " + theOnesCreated + " the ones created.");
93+
// end::statement[]
94+
}
95+
96+
// Then
97+
assertThat( stdIO.stdout(), equalTo( asList("There were 1 the ones created.") ) );
98+
}
99+
100+
@Test
101+
public void statementWithoutParameters() throws Throwable
102+
{
103+
StdIOCapture stdIO = new StdIOCapture();
104+
try( AutoCloseable captured = stdIO.capture();
105+
Driver driver = GraphDatabase.driver( "bolt://localhost" );
106+
Session session = driver.session() )
107+
{
108+
// tag::statement-without-parameters[]
109+
ResultCursor result = session.run( "CREATE (p:Person { name: 'The One' })" );
110+
111+
int theOnesCreated = result.summarize().updateStatistics().nodesCreated();
112+
System.out.println("There were " + theOnesCreated + " the ones created.");
113+
// end::statement-without-parameters[]
114+
}
115+
116+
// Then
117+
assertThat( stdIO.stdout(), equalTo( asList("There were 1 the ones created.") ) );
118+
}
119+
120+
@Test
121+
public void transactionCommit() throws Throwable
122+
{
123+
try( Driver driver = GraphDatabase.driver( "bolt://localhost" );
124+
Session session = driver.session() )
125+
{
126+
session.run("MATCH (n) DETACH DELETE n");
127+
128+
// tag::transaction-commit[]
129+
try( Transaction tx = session.beginTransaction() )
130+
{
131+
tx.run( "CREATE (p:Person { name: 'The One' })" );
132+
tx.success();
133+
}
134+
// end::transaction-commit[]
135+
136+
// Then
137+
assertThat( session.run( "MATCH (p:Person) RETURN count(p)" ).peek().value( 0 ).asInt(), equalTo( 1 ) );
138+
}
139+
}
140+
141+
@Test
142+
public void transactionRollback() throws Throwable
143+
{
144+
try( Driver driver = GraphDatabase.driver( "bolt://localhost" );
145+
Session session = driver.session() )
146+
{
147+
session.run("MATCH (n) DETACH DELETE n");
148+
149+
// tag::transaction-rollback[]
150+
try( Transaction tx = session.beginTransaction() )
151+
{
152+
tx.run( "CREATE (p:Person { name: 'The One' })" );
153+
tx.failure();
154+
}
155+
// end::transaction-rollback[]
156+
157+
// Then
158+
assertThat( session.run( "MATCH (p:Person) RETURN count(p)" ).peek().value( 0 ).asInt(), equalTo( 0 ) );
159+
}
160+
}
161+
162+
@Test
163+
public void requireEncryption() throws Throwable
164+
{
165+
// tag::tls-require-encryption[]
166+
Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build()
167+
.withTlsEnabled( true )
168+
.toConfig() );
169+
// end::tls-require-encryption[]
170+
driver.close();
171+
}
172+
173+
@Test
174+
public void trustOnFirstUse() throws Throwable
175+
{
176+
// tag::tls-trust-on-first-use[]
177+
Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build()
178+
.withTlsEnabled( true )
179+
.withTlsAuthConfig( usingKnownCerts( new File("/path/to/neo4j_known_hosts") ) )
180+
.toConfig() );
181+
// end::tls-trust-on-first-use[]
182+
driver.close();
183+
}
184+
185+
@Test
186+
public void trustSignedCertificates() throws Throwable
187+
{
188+
// tag::tls-signed[]
189+
Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build()
190+
.withTlsEnabled( true )
191+
.withTlsAuthConfig( usingTrustedCert( new File("/path/to/ca-certificate.pem") ) )
192+
.toConfig() );
193+
// end::tls-signed[]
194+
driver.close();
195+
}
196+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright (c) 2002-2015 "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.util;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import java.util.LinkedList;
24+
import java.util.List;
25+
26+
import static java.util.Arrays.asList;
27+
28+
/**
29+
* Utility that can be used to temporarily capture and store process-wide stdout and stderr output.
30+
*/
31+
public class StdIOCapture
32+
{
33+
private final List<String> stdout = new LinkedList<>();
34+
private final List<String> stderr = new LinkedList<>();
35+
36+
/** Put this in a try-with-resources block to capture all standard io that happens within the try block */
37+
public AutoCloseable capture()
38+
{
39+
final PrintStream originalStdOut = System.out;
40+
final PrintStream originalStdErr = System.err;
41+
final ByteArrayOutputStream capturedStdOut = new ByteArrayOutputStream();
42+
final ByteArrayOutputStream capturedStdErr = new ByteArrayOutputStream();
43+
44+
System.setOut( new PrintStream( capturedStdOut ) );
45+
System.setErr( new PrintStream( capturedStdErr ) );
46+
47+
return new AutoCloseable()
48+
{
49+
@Override
50+
public void close() throws Exception
51+
{
52+
System.setOut( originalStdOut );
53+
System.setErr( originalStdErr );
54+
stdout.addAll( asList( capturedStdOut.toString( "UTF-8" ).split( "\n" ) ) );
55+
stderr.addAll( asList( capturedStdErr.toString( "UTF-8" ).split( "\n" ) ) );
56+
}
57+
};
58+
}
59+
60+
public List<String> stdout()
61+
{
62+
return stdout;
63+
}
64+
65+
public List<String> stderr()
66+
{
67+
return stderr;
68+
}
69+
}

0 commit comments

Comments
 (0)