From 93bde1f047d28853d45466933527ae0bd0e8a62b Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 12 Feb 2015 11:36:28 +0100 Subject: [PATCH] Replace none static call to EsTester and DbTester by static call (when possible) --- .../issue/ScmAccountCacheLoaderTest.java | 12 +++++-- .../step/PurgeRemovedViewsStepTest.java | 13 ++++--- .../org/sonar/server/es/BulkIndexerTest.java | 8 +++-- .../index/IssueAuthorizationIndexerTest.java | 17 ++++++--- ...dexMediumTest.java => IssueIndexTest.java} | 35 +++++++++++-------- .../server/issue/index/IssueIndexerTest.java | 17 ++++++--- .../sonar/server/search/FacetsMediumTest.java | 12 +++++-- .../source/index/SourceLineIndexTest.java | 7 ++-- .../source/index/SourceLineIndexerTest.java | 12 ++++--- .../server/user/index/UserIndexTest.java | 7 ++-- .../server/user/index/UserIndexerTest.java | 17 ++++++--- .../server/view/index/ViewIndexTest.java | 7 ++-- .../server/view/index/ViewIndexerTest.java | 12 ++++--- 13 files changed, 114 insertions(+), 62 deletions(-) rename server/sonar-server/src/test/java/org/sonar/server/issue/index/{IssueIndexMediumTest.java => IssueIndexTest.java} (98%) diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/issue/ScmAccountCacheLoaderTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/issue/ScmAccountCacheLoaderTest.java index e1f448d8c28..fd070959266 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/issue/ScmAccountCacheLoaderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/issue/ScmAccountCacheLoaderTest.java @@ -19,7 +19,8 @@ */ package org.sonar.server.computation.issue; -import org.junit.Rule; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.slf4j.Logger; import org.sonar.api.config.Settings; @@ -36,8 +37,13 @@ import static org.mockito.Mockito.verify; public class ScmAccountCacheLoaderTest { - @Rule - public EsTester esTester = new EsTester().addDefinitions(new UserIndexDefinition(new Settings())); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new UserIndexDefinition(new Settings())); + + @Before + public void setUp() throws Exception { + esTester.truncateIndices(); + } @Test public void load_login_for_scm_account() throws Exception { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeRemovedViewsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeRemovedViewsStepTest.java index a11c2c80ddf..0fd843bf5ca 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeRemovedViewsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeRemovedViewsStepTest.java @@ -22,7 +22,7 @@ package org.sonar.server.computation.step; import org.junit.After; import org.junit.Before; -import org.junit.Rule; +import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.config.Settings; import org.sonar.api.resources.Qualifiers; @@ -47,11 +47,11 @@ import static org.mockito.Mockito.when; public class PurgeRemovedViewsStepTest { - @Rule - public EsTester esTester = new EsTester().addDefinitions(new ViewIndexDefinition(new Settings())); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new ViewIndexDefinition(new Settings())); - @Rule - public DbTester db = new DbTester(); + @ClassRule + public static DbTester db = new DbTester(); ComputationContext context; @@ -63,6 +63,9 @@ public class PurgeRemovedViewsStepTest { @Before public void setUp() { + esTester.truncateIndices(); + db.truncateTables(); + context = mock(ComputationContext.class); session = db.myBatis().openSession(false); dbClient = new DbClient(db.database(), db.myBatis(), new IssueDao(db.myBatis()), new ComponentDao()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/BulkIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/BulkIndexerTest.java index 5bf17aa0922..bd4be80943d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/BulkIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/BulkIndexerTest.java @@ -25,18 +25,20 @@ import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.junit.Rule; +import org.junit.ClassRule; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; public class BulkIndexerTest { - @Rule - public EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition().setReplicas(1)); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FakeIndexDefinition().setReplicas(1)); @Test public void index_nothing() throws Exception { + esTester.truncateIndices(); + BulkIndexer indexer = new BulkIndexer(esTester.client(), FakeIndexDefinition.INDEX); indexer.start(); indexer.stop(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueAuthorizationIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueAuthorizationIndexerTest.java index 52f5c381b14..f77ba3148b1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueAuthorizationIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueAuthorizationIndexerTest.java @@ -20,7 +20,8 @@ package org.sonar.server.issue.index; import org.elasticsearch.search.SearchHit; -import org.junit.Rule; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.config.Settings; @@ -38,11 +39,17 @@ import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class IssueAuthorizationIndexerTest { - @Rule - public DbTester dbTester = new DbTester(); + @ClassRule + public static DbTester dbTester = new DbTester(); - @Rule - public EsTester esTester = new EsTester().addDefinitions(new IssueIndexDefinition(new Settings())); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new IssueIndexDefinition(new Settings())); + + @Before + public void setUp() throws Exception { + dbTester.truncateTables(); + esTester.truncateIndices(); + } @Test public void index_nothing() throws Exception { diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexTest.java similarity index 98% rename from server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java rename to server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexTest.java index e8bee4adb3a..60a533f7df2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexTest.java @@ -25,22 +25,25 @@ import org.assertj.core.api.Fail; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; +import org.sonar.api.config.Settings; import org.sonar.api.issue.Issue; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.Severity; import org.sonar.api.security.DefaultGroups; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.KeyValueFormat; +import org.sonar.api.utils.System2; import org.sonar.core.component.ComponentDto; import org.sonar.server.component.ComponentTesting; +import org.sonar.server.es.EsTester; import org.sonar.server.es.SearchOptions; import org.sonar.server.es.SearchResult; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.issue.IssueQuery; import org.sonar.server.issue.IssueTesting; -import org.sonar.server.tester.ServerTester; import org.sonar.server.user.MockUserSession; import org.sonar.server.view.index.ViewDoc; +import org.sonar.server.view.index.ViewIndexDefinition; import org.sonar.server.view.index.ViewIndexer; import javax.annotation.Nullable; @@ -51,20 +54,24 @@ import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; -/** - * As soon as IssueIndex take {@link org.sonar.server.es.EsClient} in its constructor, ServerTester should be replaced by EsTester, it will make this test going faster ! - */ -public class IssueIndexMediumTest { +public class IssueIndexTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static EsTester tester = new EsTester().addDefinitions(new IssueIndexDefinition(new Settings()), new ViewIndexDefinition(new Settings())); IssueIndex index; + IssueIndexer issueIndexer; + IssueAuthorizationIndexer issueAuthorizationIndexer; + ViewIndexer viewIndexer; + @Before public void setUp() throws Exception { - tester.clearIndexes(); - index = tester.get(IssueIndex.class); + tester.truncateIndices(); + issueIndexer = new IssueIndexer(null, tester.client()); + issueAuthorizationIndexer = new IssueAuthorizationIndexer(null, tester.client()); + viewIndexer = new ViewIndexer(null, tester.client()); + index = new IssueIndex(tester.client(), System2.INSTANCE); } @Test @@ -245,8 +252,8 @@ public class IssueIndexMediumTest { assertThat(index.search(IssueQuery.builder().viewUuids(newArrayList(view)).build(), new SearchOptions()).getDocs()).hasSize(6); assertThat(index.search(IssueQuery.builder().moduleUuids(newArrayList(module.uuid())).build(), new SearchOptions()).getDocs()).hasSize(2); assertThat(index.search(IssueQuery.builder().moduleUuids(newArrayList(subModule.uuid())).build(), new SearchOptions()).getDocs()).hasSize(1); // XXX - // Misleading - // ! + // Misleading + // ! assertThat(index.search(IssueQuery.builder().fileUuids(newArrayList(file1.uuid())).build(), new SearchOptions()).getDocs()).hasSize(1); assertThat(index.search(IssueQuery.builder().fileUuids(newArrayList(file1.uuid(), file2.uuid(), file3.uuid())).build(), new SearchOptions()).getDocs()).hasSize(3); } @@ -1155,22 +1162,22 @@ public class IssueIndexMediumTest { } private void indexIssues(IssueDoc... issues) { - tester.get(IssueIndexer.class).index(Arrays.asList(issues).iterator()); + issueIndexer.index(Arrays.asList(issues).iterator()); for (IssueDoc issue : issues) { addIssueAuthorization(issue.projectUuid(), DefaultGroups.ANYONE, null); } } private void indexIssue(IssueDoc issue, @Nullable String group, @Nullable String user) { - tester.get(IssueIndexer.class).index(Iterators.singletonIterator(issue)); + issueIndexer.index(Iterators.singletonIterator(issue)); addIssueAuthorization(issue.projectUuid(), group, user); } private void addIssueAuthorization(String projectUuid, @Nullable String group, @Nullable String user) { - tester.get(IssueAuthorizationIndexer.class).index(newArrayList(new IssueAuthorizationDao.Dto(projectUuid, 1).addGroup(group).addUser(user))); + issueAuthorizationIndexer.index(newArrayList(new IssueAuthorizationDao.Dto(projectUuid, 1).addGroup(group).addUser(user))); } private void indexView(String viewUuid, List projects) { - tester.get(ViewIndexer.class).index(new ViewDoc().setUuid(viewUuid).setProjects(projects)); + viewIndexer.index(new ViewDoc().setUuid(viewUuid).setProjects(projects)); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexerTest.java index 094e35db908..cc87af2c9a8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexerTest.java @@ -20,7 +20,8 @@ package org.sonar.server.issue.index; import com.google.common.collect.Iterators; -import org.junit.Rule; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.config.Settings; @@ -37,11 +38,17 @@ import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class IssueIndexerTest { - @Rule - public DbTester dbTester = new DbTester(); + @ClassRule + public static DbTester dbTester = new DbTester(); - @Rule - public EsTester esTester = new EsTester().addDefinitions(new IssueIndexDefinition(new Settings())); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new IssueIndexDefinition(new Settings())); + + @Before + public void setUp() throws Exception { + dbTester.truncateTables(); + esTester.truncateIndices(); + } @Test public void index_nothing() throws Exception { diff --git a/server/sonar-server/src/test/java/org/sonar/server/search/FacetsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/search/FacetsMediumTest.java index 5d999246f4d..ee969cc9866 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/search/FacetsMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/search/FacetsMediumTest.java @@ -24,7 +24,8 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogram.Interval; -import org.junit.Rule; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.utils.DateUtils; import org.sonar.server.es.EsTester; @@ -45,8 +46,13 @@ public class FacetsMediumTest { private static final String FIELD_TAGS = "tags"; private static final String FIELD_CREATED_AT = "createdAt"; - @Rule - public EsTester esTester = new EsTester().addDefinitions(new FacetsTestDefinition()); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new FacetsTestDefinition()); + + @Before + public void setUp() throws Exception { + esTester.truncateIndices(); + } @Test public void should_ignore_result_without_aggregations() throws Exception { diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexTest.java index 74dd63e4f74..2b120bc0212 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexTest.java @@ -20,7 +20,7 @@ package org.sonar.server.source.index; import org.junit.Before; -import org.junit.Rule; +import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.config.Settings; import org.sonar.server.es.EsTester; @@ -30,13 +30,14 @@ import static org.assertj.core.api.Assertions.assertThat; public class SourceLineIndexTest { - @Rule - public EsTester es = new EsTester().addDefinitions(new SourceLineIndexDefinition(new Settings())); + @ClassRule + public static EsTester es = new EsTester().addDefinitions(new SourceLineIndexDefinition(new Settings())); private SourceLineIndex index; @Before public void setUp() { + es.truncateIndices(); index = new SourceLineIndex(es.client()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexerTest.java index 35ae780b0ba..5ba75c5f6ec 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/index/SourceLineIndexerTest.java @@ -29,7 +29,7 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.junit.Before; -import org.junit.Rule; +import org.junit.ClassRule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.config.Settings; @@ -55,16 +55,18 @@ import static org.sonar.server.source.index.SourceLineIndexDefinition.*; @Category(DbTests.class) public class SourceLineIndexerTest { - @Rule - public EsTester es = new EsTester().addDefinitions(new SourceLineIndexDefinition(new Settings())); + @ClassRule + public static EsTester es = new EsTester().addDefinitions(new SourceLineIndexDefinition(new Settings())); - @Rule - public DbTester db = new DbTester(); + @ClassRule + public static DbTester db = new DbTester(); private SourceLineIndexer indexer; @Before public void setUp() { + es.truncateIndices(); + db.truncateTables(); indexer = new SourceLineIndexer(new DbClient(db.database(), db.myBatis()), es.client()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexTest.java index e6c5da1d160..5341af1911a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexTest.java @@ -21,7 +21,7 @@ package org.sonar.server.user.index; import org.junit.Before; -import org.junit.Rule; +import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.config.Settings; import org.sonar.server.es.EsTester; @@ -32,13 +32,14 @@ import static org.junit.Assert.fail; public class UserIndexTest { - @Rule - public EsTester esTester = new EsTester().addDefinitions(new UserIndexDefinition(new Settings())); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new UserIndexDefinition(new Settings())); private UserIndex index; @Before public void setUp() { + esTester.truncateIndices(); index = new UserIndex(esTester.client()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexerTest.java index 405fa782333..fdbcefb81ac 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexerTest.java @@ -19,7 +19,8 @@ */ package org.sonar.server.user.index; -import org.junit.Rule; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.config.Settings; @@ -35,11 +36,17 @@ import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class UserIndexerTest { - @Rule - public DbTester dbTester = new DbTester(); + @ClassRule + public static DbTester dbTester = new DbTester(); - @Rule - public EsTester esTester = new EsTester().addDefinitions(new UserIndexDefinition(new Settings())); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new UserIndexDefinition(new Settings())); + + @Before + public void setUp() throws Exception { + dbTester.truncateTables(); + esTester.truncateIndices(); + } @Test public void index_nothing() throws Exception { diff --git a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexTest.java index 44f72a67151..f92bdc0da7b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexTest.java @@ -21,7 +21,7 @@ package org.sonar.server.view.index; import org.junit.Before; -import org.junit.Rule; +import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.config.Settings; import org.sonar.server.es.EsTester; @@ -33,13 +33,14 @@ import static org.assertj.core.api.Assertions.assertThat; public class ViewIndexTest { - @Rule - public EsTester esTester = new EsTester().addDefinitions(new ViewIndexDefinition(new Settings())); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new ViewIndexDefinition(new Settings())); private ViewIndex index; @Before public void setUp() { + esTester.truncateIndices(); index = new ViewIndex(esTester.client()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java index 6e3c26f7d46..b809a46922e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java @@ -23,7 +23,7 @@ package org.sonar.server.view.index; import com.google.common.base.Function; import com.google.common.collect.Maps; import org.junit.Before; -import org.junit.Rule; +import org.junit.ClassRule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.config.Settings; @@ -42,16 +42,18 @@ import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) public class ViewIndexerTest { - @Rule - public DbTester dbTester = new DbTester(); + @ClassRule + public static DbTester dbTester = new DbTester(); - @Rule - public EsTester esTester = new EsTester().addDefinitions(new ViewIndexDefinition(new Settings())); + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new ViewIndexDefinition(new Settings())); ViewIndexer indexer; @Before public void setUp() throws Exception { + dbTester.truncateTables(); + esTester.truncateIndices(); indexer = new ViewIndexer(new DbClient(dbTester.database(), dbTester.myBatis(), new ComponentDao()), esTester.client()); } -- 2.39.5