Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package adql.search;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import adql.parser.ADQLParser;
import adql.query.ADQLObject;
import adql.query.ADQLQuery;
import adql.query.operand.function.DefaultUDF;
import adql.query.operand.function.MathFunction;
public class TestSimpleReplaceHandler {
@Before
public void setUp() throws Exception{}
@Test
public void testReplaceRecursiveMatch(){
/* WHY THIS TEST?
*
* When a match item had also a match item inside it (e.g. function parameter or sub-query),
* both matched items (e.g. the parent and the child) must be replaced.
*
* However, if the parent is replaced first, the reference of the new parent is lost by the SimpleReplaceHandler and so,
* the replacement of the child will be performed on the former parent. Thus, after the whole process,
* in the final ADQL query, the replacement of the child won't be visible since the former parent is
* not referenced any more.
*/
String testQuery = "SELECT SQRT(ABS(81)) FROM myTable";
try{
// Parse the query:
ADQLQuery query = (new ADQLParser()).parseQuery(testQuery);
// Check it is as expected, before the replacements:
assertEquals(testQuery, query.toADQL().replaceAll("\\n", " "));
// Create a replace handler:
SimpleReplaceHandler replaceHandler = new SimpleReplaceHandler(){
@Override
protected boolean match(ADQLObject obj){
return obj instanceof MathFunction;
}
@Override
protected ADQLObject getReplacer(ADQLObject objToReplace) throws UnsupportedOperationException{
return new DefaultUDF("foo", ((MathFunction)objToReplace).getParameters());
}
};
// Apply the replacement of all mathematical functions by a dumb UDF:
replaceHandler.searchAndReplace(query);
assertEquals(2, replaceHandler.getNbMatch());
assertEquals(replaceHandler.getNbMatch(), replaceHandler.getNbReplacement());
assertEquals("SELECT foo(foo(81)) FROM myTable", query.toADQL().replaceAll("\\n", " "));
}catch(Exception ex){
ex.printStackTrace(System.err);
fail("No error should have occured here since nothing is wrong in the ADQL query used for the test. See the stack trace in the console for more details.");
}
}
}