From fdada947435070ed7f7745a2aff70cfaa67f8d3c Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Thu, 21 Aug 2014 16:25:19 +0200 Subject: [PATCH] Added synchronizerTest --- .../java/org/sonar/server/db/BaseDao.java | 3 +- .../server/search/IndexSynchronizer.java | 2 +- .../server/search/IndexSynchronizerTest.java | 81 +++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/search/IndexSynchronizerTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java b/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java index 170d446df1b..cc5ff34ea8e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java @@ -219,6 +219,7 @@ public abstract class BaseDao, K extends Serializable> imple session.enqueue(new UpsertDto(getIndexType(), item)); } } catch (Exception e) { + e.printStackTrace(); throw new IllegalStateException("Fail to insert item in db: " + item, e.getCause()); } } @@ -280,7 +281,7 @@ public abstract class BaseDao, K extends Serializable> imple @Override public final void synchronizeAfter(final DbSession session, Date date) { for (E dto : this.findAfterDate(session, date)) { - session.enqueue(new UpsertDto(getIndexType(), dto, false)); + session.enqueue(new UpsertDto(getIndexType(), dto, true)); } session.commit(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java b/server/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java index 20ed14a9ae4..d4249a020dd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java @@ -56,7 +56,7 @@ public class IndexSynchronizer { session.close(); } - private void synchronize(DbSession session, Dao dao, Index index) { + void synchronize(DbSession session, Dao dao, Index index) { dao.synchronizeAfter(session, index.getLastSynchronization()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/search/IndexSynchronizerTest.java b/server/sonar-server/src/test/java/org/sonar/server/search/IndexSynchronizerTest.java new file mode 100644 index 00000000000..911d53a9ed8 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/search/IndexSynchronizerTest.java @@ -0,0 +1,81 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.search; + +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.sonar.api.rule.RuleKey; +import org.sonar.core.persistence.DbSession; +import org.sonar.server.db.DbClient; +import org.sonar.server.platform.Platform; +import org.sonar.server.rule.RuleTesting; +import org.sonar.server.rule.index.RuleIndex; +import org.sonar.server.tester.ServerTester; + +import static org.fest.assertions.Assertions.assertThat; + +public class IndexSynchronizerTest { + + @ClassRule + public static ServerTester tester = new ServerTester(); + + IndexSynchronizer synchronizer; + DbClient dbClient; + IndexClient indexClient; + Platform platform; + DbSession dbSession; + + @Before + public void setUp() throws Exception { + dbClient = tester.get(DbClient.class); + indexClient = tester.get(IndexClient.class); + platform = tester.get(Platform.class); + dbSession = dbClient.openSession(false); + synchronizer = new IndexSynchronizer(dbClient, indexClient); + tester.clearDbAndIndexes(); + } + + @After + public void tearDown() throws Exception { + if (dbSession != null) { + dbSession.close(); + } + } + + @Test + public void can_synchronize() throws Exception { + + int numberOfRules = 100; + + for (int i = 0; i < numberOfRules; i++) { + dbClient.ruleDao().insert(dbSession, RuleTesting.newDto(RuleKey.of("test", "x" + i))); + } + dbSession.commit(); + + assertThat(indexClient.get(RuleIndex.class).countAll()).isEqualTo(numberOfRules); + tester.clearIndexes(); + assertThat(indexClient.get(RuleIndex.class).countAll()).isEqualTo(0); + + synchronizer.synchronize(dbSession, dbClient.ruleDao(), indexClient.get(RuleIndex.class)); + assertThat(indexClient.get(RuleIndex.class).countAll()).isEqualTo(numberOfRules); + } +} \ No newline at end of file -- 2.39.5