From 34be1e96fa07ea65f16289602a47fa5aa2c156b3 Mon Sep 17 00:00:00 2001 From: nishant Date: Mon, 4 May 2015 15:26:50 +0530 Subject: [PATCH] fix NPE review comments Add test fix test for java8 --- .../AggregateTopNMetricFirstAlgorithm.java | 8 +-- .../druid/query/topn/PooledTopNAlgorithm.java | 10 ++-- .../query/topn/PooledTopNAlgorithmTest.java | 53 +++++++++++++++++++ 3 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 processing/src/test/java/io/druid/query/topn/PooledTopNAlgorithmTest.java diff --git a/processing/src/main/java/io/druid/query/topn/AggregateTopNMetricFirstAlgorithm.java b/processing/src/main/java/io/druid/query/topn/AggregateTopNMetricFirstAlgorithm.java index 25b12ce6030..f892e07d839 100644 --- a/processing/src/main/java/io/druid/query/topn/AggregateTopNMetricFirstAlgorithm.java +++ b/processing/src/main/java/io/druid/query/topn/AggregateTopNMetricFirstAlgorithm.java @@ -101,9 +101,7 @@ public class AggregateTopNMetricFirstAlgorithm implements TopNAlgorithm resultsBufHolder = params.getResultsBufHolder(); + if (params != null) { + ResourceHolder resultsBufHolder = params.getResultsBufHolder(); - if (resultsBufHolder != null) { - resultsBufHolder.get().clear(); + if (resultsBufHolder != null) { + resultsBufHolder.get().clear(); + } + CloseQuietly.close(resultsBufHolder); } - CloseQuietly.close(resultsBufHolder); } public static class PooledTopNParams extends TopNParams diff --git a/processing/src/test/java/io/druid/query/topn/PooledTopNAlgorithmTest.java b/processing/src/test/java/io/druid/query/topn/PooledTopNAlgorithmTest.java new file mode 100644 index 00000000000..0e2888094d6 --- /dev/null +++ b/processing/src/test/java/io/druid/query/topn/PooledTopNAlgorithmTest.java @@ -0,0 +1,53 @@ +/* + * Druid - a distributed column store. + * Copyright 2012 - 2015 Metamarkets Group Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.druid.query.topn; + +import io.druid.collections.ResourceHolder; +import io.druid.segment.Capabilities; +import org.easymock.EasyMock; +import org.junit.Test; + +import java.io.IOException; +import java.nio.ByteBuffer; + +public class PooledTopNAlgorithmTest +{ + @Test + public void testCleanupWithNullParams() + { + PooledTopNAlgorithm pooledTopNAlgorithm = new PooledTopNAlgorithm(Capabilities.builder().build(), null, null); + pooledTopNAlgorithm.cleanup(null); + } + + @Test + public void cleanup() throws IOException + { + PooledTopNAlgorithm pooledTopNAlgorithm = new PooledTopNAlgorithm(Capabilities.builder().build(), null, null); + PooledTopNAlgorithm.PooledTopNParams params = EasyMock.createMock(PooledTopNAlgorithm.PooledTopNParams.class); + ResourceHolder resourceHolder = EasyMock.createMock(ResourceHolder.class); + EasyMock.expect(params.getResultsBufHolder()).andReturn(resourceHolder).times(1); + EasyMock.expect(resourceHolder.get()).andReturn(ByteBuffer.allocate(1)).times(1); + resourceHolder.close(); + EasyMock.expectLastCall().once(); + EasyMock.replay(params); + EasyMock.replay(resourceHolder); + pooledTopNAlgorithm.cleanup(params); + EasyMock.verify(params); + EasyMock.verify(resourceHolder); + } +}