JDBCSmellQueries extends JDBCQueriesHelper implements SmellQueries { @Override public String commitSmellsQuery(int projectId, String commitId, String smellType) { String smellsQuery = "SELECT smell.id, type, instance, file, renamed_from FROM smell " + "RIGHT JOIN smell_presence ON smell_presence.smell_id = smell.id " + "WHERE smell_presence.commit_id = " + commitId; if (smellType != null) { smellsQuery += " AND smell.type = '" + smellType + "'"; } String query = "WITH sm AS (" + smellsQuery + ") " + "SELECT sm.id, sm.type, sm.instance, sm.file, " + "parent.type AS parent_type, parent.instance AS parent_instance, parent.file AS parent_file " + "FROM sm " + "LEFT JOIN (SELECT id, type, instance, file FROM smell) parent " + "ON parent.id = sm.renamed_from"; return query; } JDBCSmellQueries(CommitQueries commitQueries); @Override String smellInsertionStatement(int projectId, Smell smell); @Override String smellCategoryInsertionStatement(int projectId, String sha1, Smell smell, SmellCategory category); @Override String lostSmellCategoryInsertionStatement(int projectId, Smell smell, SmellCategory category, int since, int until); @Override String smellIdQuery(int projectId, Smell smell); @Override String commitSmellsQuery(int projectId, String commitId, String smellType); String lastSmellIdQuery(int projectId); String allRefactoredInstancesWithSha1(int projectId); String setAsDeleted(int projectId, int refactoringId, boolean deleted);  }
@Test public void testCommitSmellsQuery() { List<Map<String, Object>> result; Commit commit = prepareCommit("sha"); Commit second_commit = prepareCommit("another_sha"); String commitIdQuery = commitQueries.idFromShaQuery(projectId, commit.sha); int commitId = (int) persistence.query(commitIdQuery).get(0).get("id"); result = persistence.query(queries.commitSmellsQuery(projectId, String.valueOf(commitId), smell.type)); assertTrue(result.isEmpty()); Smell anotherSmellType = new Smell("MIM", smell.instance, "yetanotherfile"); Smell anotherSmell = new Smell(smell.type, "anotherinstance", "anotherfile"); Smell smellOtherCommit = new Smell(smell.type, "my.smell.instance", "anotherfile"); smell.id = createSmell(projectId, smell, queries); anotherSmell.id = createSmell(projectId, anotherSmell, queries); anotherSmellType.id = createSmell(projectId, anotherSmellType, queries); smellOtherCommit.id = createSmell(projectId, smellOtherCommit, queries); executeSuccess(queries.smellCategoryInsertionStatement(projectId, commit.sha, smell, SmellCategory.PRESENCE)); executeSuccess(queries.smellCategoryInsertionStatement(projectId, commit.sha, anotherSmell, SmellCategory.PRESENCE)); executeSuccess(queries.smellCategoryInsertionStatement(projectId, commit.sha, anotherSmellType, SmellCategory.PRESENCE)); executeSuccess(queries.smellCategoryInsertionStatement(projectId, second_commit.sha, anotherSmell, SmellCategory.PRESENCE)); executeSuccess(queries.smellCategoryInsertionStatement(projectId, second_commit.sha, smellOtherCommit, SmellCategory.PRESENCE)); result = persistence.query(queries.commitSmellsQuery(projectId, String.valueOf(commitId), null)); assertEquals(3, result.size()); checkContainsSmells(result, Arrays.asList(smell, anotherSmellType, anotherSmell)); result = persistence.query(queries.commitSmellsQuery(projectId, "(" + commitIdQuery + ")", null)); assertEquals(3, result.size()); checkContainsSmells(result, Arrays.asList(smell, anotherSmellType, anotherSmell)); result = persistence.query(queries.commitSmellsQuery(projectId, String.valueOf(commitId), smell.type)); assertEquals(2, result.size()); checkContainsSmells(result, Arrays.asList(smell, anotherSmell)); Smell withParent = new Smell(smell.type, "withParentInstance", "withParentFile"); withParent.parent = smell; withParent.id = createSmell(projectId, withParent, queries); executeSuccess(queries.smellCategoryInsertionStatement(projectId, commit.sha, withParent, SmellCategory.PRESENCE)); result = persistence.query(queries.commitSmellsQuery(projectId, String.valueOf(commitId), smell.type)); assertEquals(3, result.size()); checkContainsSmells(result, Arrays.asList(smell, anotherSmell, withParent)); result = persistence.query(queries.commitSmellsQuery(projectId, "(" + commitQueries.idFromShaQuery(projectId, second_commit.sha) + ")", smell.type)); assertEquals(2, result.size()); checkContainsSmells(result, Arrays.asList(anotherSmell, smellOtherCommit)); }