Skip to content

Commit 32f7de9

Browse files
author
Zhen Li
committed
Merge pull request #148 from pontusmelke/1.0-null-checks
More null checks
2 parents 7c098cf + 3c47e0a commit 32f7de9

File tree

6 files changed

+132
-7
lines changed

6 files changed

+132
-7
lines changed

driver/src/main/java/org/neo4j/driver/internal/InternalSession.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ public StatementResult run( String statementText )
6969
@Override
7070
public StatementResult run( String statementText, Map<String, Object> statementParameters )
7171
{
72-
return run( statementText, value( statementParameters ) );
72+
Value params = statementParameters == null ? Values.EmptyMap : value(statementParameters);
73+
return run( statementText, params );
7374
}
7475

7576
@Override
7677
public StatementResult run( String statementTemplate, Record statementParameters )
7778
{
78-
// TODO: This conversion to map here is pointless, it gets converted right back
79-
return run( statementTemplate, statementParameters.asMap() );
79+
Value params = statementParameters == null ? Values.EmptyMap : value( statementParameters.asMap() );
80+
return run( statementTemplate, params );
8081
}
8182

8283
@Override

driver/src/main/java/org/neo4j/driver/internal/InternalTransaction.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.neo4j.driver.v1.types.TypeSystem;
3636

3737
import static org.neo4j.driver.v1.Values.ofValue;
38+
import static org.neo4j.driver.v1.Values.value;
3839

3940
public class InternalTransaction implements Transaction
4041
{
@@ -142,14 +143,15 @@ public StatementResult run( String statementText )
142143
@Override
143144
public StatementResult run( String statementText, Map<String,Object> statementParameters )
144145
{
145-
return run( statementText, Values.value( statementParameters ) );
146+
Value params = statementParameters == null ? Values.EmptyMap : value(statementParameters);
147+
return run( statementText, params );
146148
}
147149

148150
@Override
149151
public StatementResult run( String statementTemplate, Record statementParameters )
150152
{
151-
// TODO: This conversion to map here is pointless, it gets converted right back
152-
return run( statementTemplate, statementParameters.asMap() );
153+
Value params = statementParameters == null ? Values.EmptyMap : value( statementParameters.asMap() );
154+
return run( statementTemplate, params );
153155
}
154156

155157
@Override

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public static Driver driver( String url, AuthToken authToken, Config config )
122122
*/
123123
public static Driver driver( URI url, AuthToken authToken, Config config )
124124
{
125-
return new InternalDriver( url, authToken, config );
125+
AuthToken tokenToUse = authToken != null ? authToken: AuthTokens.none();
126+
Config configToUse = config != null ? config: Config.defaultConfig();
127+
128+
return new InternalDriver( url, tokenToUse, configToUse );
126129
}
127130
}

driver/src/test/java/org/neo4j/driver/v1/integration/SessionIT.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.junit.Rule;
2222
import org.junit.Test;
2323

24+
import org.neo4j.driver.v1.AuthToken;
25+
import org.neo4j.driver.v1.AuthTokens;
2426
import org.neo4j.driver.v1.Driver;
2527
import org.neo4j.driver.v1.GraphDatabase;
2628
import org.neo4j.driver.v1.Session;
@@ -46,4 +48,34 @@ public void shouldKnowSessionIsClosed() throws Throwable
4648
// Then
4749
assertFalse( session.isOpen() );
4850
}
51+
52+
@Test
53+
public void shouldHandleNullConfig() throws Throwable
54+
{
55+
// Given
56+
Driver driver = GraphDatabase.driver( neo4j.address(), AuthTokens.none(), null );
57+
Session session = driver.session();
58+
59+
// When
60+
session.close();
61+
62+
// Then
63+
assertFalse( session.isOpen() );
64+
}
65+
66+
@SuppressWarnings( "ConstantConditions" )
67+
@Test
68+
public void shouldHandleNullAuthToken() throws Throwable
69+
{
70+
// Given
71+
AuthToken token = null;
72+
Driver driver = GraphDatabase.driver( neo4j.address(), token);
73+
Session session = driver.session();
74+
75+
// When
76+
session.close();
77+
78+
// Then
79+
assertFalse( session.isOpen() );
80+
}
4981
}

