import org.sonar.api.rules.Violation;
import org.sonar.api.violations.ViolationQuery;
import org.sonar.batch.index.ResourcePersister;
+import org.sonar.core.persistence.DaoTestCase;
import org.sonar.core.review.ReviewDao;
import org.sonar.core.review.ReviewDto;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
import java.util.Collections;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.*;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
-public class ReviewWorkflowDecoratorTest extends AbstractDbUnitTestCase {
+public class ReviewWorkflowDecoratorTest extends DaoTestCase {
private ReviewWorkflowDecorator decorator;
private ReviewNotifications notifications;
decorator.decorate(resource, context);
verify(notifications, times(2)).notifyClosed(any(ReviewDto.class), any(Project.class), eq(resource));
- checkTablesWithExcludedColumns("shouldCloseReviewsOnResolvedViolations", new String[]{"updated_at"}, "reviews");
+ checkTables("shouldCloseReviewsOnResolvedViolations", new String[] {"updated_at"}, "reviews");
}
@Test
decorator.decorate(resource, context);
verify(notifications).notifyClosed(any(ReviewDto.class), any(Project.class), eq(resource));
- checkTablesWithExcludedColumns("shouldCloseResolvedManualViolations", new String[]{"updated_at"}, "reviews");
+ checkTables("shouldCloseResolvedManualViolations", new String[] {"updated_at"}, "reviews");
}
@Test
decorator.decorate(resource, context);
verify(notifications).notifyReopened(any(ReviewDto.class), any(Project.class), eq(resource));
- checkTablesWithExcludedColumns("shouldReopenViolations", new String[]{"updated_at"}, "reviews");
+ checkTables("shouldReopenViolations", new String[] {"updated_at"}, "reviews");
}
@Test
<reviews id="1" project_id="[null]" resource_id="100" status="OPEN" rule_failure_permanent_id="1000"
- resolution="[null]" created_at="[null]" updated_at="[null]"
+ resolution="[null]" created_at="[null]"
resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="true" manual_severity="false"
title="[null]" assignee_id="[null]" data="[null]"/>
<reviews id="2" project_id="[null]" resource_id="100" status="CLOSED" rule_failure_permanent_id="1000"
- resolution="FIXED" created_at="[null]" updated_at="[null]"
+ resolution="FIXED" created_at="[null]"
resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="true" manual_severity="false"
title="[null]" assignee_id="[null]" data="[null]"/>
<!-- open reviews ==> CLOSED -->
<reviews id="1" project_id="[null]" resource_id="100" status="CLOSED" rule_failure_permanent_id="1000"
- resolution="[null]" created_at="[null]" updated_at="[null]"
+ resolution="[null]" created_at="[null]"
resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="false" manual_severity="false"
title="[null]" assignee_id="[null]" data="[null]"/>
<reviews id="2" project_id="[null]" resource_id="100" status="CLOSED" rule_failure_permanent_id="1001"
- resolution="FALSE-POSITIVE" created_at="[null]" updated_at="[null]"
+ resolution="FALSE-POSITIVE" created_at="[null]"
resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="false" manual_severity="false"
title="[null]" assignee_id="[null]" data="[null]"/>
<!-- closed reviews -->
<reviews id="3" project_id="[null]" resource_id="100" status="CLOSED" rule_failure_permanent_id="1002"
- resolution="RESOLVED" created_at="[null]" updated_at="[null]"
+ resolution="RESOLVED" created_at="[null]"
resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="false" manual_severity="false"
title="[null]" assignee_id="[null]" data="[null]"/>
<!-- other resource -->
<reviews id="4" project_id="[null]" resource_id="101" status="OPEN" rule_failure_permanent_id="1100"
- resolution="[null]" created_at="[null]" updated_at="[null]"
+ resolution="[null]" created_at="[null]"
resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="false" manual_severity="false"
title="[null]" assignee_id="[null]" data="[null]"/>
</dataset>
\ No newline at end of file
<reviews id="1" project_id="[null]" resource_id="100" status="REOPENED" rule_failure_permanent_id="1000"
- resolution="[null]" created_at="[null]" updated_at="[null]"
+ resolution="[null]" created_at="[null]"
resource_line="[null]" severity="[null]" user_id="[null]" rule_id="[null]" manual_violation="false" manual_severity="false"
title="[null]" assignee_id="[null]" data="[null]"/>
ResourcePersister persister = new DefaultResourcePersister(getSession());
persister.saveProject(singleProject, null);
- checkTablesWithExcludedColumns("shouldSaveNewProject", new String[] {"build_date"}, "projects", "snapshots");
+ checkTables("shouldSaveNewProject", new String[] {"build_date"}, "projects", "snapshots");
}
@Test
persister.saveProject(moduleB, multiModuleProject);
persister.saveProject(moduleB1, moduleB);
- checkTablesWithExcludedColumns("shouldSaveNewMultiModulesProject", new String[] {"build_date"}, "projects", "snapshots");
+ checkTables("shouldSaveNewMultiModulesProject", new String[] {"build_date"}, "projects", "snapshots");
}
@Test
persister.saveResource(singleProject, new JavaPackage("org.foo").setEffectiveKey("foo:org.foo"));
// check that the directory is attached to the project
- checkTablesWithExcludedColumns("shouldSaveNewDirectory", new String[] {"build_date"}, "projects", "snapshots");
+ checkTables("shouldSaveNewDirectory", new String[] {"build_date"}, "projects", "snapshots");
}
@Test
persister.saveResource(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit"));// do nothing, already saved
persister.saveResource(singleProject, new Library("junit:junit", "3.2").setEffectiveKey("junit:junit"));
- checkTablesWithExcludedColumns("shouldSaveNewLibrary", new String[] {"build_date"}, "projects", "snapshots");
+ checkTables("shouldSaveNewLibrary", new String[] {"build_date"}, "projects", "snapshots");
}
@Test
singleProject.setDescription("new description");
persister.saveProject(singleProject, null);
- checkTablesWithExcludedColumns("shouldUpdateExistingResource", new String[] {"build_date"}, "projects", "snapshots");
+ checkTables("shouldUpdateExistingResource", new String[] {"build_date"}, "projects", "snapshots");
}
// SONAR-1700
ResourcePersister persister = new DefaultResourcePersister(getSession());
persister.saveProject(singleProject, null);
- checkTablesWithExcludedColumns("shouldRemoveRootIndexIfResourceIsProject", new String[] {"build_date"}, "projects", "snapshots");
+ checkTables("shouldRemoveRootIndexIfResourceIsProject", new String[] {"build_date"}, "projects", "snapshots");
}
}
import org.dbunit.DatabaseUnitException;
import org.dbunit.IDatabaseTester;
import org.dbunit.database.IDatabaseConnection;
-import org.dbunit.dataset.*;
+import org.dbunit.dataset.Column;
+import org.dbunit.dataset.CompositeDataSet;
+import org.dbunit.dataset.DataSetException;
+import org.dbunit.dataset.IDataSet;
+import org.dbunit.dataset.ITable;
+import org.dbunit.dataset.ITableMetaData;
+import org.dbunit.dataset.ReplacementDataSet;
import org.dbunit.dataset.filter.DefaultColumnFilter;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;
-import org.junit.*;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
import org.sonar.api.database.DatabaseSession;
import org.sonar.core.persistence.Database;
import org.sonar.core.persistence.DatabaseCommands;
import org.sonar.core.persistence.InMemoryDatabase;
-import org.sonar.core.persistence.MyBatis;
import org.sonar.jpa.session.DatabaseSessionFactory;
import org.sonar.jpa.session.DefaultDatabaseConnector;
import org.sonar.jpa.session.JpaDatabaseSession;
import org.sonar.jpa.session.MemoryDatabaseConnector;
import java.io.InputStream;
-import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
* Heavily duplicates DaoTestCase as long as Hibernate is in use.
*/
public abstract class AbstractDbUnitTestCase {
+ private static Database database;
+ private static DefaultDatabaseConnector dbConnector;
+ private static DatabaseCommands databaseCommands;
private JpaDatabaseSession session;
private IDatabaseTester databaseTester;
private IDatabaseConnection connection;
- private static Database database;
- private static MyBatis myBatis;
- private static DefaultDatabaseConnector dbConnector;
- private static DatabaseCommands databaseCommands;
@BeforeClass
public static void startDatabase() throws Exception {
database = new InMemoryDatabase();
database.start();
- myBatis = new MyBatis(database);
- myBatis.start();
-
dbConnector = new MemoryDatabaseConnector(database);
dbConnector.start();
@Before
public void startConnection() throws Exception {
- databaseCommands.truncateDatabase(myBatis.openSession().getConnection());
+ databaseCommands.truncateDatabase(database.getDataSource().getConnection());
databaseTester = new DataSourceDatabaseTester(database.getDataSource());
session = new JpaDatabaseSession(dbConnector);
database.stop();
}
- protected MyBatis getMyBatis() {
- return myBatis;
- }
-
public DatabaseSession getSession() {
return session;
}
}
}
- protected final void setupData(InputStream... dataSetStream) {
+ private final void setupData(InputStream... dataSetStream) {
try {
IDataSet[] dataSets = new IDataSet[dataSetStream.length];
for (int i = 0; i < dataSetStream.length; i++) {
}
protected final void checkTables(String testName, String... tables) {
- checkTablesWithExcludedColumns(testName, new String[]{}, tables);
+ checkTables(testName, new String[0], tables);
}
- protected final void checkTablesWithExcludedColumns(String testName, String[] excludedColumnNames, String... tables) {
+ protected final void checkTables(String testName, String[] excludedColumnNames, String... tables) {
getSession().commit();
try {
IDataSet dataSet = getCurrentDataSet();
}
}
- protected final void assertEmptyTables(String... emptyTables) {
- for (String table : emptyTables) {
- try {
- Assert.assertEquals(0, getCurrentDataSet().getTable(table).getRowCount());
- } catch (DataSetException e) {
- throw translateException("Error while checking results", e);
- }
- }
- }
-
- protected final IDataSet getExpectedData(String testName) {
+ private final IDataSet getExpectedData(String testName) {
String className = getClass().getName();
className = String.format("/%s/%s-result.xml", className.replace(".", "/"), testName);
}
}
- protected final IDataSet getData(InputStream stream) {
+ private final IDataSet getData(InputStream stream) {
try {
ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(stream));
dataSet.addReplacementObject("[null]", null);
}
}
- protected final IDataSet getCurrentDataSet() {
+ private final IDataSet getCurrentDataSet() {
try {
return connection.createDataSet();
} catch (SQLException e) {
}
}
- protected String getCurrentDataSetAsXML() {
- return getDataSetAsXML(getCurrentDataSet());
- }
-
- protected String getDataSetAsXML(IDataSet dataset) {
- try {
- StringWriter writer = new StringWriter();
- FlatXmlDataSet.write(dataset, writer);
- return writer.getBuffer().toString();
- } catch (Exception e) {
- throw translateException("Could not build XML from dataset", e);
- }
- }
-
private static RuntimeException translateException(String msg, Exception cause) {
RuntimeException runtimeException = new RuntimeException(String.format("%s: [%s] %s", msg, cause.getClass().getName(), cause.getMessage()));
runtimeException.setStackTrace(cause.getStackTrace());
return runtimeException;
}
- protected Long getHQLCount(final Class<?> hqlClass) {
+ protected Long getHQLCount(Class<?> hqlClass) {
String hqlCount = "SELECT count(o) from " + hqlClass.getSimpleName() + " o";
return (Long) getSession().createQuery(hqlCount).getSingleResult();
}
-
- protected IDatabaseConnection getConnection() {
- return connection;
- }
-
- protected IDatabaseTester getDatabaseTester() {
- return databaseTester;
- }
-
}
public void testRuleActivated() {
setupData("initialData");
profilesManager.activated(2, 3, "admin");
- checkTablesWithExcludedColumns("ruleActivated", new String[]{"change_date"}, "active_rule_changes");
+ checkTables("ruleActivated", new String[]{"change_date"}, "active_rule_changes");
}
@Test
public void testRuleDeactivated() {
setupData("initialData");
profilesManager.deactivated(2, 3, "admin");
- checkTablesWithExcludedColumns("ruleDeactivated", new String[]{"change_date"}, "active_rule_changes");
+ checkTables("ruleDeactivated", new String[]{"change_date"}, "active_rule_changes");
}
@Test
public void testRuleParamChanged() {
setupData("initialData");
profilesManager.ruleParamChanged(2, 3, "param1", "20", "30", "admin");
- checkTablesWithExcludedColumns("ruleParamChanged", new String[]{"change_date"}, "active_rule_changes", "active_rule_param_changes");
+ checkTables("ruleParamChanged", new String[]{"change_date"}, "active_rule_changes", "active_rule_param_changes");
}
@Test
public void testRuleSeverityChanged() {
setupData("initialData");
profilesManager.ruleSeverityChanged(2, 3, RulePriority.BLOCKER, RulePriority.CRITICAL, "admin");
- checkTablesWithExcludedColumns("ruleSeverityChanged", new String[]{"change_date"}, "active_rule_changes");
+ checkTables("ruleSeverityChanged", new String[]{"change_date"}, "active_rule_changes");
}
@Test
public void testRuleReverted() {
setupData("ruleReverted");
profilesManager.revert(2, 3, "admin");
- checkTablesWithExcludedColumns("ruleReverted", new String[]{"change_date"}, "active_rule_changes", "active_rule_param_changes");
+ checkTables("ruleReverted", new String[]{"change_date"}, "active_rule_changes", "active_rule_param_changes");
}
@Test
public void testChangeParentProfile() {
setupData("changeParentProfile");
profilesManager.changeParentProfile(2, "parent", "admin");
- checkTablesWithExcludedColumns("changeParentProfile", new String[]{"change_date"}, "active_rule_changes");
+ checkTables("changeParentProfile", new String[]{"change_date"}, "active_rule_changes");
}
import org.apache.commons.configuration.BaseConfiguration;
import org.junit.Test;
import org.sonar.api.config.PropertyDefinitions;
+import org.sonar.core.persistence.DaoTestCase;
import org.sonar.core.properties.PropertiesDao;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
import java.io.File;
import java.net.URISyntaxException;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
-public class ServerSettingsTest extends AbstractDbUnitTestCase {
+public class ServerSettingsTest extends DaoTestCase {
private static File home = getHome();