aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-09-21 21:25:29 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-09-21 21:25:29 +0000
commitd46e081eacea3680bae90faa5df1ae43157d1e87 (patch)
tree3e7bea37e9b306f335cd6a52910b6ffb4068bec2 /sonar-core
parentd4963b41c34bc8a0d94ad80fe098cd088bb4a5a0 (diff)
downloadsonarqube-d46e081eacea3680bae90faa5df1ae43157d1e87.tar.gz
sonarqube-d46e081eacea3680bae90faa5df1ae43157d1e87.zip
quality models: limit the methods of org.sonar.api.qualitymodel.ModelFinder to read methods. Management methods are restricted to core => extracted to org.sonar.server.qualitymodel.ModelManager
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/qualitymodel/DefaultModelFinder.java89
-rw-r--r--sonar-core/src/test/java/org/sonar/core/qualitymodel/DefaultModelFinderTest.java79
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/noDefinitionsToRegister-result.xml10
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/registerOnlyNewDefinitions-result.xml13
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/reset-result.xml15
5 files changed, 2 insertions, 204 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/qualitymodel/DefaultModelFinder.java b/sonar-core/src/main/java/org/sonar/core/qualitymodel/DefaultModelFinder.java
index 11d20a2ad74..5606e5e0907 100644
--- a/sonar-core/src/main/java/org/sonar/core/qualitymodel/DefaultModelFinder.java
+++ b/sonar-core/src/main/java/org/sonar/core/qualitymodel/DefaultModelFinder.java
@@ -19,88 +19,17 @@
*/
package org.sonar.core.qualitymodel;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.LoggerFactory;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.qualitymodel.Model;
-import org.sonar.api.qualitymodel.ModelDefinition;
import org.sonar.api.qualitymodel.ModelFinder;
-import org.sonar.api.utils.Logs;
-import org.sonar.api.utils.SonarException;
import org.sonar.jpa.session.DatabaseSessionFactory;
-import javax.persistence.Query;
-
public class DefaultModelFinder implements ModelFinder {
- private ModelDefinition[] definitions;
private DatabaseSessionFactory sessionFactory;
- public DefaultModelFinder(DatabaseSessionFactory sessionFactory, ModelDefinition[] definitions) {
- this.sessionFactory = sessionFactory;
- this.definitions = definitions;
- }
-
- /**
- * This constructor is used when there are no templates
- */
public DefaultModelFinder(DatabaseSessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
- this.definitions = new ModelDefinition[0];
- }
-
- /**
- * Executed when the server starts
- */
- public void registerDefinitions() {
- DatabaseSession session = sessionFactory.getSession();
- for (ModelDefinition definition : definitions) {
- if (StringUtils.isNotBlank(definition.getName()) && !exists(session, definition.getName())) {
- Logs.INFO.info("Register quality model: " + definition.getName());
- Model model = definition.create();
- if (StringUtils.isBlank(model.getName())) {
- model.setName(definition.getName());
- }
- insert(session, model);
- session.commit();
- }
- }
- }
-
- public Model reset(String name) {
- ModelDefinition definition = findDefinitionByName(name);
- if (definition == null) {
- throw new SonarException("Can not reset quality model. Definition not found: " + name);
- }
-
- LoggerFactory.getLogger(getClass()).info("Reset quality model: " + name);
- Model model = definition.create();
- return reset(model);
- }
-
-
-
- Model reset(Model model) {
- DatabaseSession session = sessionFactory.getSession();
- try {
- delete(session, model.getName());
- model = insert(session, model);
- session.commit();
- return model;
-
- } catch (RuntimeException e) {
- session.rollback();
- throw e;
- }
- }
-
- public ModelDefinition findDefinitionByName(String name) {
- for (ModelDefinition definition : definitions) {
- if (StringUtils.equals(name, definition.getName())) {
- return definition;
- }
- }
- return null;
}
public Model findByName(String name) {
@@ -108,22 +37,4 @@ public class DefaultModelFinder implements ModelFinder {
return session.getSingleResult(Model.class, "name", name);
}
- public static void delete(DatabaseSession session, String name) {
- Model model = session.getSingleResult(Model.class, "name", name);
- if (model != null) {
- session.removeWithoutFlush(model);
- session.commit();
- }
- }
-
- public static Model insert(DatabaseSession session, Model model) {
- return (Model) session.saveWithoutFlush(model);
- }
-
- public static boolean exists(DatabaseSession session, String name) {
- Query query = session.getEntityManager().createQuery("SELECT COUNT(qm) FROM " + Model.class.getSimpleName() + " qm WHERE qm.name=:name");
- query.setParameter("name", name);
- Number count = (Number) query.getSingleResult();
- return count.intValue() > 0;
- }
}
diff --git a/sonar-core/src/test/java/org/sonar/core/qualitymodel/DefaultModelFinderTest.java b/sonar-core/src/test/java/org/sonar/core/qualitymodel/DefaultModelFinderTest.java
index 1808348c376..9069fab38f4 100644
--- a/sonar-core/src/test/java/org/sonar/core/qualitymodel/DefaultModelFinderTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/qualitymodel/DefaultModelFinderTest.java
@@ -20,39 +20,15 @@
package org.sonar.core.qualitymodel;
import org.junit.Test;
-import org.sonar.api.qualitymodel.Characteristic;
import org.sonar.api.qualitymodel.Model;
-import org.sonar.api.qualitymodel.ModelDefinition;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
public class DefaultModelFinderTest extends AbstractDbUnitTestCase {
@Test
- public void reset() {
- setupData("shared");
- DefaultModelFinder provider = new DefaultModelFinder(getSessionFactory());
-
- Model model = Model.createByName("M1");
- Characteristic c1 = model.createCharacteristicByName("NEWM1C1");
- Characteristic c1a = model.createCharacteristicByName("NEWM1C1A");
- c1.addChild(c1a);
-
- model.createCharacteristicByName("NEWM1C2");
- model = provider.reset(model);
-
- model = getSession().getSingleResult(Model.class, "name", "M1");
- assertNotNull(model);
- assertThat(model.getCharacteristics().size(), is(3));
- assertThat(model.getCharacteristicByName("NEWM1C1A").getParents().size(), is(1));
- assertNotNull(model.getCharacteristicByName("NEWM1C1A").getParent("NEWM1C1"));
- }
-
- @Test
public void findByName() {
setupData("shared");
DefaultModelFinder provider = new DefaultModelFinder(getSessionFactory());
@@ -67,55 +43,4 @@ public class DefaultModelFinderTest extends AbstractDbUnitTestCase {
DefaultModelFinder provider = new DefaultModelFinder(getSessionFactory());
assertNull(provider.findByName("UNKNOWN"));
}
-
- @Test
- public void noDefinitionsToRegister() {
- setupData("shared");
- DefaultModelFinder provider = new DefaultModelFinder(getSessionFactory());
- provider.registerDefinitions();
-
- // same state
- List<Model> models = getSession().getResults(Model.class);
- assertThat(models.size(), is(2));
- }
-
- @Test
- public void registerOnlyNewDefinitions() {
- setupData("shared");
-
- ModelDefinition existingDefinition = new FakeDefinition("M1");
- ModelDefinition newDefinition = new FakeDefinition("NEWMODEL");
-
- ModelDefinition[] definitions = new ModelDefinition[]{existingDefinition, newDefinition};
- DefaultModelFinder provider = new DefaultModelFinder(getSessionFactory(), definitions);
- provider.registerDefinitions();
-
- List<Model> models = getSession().getResults(Model.class);
- assertThat(models.size(), is(3)); // 2 existing + one new
- }
-
- @Test
- public void exists() {
- setupData("shared");
- assertTrue(DefaultModelFinder.exists(getSession(), "M1"));
- }
-
- @Test
- public void notExists() {
- setupData("shared");
- assertFalse(DefaultModelFinder.exists(getSession(), "UNKNOWN"));
- }
-}
-
-class FakeDefinition extends ModelDefinition {
-
- public FakeDefinition(String name) {
- super(name);
- }
-
- @Override
- public Model create() {
- return Model.create();
- }
-
} \ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/noDefinitionsToRegister-result.xml b/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/noDefinitionsToRegister-result.xml
deleted file mode 100644
index c26de5371bb..00000000000
--- a/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/noDefinitionsToRegister-result.xml
+++ /dev/null
@@ -1,10 +0,0 @@
-<dataset>
- <quality_models id="1" name="M1" />
- <quality_models id="2" name="M2" />
-
- <characteristics id="1" kee="M1C1" name="M1C1" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true" />
- <characteristics id="2" kee="M1C2" name="M1C2" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="2" description="[null]" enabled="true" />
- <characteristics id="3" kee="M2C1" name="M2C1" quality_model_id="2" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true"/>
-
- <characteristic_edges child_id="2" parent_id="1"/>
-</dataset> \ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/registerOnlyNewDefinitions-result.xml b/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/registerOnlyNewDefinitions-result.xml
deleted file mode 100644
index 455cfb41cde..00000000000
--- a/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/registerOnlyNewDefinitions-result.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-<dataset>
- <quality_models id="1" name="M1" />
- <quality_models id="2" name="M2" />
-
- <!-- NEW MODEL -->
- <quality_models id="3" name="NEWMODEL" />
-
- <characteristics id="1" kee="M1C1" name="M1C1" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true" />
- <characteristics id="2" kee="M1C2" name="M1C2" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="2" description="[null]" enabled="true" />
- <characteristics id="3" kee="M2C1" name="M2C1" quality_model_id="2" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true"/>
-
- <characteristic_edges child_id="2" parent_id="1"/>
-</dataset> \ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/reset-result.xml b/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/reset-result.xml
deleted file mode 100644
index bbffd8e4a77..00000000000
--- a/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/reset-result.xml
+++ /dev/null
@@ -1,15 +0,0 @@
-<dataset>
- <!--<quality_models id="1" name="M1" />-->
- <quality_models id="2" name="M2" />
- <quality_models id="3" name="M1" />
-
- <!--<characteristics id="1" kee="M1C1" name="M1C1" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true" />-->
- <!--<characteristics id="2" kee="M1C2" name="M1C2" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="2" description="[null]" enabled="true"/>-->
- <characteristics id="3" kee="M2C1" name="M2C1" quality_model_id="2" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true" />
- <characteristics id="4" kee="NEWM1C1A" name="NEWM1C1A" quality_model_id="3" rule_id="[null]" characteristic_order="1" depth="2" description="[null]" enabled="true"/>
- <characteristics id="5" kee="NEWM1C1" name="NEWM1C1" quality_model_id="3" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true"/>
- <characteristics id="6" kee="NEWM1C2" name="NEWM1C2" quality_model_id="3" rule_id="[null]" characteristic_order="2" depth="1" description="[null]" enabled="true"/>
-
- <!--<characteristic_edges child_id="2" parent_id="1"/>-->
- <characteristic_edges child_id="4" parent_id="5"/>
-</dataset> \ No newline at end of file