com.github.siwenyan.superglue.QueryShared Maven / Gradle / Ivy
package com.github.siwenyan.superglue;
import com.github.siwenyan.common.DataTableTools;
import com.github.siwenyan.common.ImmediateAbortException;
import com.github.siwenyan.common.StringTools;
import com.github.siwenyan.common.WhiteBoard;
import com.github.siwenyan.query.IQueryFactory;
import com.github.siwenyan.query.MyBatisUtils;
import com.github.siwenyan.query.QueryBase;
import com.github.siwenyan.query.QueryExpression;
import com.github.siwenyan.web.WebUtils;
import cucumber.api.DataTable;
import cucumber.api.Scenario;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
public class QueryShared extends AbstractStepSet {
public QueryShared(Scenario scenario) {
super(scenario, null);
QueryExpression.putFactory("javascript", new IQueryFactory() {
@Override
public QueryBase createQuery(String source) {
AbstractLogin login = Login.getLastAccessedLogin(scenario);
WebDriver driver = login.driver();
return new QueryBase() {
@Override
public Object query(String path) {
return WebUtils.js(driver, path);
}
};
}
});
}
public void isExecutedInDatabase(String sqlOrFileName0OrMyBatisSqlIdWithParams0, String sqlSessionFactoryClassName0) {
String sqlOrFileName0OrMyBatisSqlIdWithParams1 = WhiteBoard.getInstance().resolve(getOwner(), sqlOrFileName0OrMyBatisSqlIdWithParams0);
String sqlOrMyBatisSqlIdWithParams0 = StringTools.readOrReturn(sqlOrFileName0OrMyBatisSqlIdWithParams1);
String sqlOrMyBatisSqlIdWithParams1 = WhiteBoard.getInstance().resolve(getOwner(), sqlOrMyBatisSqlIdWithParams0);
String sqlSessionFactoryClassName1 = WhiteBoard.getInstance().resolve(getOwner(), sqlSessionFactoryClassName0);
SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory(sqlSessionFactoryClassName1);
if (sqlOrMyBatisSqlIdWithParams1.startsWith("mybatis")) {
try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
String myBatisSqlIdWithParams = sqlOrMyBatisSqlIdWithParams1;
String[] p = myBatisSqlIdWithParams.split("\\s+", 3);
String sqlId = p[1];
String paramString = (3 == p.length ? p[2] : null);
if (null == paramString) {
// no parameters
sqlSession.update(sqlId);
} else {
if (paramString.contains("=")) {
// hashmap parameters
Map params = DataTableTools.asMap(paramString);
sqlSession.update(sqlId, params);
} else {
// single string parameter
sqlSession.update(sqlId, paramString);
}
}
} catch (Exception e) {
Assert.fail("Execution fail: " + sqlOrMyBatisSqlIdWithParams1 + "\r\n" + e.getMessage());
}
} else {
String sql = sqlOrMyBatisSqlIdWithParams1;
Reader reader = new StringReader(sql);
MyBatisUtils.execute(sqlSessionFactory, reader, true, true);
}
}
public void isExtractedFromWithQuery(String varName0, String sourceString0, String queryExpression0) {
String varName = WhiteBoard.getInstance().resolve(getOwner(), varName0);
String sourceString = WhiteBoard.getInstance().resolve(getOwner(), sourceString0);
String queryExpression = WhiteBoard.getInstance().resolve(getOwner(), queryExpression0);
scenarioLogger.warn("varName=" + varName);
scenarioLogger.warn("sourceString=" + sourceString);
scenarioLogger.warn("queryExpression=" + queryExpression);
WhiteBoard.getInstance().removeAll(getOwner(), varName + ".*");
QueryExpression qe = new QueryExpression(queryExpression);
QueryBase q = qe.compile(sourceString);
Object result = q.query(qe.getQuery());
if (result instanceof List) {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy