Skip to content

Commit

Permalink
Reset stored rules when criterion changes
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Aug 13, 2024
1 parent a741740 commit 1de0f43
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 11 deletions.
21 changes: 18 additions & 3 deletions net/QACover/Translated/Giis.Qacover.Core/Controller.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion net/QACoverTest/Test4in2test.Qacoverapp/AppBase.N.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,13 @@ public virtual StackLocator MyGetStackTraceIgnoredMethod()
{
return new StackLocator();
}
}

// Mutant test not run in net, this is only to compile the base class (that is translated)
public virtual ResultSet ExecuteQueryMut(string sql, string param1)
{
return new ResultSet(conn.ExecuteQuery(JdbcParamsToAssert(sql, 1),
new DbParameter[] { GetNetParam(1, param1) }));
}

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions qacover-core/src/main/java/giis/qacover/core/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private CoverageManager mainProcessSql(RuleServices svc, StoreService store, Sta
// CoverageManager is constructed from rules generated in a previous query
// or by generating a fresh set of rules
RuleDriver rd = new RuleDriverFactory().getDriver(); // delegate to get and evaluate the rules
CoverageManager rm = getCoverageManager(rd, store, stack, stmt);
CoverageManager rm = getCoverageManager(rd, store, stack, stmt, options.getRuleCriterion());
if (rm == null) {
log.debug("Generating new coverage rules for this query");
rm = new CoverageManager(rd);
Expand All @@ -103,9 +103,20 @@ private CoverageManager mainProcessSql(RuleServices svc, StoreService store, Sta
return rm;
}

private CoverageManager getCoverageManager(RuleDriver ruleDriver, StoreService store, StackLocator stack, QueryStatement stmt) {
private CoverageManager getCoverageManager(RuleDriver ruleDriver, StoreService store, StackLocator stack, QueryStatement stmt, String currentCriterion) {
QueryModel model = store.get(stack.getClassName(), stack.getMethodName(), stack.getLineNumber(), stmt.getSql());
return model == null ? null : new CoverageManager(ruleDriver, model);
if (model == null)
return null; // to signal the need to create a new model
// If a previous run generated a model with a different criterion,
// a new coverage manager must be created to overwrite the existing model
String ruleCriterion = model.getModel().getRulesClass();
if (!ruleCriterion.equals(currentCriterion)) {
log.warn("Current {} coverage criterion does not match with the stored rule {} criterion."
+ " Existing rule will be overwritten", currentCriterion, ruleCriterion);
return null; // to signal the need to create a new model that will overwrite the stored rule
}
// new empty rule
return new CoverageManager(ruleDriver, model);
}

}
30 changes: 28 additions & 2 deletions qacover-core/src/test/java/test4giis/qacover/TestMutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.Before;
import org.junit.Test;

import giis.qacover.core.services.Configuration;
import giis.qacover.driver.QueryStatementReader;
import giis.qacover.model.Variability;
import test4giis.qacoverapp.AppSimpleJdbc;
Expand Down Expand Up @@ -44,8 +45,25 @@ public void setUpTestData() {

@Test
public void testEvalMutants() throws SQLException {
configureTestOptions().setRuleCriterion("mutation")
.setRuleOptions("nomutate=equivalent,ir,abs,uoi,lcr,nl");
configureTestOptions().setRuleCriterion("mutation").setRuleOptions("nomutate=equivalent,ir,abs,uoi,lcr,nl");
doEvalMutation();

// Updates the current configuration to generate fpc.
// Although a mutation rule is stored for this query, it will be replaced by a new fpc rule
Configuration.getInstance().setRuleCriterion("fpc").setRuleOptions("");
doEvalFpc();
}

@Test
public void testEvalMutantsAfterFpc() throws SQLException {
// Same than before, but first fpc and then mutation
configureTestOptions().setRuleCriterion("fpc");
doEvalFpc();
Configuration.getInstance().setRuleCriterion("mutation").setRuleOptions("nomutate=equivalent,ir,abs,uoi,lcr,nl");
doEvalMutation();
}

private void doEvalMutation() throws SQLException {
rs = app.queryMutParameters("abc");
assertEvalResults("select id,txt from test where txt=?", "1 abc", SqlUtil.resultSet2csv(rs, " "),
"UNCOVERED SELECT DISTINCT id , txt FROM test WHERE txt = 'abc'\n"
Expand All @@ -58,6 +76,14 @@ public void testEvalMutants() throws SQLException {
+ "COVERED SELECT id , txt FROM test WHERE (1=0)",
"{?1?='abc'}", false, new Variability().isNetCore());
}
private void doEvalFpc() throws SQLException {
rs = app.queryMutParameters("abc");
assertEvalResults("select id,txt from test where txt=?", "1 abc", SqlUtil.resultSet2csv(rs, " "),
"UNCOVERED SELECT id , txt FROM test WHERE NOT(txt = 'abc')\n"
+ "COVERED SELECT id , txt FROM test WHERE (txt = 'abc')\n"
+ "COVERED SELECT id , txt FROM test WHERE (txt IS NULL)",
"{?1?='abc'}", false, new Variability().isNetCore());
}

// Detailed evaluation of differences
@Test
Expand Down
7 changes: 5 additions & 2 deletions qacover-core/src/test/java/test4giis/qacoverapp/AppBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,15 @@ public StackLocator myGetStackTraceIgnoredMethod() {
return new StackLocator();
}

// To test mutations (java only)

public Connection getConnectionNative() throws SQLException {
return cf.getNativeConnection();
}
public ResultSet queryMutParameters(String param1) throws SQLException {
PreparedStatement pstmt = conn.prepareStatement("select id,txt from test where txt=?");
public ResultSet executeQueryMut(String sql, String param1) throws SQLException {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, param1);
return pstmt.executeQuery();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ private String quoteIdentifier(String name, boolean doQuote) {
public ResultSet queryParametersNamed(int param1, int param2, String param3) throws SQLException {
return executeQuery("/* params=?1?,?1?,?2? */ select id,num,text from test where id=? or num=? or text=?",param1, param2, param3);
}

public ResultSet queryMutParameters(String param1) throws SQLException {
return executeQueryMut("select id,txt from test where txt=?", param1);
//PreparedStatement pstmt = conn.prepareStatement("select id,txt from test where txt=?");
//pstmt.setString(1, param1);
//return pstmt.executeQuery();
}

}

0 comments on commit 1de0f43

Please sign in to comment.