From fa0b5ce4d701431df47885059c3fb9c4922aa33d Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 12 Feb 2015 10:26:46 +0100 Subject: [PATCH] Add unit tests --- .../sonar/core/persistence/DaoUtilsTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DaoUtilsTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/DaoUtilsTest.java index db891b609ec..03e1d41d380 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DaoUtilsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DaoUtilsTest.java @@ -19,11 +19,16 @@ */ package org.sonar.core.persistence; +import com.google.common.base.Function; +import com.google.common.collect.Iterables; import org.junit.Test; +import java.util.Collections; import java.util.List; +import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; public class DaoUtilsTest { @@ -38,6 +43,44 @@ public class DaoUtilsTest { public void repeatCondition() throws Exception { assertThat(DaoUtils.repeatCondition("uuid=?", 1, "or")).isEqualTo("uuid=?"); assertThat(DaoUtils.repeatCondition("uuid=?", 3, "or")).isEqualTo("uuid=? or uuid=? or uuid=?"); + } + + @Test + public void execute_large_inputs() throws Exception { + List inputs = newArrayList(); + List expectedOutputs = newArrayList(); + for (int i = 0; i < 2010; i++) { + inputs.add(i); + expectedOutputs.add(Integer.toString(i)); + } + + List outputs = DaoUtils.executeLargeInputs(inputs, new Function, List>() { + @Override + public List apply(List input) { + // Check that each partition is only done on 1000 elements max + assertThat(input.size()).isLessThanOrEqualTo(1000); + return newArrayList(Iterables.transform(input, new Function() { + @Override + public String apply(Integer input) { + return Integer.toString(input); + } + })); + } + }); + + assertThat(outputs).isEqualTo(expectedOutputs); + } + + @Test + public void execute_large_inputs_on_empty_list() throws Exception { + List outputs = DaoUtils.executeLargeInputs(Collections.emptyList(), new Function, List>() { + @Override + public List apply(List input) { + fail("No partition should be made on empty list"); + return Collections.emptyList(); + } + }); + assertThat(outputs).isEmpty(); } } -- 2.39.5