KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > filter > TestExpressionHelper


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2002 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23 package org.objectweb.medor.filter;
24
25 import org.objectweb.jorm.type.api.PType;
26 import org.objectweb.medor.api.Field;
27 import org.objectweb.medor.api.MedorException;
28 import org.objectweb.medor.expression.api.Expression;
29 import org.objectweb.medor.expression.api.Operator;
30 import org.objectweb.medor.expression.api.TypingException;
31 import org.objectweb.medor.expression.lib.BasicOperand;
32 import org.objectweb.medor.filter.api.FieldOperand;
33 import org.objectweb.medor.filter.jorm.lib.CompositePName;
34 import org.objectweb.medor.filter.jorm.lib.SinglePName;
35 import org.objectweb.medor.filter.lib.ExpressionPrinter;
36 import org.objectweb.medor.lib.TestMedorHelper;
37 import org.objectweb.util.monolog.api.BasicLevel;
38 import org.objectweb.util.monolog.api.Logger;
39
40 /*
41  * @author S.Chassande-Barrioz
42  */

43
44 public class TestExpressionHelper extends TestMedorHelper {
45
46     public TestExpressionHelper(String JavaDoc testName) {
47         super(testName);
48     };
49
50     public static void equals(String JavaDoc msg, Expression e1, Expression e2, Logger log)
51             throws MedorException {
52         log.log(BasicLevel.DEBUG, "Comparing expression "
53             + ExpressionPrinter.e2str(e1) + " to "
54             + ExpressionPrinter.e2str(e2));
55         if (e1==null && e2==null)
56             return;
57         if (e1==null)
58             fail(msg + " The e1 is null and e2=" + ExpressionPrinter.e2str(e2));
59         if (e2==null)
60             fail(msg + " The e2 is null and e1=" + ExpressionPrinter.e2str(e1));
61         log.log(BasicLevel.DEBUG, e1.getClass().getName());
62         if (e1.getClass() != e2.getClass()) {
63             fail(msg + "Not same class: \n"
64                 + "e1=" + e1.getClass().getName() + ": "
65                 + ExpressionPrinter.e2str(e1) + "\n"
66                 + "e2=" + e2.getClass().getName() + ": "
67                 + ExpressionPrinter.e2str(e2));
68         }
69         if (e1 instanceof CompositePName || e1 instanceof SinglePName) {
70             //Nothing
71
}
72         else if (e1 instanceof Operator) {
73             for(int i=0; i<((Operator) e1).getOperandNumber(); i++) {
74                 equals(msg + " " + ((Operator)e1).getOperatorString() + "." + i,
75                 ((Operator) e1).getExpression(i),
76                 ((Operator) e2).getExpression(i),
77                     log);
78             }
79         }
80         else if (e1 instanceof FieldOperand) {
81             Field f1 = ((FieldOperand) e1).getField();
82             Field f2 = ((FieldOperand) e2).getField();
83             assertEquals(msg + " FieldOperand does not have the same name",
84                 f1.getName(), f2.getName());
85             log.log(BasicLevel.DEBUG, msg +" FieldOperand " + f1.getName());
86             log.log(BasicLevel.DEBUG, msg +" FieldOperand f1.type=" + f1.getType());
87             log.log(BasicLevel.DEBUG, msg +" FieldOperand f2.type=" + f2.getType());
88             assertEquals(msg + " FieldOperand (" + f1.getName() + ", "
89                 + f2.getName()
90                 + ") does not have the same type",
91                 f1.getType().getTypeCode(), f2.getType().getTypeCode());
92         }
93         else if (e1 instanceof BasicOperand) {
94             BasicOperand bo1 = (BasicOperand) e1;
95             BasicOperand bo2 = (BasicOperand) e2;
96             assertEquals(msg + " Basic operand does not have the same type",
97                 bo1.getType().getTypeCode(),bo2.getType().getTypeCode());
98             log.log(BasicLevel.DEBUG, "type=" + bo1.getType().getJavaName());
99             try {
100                 switch(bo1.getType().getTypeCode()) {
101                 case PType.TYPECODE_BOOLEAN:
102                     assertEquals(msg + " The operands do not have the same value",
103                         bo1.getBoolean(), bo2.getBoolean());
104                     break;
105                 case PType.TYPECODE_CHAR:
106                     assertEquals(msg + " The operands do not have the same value",
107                         bo1.getChar(), bo2.getChar());
108                     break;
109                 case PType.TYPECODE_BYTE:
110                     assertEquals(msg + " The operands do not have the same value",
111                         bo1.getByte(), bo2.getByte());
112                     break;
113                 case PType.TYPECODE_SHORT:
114                     assertEquals(msg + " The operands do not have the same value",
115                         bo1.getShort(), bo2.getShort());
116                     break;
117                 case PType.TYPECODE_INT:
118                     assertEquals(msg + " The operands do not have the same value",
119                         bo1.getInt(), bo2.getInt());
120                     break;
121                 case PType.TYPECODE_LONG:
122                     assertEquals(msg + " The operands do not have the same value",
123                         bo1.getLong(), bo2.getLong());
124                     break;
125                 case PType.TYPECODE_STRING:
126                     assertEquals(msg + " The operands do not have the same value",
127                         bo1.getString(), bo2.getString());
128                     break;
129                 case PType.TYPECODE_DATE:
130                     assertEquals(msg + " The operands do not have the same value",
131                         bo1.getDate(), bo2.getDate());
132                     break;
133                 case PType.TYPECODE_FLOAT:
134                 case PType.TYPECODE_DOUBLE:
135                     //Uncomparable value
136
}
137             }
138             catch (TypingException e) {
139                 throw new MedorException(e);
140             }
141         }
142         else
143             throw new MedorException("Expression unknown: " + e1);
144     }
145 }
146
Popular Tags