querydsl/querydsl-sql/src/test/java/com/mysema/query/AbstractBaseTest.java
2012-12-11 23:34:22 +02:00

147 lines
4.7 KiB
Java

/*
* Copyright 2011, Mysema Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mysema.query;
import static org.junit.Assert.assertEquals;
import java.sql.Connection;
import javax.annotation.Nullable;
import junit.framework.Assert;
import org.junit.Rule;
import org.junit.rules.MethodRule;
import com.mysema.query.sql.AbstractSQLQuery;
import com.mysema.query.sql.AbstractSQLSubQuery;
import com.mysema.query.sql.Configuration;
import com.mysema.query.sql.RelationalPath;
import com.mysema.query.sql.SQLCommonQuery;
import com.mysema.query.sql.SQLSubQuery;
import com.mysema.query.sql.SQLTemplates;
import com.mysema.query.sql.dml.SQLDeleteClause;
import com.mysema.query.sql.dml.SQLInsertClause;
import com.mysema.query.sql.dml.SQLMergeClause;
import com.mysema.query.sql.dml.SQLUpdateClause;
import com.mysema.query.sql.mysql.MySQLQuery;
import com.mysema.query.sql.mysql.MySQLReplaceClause;
import com.mysema.query.sql.oracle.OracleQuery;
public abstract class AbstractBaseTest {
protected final class TestQuery extends AbstractSQLQuery<TestQuery> implements SQLCommonQuery<TestQuery> {
private TestQuery(Connection conn, Configuration configuration) {
super(conn, configuration);
}
private TestQuery(Connection conn, Configuration configuration, QueryMetadata metadata) {
super(conn, configuration, metadata);
}
@Override
protected String buildQueryString(boolean countRow) {
String rv = super.buildQueryString(countRow);
if (expectedQuery != null) {
assertEquals(expectedQuery, rv.replace('\n', ' '));
expectedQuery = null;
}
System.out.println(rv);
return rv;
}
public TestQuery clone(Connection conn) {
TestQuery q = new TestQuery(conn, getConfiguration(), getMetadata().clone());
q.union = union;
q.unionAll = unionAll;
return q;
}
}
private Connection connection = Connections.getConnection();
private SQLTemplates templates = Connections.getTemplates();
@Nullable
protected String expectedQuery;
@Rule
public static MethodRule skipForQuotedRule = new SkipForQuotedRule();
@Rule
public static MethodRule targetRule = new TargetRule();
protected SQLUpdateClause update(RelationalPath<?> e){
return new SQLUpdateClause(connection, templates, e);
}
protected SQLInsertClause insert(RelationalPath<?> e){
return new SQLInsertClause(connection, templates, e);
}
protected SQLInsertClause insert(RelationalPath<?> e, AbstractSQLSubQuery<?> sq) {
return new SQLInsertClause(connection, templates, e, sq);
}
protected SQLDeleteClause delete(RelationalPath<?> e){
return new SQLDeleteClause(connection, templates, e);
}
protected SQLMergeClause merge(RelationalPath<?> e){
return new SQLMergeClause(connection, templates, e);
}
protected ExtendedSQLQuery extQuery() {
return new ExtendedSQLQuery(connection, templates);
}
protected MySQLQuery mysqlQuery(){
return new MySQLQuery(connection, templates);
}
protected SQLInsertClause mysqlReplace(RelationalPath<?> path) {
return new MySQLReplaceClause(connection, templates, path);
}
protected TestQuery query() {
return new TestQuery(connection, new Configuration(templates));
}
protected TestQuery testQuery() {
return new TestQuery(connection, new Configuration(templates),
new DefaultQueryMetadata().noValidate());
}
protected OracleQuery oracleQuery(){
return new OracleQuery(connection, templates){
@Override
protected String buildQueryString(boolean forCountRow) {
String rv = super.buildQueryString(forCountRow);
if (expectedQuery != null){
Assert.assertEquals(expectedQuery, rv.replace('\n', ' '));
expectedQuery = null;
}
System.out.println(rv);
return rv;
}
};
}
protected SQLSubQuery sq(){
return new SQLSubQuery();
}
}