|
22 | 22 | import org.junit.Test; |
23 | 23 | import org.junit.rules.ExpectedException; |
24 | 24 |
|
| 25 | +import java.util.concurrent.ThreadLocalRandom; |
| 26 | + |
25 | 27 | import org.neo4j.driver.internal.util.ServerVersion; |
26 | 28 | import org.neo4j.driver.v1.Record; |
27 | 29 | import org.neo4j.driver.v1.StatementResult; |
@@ -149,30 +151,17 @@ public void shouldBeAbleToSetAndReturnDoubleProperty() |
149 | 151 | } |
150 | 152 | } |
151 | 153 |
|
152 | | - private boolean supportsBytes() |
153 | | - { |
154 | | - String versionString = session.run( "RETURN 1" ).consume().server().version(); |
155 | | - return version( versionString ).greaterThanOrEqual( ServerVersion.v3_2_0 ); |
156 | | - } |
157 | | - |
158 | 154 | @Test |
159 | 155 | public void shouldBeAbleToSetAndReturnBytesProperty() |
160 | 156 | { |
161 | 157 | assumeTrue( supportsBytes() ); |
162 | 158 |
|
163 | | - // Given |
164 | | - byte[] byteArray = "hello, world".getBytes(); |
165 | | - |
166 | | - // When |
167 | | - StatementResult result = session.run( |
168 | | - "CREATE (a {value:{value}}) RETURN a.value", parameters( "value", byteArray ) ); |
169 | | - |
170 | | - // Then |
171 | | - for ( Record record : result.list() ) |
| 159 | + testBytesProperty( new byte[0] ); |
| 160 | + for ( int i = 0; i < 16; i++ ) |
172 | 161 | { |
173 | | - Value value = record.get( "a.value" ); |
174 | | - assertThat( value.hasType( session.typeSystem().BYTES() ), equalTo( true ) ); |
175 | | - assertThat( value.asByteArray(), equalTo( byteArray ) ); |
| 162 | + int length = (int) Math.pow( 2, i ); |
| 163 | + testBytesProperty( randomByteArray( length ) ); |
| 164 | + testBytesProperty( randomByteArray( length - 1 ) ); |
176 | 165 | } |
177 | 166 | } |
178 | 167 |
|
@@ -201,18 +190,10 @@ public void shouldThrowExceptionWhenServerDoesNotSupportBytes() |
201 | 190 | @Test |
202 | 191 | public void shouldBeAbleToSetAndReturnStringProperty() |
203 | 192 | { |
204 | | - // When |
205 | | - StatementResult result = session.run( |
206 | | - "CREATE (a {value:{value}}) RETURN a.value", parameters( "value", "Mjölnir" ) ); |
207 | | - |
208 | | - // Then |
209 | | - for ( Record record : result.list() ) |
210 | | - { |
211 | | - Value value = record.get( "a.value" ); |
212 | | - assertThat( value.hasType( session.typeSystem().STRING() ), equalTo( true ) ); |
213 | | - assertThat( value.asString(), equalTo( "Mjölnir" ) ); |
214 | | - } |
215 | | - |
| 193 | + testStringProperty( "" ); |
| 194 | + testStringProperty( "π≈3.14" ); |
| 195 | + testStringProperty( "Mjölnir" ); |
| 196 | + testStringProperty( "*** Hello World! ***" ); |
216 | 197 | } |
217 | 198 |
|
218 | 199 | @Test |
@@ -459,4 +440,44 @@ public void shouldNotBePossibleToUsePathAsParameter() |
459 | 440 | // WHEN |
460 | 441 | session.run( "RETURN {a}", parameters( "a", path ) ); |
461 | 442 | } |
| 443 | + |
| 444 | + private void testBytesProperty( byte[] array ) |
| 445 | + { |
| 446 | + assumeTrue( supportsBytes() ); |
| 447 | + |
| 448 | + StatementResult result = session.run( |
| 449 | + "CREATE (a {value:{value}}) RETURN a.value", parameters( "value", array ) ); |
| 450 | + |
| 451 | + for ( Record record : result.list() ) |
| 452 | + { |
| 453 | + Value value = record.get( "a.value" ); |
| 454 | + assertThat( value.hasType( session.typeSystem().BYTES() ), equalTo( true ) ); |
| 455 | + assertThat( value.asByteArray(), equalTo( array ) ); |
| 456 | + } |
| 457 | + } |
| 458 | + |
| 459 | + private void testStringProperty( String string ) |
| 460 | + { |
| 461 | + StatementResult result = session.run( |
| 462 | + "CREATE (a {value:{value}}) RETURN a.value", parameters( "value", string ) ); |
| 463 | + |
| 464 | + for ( Record record : result.list() ) |
| 465 | + { |
| 466 | + Value value = record.get( "a.value" ); |
| 467 | + assertThat( value.hasType( session.typeSystem().STRING() ), equalTo( true ) ); |
| 468 | + assertThat( value.asString(), equalTo( string ) ); |
| 469 | + } |
| 470 | + } |
| 471 | + |
| 472 | + private boolean supportsBytes() |
| 473 | + { |
| 474 | + return version( session.driver() ).greaterThanOrEqual( ServerVersion.v3_2_0 ); |
| 475 | + } |
| 476 | + |
| 477 | + private static byte[] randomByteArray( int length ) |
| 478 | + { |
| 479 | + byte[] result = new byte[length]; |
| 480 | + ThreadLocalRandom.current().nextBytes( result ); |
| 481 | + return result; |
| 482 | + } |
462 | 483 | } |
0 commit comments