driver/src/test/java/org/neo4j/driver/v1/integration/StatementIT.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collections;
2525
import java.util.Iterator;
2626
import java.util.List;
27+
import java.util.Map;
2728

2829
import org.neo4j.driver.v1.Record;
2930
import org.neo4j.driver.v1.StatementResult;
@@ -74,6 +75,45 @@ public void shouldRunWithParameters() throws Throwable
7475
// Then nothing should've failed
7576
}
7677

78+
@SuppressWarnings( "ConstantConditions" )
79+
@Test
80+
public void shouldRunWithNullValuesAsParameters() throws Throwable
81+
{
82+
// Given
83+
Value params = null;
84+
85+
// When
86+
session.run( "CREATE (n:FirstNode {name:'Steven'})", params );
87+
88+
// Then nothing should've failed
89+
}
90+
91+
@SuppressWarnings( "ConstantConditions" )
92+
@Test
93+
public void shouldRunWithNullRecordAsParameters() throws Throwable
94+
{
95+
// Given
96+
Record params = null;
97+
98+
// When
99+
session.run( "CREATE (n:FirstNode {name:'Steven'})", params );
100+
101+
// Then nothing should've failed
102+
}
103+
104+
@SuppressWarnings( "ConstantConditions" )
105+
@Test
106+
public void shouldRunWithNullMapAsParameters() throws Throwable
107+
{
108+
// Given
109+
Map<String, Object> params = null;
110+
111+
// When
112+
session.run( "CREATE (n:FirstNode {name:'Steven'})", params );
113+
114+
// Then nothing should've failed
115+
}
116+
77117
@Test
78118
public void shouldRunWithCollectionAsParameter() throws Throwable
79119
{

driver/src/test/java/org/neo4j/driver/v1/integration/TransactionIT.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.junit.Test;
2323
import org.junit.rules.ExpectedException;
2424

25+
import java.util.Map;
26+
27+
import org.neo4j.driver.v1.Record;
2528
import org.neo4j.driver.v1.StatementResult;
2629
import org.neo4j.driver.v1.Transaction;
2730
import org.neo4j.driver.v1.Value;
@@ -164,4 +167,48 @@ public void shouldHandleFailureAfterClosingTransaction()
164167
session.run("CREAT (n) RETURN n").consume();
165168
}
166169

170+
@SuppressWarnings( "ConstantConditions" )
171+
@Test
172+
public void shouldHandleNullRecordParameters() throws Throwable
173+
{
174+
// When
175+
try ( Transaction tx = session.beginTransaction() )
176+
{
177+
Record params = null;
178+
tx.run( "CREATE (n:FirstNode)", params );
179+
tx.success();
180+
}
181+
182+
// Then it wasn't the end of the world as we know it
183+
}
184+
185+
@SuppressWarnings( "ConstantConditions" )
186+
@Test
187+
public void shouldHandleNullValueParameters() throws Throwable
188+
{
189+
// When
190+
try ( Transaction tx = session.beginTransaction() )
191+
{
192+
Value params = null;
193+
tx.run( "CREATE (n:FirstNode)", params );
194+
tx.success();
195+
}
196+
197+
// Then it wasn't the end of the world as we know it
198+
}
199+
200+
@SuppressWarnings( "ConstantConditions" )
201+
@Test
202+
public void shouldHandleNullMapParameters() throws Throwable
203+
{
204+
// When
205+
try ( Transaction tx = session.beginTransaction() )
206+
{
207+
Map<String, Object> params = null;
208+
tx.run( "CREATE (n:FirstNode)", params );
209+
tx.success();
210+
}
211+
212+
// Then it wasn't the end of the world as we know it
213+
}
167214
}

0 commit comments

Comments
 (0)