diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-10-05 21:54:22 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-10-05 21:54:22 +0200 |
commit | b96df0defa1e12f9f679e4c6e8b451bde1a7a566 (patch) | |
tree | 8fcb6c8413b957f3fc0d3ac316842662d6d8aaca /sonar-batch | |
parent | 40ef7ce47f9400d88d351db0bf7e6c8dd7abba68 (diff) | |
download | sonarqube-b96df0defa1e12f9f679e4c6e8b451bde1a7a566.tar.gz sonarqube-b96df0defa1e12f9f679e4c6e8b451bde1a7a566.zip |
SONAR-5381 SONAR-3350 replace mysql MEDIUMTEXT columns by LONGTEXT
Diffstat (limited to 'sonar-batch')
5 files changed, 55 insertions, 60 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java index 6aced328ad6..161a5396144 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java @@ -20,23 +20,25 @@ package org.sonar.batch.index; import com.google.common.collect.Sets; -import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; -import org.sonar.api.database.model.SnapshotSource; import org.sonar.api.resources.DuplicatedSourceException; import org.sonar.api.resources.Resource; +import org.sonar.core.source.db.SnapshotSourceDao; +import org.sonar.core.source.db.SnapshotSourceDto; + +import javax.annotation.CheckForNull; import java.util.Set; public final class SourcePersister { - private DatabaseSession session; private Set<Integer> savedSnapshotIds = Sets.newHashSet(); private ResourcePersister resourcePersister; + private final SnapshotSourceDao sourceDao; - public SourcePersister(DatabaseSession session, ResourcePersister resourcePersister) { - this.session = session; + public SourcePersister(ResourcePersister resourcePersister, SnapshotSourceDao sourceDao) { this.resourcePersister = resourcePersister; + this.sourceDao = sourceDao; } public void saveSource(Resource resource, String source) { @@ -44,18 +46,20 @@ public final class SourcePersister { if (isCached(snapshot)) { throw new DuplicatedSourceException(resource); } - session.save(new SnapshotSource(snapshot.getId(), source)); - session.commit(); + SnapshotSourceDto dto = new SnapshotSourceDto(); + dto.setSnapshotId(snapshot.getId().longValue()); + dto.setData(source); + sourceDao.insert(dto); addToCache(snapshot); } + @CheckForNull public String getSource(Resource resource) { - SnapshotSource source = null; Snapshot snapshot = resourcePersister.getSnapshot(resource); - if (snapshot!=null && snapshot.getId()!=null) { - source = session.getSingleResult(SnapshotSource.class, "snapshotId", snapshot.getId()); + if (snapshot != null && snapshot.getId() != null) { + return sourceDao.selectSnapshotSource(snapshot.getId()); } - return source!=null ? source.getData() : null; + return null; } private boolean isCached(Snapshot snapshot) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java b/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java index b36d4dc6852..be876cbc0c9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java @@ -19,33 +19,32 @@ */ package org.sonar.batch.scan; +import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchComponent; -import org.sonar.api.database.DatabaseSession; -import org.sonar.api.database.model.ResourceModel; -import org.sonar.api.database.model.Snapshot; -import org.sonar.api.database.model.SnapshotSource; import org.sonar.api.resources.Resource; import org.sonar.api.resources.ResourceUtils; import org.sonar.api.utils.HttpDownloader; import org.sonar.batch.bootstrap.AnalysisMode; import org.sonar.batch.bootstrap.ServerClient; +import org.sonar.core.source.db.SnapshotSourceDao; -import javax.persistence.Query; +import javax.annotation.CheckForNull; public class LastSnapshots implements BatchComponent { private final AnalysisMode analysisMode; - private final DatabaseSession session; private final ServerClient server; + private final SnapshotSourceDao sourceDao; - public LastSnapshots(AnalysisMode analysisMode, DatabaseSession session, ServerClient server) { + public LastSnapshots(AnalysisMode analysisMode, SnapshotSourceDao dao, ServerClient server) { this.analysisMode = analysisMode; - this.session = session; + this.sourceDao = dao; this.server = server; } + @CheckForNull public String getSource(Resource resource) { - String source = ""; + String source = null; if (ResourceUtils.isFile(resource)) { if (analysisMode.isPreview()) { source = loadSourceFromWs(resource); @@ -53,9 +52,10 @@ public class LastSnapshots implements BatchComponent { source = loadSourceFromDb(resource); } } - return source; + return StringUtils.defaultString(source, ""); } + @CheckForNull private String loadSourceFromWs(Resource resource) { try { return server.request("/api/sources?resource=" + resource.getEffectiveKey() + "&format=txt", false, analysisMode.getPreviewReadTimeoutSec() * 1000); @@ -67,22 +67,8 @@ public class LastSnapshots implements BatchComponent { } } + @CheckForNull private String loadSourceFromDb(Resource resource) { - Snapshot snapshot = getSnapshot(resource); - if (snapshot != null) { - SnapshotSource source = session.getSingleResult(SnapshotSource.class, "snapshotId", snapshot.getId()); - if (source != null) { - return source.getData(); - } - } - return ""; - } - - private Snapshot getSnapshot(Resource resource) { - Query query = session.createQuery("from " + Snapshot.class.getSimpleName() + " s where s.last=:last and s.resourceId=(select r.id from " - + ResourceModel.class.getSimpleName() + " r where r.key=:key)"); - query.setParameter("key", resource.getEffectiveKey()); - query.setParameter("last", Boolean.TRUE); - return session.getSingleResult(query, null); + return sourceDao.selectSnapshotSourceByComponentKey(resource.getEffectiveKey()); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java index f7f289ddf64..1e185487a05 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java @@ -76,6 +76,7 @@ public class ComponentIndexer implements BatchComponent { sonarFile.setDeprecatedKey(pathFromSourceDir); } sonarIndex.index(sonarFile); + importSources(fs, shouldImportSource, inputFile, sonarFile); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java index 39f4095ccd2..451fa91f691 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java @@ -25,23 +25,25 @@ import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.DuplicatedSourceException; import org.sonar.api.resources.File; import org.sonar.api.resources.Resource; -import org.sonar.jpa.test.AbstractDbUnitTestCase; +import org.sonar.core.persistence.AbstractDaoTestCase; +import org.sonar.core.source.db.SnapshotSourceDao; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class SourcePersisterTest extends AbstractDbUnitTestCase { +public class SourcePersisterTest extends AbstractDaoTestCase { private SourcePersister sourcePersister; @Before public void before() { setupData("shared"); - Snapshot snapshot = getSession().getSingleResult(Snapshot.class, "id", 1000); ResourcePersister resourcePersister = mock(ResourcePersister.class); + Snapshot snapshot = new Snapshot(); + snapshot.setId(1000); when(resourcePersister.getSnapshotOrFail(any(Resource.class))).thenReturn(snapshot); - sourcePersister = new SourcePersister(getSession(), resourcePersister); + sourcePersister = new SourcePersister(resourcePersister, new SnapshotSourceDao(getMyBatis())); } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java index d6cddb4447d..505b644d98e 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java @@ -28,7 +28,8 @@ import org.sonar.api.resources.Project; import org.sonar.api.utils.HttpDownloader; import org.sonar.batch.bootstrap.AnalysisMode; import org.sonar.batch.bootstrap.ServerClient; -import org.sonar.jpa.test.AbstractDbUnitTestCase; +import org.sonar.core.persistence.TestDatabase; +import org.sonar.core.source.db.SnapshotSourceDao; import java.net.URI; import java.net.URISyntaxException; @@ -36,15 +37,16 @@ import java.net.URISyntaxException; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; -public class LastSnapshotsTest extends AbstractDbUnitTestCase { +public class LastSnapshotsTest { @Rule public ExpectedException thrown = ExpectedException.none(); + + @Rule + public TestDatabase db = new TestDatabase(); + private AnalysisMode mode; @Before @@ -55,10 +57,10 @@ public class LastSnapshotsTest extends AbstractDbUnitTestCase { @Test public void should_get_source_of_last_snapshot() { - setupData("last_snapshot"); - ServerClient server = mock(ServerClient.class); + db.prepareDbUnit(getClass(), "last_snapshot.xml"); - LastSnapshots lastSnapshots = new LastSnapshots(mode, getSession(), server); + ServerClient server = mock(ServerClient.class); + LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); assertThat(lastSnapshots.getSource(newFile())).isEqualTo("this is bar"); verifyZeroInteractions(server); @@ -66,10 +68,10 @@ public class LastSnapshotsTest extends AbstractDbUnitTestCase { @Test public void should_return_empty_source_if_no_last_snapshot() { - setupData("no_last_snapshot"); + db.prepareDbUnit(getClass(), "no_last_snapshot.xml"); ServerClient server = mock(ServerClient.class); - LastSnapshots lastSnapshots = new LastSnapshots(mode, getSession(), server); + LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); assertThat(lastSnapshots.getSource(newFile())).isEqualTo(""); verifyZeroInteractions(server); @@ -77,12 +79,12 @@ public class LastSnapshotsTest extends AbstractDbUnitTestCase { @Test public void should_download_source_from_ws_if_preview_mode() { - setupData("last_snapshot"); + db.prepareDbUnit(getClass(), "last_snapshot.xml"); ServerClient server = mock(ServerClient.class); when(server.request(anyString(), eq(false), eq(30 * 1000))).thenReturn("downloaded source of Bar.c"); when(mode.isPreview()).thenReturn(true); - LastSnapshots lastSnapshots = new LastSnapshots(mode, getSession(), server); + LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); String source = lastSnapshots.getSource(newFile()); assertThat(source).isEqualTo("downloaded source of Bar.c"); @@ -91,12 +93,12 @@ public class LastSnapshotsTest extends AbstractDbUnitTestCase { @Test public void should_fail_to_download_source_from_ws() throws URISyntaxException { - setupData("last_snapshot"); + db.prepareDbUnit(getClass(), "last_snapshot.xml"); ServerClient server = mock(ServerClient.class); when(server.request(anyString(), eq(false), eq(30 * 1000))).thenThrow(new HttpDownloader.HttpException(new URI(""), 500)); when(mode.isPreview()).thenReturn(true); - LastSnapshots lastSnapshots = new LastSnapshots(mode, getSession(), server); + LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); thrown.expect(HttpDownloader.HttpException.class); lastSnapshots.getSource(newFile()); @@ -104,12 +106,12 @@ public class LastSnapshotsTest extends AbstractDbUnitTestCase { @Test public void should_return_empty_source_if_preview_mode_and_no_last_snapshot() throws URISyntaxException { - setupData("last_snapshot"); + db.prepareDbUnit(getClass(), "last_snapshot.xml"); ServerClient server = mock(ServerClient.class); when(server.request(anyString(), eq(false), eq(30 * 1000))).thenThrow(new HttpDownloader.HttpException(new URI(""), 404)); when(mode.isPreview()).thenReturn(true); - LastSnapshots lastSnapshots = new LastSnapshots(mode, getSession(), server); + LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); String source = lastSnapshots.getSource(newFile()); assertThat(source).isEqualTo(""); @@ -118,10 +120,10 @@ public class LastSnapshotsTest extends AbstractDbUnitTestCase { @Test public void should_not_load_source_of_non_files() throws URISyntaxException { - setupData("last_snapshot"); + db.prepareDbUnit(getClass(), "last_snapshot.xml"); ServerClient server = mock(ServerClient.class); - LastSnapshots lastSnapshots = new LastSnapshots(mode, getSession(), server); + LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server); String source = lastSnapshots.getSource(new Project("my-project")); assertThat(source).isEqualTo(""); |