http://git-wip-us.apache.org/repos/asf/storm/blob/81ec15d1/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/IntermediateRankingsBoltTest.java ---------------------------------------------------------------------- diff --git a/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/IntermediateRankingsBoltTest.java b/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/IntermediateRankingsBoltTest.java index 23326c5..2580dfb 100644 --- a/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/IntermediateRankingsBoltTest.java +++ b/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/IntermediateRankingsBoltTest.java @@ -1,22 +1,19 @@ /** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The ASF licenses this file to you 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. + * 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 org.apache.storm.starter.bolt; +import com.google.common.collect.Lists; +import java.util.Map; import org.apache.storm.Config; import org.apache.storm.topology.BasicOutputCollector; import org.apache.storm.topology.OutputFieldsDeclarer; @@ -24,123 +21,124 @@ import org.apache.storm.tuple.Fields; import org.apache.storm.tuple.Tuple; import org.apache.storm.tuple.Values; import org.apache.storm.utils.MockTupleHelpers; -import com.google.common.collect.Lists; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import java.util.Map; - import static org.fest.assertions.api.Assertions.assertThat; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; public class IntermediateRankingsBoltTest { - private static final String ANY_NON_SYSTEM_COMPONENT_ID = "irrelevant_component_id"; - private static final String ANY_NON_SYSTEM_STREAM_ID = "irrelevant_stream_id"; - private static final Object ANY_OBJECT = new Object(); - private static final int ANY_TOPN = 10; - private static final long ANY_COUNT = 42; - - private Tuple mockRankableTuple(Object obj, long count) { - Tuple tuple = MockTupleHelpers.mockTuple(ANY_NON_SYSTEM_COMPONENT_ID, ANY_NON_SYSTEM_STREAM_ID); - when(tuple.getValues()).thenReturn(Lists.newArrayList(ANY_OBJECT, ANY_COUNT)); - return tuple; - } - - @DataProvider - public Object[][] illegalTopN() { - return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } }; - } - - @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalTopN") - public void negativeOrZeroTopNShouldThrowIAE(int topN) { - new IntermediateRankingsBolt(topN); - } - - @DataProvider - public Object[][] illegalEmitFrequency() { - return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } }; - } - - @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalEmitFrequency") - public void negativeOrZeroEmitFrequencyShouldThrowIAE(int emitFrequencyInSeconds) { - new IntermediateRankingsBolt(ANY_TOPN, emitFrequencyInSeconds); - } - - @DataProvider - public Object[][] legalTopN() { - return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } }; - } - - @Test(dataProvider = "legalTopN") - public void positiveTopNShouldBeOk(int topN) { - new IntermediateRankingsBolt(topN); - } - - @DataProvider - public Object[][] legalEmitFrequency() { - return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } }; - } - - @Test(dataProvider = "legalEmitFrequency") - public void positiveEmitFrequencyShouldBeOk(int emitFrequencyInSeconds) { - new IntermediateRankingsBolt(ANY_TOPN, emitFrequencyInSeconds); - } - - @Test - public void shouldEmitSomethingIfTickTupleIsReceived() { - // given - Tuple tickTuple = MockTupleHelpers.mockTickTuple(); - BasicOutputCollector collector = mock(BasicOutputCollector.class); - IntermediateRankingsBolt bolt = new IntermediateRankingsBolt(); - - // when - bolt.execute(tickTuple, collector); - - // then - // verifyZeroInteractions(collector); - verify(collector).emit(any(Values.class)); - } - - @Test - public void shouldEmitNothingIfNormalTupleIsReceived() { - // given - Tuple normalTuple = mockRankableTuple(ANY_OBJECT, ANY_COUNT); - BasicOutputCollector collector = mock(BasicOutputCollector.class); - IntermediateRankingsBolt bolt = new IntermediateRankingsBolt(); - - // when - bolt.execute(normalTuple, collector); - - // then - verifyZeroInteractions(collector); - } - - @Test - public void shouldDeclareOutputFields() { - // given - OutputFieldsDeclarer declarer = mock(OutputFieldsDeclarer.class); - IntermediateRankingsBolt bolt = new IntermediateRankingsBolt(); - - // when - bolt.declareOutputFields(declarer); - - // then - verify(declarer, times(1)).declare(any(Fields.class)); - } - - @Test - public void shouldSetTickTupleFrequencyInComponentConfigurationToNonZeroValue() { - // given - IntermediateRankingsBolt bolt = new IntermediateRankingsBolt(); - - // when - Map<String, Object> componentConfig = bolt.getComponentConfiguration(); - - // then - assertThat(componentConfig).containsKey(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); - Integer emitFrequencyInSeconds = (Integer) componentConfig.get(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); - assertThat(emitFrequencyInSeconds).isGreaterThan(0); - } + private static final String ANY_NON_SYSTEM_COMPONENT_ID = "irrelevant_component_id"; + private static final String ANY_NON_SYSTEM_STREAM_ID = "irrelevant_stream_id"; + private static final Object ANY_OBJECT = new Object(); + private static final int ANY_TOPN = 10; + private static final long ANY_COUNT = 42; + + private Tuple mockRankableTuple(Object obj, long count) { + Tuple tuple = MockTupleHelpers.mockTuple(ANY_NON_SYSTEM_COMPONENT_ID, ANY_NON_SYSTEM_STREAM_ID); + when(tuple.getValues()).thenReturn(Lists.newArrayList(ANY_OBJECT, ANY_COUNT)); + return tuple; + } + + @DataProvider + public Object[][] illegalTopN() { + return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } }; + } + + @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalTopN") + public void negativeOrZeroTopNShouldThrowIAE(int topN) { + new IntermediateRankingsBolt(topN); + } + + @DataProvider + public Object[][] illegalEmitFrequency() { + return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } }; + } + + @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalEmitFrequency") + public void negativeOrZeroEmitFrequencyShouldThrowIAE(int emitFrequencyInSeconds) { + new IntermediateRankingsBolt(ANY_TOPN, emitFrequencyInSeconds); + } + + @DataProvider + public Object[][] legalTopN() { + return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } }; + } + + @Test(dataProvider = "legalTopN") + public void positiveTopNShouldBeOk(int topN) { + new IntermediateRankingsBolt(topN); + } + + @DataProvider + public Object[][] legalEmitFrequency() { + return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } }; + } + + @Test(dataProvider = "legalEmitFrequency") + public void positiveEmitFrequencyShouldBeOk(int emitFrequencyInSeconds) { + new IntermediateRankingsBolt(ANY_TOPN, emitFrequencyInSeconds); + } + + @Test + public void shouldEmitSomethingIfTickTupleIsReceived() { + // given + Tuple tickTuple = MockTupleHelpers.mockTickTuple(); + BasicOutputCollector collector = mock(BasicOutputCollector.class); + IntermediateRankingsBolt bolt = new IntermediateRankingsBolt(); + + // when + bolt.execute(tickTuple, collector); + + // then + // verifyZeroInteractions(collector); + verify(collector).emit(any(Values.class)); + } + + @Test + public void shouldEmitNothingIfNormalTupleIsReceived() { + // given + Tuple normalTuple = mockRankableTuple(ANY_OBJECT, ANY_COUNT); + BasicOutputCollector collector = mock(BasicOutputCollector.class); + IntermediateRankingsBolt bolt = new IntermediateRankingsBolt(); + + // when + bolt.execute(normalTuple, collector); + + // then + verifyZeroInteractions(collector); + } + + @Test + public void shouldDeclareOutputFields() { + // given + OutputFieldsDeclarer declarer = mock(OutputFieldsDeclarer.class); + IntermediateRankingsBolt bolt = new IntermediateRankingsBolt(); + + // when + bolt.declareOutputFields(declarer); + + // then + verify(declarer, times(1)).declare(any(Fields.class)); + } + + @Test + public void shouldSetTickTupleFrequencyInComponentConfigurationToNonZeroValue() { + // given + IntermediateRankingsBolt bolt = new IntermediateRankingsBolt(); + + // when + Map<String, Object> componentConfig = bolt.getComponentConfiguration(); + + // then + assertThat(componentConfig).containsKey(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); + Integer emitFrequencyInSeconds = (Integer) componentConfig.get(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); + assertThat(emitFrequencyInSeconds).isGreaterThan(0); + } }
http://git-wip-us.apache.org/repos/asf/storm/blob/81ec15d1/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/RollingCountBoltTest.java ---------------------------------------------------------------------- diff --git a/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/RollingCountBoltTest.java b/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/RollingCountBoltTest.java index e58c356..ccac7bc 100644 --- a/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/RollingCountBoltTest.java +++ b/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/RollingCountBoltTest.java @@ -1,22 +1,18 @@ /** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The ASF licenses this file to you 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. + * 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 org.apache.storm.starter.bolt; +import java.util.Map; import org.apache.storm.Config; import org.apache.storm.task.OutputCollector; import org.apache.storm.task.TopologyContext; @@ -27,87 +23,89 @@ import org.apache.storm.tuple.Values; import org.apache.storm.utils.MockTupleHelpers; import org.testng.annotations.Test; -import java.util.Map; - import static org.fest.assertions.api.Assertions.assertThat; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; public class RollingCountBoltTest { - private static final String ANY_NON_SYSTEM_COMPONENT_ID = "irrelevant_component_id"; - private static final String ANY_NON_SYSTEM_STREAM_ID = "irrelevant_stream_id"; - - private Tuple mockNormalTuple(Object obj) { - Tuple tuple = MockTupleHelpers.mockTuple(ANY_NON_SYSTEM_COMPONENT_ID, ANY_NON_SYSTEM_STREAM_ID); - when(tuple.getValue(0)).thenReturn(obj); - return tuple; - } - - @SuppressWarnings("rawtypes") - @Test - public void shouldEmitNothingIfNoObjectHasBeenCountedYetAndTickTupleIsReceived() { - // given - Tuple tickTuple = MockTupleHelpers.mockTickTuple(); - RollingCountBolt bolt = new RollingCountBolt(); - Map<String, Object> conf = mock(Map.class); - TopologyContext context = mock(TopologyContext.class); - OutputCollector collector = mock(OutputCollector.class); - bolt.prepare(conf, context, collector); - - // when - bolt.execute(tickTuple); - - // then - verifyZeroInteractions(collector); - } - - @SuppressWarnings("rawtypes") - @Test - public void shouldEmitSomethingIfAtLeastOneObjectWasCountedAndTickTupleIsReceived() { - // given - Tuple normalTuple = mockNormalTuple(new Object()); - Tuple tickTuple = MockTupleHelpers.mockTickTuple(); - - RollingCountBolt bolt = new RollingCountBolt(); - Map<String, Object> conf = mock(Map.class); - TopologyContext context = mock(TopologyContext.class); - OutputCollector collector = mock(OutputCollector.class); - bolt.prepare(conf, context, collector); - - // when - bolt.execute(normalTuple); - bolt.execute(tickTuple); - - // then - verify(collector).emit(any(Values.class)); - } - - @Test - public void shouldDeclareOutputFields() { - // given - OutputFieldsDeclarer declarer = mock(OutputFieldsDeclarer.class); - RollingCountBolt bolt = new RollingCountBolt(); - - // when - bolt.declareOutputFields(declarer); - - // then - verify(declarer, times(1)).declare(any(Fields.class)); - - } - - @Test - public void shouldSetTickTupleFrequencyInComponentConfigurationToNonZeroValue() { - // given - RollingCountBolt bolt = new RollingCountBolt(); - - // when - Map<String, Object> componentConfig = bolt.getComponentConfiguration(); - - // then - assertThat(componentConfig).containsKey(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); - Integer emitFrequencyInSeconds = (Integer) componentConfig.get(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); - assertThat(emitFrequencyInSeconds).isGreaterThan(0); - } + private static final String ANY_NON_SYSTEM_COMPONENT_ID = "irrelevant_component_id"; + private static final String ANY_NON_SYSTEM_STREAM_ID = "irrelevant_stream_id"; + + private Tuple mockNormalTuple(Object obj) { + Tuple tuple = MockTupleHelpers.mockTuple(ANY_NON_SYSTEM_COMPONENT_ID, ANY_NON_SYSTEM_STREAM_ID); + when(tuple.getValue(0)).thenReturn(obj); + return tuple; + } + + @SuppressWarnings("rawtypes") + @Test + public void shouldEmitNothingIfNoObjectHasBeenCountedYetAndTickTupleIsReceived() { + // given + Tuple tickTuple = MockTupleHelpers.mockTickTuple(); + RollingCountBolt bolt = new RollingCountBolt(); + Map<String, Object> conf = mock(Map.class); + TopologyContext context = mock(TopologyContext.class); + OutputCollector collector = mock(OutputCollector.class); + bolt.prepare(conf, context, collector); + + // when + bolt.execute(tickTuple); + + // then + verifyZeroInteractions(collector); + } + + @SuppressWarnings("rawtypes") + @Test + public void shouldEmitSomethingIfAtLeastOneObjectWasCountedAndTickTupleIsReceived() { + // given + Tuple normalTuple = mockNormalTuple(new Object()); + Tuple tickTuple = MockTupleHelpers.mockTickTuple(); + + RollingCountBolt bolt = new RollingCountBolt(); + Map<String, Object> conf = mock(Map.class); + TopologyContext context = mock(TopologyContext.class); + OutputCollector collector = mock(OutputCollector.class); + bolt.prepare(conf, context, collector); + + // when + bolt.execute(normalTuple); + bolt.execute(tickTuple); + + // then + verify(collector).emit(any(Values.class)); + } + + @Test + public void shouldDeclareOutputFields() { + // given + OutputFieldsDeclarer declarer = mock(OutputFieldsDeclarer.class); + RollingCountBolt bolt = new RollingCountBolt(); + + // when + bolt.declareOutputFields(declarer); + + // then + verify(declarer, times(1)).declare(any(Fields.class)); + + } + + @Test + public void shouldSetTickTupleFrequencyInComponentConfigurationToNonZeroValue() { + // given + RollingCountBolt bolt = new RollingCountBolt(); + + // when + Map<String, Object> componentConfig = bolt.getComponentConfiguration(); + + // then + assertThat(componentConfig).containsKey(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); + Integer emitFrequencyInSeconds = (Integer) componentConfig.get(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); + assertThat(emitFrequencyInSeconds).isGreaterThan(0); + } } http://git-wip-us.apache.org/repos/asf/storm/blob/81ec15d1/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/TotalRankingsBoltTest.java ---------------------------------------------------------------------- diff --git a/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/TotalRankingsBoltTest.java b/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/TotalRankingsBoltTest.java index c3582d5..bc3ca1f 100644 --- a/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/TotalRankingsBoltTest.java +++ b/examples/storm-starter/test/jvm/org/apache/storm/starter/bolt/TotalRankingsBoltTest.java @@ -1,23 +1,20 @@ /** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The ASF licenses this file to you 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. + * 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 org.apache.storm.starter.bolt; +import java.util.Map; import org.apache.storm.Config; +import org.apache.storm.starter.tools.Rankings; import org.apache.storm.topology.BasicOutputCollector; import org.apache.storm.topology.OutputFieldsDeclarer; import org.apache.storm.tuple.Fields; @@ -26,122 +23,123 @@ import org.apache.storm.tuple.Values; import org.apache.storm.utils.MockTupleHelpers; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import org.apache.storm.starter.tools.Rankings; - -import java.util.Map; import static org.fest.assertions.api.Assertions.assertThat; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; public class TotalRankingsBoltTest { - private static final String ANY_NON_SYSTEM_COMPONENT_ID = "irrelevant_component_id"; - private static final String ANY_NON_SYSTEM_STREAM_ID = "irrelevant_stream_id"; - private static final Object ANY_OBJECT = new Object(); - private static final int ANY_TOPN = 10; - private static final long ANY_COUNT = 42; - - private Tuple mockRankingsTuple(Object obj, long count) { - Tuple tuple = MockTupleHelpers.mockTuple(ANY_NON_SYSTEM_COMPONENT_ID, ANY_NON_SYSTEM_STREAM_ID); - Rankings rankings = mock(Rankings.class); - when(tuple.getValue(0)).thenReturn(rankings); - return tuple; - } - - @DataProvider - public Object[][] illegalTopN() { - return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } }; - } - - @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalTopN") - public void negativeOrZeroTopNShouldThrowIAE(int topN) { - new TotalRankingsBolt(topN); - } - - @DataProvider - public Object[][] illegalEmitFrequency() { - return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } }; - } - - @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalEmitFrequency") - public void negativeOrZeroEmitFrequencyShouldThrowIAE(int emitFrequencyInSeconds) { - new TotalRankingsBolt(ANY_TOPN, emitFrequencyInSeconds); - } - - @DataProvider - public Object[][] legalTopN() { - return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } }; - } - - @Test(dataProvider = "legalTopN") - public void positiveTopNShouldBeOk(int topN) { - new TotalRankingsBolt(topN); - } - - @DataProvider - public Object[][] legalEmitFrequency() { - return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } }; - } - - @Test(dataProvider = "legalEmitFrequency") - public void positiveEmitFrequencyShouldBeOk(int emitFrequencyInSeconds) { - new TotalRankingsBolt(ANY_TOPN, emitFrequencyInSeconds); - } - - @Test - public void shouldEmitSomethingIfTickTupleIsReceived() { - // given - Tuple tickTuple = MockTupleHelpers.mockTickTuple(); - BasicOutputCollector collector = mock(BasicOutputCollector.class); - TotalRankingsBolt bolt = new TotalRankingsBolt(); - - // when - bolt.execute(tickTuple, collector); - - // then - // verifyZeroInteractions(collector); - verify(collector).emit(any(Values.class)); - } - - @Test - public void shouldEmitNothingIfNormalTupleIsReceived() { - // given - Tuple normalTuple = mockRankingsTuple(ANY_OBJECT, ANY_COUNT); - BasicOutputCollector collector = mock(BasicOutputCollector.class); - TotalRankingsBolt bolt = new TotalRankingsBolt(); - - // when - bolt.execute(normalTuple, collector); - - // then - verifyZeroInteractions(collector); - } - - @Test - public void shouldDeclareOutputFields() { - // given - OutputFieldsDeclarer declarer = mock(OutputFieldsDeclarer.class); - TotalRankingsBolt bolt = new TotalRankingsBolt(); - - // when - bolt.declareOutputFields(declarer); - - // then - verify(declarer, times(1)).declare(any(Fields.class)); - } - - @Test - public void shouldSetTickTupleFrequencyInComponentConfigurationToNonZeroValue() { - // given - TotalRankingsBolt bolt = new TotalRankingsBolt(); - - // when - Map<String, Object> componentConfig = bolt.getComponentConfiguration(); - - // then - assertThat(componentConfig).containsKey(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); - Integer emitFrequencyInSeconds = (Integer) componentConfig.get(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); - assertThat(emitFrequencyInSeconds).isGreaterThan(0); - } + private static final String ANY_NON_SYSTEM_COMPONENT_ID = "irrelevant_component_id"; + private static final String ANY_NON_SYSTEM_STREAM_ID = "irrelevant_stream_id"; + private static final Object ANY_OBJECT = new Object(); + private static final int ANY_TOPN = 10; + private static final long ANY_COUNT = 42; + + private Tuple mockRankingsTuple(Object obj, long count) { + Tuple tuple = MockTupleHelpers.mockTuple(ANY_NON_SYSTEM_COMPONENT_ID, ANY_NON_SYSTEM_STREAM_ID); + Rankings rankings = mock(Rankings.class); + when(tuple.getValue(0)).thenReturn(rankings); + return tuple; + } + + @DataProvider + public Object[][] illegalTopN() { + return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } }; + } + + @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalTopN") + public void negativeOrZeroTopNShouldThrowIAE(int topN) { + new TotalRankingsBolt(topN); + } + + @DataProvider + public Object[][] illegalEmitFrequency() { + return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } }; + } + + @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalEmitFrequency") + public void negativeOrZeroEmitFrequencyShouldThrowIAE(int emitFrequencyInSeconds) { + new TotalRankingsBolt(ANY_TOPN, emitFrequencyInSeconds); + } + + @DataProvider + public Object[][] legalTopN() { + return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } }; + } + + @Test(dataProvider = "legalTopN") + public void positiveTopNShouldBeOk(int topN) { + new TotalRankingsBolt(topN); + } + + @DataProvider + public Object[][] legalEmitFrequency() { + return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } }; + } + + @Test(dataProvider = "legalEmitFrequency") + public void positiveEmitFrequencyShouldBeOk(int emitFrequencyInSeconds) { + new TotalRankingsBolt(ANY_TOPN, emitFrequencyInSeconds); + } + + @Test + public void shouldEmitSomethingIfTickTupleIsReceived() { + // given + Tuple tickTuple = MockTupleHelpers.mockTickTuple(); + BasicOutputCollector collector = mock(BasicOutputCollector.class); + TotalRankingsBolt bolt = new TotalRankingsBolt(); + + // when + bolt.execute(tickTuple, collector); + + // then + // verifyZeroInteractions(collector); + verify(collector).emit(any(Values.class)); + } + + @Test + public void shouldEmitNothingIfNormalTupleIsReceived() { + // given + Tuple normalTuple = mockRankingsTuple(ANY_OBJECT, ANY_COUNT); + BasicOutputCollector collector = mock(BasicOutputCollector.class); + TotalRankingsBolt bolt = new TotalRankingsBolt(); + + // when + bolt.execute(normalTuple, collector); + + // then + verifyZeroInteractions(collector); + } + + @Test + public void shouldDeclareOutputFields() { + // given + OutputFieldsDeclarer declarer = mock(OutputFieldsDeclarer.class); + TotalRankingsBolt bolt = new TotalRankingsBolt(); + + // when + bolt.declareOutputFields(declarer); + + // then + verify(declarer, times(1)).declare(any(Fields.class)); + } + + @Test + public void shouldSetTickTupleFrequencyInComponentConfigurationToNonZeroValue() { + // given + TotalRankingsBolt bolt = new TotalRankingsBolt(); + + // when + Map<String, Object> componentConfig = bolt.getComponentConfiguration(); + + // then + assertThat(componentConfig).containsKey(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); + Integer emitFrequencyInSeconds = (Integer) componentConfig.get(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS); + assertThat(emitFrequencyInSeconds).isGreaterThan(0); + } } http://git-wip-us.apache.org/repos/asf/storm/blob/81ec15d1/examples/storm-starter/test/jvm/org/apache/storm/starter/tools/NthLastModifiedTimeTrackerTest.java ---------------------------------------------------------------------- diff --git a/examples/storm-starter/test/jvm/org/apache/storm/starter/tools/NthLastModifiedTimeTrackerTest.java b/examples/storm-starter/test/jvm/org/apache/storm/starter/tools/NthLastModifiedTimeTrackerTest.java index 2c07168..f4393ed 100644 --- a/examples/storm-starter/test/jvm/org/apache/storm/starter/tools/NthLastModifiedTimeTrackerTest.java +++ b/examples/storm-starter/test/jvm/org/apache/storm/starter/tools/NthLastModifiedTimeTrackerTest.java @@ -1,102 +1,98 @@ /** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The ASF licenses this file to you 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. + * 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 org.apache.storm.starter.tools; import org.apache.storm.utils.Time; +import org.apache.storm.utils.Time.SimulatedTime; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import static org.fest.assertions.api.Assertions.assertThat; -import org.apache.storm.utils.Time.SimulatedTime; - public class NthLastModifiedTimeTrackerTest { - private static final int ANY_NUM_TIMES_TO_TRACK = 3; - private static final int MILLIS_IN_SEC = 1000; - - @DataProvider - public Object[][] illegalNumTimesData() { - return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } }; - } - - @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalNumTimesData") - public void negativeOrZeroNumTimesToTrackShouldThrowIAE(int numTimesToTrack) { - new NthLastModifiedTimeTracker(numTimesToTrack); - } - - @DataProvider - public Object[][] legalNumTimesData() { - return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } }; - } - - @Test(dataProvider = "legalNumTimesData") - public void positiveNumTimesToTrackShouldBeOk(int numTimesToTrack) { - new NthLastModifiedTimeTracker(numTimesToTrack); - } - - @DataProvider - public Object[][] whenNotYetMarkedAsModifiedData() { - return new Object[][]{ { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 8 }, { 10 } }; - } - - @Test(dataProvider = "whenNotYetMarkedAsModifiedData") - public void shouldReturnCorrectModifiedTimeEvenWhenNotYetMarkedAsModified(int secondsToAdvance) { - // given - try (SimulatedTime t = new SimulatedTime()) { - NthLastModifiedTimeTracker tracker = new NthLastModifiedTimeTracker(ANY_NUM_TIMES_TO_TRACK); - - // when - Time.advanceTimeSecs(secondsToAdvance); - int seconds = tracker.secondsSinceOldestModification(); - - // then - assertThat(seconds).isEqualTo(secondsToAdvance); - } - } - - @DataProvider - public Object[][] simulatedTrackerIterations() { - return new Object[][]{ { 1, new int[]{ 0, 1 }, new int[]{ 0, 0 } }, { 1, new int[]{ 0, 2 }, new int[]{ 0, 0 } }, - { 2, new int[]{ 2, 2 }, new int[]{ 2, 2 } }, { 2, new int[]{ 0, 4 }, new int[]{ 0, 4 } }, - { 1, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 0, 0, 0, 0, 0, 0, 0 } }, - { 1, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 0, 0, 0, 0, 0, 0, 0 } }, - { 2, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 1, 1, 1, 1, 1, 1, 1 } }, - { 2, new int[]{ 2, 2, 2, 2, 2, 2, 2 }, new int[]{ 2, 2, 2, 2, 2, 2, 2 } }, - { 2, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 1, 2, 3, 4, 5, 6, 7 } }, - { 3, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 1, 2, 2, 2, 2, 2, 2 } }, - { 3, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 1, 3, 5, 7, 9, 11, 13 } }, - { 3, new int[]{ 2, 2, 2, 2, 2, 2, 2 }, new int[]{ 2, 4, 4, 4, 4, 4, 4 } }, - { 4, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 1, 2, 3, 3, 3, 3, 3 } }, - { 4, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 1, 3, 6, 9, 12, 15, 18 } }, - { 4, new int[]{ 2, 2, 2, 2, 2, 2, 2 }, new int[]{ 2, 4, 6, 6, 6, 6, 6 } }, - { 5, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 1, 2, 3, 4, 4, 4, 4 } }, - { 5, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 1, 3, 6, 10, 14, 18, 22 } }, - { 5, new int[]{ 2, 2, 2, 2, 2, 2, 2 }, new int[]{ 2, 4, 6, 8, 8, 8, 8 } }, - { 6, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 1, 2, 3, 4, 5, 5, 5 } }, - { 6, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 1, 3, 6, 10, 15, 20, 25 } }, - { 6, new int[]{ 2, 2, 2, 2, 2, 2, 2 }, new int[]{ 2, 4, 6, 8, 10, 10, 10 } }, - { 3, new int[]{ 1, 2, 3 }, new int[]{ 1, 3, 5 } } }; - } + private static final int ANY_NUM_TIMES_TO_TRACK = 3; + private static final int MILLIS_IN_SEC = 1000; + + @DataProvider + public Object[][] illegalNumTimesData() { + return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } }; + } + + @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalNumTimesData") + public void negativeOrZeroNumTimesToTrackShouldThrowIAE(int numTimesToTrack) { + new NthLastModifiedTimeTracker(numTimesToTrack); + } + + @DataProvider + public Object[][] legalNumTimesData() { + return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } }; + } + + @Test(dataProvider = "legalNumTimesData") + public void positiveNumTimesToTrackShouldBeOk(int numTimesToTrack) { + new NthLastModifiedTimeTracker(numTimesToTrack); + } + + @DataProvider + public Object[][] whenNotYetMarkedAsModifiedData() { + return new Object[][]{ { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 8 }, { 10 } }; + } + + @Test(dataProvider = "whenNotYetMarkedAsModifiedData") + public void shouldReturnCorrectModifiedTimeEvenWhenNotYetMarkedAsModified(int secondsToAdvance) { + // given + try (SimulatedTime t = new SimulatedTime()) { + NthLastModifiedTimeTracker tracker = new NthLastModifiedTimeTracker(ANY_NUM_TIMES_TO_TRACK); + + // when + Time.advanceTimeSecs(secondsToAdvance); + int seconds = tracker.secondsSinceOldestModification(); + + // then + assertThat(seconds).isEqualTo(secondsToAdvance); + } + } + + @DataProvider + public Object[][] simulatedTrackerIterations() { + return new Object[][]{ + { 1, new int[]{ 0, 1 }, new int[]{ 0, 0 } }, { 1, new int[]{ 0, 2 }, new int[]{ 0, 0 } }, + { 2, new int[]{ 2, 2 }, new int[]{ 2, 2 } }, { 2, new int[]{ 0, 4 }, new int[]{ 0, 4 } }, + { 1, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 0, 0, 0, 0, 0, 0, 0 } }, + { 1, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 0, 0, 0, 0, 0, 0, 0 } }, + { 2, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 1, 1, 1, 1, 1, 1, 1 } }, + { 2, new int[]{ 2, 2, 2, 2, 2, 2, 2 }, new int[]{ 2, 2, 2, 2, 2, 2, 2 } }, + { 2, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 1, 2, 3, 4, 5, 6, 7 } }, + { 3, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 1, 2, 2, 2, 2, 2, 2 } }, + { 3, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 1, 3, 5, 7, 9, 11, 13 } }, + { 3, new int[]{ 2, 2, 2, 2, 2, 2, 2 }, new int[]{ 2, 4, 4, 4, 4, 4, 4 } }, + { 4, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 1, 2, 3, 3, 3, 3, 3 } }, + { 4, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 1, 3, 6, 9, 12, 15, 18 } }, + { 4, new int[]{ 2, 2, 2, 2, 2, 2, 2 }, new int[]{ 2, 4, 6, 6, 6, 6, 6 } }, + { 5, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 1, 2, 3, 4, 4, 4, 4 } }, + { 5, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 1, 3, 6, 10, 14, 18, 22 } }, + { 5, new int[]{ 2, 2, 2, 2, 2, 2, 2 }, new int[]{ 2, 4, 6, 8, 8, 8, 8 } }, + { 6, new int[]{ 1, 1, 1, 1, 1, 1, 1 }, new int[]{ 1, 2, 3, 4, 5, 5, 5 } }, + { 6, new int[]{ 1, 2, 3, 4, 5, 6, 7 }, new int[]{ 1, 3, 6, 10, 15, 20, 25 } }, + { 6, new int[]{ 2, 2, 2, 2, 2, 2, 2 }, new int[]{ 2, 4, 6, 8, 10, 10, 10 } }, + { 3, new int[]{ 1, 2, 3 }, new int[]{ 1, 3, 5 } } + }; + } @Test(dataProvider = "simulatedTrackerIterations") public void shouldReturnCorrectModifiedTimeWhenMarkedAsModified(int numTimesToTrack, - int[] secondsToAdvancePerIteration, int[] expLastModifiedTimes) { + int[] secondsToAdvancePerIteration, int[] expLastModifiedTimes) { // given try (SimulatedTime t = new SimulatedTime()) { NthLastModifiedTimeTracker tracker = new NthLastModifiedTimeTracker(numTimesToTrack); http://git-wip-us.apache.org/repos/asf/storm/blob/81ec15d1/examples/storm-starter/test/jvm/org/apache/storm/starter/tools/RankableObjectWithFieldsTest.java ---------------------------------------------------------------------- diff --git a/examples/storm-starter/test/jvm/org/apache/storm/starter/tools/RankableObjectWithFieldsTest.java b/examples/storm-starter/test/jvm/org/apache/storm/starter/tools/RankableObjectWithFieldsTest.java index 9837569..aa4d470 100644 --- a/examples/storm-starter/test/jvm/org/apache/storm/starter/tools/RankableObjectWithFieldsTest.java +++ b/examples/storm-starter/test/jvm/org/apache/storm/starter/tools/RankableObjectWithFieldsTest.java @@ -1,252 +1,275 @@ /** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The ASF licenses this file to you 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. + * 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 org.apache.storm.starter.tools; -import org.apache.storm.tuple.Tuple; import com.google.common.collect.Lists; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - import java.util.ArrayList; import java.util.List; +import org.apache.storm.tuple.Tuple; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; import static org.fest.assertions.api.Assertions.assertThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; public class RankableObjectWithFieldsTest { - private static final Object ANY_OBJECT = new Object(); - private static final long ANY_COUNT = 271; - private static final String ANY_FIELD = "someAdditionalField"; - private static final int GREATER_THAN = 1; - private static final int EQUAL_TO = 0; - private static final int SMALLER_THAN = -1; - - @Test(expectedExceptions = IllegalArgumentException.class) - public void constructorWithNullObjectAndNoFieldsShouldThrowIAE() { - new RankableObjectWithFields(null, ANY_COUNT); - } - - @Test(expectedExceptions = IllegalArgumentException.class) - public void constructorWithNullObjectAndFieldsShouldThrowIAE() { - Object someAdditionalField = new Object(); - new RankableObjectWithFields(null, ANY_COUNT, someAdditionalField); - } - - @Test(expectedExceptions = IllegalArgumentException.class) - public void constructorWithNegativeCountAndNoFieldsShouldThrowIAE() { - new RankableObjectWithFields(ANY_OBJECT, -1); - } - - @Test(expectedExceptions = IllegalArgumentException.class) - public void constructorWithNegativeCountAndFieldsShouldThrowIAE() { - Object someAdditionalField = new Object(); - new RankableObjectWithFields(ANY_OBJECT, -1, someAdditionalField); - } - - @Test - public void shouldBeEqualToItself() { - RankableObjectWithFields r = new RankableObjectWithFields(ANY_OBJECT, ANY_COUNT); - assertThat(r).isEqualTo(r); - } - - @DataProvider - public Object[][] otherClassesData() { - return new Object[][]{ { new String("foo") }, { new Object() }, { Integer.valueOf(4) }, { Lists.newArrayList(7, 8, - 9) } }; - } - - @Test(dataProvider = "otherClassesData") - public void shouldNotBeEqualToInstancesOfOtherClasses(Object notARankable) { - RankableObjectWithFields r = new RankableObjectWithFields(ANY_OBJECT, ANY_COUNT); - assertFalse(r.equals(notARankable), r + " is equal to " + notARankable + " but it should not be"); - } - - @DataProvider - public Object[][] falseDuplicatesData() { - return new Object[][]{ { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 1) }, - { new RankableObjectWithFields("foo", 1), new RankableObjectWithFields("Foo", 1) }, - { new RankableObjectWithFields("foo", 1), new RankableObjectWithFields("FOO", 1) }, - { new RankableObjectWithFields("foo", 1), new RankableObjectWithFields("bar", 1) }, - { new RankableObjectWithFields("", 0), new RankableObjectWithFields("", 1) }, { new RankableObjectWithFields("", - 1), new RankableObjectWithFields("bar", 1) } }; - } - - @Test(dataProvider = "falseDuplicatesData") - public void shouldNotBeEqualToFalseDuplicates(RankableObjectWithFields r, RankableObjectWithFields falseDuplicate) { - assertFalse(r.equals(falseDuplicate), r + " is equal to " + falseDuplicate + " but it should not be"); - } - - @Test(dataProvider = "falseDuplicatesData") - public void shouldHaveDifferentHashCodeThanFalseDuplicates(RankableObjectWithFields r, - RankableObjectWithFields falseDuplicate) { - assertThat(r.hashCode()).isNotEqualTo(falseDuplicate.hashCode()); - } - - @DataProvider - public Object[][] trueDuplicatesData() { - return new Object[][]{ { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 0) }, - { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 0, "someOtherField") }, - { new RankableObjectWithFields("foo", 0, "someField"), new RankableObjectWithFields("foo", 0, - "someOtherField") } }; - } - - @Test(dataProvider = "trueDuplicatesData") - public void shouldBeEqualToTrueDuplicates(RankableObjectWithFields r, RankableObjectWithFields trueDuplicate) { - assertTrue(r.equals(trueDuplicate), r + " is not equal to " + trueDuplicate + " but it should be"); - } - - @Test(dataProvider = "trueDuplicatesData") - public void shouldHaveSameHashCodeAsTrueDuplicates(RankableObjectWithFields r, - RankableObjectWithFields trueDuplicate) { - assertThat(r.hashCode()).isEqualTo(trueDuplicate.hashCode()); - } - - @DataProvider - public Object[][] compareToData() { - return new Object[][]{ { new RankableObjectWithFields("foo", 1000), new RankableObjectWithFields("foo", 0), - GREATER_THAN }, { new RankableObjectWithFields("foo", 1), new RankableObjectWithFields("foo", 0), - GREATER_THAN }, { new RankableObjectWithFields("foo", 1000), new RankableObjectWithFields("bar", 0), - GREATER_THAN }, { new RankableObjectWithFields("foo", 1), new RankableObjectWithFields("bar", 0), - GREATER_THAN }, { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 0), EQUAL_TO }, - { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("bar", 0), EQUAL_TO }, - { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 1000), SMALLER_THAN }, - { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 1), SMALLER_THAN }, - { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("bar", 1), SMALLER_THAN }, - { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("bar", 1000), SMALLER_THAN }, }; - } - - @Test(dataProvider = "compareToData") - public void verifyCompareTo(RankableObjectWithFields first, RankableObjectWithFields second, int expCompareToValue) { - assertThat(first.compareTo(second)).isEqualTo(expCompareToValue); - } - - @DataProvider - public Object[][] toStringData() { - return new Object[][]{ { new String("foo"), 0L }, { new String("BAR"), 8L } }; - } - - @Test(dataProvider = "toStringData") - public void toStringShouldContainStringRepresentationsOfObjectAndCount(Object obj, long count) { - // given - RankableObjectWithFields r = new RankableObjectWithFields(obj, count); - - // when - String strRepresentation = r.toString(); - - // then - assertThat(strRepresentation).contains(obj.toString()).contains("" + count); - } - - @Test - public void shouldReturnTheObject() { - // given - RankableObjectWithFields r = new RankableObjectWithFields(ANY_OBJECT, ANY_COUNT, ANY_FIELD); - - // when - Object obj = r.getObject(); - - // then - assertThat(obj).isEqualTo(ANY_OBJECT); - } - - @Test - public void shouldReturnTheCount() { - // given - RankableObjectWithFields r = new RankableObjectWithFields(ANY_OBJECT, ANY_COUNT, ANY_FIELD); - - // when - long count = r.getCount(); - - // then - assertThat(count).isEqualTo(ANY_COUNT); - } - - @DataProvider - public Object[][] fieldsData() { - return new Object[][]{ { ANY_OBJECT, ANY_COUNT, new Object[]{ ANY_FIELD } }, - { "quux", 42L, new Object[]{ "one", "two", "three" } } }; - } - - @Test(dataProvider = "fieldsData") - public void shouldReturnTheFields(Object obj, long count, Object[] fields) { - // given - RankableObjectWithFields r = new RankableObjectWithFields(obj, count, fields); - - // when - List<Object> actualFields = r.getFields(); - - // then - assertThat(actualFields).isEqualTo(Lists.newArrayList(fields)); - } - - @Test(expectedExceptions = UnsupportedOperationException.class) - public void fieldsShouldBeImmutable() { - // given - RankableObjectWithFields r = new RankableObjectWithFields(ANY_OBJECT, ANY_COUNT, ANY_FIELD); - - // when - List<Object> fields = r.getFields(); - // try to modify the list, which should fail - fields.remove(0); - - // then (exception) - } - - @Test - public void shouldCreateRankableObjectFromTuple() { - // given - Tuple tuple = mock(Tuple.class); - List<Object> tupleValues = Lists.newArrayList(ANY_OBJECT, ANY_COUNT, ANY_FIELD); - when(tuple.getValues()).thenReturn(tupleValues); - - // when - RankableObjectWithFields r = RankableObjectWithFields.from(tuple); - - // then - assertThat(r.getObject()).isEqualTo(ANY_OBJECT); - assertThat(r.getCount()).isEqualTo(ANY_COUNT); - List<Object> fields = new ArrayList<Object>(); - fields.add(ANY_FIELD); - assertThat(r.getFields()).isEqualTo(fields); - - } - - @DataProvider - public Object[][] copyData() { - return new Object[][]{ { new RankableObjectWithFields("foo", 0) }, { new RankableObjectWithFields("foo", 3, - "someOtherField") }, { new RankableObjectWithFields("foo", 0, "someField") } }; - } - - // TODO: What would be a good test to ensure that RankableObjectWithFields is at least somewhat defensively copied? - // The contract of Rankable#copy() returns a Rankable value, not a RankableObjectWithFields. - @Test(dataProvider = "copyData") - public void copyShouldReturnCopy(RankableObjectWithFields original) { - // given - - // when - Rankable copy = original.copy(); - - // then - assertThat(copy.getObject()).isEqualTo(original.getObject()); - assertThat(copy.getCount()).isEqualTo(original.getCount()); - } + private static final Object ANY_OBJECT = new Object(); + private static final long ANY_COUNT = 271; + private static final String ANY_FIELD = "someAdditionalField"; + private static final int GREATER_THAN = 1; + private static final int EQUAL_TO = 0; + private static final int SMALLER_THAN = -1; + + @Test(expectedExceptions = IllegalArgumentException.class) + public void constructorWithNullObjectAndNoFieldsShouldThrowIAE() { + new RankableObjectWithFields(null, ANY_COUNT); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void constructorWithNullObjectAndFieldsShouldThrowIAE() { + Object someAdditionalField = new Object(); + new RankableObjectWithFields(null, ANY_COUNT, someAdditionalField); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void constructorWithNegativeCountAndNoFieldsShouldThrowIAE() { + new RankableObjectWithFields(ANY_OBJECT, -1); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void constructorWithNegativeCountAndFieldsShouldThrowIAE() { + Object someAdditionalField = new Object(); + new RankableObjectWithFields(ANY_OBJECT, -1, someAdditionalField); + } + + @Test + public void shouldBeEqualToItself() { + RankableObjectWithFields r = new RankableObjectWithFields(ANY_OBJECT, ANY_COUNT); + assertThat(r).isEqualTo(r); + } + + @DataProvider + public Object[][] otherClassesData() { + return new Object[][]{ + { new String("foo") }, { new Object() }, { Integer.valueOf(4) }, { + Lists.newArrayList(7, 8, + 9) + } + }; + } + + @Test(dataProvider = "otherClassesData") + public void shouldNotBeEqualToInstancesOfOtherClasses(Object notARankable) { + RankableObjectWithFields r = new RankableObjectWithFields(ANY_OBJECT, ANY_COUNT); + assertFalse(r.equals(notARankable), r + " is equal to " + notARankable + " but it should not be"); + } + + @DataProvider + public Object[][] falseDuplicatesData() { + return new Object[][]{ + { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 1) }, + { new RankableObjectWithFields("foo", 1), new RankableObjectWithFields("Foo", 1) }, + { new RankableObjectWithFields("foo", 1), new RankableObjectWithFields("FOO", 1) }, + { new RankableObjectWithFields("foo", 1), new RankableObjectWithFields("bar", 1) }, + { new RankableObjectWithFields("", 0), new RankableObjectWithFields("", 1) }, { + new RankableObjectWithFields("", + 1), new RankableObjectWithFields("bar", 1) + } + }; + } + + @Test(dataProvider = "falseDuplicatesData") + public void shouldNotBeEqualToFalseDuplicates(RankableObjectWithFields r, RankableObjectWithFields falseDuplicate) { + assertFalse(r.equals(falseDuplicate), r + " is equal to " + falseDuplicate + " but it should not be"); + } + + @Test(dataProvider = "falseDuplicatesData") + public void shouldHaveDifferentHashCodeThanFalseDuplicates(RankableObjectWithFields r, + RankableObjectWithFields falseDuplicate) { + assertThat(r.hashCode()).isNotEqualTo(falseDuplicate.hashCode()); + } + + @DataProvider + public Object[][] trueDuplicatesData() { + return new Object[][]{ + { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 0) }, + { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 0, "someOtherField") }, + { + new RankableObjectWithFields("foo", 0, "someField"), new RankableObjectWithFields("foo", 0, + "someOtherField") + } + }; + } + + @Test(dataProvider = "trueDuplicatesData") + public void shouldBeEqualToTrueDuplicates(RankableObjectWithFields r, RankableObjectWithFields trueDuplicate) { + assertTrue(r.equals(trueDuplicate), r + " is not equal to " + trueDuplicate + " but it should be"); + } + + @Test(dataProvider = "trueDuplicatesData") + public void shouldHaveSameHashCodeAsTrueDuplicates(RankableObjectWithFields r, + RankableObjectWithFields trueDuplicate) { + assertThat(r.hashCode()).isEqualTo(trueDuplicate.hashCode()); + } + + @DataProvider + public Object[][] compareToData() { + return new Object[][]{ + { + new RankableObjectWithFields("foo", 1000), new RankableObjectWithFields("foo", 0), + GREATER_THAN + }, { + new RankableObjectWithFields("foo", 1), new RankableObjectWithFields("foo", 0), + GREATER_THAN + }, { + new RankableObjectWithFields("foo", 1000), new RankableObjectWithFields("bar", 0), + GREATER_THAN + }, { + new RankableObjectWithFields("foo", 1), new RankableObjectWithFields("bar", 0), + GREATER_THAN + }, { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 0), EQUAL_TO }, + { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("bar", 0), EQUAL_TO }, + { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 1000), SMALLER_THAN }, + { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("foo", 1), SMALLER_THAN }, + { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("bar", 1), SMALLER_THAN }, + { new RankableObjectWithFields("foo", 0), new RankableObjectWithFields("bar", 1000), SMALLER_THAN }, + }; + } + + @Test(dataProvider = "compareToData") + public void verifyCompareTo(RankableObjectWithFields first, RankableObjectWithFields second, int expCompareToValue) { + assertThat(first.compareTo(second)).isEqualTo(expCompareToValue); + } + + @DataProvider + public Object[][] toStringData() { + return new Object[][]{ { new String("foo"), 0L }, { new String("BAR"), 8L } }; + } + + @Test(dataProvider = "toStringData") + public void toStringShouldContainStringRepresentationsOfObjectAndCount(Object obj, long count) { + // given + RankableObjectWithFields r = new RankableObjectWithFields(obj, count); + + // when + String strRepresentation = r.toString(); + + // then + assertThat(strRepresentation).contains(obj.toString()).contains("" + count); + } + + @Test + public void shouldReturnTheObject() { + // given + RankableObjectWithFields r = new RankableObjectWithFields(ANY_OBJECT, ANY_COUNT, ANY_FIELD); + + // when + Object obj = r.getObject(); + + // then + assertThat(obj).isEqualTo(ANY_OBJECT); + } + + @Test + public void shouldReturnTheCount() { + // given + RankableObjectWithFields r = new RankableObjectWithFields(ANY_OBJECT, ANY_COUNT, ANY_FIELD); + + // when + long count = r.getCount(); + + // then + assertThat(count).isEqualTo(ANY_COUNT); + } + + @DataProvider + public Object[][] fieldsData() { + return new Object[][]{ + { ANY_OBJECT, ANY_COUNT, new Object[]{ ANY_FIELD } }, + { "quux", 42L, new Object[]{ "one", "two", "three" } } + }; + } + + @Test(dataProvider = "fieldsData") + public void shouldReturnTheFields(Object obj, long count, Object[] fields) { + // given + RankableObjectWithFields r = new RankableObjectWithFields(obj, count, fields); + + // when + List<Object> actualFields = r.getFields(); + + // then + assertThat(actualFields).isEqualTo(Lists.newArrayList(fields)); + } + + @Test(expectedExceptions = UnsupportedOperationException.class) + public void fieldsShouldBeImmutable() { + // given + RankableObjectWithFields r = new RankableObjectWithFields(ANY_OBJECT, ANY_COUNT, ANY_FIELD); + + // when + List<Object> fields = r.getFields(); + // try to modify the list, which should fail + fields.remove(0); + + // then (exception) + } + + @Test + public void shouldCreateRankableObjectFromTuple() { + // given + Tuple tuple = mock(Tuple.class); + List<Object> tupleValues = Lists.newArrayList(ANY_OBJECT, ANY_COUNT, ANY_FIELD); + when(tuple.getValues()).thenReturn(tupleValues); + + // when + RankableObjectWithFields r = RankableObjectWithFields.from(tuple); + + // then + assertThat(r.getObject()).isEqualTo(ANY_OBJECT); + assertThat(r.getCount()).isEqualTo(ANY_COUNT); + List<Object> fields = new ArrayList<Object>(); + fields.add(ANY_FIELD); + assertThat(r.getFields()).isEqualTo(fields); + + } + + @DataProvider + public Object[][] copyData() { + return new Object[][]{ + { new RankableObjectWithFields("foo", 0) }, { + new RankableObjectWithFields("foo", 3, + "someOtherField") + }, { new RankableObjectWithFields("foo", 0, "someField") } + }; + } + + // TODO: What would be a good test to ensure that RankableObjectWithFields is at least somewhat defensively copied? + // The contract of Rankable#copy() returns a Rankable value, not a RankableObjectWithFields. + @Test(dataProvider = "copyData") + public void copyShouldReturnCopy(RankableObjectWithFields original) { + // given + + // when + Rankable copy = original.copy(); + + // then + assertThat(copy.getObject()).isEqualTo(original.getObject()); + assertThat(copy.getCount()).isEqualTo(original.getCount()); + } }
