Skip to content

Commit 4a58e4d

Browse files
author
Zhen Li
committed
Merge pull request #74 from neo4j/fix-null-stats
Return an empty stats if no stat is included in summary
2 parents 378484b + 2cfd5cf commit 4a58e4d

File tree

4 files changed

+90
-7
lines changed

4 files changed

+90
-7
lines changed

driver/src/main/java/org/neo4j/driver/ResultSummary.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
* The result summary of running a statement. The result summary interface can be used to investigate
2525
* details about the result, like the type of query run, how many and which kinds of updates have been executed,
2626
* and query plan and profiling information if available.
27-
* <p>
27+
*
2828
* The result summary is only available after all result records have been consumed.
29-
* <p>
29+
*
3030
* Keeping the result summary around does not influence the lifecycle of any associated session and/or transaction.
3131
*/
3232
public interface ResultSummary
@@ -59,8 +59,7 @@ public interface ResultSummary
5959
/**
6060
* This describes how the database will execute your statement.
6161
*
62-
* @throws IllegalStateException if {@link #hasPlan()} is false
63-
* @return statement plan for the executed statement if available
62+
* @return statement plan for the executed statement if available, otherwise null
6463
*/
6564
Plan plan();
6665

@@ -71,8 +70,7 @@ public interface ResultSummary
7170
* information about what each step of the plan did. That more in-depth version of the statement plan becomes
7271
* available here.
7372
*
74-
* @throws IllegalStateException if {@link #hasProfile()} is false
75-
* @return profiled statement plan for the executed statement if available
73+
* @return profiled statement plan for the executed statement if available, otherwise null
7674
*/
7775
ProfiledPlan profile();
7876

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
public class SimpleUpdateStatistics implements UpdateStatistics
2424
{
25+
public static final SimpleUpdateStatistics EMPTY_STATS =
26+
new SimpleUpdateStatistics( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
2527
private final int nodesCreated;
2628
private final int nodesDeleted;
2729
private final int relationshipsCreated;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.neo4j.driver.exceptions.ClientException;
3333
import org.neo4j.driver.internal.spi.StreamCollector;
3434

35+
import static org.neo4j.driver.internal.summary.SimpleUpdateStatistics.EMPTY_STATS;
36+
3537
public class SummaryBuilder implements StreamCollector
3638
{
3739
private final Statement statement;
@@ -136,7 +138,7 @@ public Statement statement()
136138
@Override
137139
public UpdateStatistics updateStatistics()
138140
{
139-
return statistics;
141+
return statistics == null ? EMPTY_STATS : statistics;
140142
}
141143

142144
@Override
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.internal;
20+
21+
import org.junit.Test;
22+
23+
import org.neo4j.driver.ResultSummary;
24+
import org.neo4j.driver.Statement;
25+
import org.neo4j.driver.UpdateStatistics;
26+
import org.neo4j.driver.internal.summary.SimpleUpdateStatistics;
27+
import org.neo4j.driver.internal.summary.SummaryBuilder;
28+
29+
import static org.hamcrest.CoreMatchers.equalTo;
30+
import static org.hamcrest.MatcherAssert.assertThat;
31+
import static org.junit.Assert.assertEquals;
32+
import static org.junit.Assert.assertNull;
33+
import static org.mockito.Mockito.mock;
34+
35+
public class SummaryBuilderTest
36+
{
37+
@Test
38+
public void shouldReturnEmptyStatisticsIfNotProvided() throws Throwable
39+
{
40+
// Given
41+
SummaryBuilder builder = new SummaryBuilder( mock( Statement.class ) );
42+
43+
// When
44+
ResultSummary summary = builder.build();
45+
UpdateStatistics stats = summary.updateStatistics();
46+
47+
// Then
48+
assertEquals( stats, new SimpleUpdateStatistics( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) );
49+
}
50+
51+
52+
@Test
53+
public void shouldReturnNullIfNoPlanProfileProvided() throws Throwable
54+
{
55+
// Given
56+
SummaryBuilder builder = new SummaryBuilder( mock( Statement.class ) );
57+
58+
// When
59+
ResultSummary summary = builder.build();
60+
61+
// Then
62+
assertThat( summary.hasPlan(), equalTo( false ) );
63+
assertThat( summary.hasProfile(), equalTo( false ) );
64+
assertNull( summary.plan() );
65+
assertNull( summary.profile() );
66+
}
67+
68+
69+
@Test
70+
public void shouldReturnNullIfNoStatementTypeProvided() throws Throwable
71+
{
72+
// Given
73+
SummaryBuilder builder = new SummaryBuilder( mock( Statement.class ) );
74+
75+
// When
76+
ResultSummary summary = builder.build();
77+
78+
// Then
79+
assertNull( summary.statementType() );
80+
}
81+
}

0 commit comments

Comments
 (0)