diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-12-06 23:56:22 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-12-06 23:56:22 +0100 |
commit | b951a180f8f247d809ba4358d409460192c67416 (patch) | |
tree | 056d3eaec7d6be64c84ffdf5a7e886c0ee9c52ab /sonar-core/src | |
parent | 51a54e747245ac047cab3aa36f91b026f04fa36d (diff) | |
download | sonarqube-b951a180f8f247d809ba4358d409460192c67416.tar.gz sonarqube-b951a180f8f247d809ba4358d409460192c67416.zip |
Fix quality flaws
Diffstat (limited to 'sonar-core/src')
4 files changed, 97 insertions, 27 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterCondition.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterCondition.java index 2485c905583..746902aff25 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterCondition.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterCondition.java @@ -35,10 +35,6 @@ public class MeasureFilterCondition { this.sql = sql; } - public String getCode() { - return code; - } - public String getSql() { return sql; } @@ -51,15 +47,6 @@ public class MeasureFilterCondition { } throw new IllegalArgumentException("Unknown operator code: " + code); } - - public static Operator fromSql(String sql) { - for (Operator operator : values()) { - if (operator.sql.equals(sql)) { - return operator; - } - } - throw new IllegalArgumentException("Unknown operator sql: " + sql); - } } private final Metric metric; @@ -101,10 +88,11 @@ public class MeasureFilterCondition { return "pm.value"; } - void appendSqlCondition(StringBuilder sql) { + StringBuilder appendSqlCondition(StringBuilder sql) { sql.append(" pm.metric_id="); sql.append(metric.getId()); sql.append(" AND ").append(valueColumn()).append(operator.getSql()).append(value); + return sql; } @Override diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java index dba97bb0a70..3211cc55c39 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseMigrator.java @@ -57,17 +57,11 @@ public class DatabaseMigrator implements ServerComponent { connection = session.getConnection(); DdlUtils.createSchema(connection, database.getDialect().getId()); } finally { - try { - MyBatis.closeQuietly(session); + MyBatis.closeQuietly(session); - // The connection is probably already closed by session.close() - // but it's not documented in mybatis javadoc. - if (null != connection) { - connection.close(); - } - } catch (Exception e) { - // ignore - } + // The connection is probably already closed by session.close() + // but it's not documented in mybatis javadoc. + DatabaseUtils.closeQuietly(connection); } return true; } diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterConditionTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterConditionTest.java new file mode 100644 index 00000000000..112975ff908 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterConditionTest.java @@ -0,0 +1,82 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.measure; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.measures.Metric; + +import static org.fest.assertions.Assertions.assertThat; + +public class MeasureFilterConditionTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void create_operator_from_code() { + assertThat(MeasureFilterCondition.Operator.fromCode("eq")).isEqualTo(MeasureFilterCondition.Operator.EQUALS); + assertThat(MeasureFilterCondition.Operator.fromCode("lte")).isEqualTo(MeasureFilterCondition.Operator.LESS_OR_EQUALS); + } + + @Test + public void fail_if_operator_code_not_found() { + thrown.expect(IllegalArgumentException.class); + MeasureFilterCondition.Operator.fromCode("xxx"); + } + + @Test + public void operator_sql() { + assertThat(MeasureFilterCondition.Operator.EQUALS.getSql()).isEqualTo("="); + assertThat(MeasureFilterCondition.Operator.LESS_OR_EQUALS.getSql()).isEqualTo("<="); + assertThat(MeasureFilterCondition.Operator.GREATER.getSql()).isEqualTo(">"); + } + + @Test + public void value_condition() { + Metric ncloc = new Metric.Builder("ncloc", "NCLOC", Metric.ValueType.INT).create(); + ncloc.setId(123); + MeasureFilterCondition condition = new MeasureFilterCondition(ncloc, MeasureFilterCondition.Operator.GREATER, 10.0); + + assertThat(condition.metric()).isEqualTo(ncloc); + assertThat(condition.operator()).isEqualTo(MeasureFilterCondition.Operator.GREATER); + assertThat(condition.period()).isNull(); + assertThat(condition.value()).isEqualTo(10.0); + assertThat(condition.valueColumn()).isEqualTo("pm.value"); + assertThat(condition.toString()).isNotEmpty(); + assertThat(condition.appendSqlCondition(new StringBuilder()).toString()).isEqualTo(" pm.metric_id=123 AND pm.value>10.0"); + } + + @Test + public void variation_condition() { + Metric ncloc = new Metric.Builder("ncloc", "NCLOC", Metric.ValueType.INT).create(); + ncloc.setId(123); + MeasureFilterCondition condition = new MeasureFilterCondition(ncloc, MeasureFilterCondition.Operator.LESS_OR_EQUALS, 10.0); + condition.setPeriod(3); + + assertThat(condition.metric()).isEqualTo(ncloc); + assertThat(condition.operator()).isEqualTo(MeasureFilterCondition.Operator.LESS_OR_EQUALS); + assertThat(condition.period()).isEqualTo(3); + assertThat(condition.value()).isEqualTo(10.0); + assertThat(condition.valueColumn()).isEqualTo("pm.variation_value_3"); + assertThat(condition.toString()).isNotEmpty(); + assertThat(condition.appendSqlCondition(new StringBuilder()).toString()).isEqualTo(" pm.metric_id=123 AND pm.variation_value_3<=10.0"); + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/dialect/OracleSequenceGeneratorTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/dialect/OracleSequenceGeneratorTest.java index 9333cb0dd53..e50efdac844 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/dialect/OracleSequenceGeneratorTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/dialect/OracleSequenceGeneratorTest.java @@ -24,8 +24,7 @@ import org.junit.Test; import java.util.Properties; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.fest.assertions.Assertions.assertThat; public class OracleSequenceGeneratorTest { @@ -37,7 +36,14 @@ public class OracleSequenceGeneratorTest { OracleSequenceGenerator generator = new OracleSequenceGenerator(); generator.configure(null, props, new Oracle.Oracle10gWithDecimalDialect()); - assertThat(generator.getSequenceName(), is("MY_TABLE_SEQ")); + assertThat(generator.getSequenceName()).isEqualTo("MY_TABLE_SEQ"); } + @Test + public void should_not_fail_if_table_name_can_not_be_loaded() { + Properties props = new Properties(); + OracleSequenceGenerator generator = new OracleSequenceGenerator(); + generator.configure(null, props, new Oracle.Oracle10gWithDecimalDialect()); + assertThat(generator.getSequenceName()).isNotEmpty(); + } } |