KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > expression > 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.expression;
24
25 import org.objectweb.jorm.type.api.PType;
26 import org.objectweb.medor.expression.api.Expression;
27 import org.objectweb.medor.expression.api.ExpressionException;
28 import org.objectweb.medor.expression.api.Operator;
29 import org.objectweb.medor.expression.api.TypingException;
30 import org.objectweb.medor.expression.lib.BasicOperand;
31 import org.objectweb.medor.expression.lib.ExpressionPrinter;
32 import org.objectweb.util.monolog.api.BasicLevel;
33 import org.objectweb.util.monolog.api.Logger;
34
35 /*
36  * @author S.Chassande-Barrioz
37  */

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