summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorStephane Gamard <stephane.gamard@searchbox.com>2014-08-21 16:25:19 +0200
committerStephane Gamard <stephane.gamard@searchbox.com>2014-08-21 16:25:19 +0200
commitfdada947435070ed7f7745a2aff70cfaa67f8d3c (patch)
treee7b6e28c71ffbc11a954d8bbffbfc445cd594362 /server
parent4c33c455212a470e82c8694a66ecc39ab009d49d (diff)
downloadsonarqube-fdada947435070ed7f7745a2aff70cfaa67f8d3c.tar.gz
sonarqube-fdada947435070ed7f7745a2aff70cfaa67f8d3c.zip
Added synchronizerTest
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java3
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/search/IndexSynchronizerTest.java81
3 files changed, 84 insertions, 2 deletions
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<M, E extends Dto<K>, K extends Serializable> imple
session.enqueue(new UpsertDto<E>(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<M, E extends Dto<K>, 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<E>(getIndexType(), dto, false));
+ session.enqueue(new UpsertDto<E>(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