Skip to content

Commit 54a62b4

Browse files
committed
Merge pull request #139 from pontusmelke/1.0-cast-bug
Properly handle collections in `Values`
2 parents 85b8404 + 0a54294 commit 54a62b4

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
import org.neo4j.driver.internal.value.NullValue;
3636
import org.neo4j.driver.internal.value.StringValue;
3737
import org.neo4j.driver.v1.exceptions.ClientException;
38-
import org.neo4j.driver.v1.types.TypeSystem;
39-
import org.neo4j.driver.v1.util.Function;
4038
import org.neo4j.driver.v1.types.Entity;
4139
import org.neo4j.driver.v1.types.Node;
4240
import org.neo4j.driver.v1.types.Path;
4341
import org.neo4j.driver.v1.types.Relationship;
42+
import org.neo4j.driver.v1.types.TypeSystem;
43+
import org.neo4j.driver.v1.util.Function;
4444

4545
/**
4646
* Utility for wrapping regular Java types and exposing them as {@link Value}
@@ -78,7 +78,7 @@ public static Value value( Object value )
7878
if ( value instanceof Double ) { return value( (double) value ); }
7979
if ( value instanceof Float ) { return value( (float) value ); }
8080

81-
if ( value instanceof Collection<?> ) { return value( (List<Object>) value ); }
81+
if ( value instanceof List<?> ) { return value( (List<Object>) value ); }
8282
if ( value instanceof Iterable<?> ) { return value( (Iterable<Object>) value ); }
8383
if ( value instanceof Map<?, ?> ) { return value( (Map<String,Object>) value ); }
8484

@@ -171,12 +171,13 @@ public static Value value( float... input )
171171
return new ListValue( values );
172172
}
173173

174-
public static Value value( List<Object> val )
174+
public static Value value( List<Object> vals )
175175
{
176-
Value[] values = new Value[val.size()];
177-
for ( int i = 0; i < val.size(); i++ )
176+
Value[] values = new Value[vals.size()];
177+
int i = 0;
178+
for ( Object val : vals )
178179
{
179-
values[i] = value( val.get( i ) );
180+
values[i++] = value( val );
180181
}
181182
return new ListValue( values );
182183
}

driver/src/test/java/org/neo4j/driver/internal/ValuesTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
*/
1919
package org.neo4j.driver.internal;
2020

21+
import org.hamcrest.Matchers;
2122
import org.junit.Rule;
2223
import org.junit.Test;
2324
import org.junit.rules.ExpectedException;
2425

26+
import java.util.ArrayDeque;
27+
import java.util.Collection;
2528
import java.util.HashMap;
2629
import java.util.HashSet;
2730
import java.util.Iterator;
@@ -241,6 +244,7 @@ public void shouldMapString() throws Throwable
241244
assertThat( val.asList( ofObject() ), contains((Object)"hello", "world") );
242245
}
243246

247+
@SuppressWarnings( "unchecked" )
244248
@Test
245249
public void shouldMapMapOfString() throws Throwable
246250
{
@@ -253,4 +257,17 @@ public void shouldMapMapOfString() throws Throwable
253257
assertThat( val.asList( ofMap() ), contains(map, map) );
254258
assertThat( val.asList( ofObject() ), contains((Object)map, map) );
255259
}
260+
261+
@Test
262+
public void shouldHandleCollection() throws Throwable
263+
{
264+
// Given
265+
Collection<String> collection = new ArrayDeque<>();
266+
collection.add( "hello");
267+
collection.add( "world");
268+
Value val = value( collection );
269+
270+
// When/Then
271+
assertThat( val.asList(), Matchers.<Object>containsInAnyOrder( "hello", "world" ));
272+
}
256273
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class ScalarTypeIT
5454
public static Collection<Object[]> typesToTest()
5555
{
5656
return Arrays.asList(
57-
new Object[]{"RETURN 1 as v", Values.value( 1l )},
57+
new Object[]{"RETURN 1 as v", Values.value( 1L )},
5858
new Object[]{"RETURN 1.1 as v", Values.value( 1.1d )},
5959
new Object[]{"RETURN 'hello' as v", Values.value( "hello" )},
6060
new Object[]{"RETURN true as v", Values.value( true )},

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

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

24+
import java.util.Collections;
2425
import java.util.List;
2526

2627
import org.neo4j.driver.v1.Record;
@@ -49,9 +50,9 @@ public void shouldRunWithResult() throws Throwable
4950
assertThat( result.size(), equalTo( 3 ) );
5051

5152
// And it should allow random access
52-
assertThat( result.get( 0 ).get( "k" ).asLong(), equalTo( 1l ) );
53-
assertThat( result.get( 1 ).get( "k" ).asLong(), equalTo( 2l ) );
54-
assertThat( result.get( 2 ).get( "k" ).asLong(), equalTo( 3l ) );
53+
assertThat( result.get( 0 ).get( "k" ).asLong(), equalTo( 1L ) );
54+
assertThat( result.get( 1 ).get( "k" ).asLong(), equalTo( 2L ) );
55+
assertThat( result.get( 2 ).get( "k" ).asLong(), equalTo( 3L ) );
5556

5657
// And it should allow iteration
5758
long expected = 0;
@@ -60,7 +61,7 @@ public void shouldRunWithResult() throws Throwable
6061
expected += 1;
6162
assertThat( value.get( "k" ), equalTo( Values.value( expected ) ) );
6263
}
63-
assertThat( expected, equalTo( 3l ) );
64+
assertThat( expected, equalTo( 3L ) );
6465
}
6566

6667
@Test
@@ -72,6 +73,15 @@ public void shouldRunWithParameters() throws Throwable
7273
// Then nothing should've failed
7374
}
7475

76+
@Test
77+
public void shouldRunWithCollectionAsParameter() throws Throwable
78+
{
79+
// When
80+
session.run( "RETURN {param}", parameters( "param", Collections.singleton( "FOO" ) ) );
81+
82+
// Then nothing should've failed
83+
}
84+
7585
@Test
7686
public void shouldRun() throws Throwable
7787
{

0 commit comments

Comments
 (0)