Skip to content

Commit d1b4737

Browse files
author
Zhen Li
committed
Merge pull request #122 from pontusmelke/more-tests
Increase test coverage and fix broken build
2 parents ca1c1d3 + 777ba7e commit d1b4737

File tree

6 files changed

+376
-55
lines changed

6 files changed

+376
-55
lines changed

driver/src/main/java/org/neo4j/driver/internal/summary/InternalNotification.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.neo4j.driver.v1.Notification;
2424
import org.neo4j.driver.v1.Value;
2525

26+
import static org.neo4j.driver.internal.value.NullValue.NULL;
27+
2628
public class InternalNotification implements Notification
2729
{
2830
public static final Function<Value, Notification> VALUE_TO_NOTIFICATION = new Function<Value,Notification>()
@@ -36,7 +38,7 @@ public Notification apply( Value value )
3638

3739
Value posValue = value.get( "position" );
3840
InputPosition position = null;
39-
if( posValue != null )
41+
if( posValue != NULL )
4042
{
4143
position = new InternalInputPosition( posValue.get( "offset" ).asInt(),
4244
posValue.get( "line" ).asInt(),

driver/src/test/java/org/neo4j/driver/internal/packstream/PackStreamTest.java

Lines changed: 144 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,26 @@
1818
*/
1919
package org.neo4j.driver.internal.packstream;
2020

21+
import org.hamcrest.MatcherAssert;
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
import org.junit.rules.ExpectedException;
25+
2126
import java.io.ByteArrayInputStream;
2227
import java.io.ByteArrayOutputStream;
2328
import java.io.IOException;
2429
import java.nio.channels.Channels;
2530
import java.nio.channels.ReadableByteChannel;
2631
import java.nio.channels.WritableByteChannel;
32+
import java.util.ArrayList;
33+
import java.util.HashMap;
2734
import java.util.LinkedHashMap;
2835
import java.util.Map;
2936

30-
import org.hamcrest.MatcherAssert;
31-
import org.junit.Test;
32-
3337
import org.neo4j.driver.internal.util.BytePrinter;
3438

3539
import static java.nio.charset.StandardCharsets.UTF_8;
3640
import static java.util.Arrays.asList;
37-
3841
import static junit.framework.TestCase.assertFalse;
3942
import static org.hamcrest.CoreMatchers.equalTo;
4043
import static org.hamcrest.MatcherAssert.assertThat;
@@ -47,6 +50,9 @@
4750
public class PackStreamTest
4851
{
4952

53+
@Rule
54+
public ExpectedException exception = ExpectedException.none();
55+
5056
public static Map<String, Object> asMap( Object... keysAndValues )
5157
{
5258
Map<String, Object> map = new LinkedHashMap<>( keysAndValues.length / 2 );
@@ -497,10 +503,10 @@ public void testCanPackAndUnpackListOneItemAtATime() throws Throwable
497503
PackType packType = unpacker.peekNextType();
498504

499505
assertThat( packType, equalTo( PackType.LIST ) );
500-
assertThat( unpacker.unpackListHeader(), equalTo( 3l ) );
501-
assertThat( unpacker.unpackLong(), equalTo( 12l ) );
502-
assertThat( unpacker.unpackLong(), equalTo( 13l ) );
503-
assertThat( unpacker.unpackLong(), equalTo( 14l ) );
506+
assertThat( unpacker.unpackListHeader(), equalTo( 3L ) );
507+
assertThat( unpacker.unpackLong(), equalTo( 12L ) );
508+
assertThat( unpacker.unpackLong(), equalTo( 13L ) );
509+
assertThat( unpacker.unpackLong(), equalTo( 14L ) );
504510

505511
}
506512

@@ -520,7 +526,7 @@ public void testCanPackAndUnpackListOfString() throws Throwable
520526
PackType packType = unpacker.peekNextType();
521527

522528
assertThat( packType, equalTo( PackType.LIST ) );
523-
assertThat( unpacker.unpackListHeader(), equalTo( 3l ) );
529+
assertThat( unpacker.unpackListHeader(), equalTo( 3L ) );
524530
assertThat( unpacker.unpackString(), equalTo( "eins" ) );
525531
assertThat( unpacker.unpackString(), equalTo( "zwei" ) );
526532
assertThat( unpacker.unpackString(), equalTo( "drei" ) );
@@ -530,23 +536,10 @@ public void testCanPackAndUnpackListOfString() throws Throwable
530536
@Test
531537
public void testCanPackAndUnpackListOfSpecialStrings() throws Throwable
532538
{
533-
// Given
534-
Machine machine = new Machine();
535-
536-
// When
537-
PackStream.Packer packer = machine.packer();
538-
packer.pack( asList( "Mjölnir", "Mjölnir", "Mjölnir" ) );
539-
packer.flush();
540-
541-
// Then
542-
PackStream.Unpacker unpacker = newUnpacker( machine.output() );
543-
PackType packType = unpacker.peekNextType();
544-
545-
assertThat( packType, equalTo( PackType.LIST ) );
546-
assertThat( unpacker.unpackListHeader(), equalTo( 3l ) );
547-
assertThat( unpacker.unpackString(), equalTo( "Mjölnir" ) );
548-
assertThat( unpacker.unpackString(), equalTo( "Mjölnir" ) );
549-
assertThat( unpacker.unpackString(), equalTo( "Mjölnir" ) );
539+
assertPackStringLists( 3, "Mjölnir" );
540+
assertPackStringLists( 126, "Mjölnir" );
541+
assertPackStringLists( 3000, "Mjölnir" );
542+
assertPackStringLists( 32768, "Mjölnir" );
550543
}
551544

552545
@Test
@@ -571,7 +564,7 @@ public void testCanPackAndUnpackListOfStringOneByOne() throws Throwable
571564
PackType packType = unpacker.peekNextType();
572565

573566
assertThat( packType, equalTo( PackType.LIST ) );
574-
assertThat( unpacker.unpackListHeader(), equalTo( 3l ) );
567+
assertThat( unpacker.unpackListHeader(), equalTo( 3L ) );
575568
assertThat( unpacker.unpackString(), equalTo( "eins" ) );
576569
assertThat( unpacker.unpackString(), equalTo( "zwei" ) );
577570
assertThat( unpacker.unpackString(), equalTo( "drei" ) );
@@ -600,7 +593,7 @@ public void testCanPackAndUnpackListOfSpecialStringOneByOne() throws Throwable
600593
PackType packType = unpacker.peekNextType();
601594

602595
assertThat( packType, equalTo( PackType.LIST ) );
603-
assertThat( unpacker.unpackListHeader(), equalTo( 3l ) );
596+
assertThat( unpacker.unpackListHeader(), equalTo( 3L ) );
604597
assertThat( unpacker.unpackString(), equalTo( "Mjölnir" ) );
605598
assertThat( unpacker.unpackString(), equalTo( "Mjölnir" ) );
606599
assertThat( unpacker.unpackString(), equalTo( "Mjölnir" ) );
@@ -610,26 +603,10 @@ public void testCanPackAndUnpackListOfSpecialStringOneByOne() throws Throwable
610603
@Test
611604
public void testCanPackAndUnpackMap() throws Throwable
612605
{
613-
// Given
614-
Machine machine = new Machine();
615-
616-
// When
617-
PackStream.Packer packer = machine.packer();
618-
packer.pack( asMap( "one", 1, "two", 2 ) );
619-
packer.flush();
620-
621-
// Then
622-
PackStream.Unpacker unpacker = newUnpacker( machine.output() );
623-
PackType packType = unpacker.peekNextType();
624-
625-
assertThat( packType, equalTo( PackType.MAP ) );
626-
assertThat( unpacker.unpackMapHeader(), equalTo( 2l ) );
627-
628-
assertThat( unpacker.unpackString(), equalTo( "one") );
629-
assertThat( unpacker.unpackLong(), equalTo( 1l) );
630-
631-
assertThat( unpacker.unpackString(), equalTo( "two") );
632-
assertThat( unpacker.unpackLong(), equalTo( 2l) );
606+
assertMap( 2 );
607+
assertMap( 126 );
608+
assertMap( 2439 );
609+
assertMap( 32768 );
633610
}
634611

635612
@Test
@@ -651,20 +628,32 @@ public void testCanPackAndUnpackStruct() throws Throwable
651628
PackType packType = unpacker.peekNextType();
652629

653630
assertThat( packType, equalTo( PackType.STRUCT ) );
654-
assertThat( unpacker.unpackStructHeader(), equalTo( 3l ) );
631+
assertThat( unpacker.unpackStructHeader(), equalTo( 3L ) );
655632
assertThat( unpacker.unpackStructSignature(), equalTo( (byte)'N' ) );
656633

657-
assertThat( unpacker.unpackLong(), equalTo( 12l ));
634+
assertThat( unpacker.unpackLong(), equalTo( 12L ) );
658635

659-
assertThat( unpacker.unpackListHeader(), equalTo( 2l ));
636+
assertThat( unpacker.unpackListHeader(), equalTo( 2L ) );
660637
assertThat( unpacker.unpackString(), equalTo( "Person" ));
661638
assertThat( unpacker.unpackString(), equalTo( "Employee" ));
662639

663-
assertThat( unpacker.unpackMapHeader(), equalTo( 2l ));
640+
assertThat( unpacker.unpackMapHeader(), equalTo( 2L ) );
664641
assertThat( unpacker.unpackString(), equalTo( "name" ));
665642
assertThat( unpacker.unpackString(), equalTo( "Alice" ));
666643
assertThat( unpacker.unpackString(), equalTo( "age" ));
667-
assertThat( unpacker.unpackLong(), equalTo( 33l ));
644+
assertThat( unpacker.unpackLong(), equalTo( 33L ) );
645+
}
646+
647+
@Test
648+
public void testCanPackAndUnpackStructsOfDifferentSizes() throws Throwable
649+
{
650+
assertStruct( 2 );
651+
assertStruct( 126 );
652+
assertStruct( 2439 );
653+
654+
//we cannot have 'too many' fields
655+
exception.expect( PackStream.Overflow.class );
656+
assertStruct( 32768 );
668657
}
669658

670659
@Test
@@ -843,6 +832,22 @@ public void testCanPeekOnNextType() throws Throwable
843832
assertPeekType( PackType.MAP, asMap( "l",3 ) );
844833
}
845834

835+
@Test
836+
public void shouldFailForUnknownValue() throws IOException
837+
{
838+
// Given
839+
Machine machine = new Machine();
840+
PackStream.Packer packer = machine.packer();
841+
842+
// Expect
843+
exception.expect( PackStream.UnPackable.class );
844+
845+
// When
846+
packer.pack( new MyRandomClass() );
847+
}
848+
849+
private static class MyRandomClass{}
850+
846851
void assertPeekType( PackType type, Object value ) throws IOException
847852
{
848853
// Given
@@ -856,4 +861,90 @@ void assertPeekType( PackType type, Object value ) throws IOException
856861
// When & Then
857862
assertEquals( type, unpacker.peekNextType() );
858863
}
864+
865+
private void assertPackStringLists( int size, String value ) throws Throwable
866+
{
867+
// Given
868+
Machine machine = new Machine();
869+
870+
// When
871+
PackStream.Packer packer = machine.packer();
872+
ArrayList<String> strings = new ArrayList<>( size );
873+
for ( int i = 0; i < size; i++ )
874+
{
875+
strings.add( i, value );
876+
}
877+
packer.pack( strings );
878+
packer.flush();
879+
880+
// Then
881+
PackStream.Unpacker unpacker = newUnpacker( machine.output() );
882+
PackType packType = unpacker.peekNextType();
883+
assertThat( packType, equalTo( PackType.LIST ) );
884+
885+
assertThat( unpacker.unpackListHeader(), equalTo( (long) size ) );
886+
for ( int i = 0; i < size; i++ )
887+
{
888+
assertThat( unpacker.unpackString(), equalTo( "Mjölnir" ) );
889+
}
890+
}
891+
892+
private void assertMap( int size ) throws Throwable
893+
{
894+
// Given
895+
Machine machine = new Machine();
896+
897+
// When
898+
PackStream.Packer packer = machine.packer();
899+
HashMap<String,Integer> map = new HashMap<>();
900+
for ( int i = 0; i < size; i++ )
901+
{
902+
map.put( Integer.toString( i ), i );
903+
}
904+
packer.pack( map );
905+
packer.flush();
906+
907+
// Then
908+
PackStream.Unpacker unpacker = newUnpacker( machine.output() );
909+
PackType packType = unpacker.peekNextType();
910+
911+
assertThat( packType, equalTo( PackType.MAP ) );
912+
913+
914+
assertThat( unpacker.unpackMapHeader(), equalTo( (long) size ) );
915+
916+
for ( int i = 0; i < size; i++ )
917+
{
918+
assertThat( unpacker.unpackString(), equalTo( Long.toString( unpacker.unpackLong() ) ) );
919+
}
920+
}
921+
922+
private void assertStruct( int size ) throws Throwable
923+
{
924+
// Given
925+
Machine machine = new Machine();
926+
927+
// When
928+
PackStream.Packer packer = machine.packer();
929+
packer.packStructHeader( size, (byte) 'N' );
930+
for ( int i = 0; i < size; i++ )
931+
{
932+
packer.pack( i );
933+
}
934+
935+
packer.flush();
936+
937+
// Then
938+
PackStream.Unpacker unpacker = newUnpacker( machine.output() );
939+
PackType packType = unpacker.peekNextType();
940+
941+
assertThat( packType, equalTo( PackType.STRUCT ) );
942+
assertThat( unpacker.unpackStructHeader(), equalTo( (long) size ) );
943+
assertThat( unpacker.unpackStructSignature(), equalTo( (byte) 'N' ) );
944+
945+
for ( int i = 0; i < size; i++ )
946+
{
947+
assertThat( unpacker.unpackLong(), equalTo( (long) i ) );
948+
}
949+
}
859950
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.internal.summary;
20+
21+
import org.junit.Test;
22+
23+
import static org.hamcrest.CoreMatchers.equalTo;
24+
import static org.hamcrest.MatcherAssert.assertThat;
25+
26+
public class InternalInputPositionTest
27+
{
28+
@Test
29+
public void shouldBehaveAsExpected()
30+
{
31+
// GIVEN, WHEN
32+
InternalInputPosition position = new InternalInputPosition( 0, 2, 1 );
33+
34+
// THEN
35+
assertThat( position.offset(), equalTo( 0 ) );
36+
assertThat( position.column(), equalTo( 1 ) );
37+
assertThat( position.line(), equalTo( 2 ) );
38+
assertThat( position.toString(), equalTo( "offset=0, line=2, column=1" ) );
39+
}
40+
41+
}

0 commit comments

Comments
 (0)