KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > clone > TestExpressionCloning


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

18 package org.objectweb.medor.clone;
19
20 import org.objectweb.medor.filter.TestExpressionHelper;
21 import org.objectweb.medor.clone.lib.BasicCloneable;
22 import org.objectweb.medor.expression.api.*;
23 import org.objectweb.medor.expression.lib.*;
24
25 import org.objectweb.jorm.type.api.PTypeSpace;
26
27 /**
28  *
29  * @author S.Chassande-Barrioz
30  */

31 public class TestExpressionCloning extends TestExpressionHelper {
32
33     public TestExpressionCloning(String JavaDoc n) {
34         super(n);
35     }
36
37     private Object JavaDoc checkClone(Expression obj, boolean withContext) throws Exception JavaDoc {
38         Object JavaDoc clone = (withContext
39             ? ((BasicCloneable) obj).clone()
40             : ((BasicCloneable) obj).clone(null));
41         assertNotNull("Null clone", clone);
42         assertEquals("Bad class", obj.getClass(), clone.getClass());
43         assertEquals("Bad type", obj.getType(), ((Expression) clone).getType());
44         return clone;
45     }
46
47     public void testOperandWithoutContext() throws Exception JavaDoc {
48         testOperand(false);
49     }
50
51     public void testOperandWithContext() throws Exception JavaDoc {
52         testOperand(true);
53     }
54
55     public void testOperand(boolean withContext) throws Exception JavaDoc {
56         BasicOperand bo;
57         Object JavaDoc clone;
58
59         //BasicOperand
60
bo = new BasicOperand(true);
61         clone = checkClone(bo, withContext);
62         assertEquals("Bad operand value", bo.getBoolean(), ((BasicOperand) clone).getBoolean());
63
64         bo = new BasicOperand(12);
65         clone = checkClone(bo, withContext);
66         assertEquals("Bad operand value", bo.getInt(), ((BasicOperand) clone).getInt());
67
68         bo = new BasicOperand("azerty");
69         clone = checkClone(bo, withContext);
70         assertEquals("Bad operand value", bo.getString(), ((BasicOperand) clone).getString());
71
72         //BasicVariableOperand
73
bo = new BasicVariableOperand(true);
74         clone = checkClone(bo, withContext);
75         assertEquals("Bad operand value", bo.getBoolean(), ((BasicOperand) clone).getBoolean());
76
77         bo = new BasicVariableOperand(12);
78         clone = checkClone(bo, withContext);
79         assertEquals("Bad operand value", bo.getInt(), ((BasicOperand) clone).getInt());
80
81         bo = new BasicVariableOperand("azerty");
82         clone = checkClone(bo, withContext);
83         assertEquals("Bad operand value", bo.getString(), ((BasicOperand) clone).getString());
84
85         //BasicParameterOperand
86
bo = new BasicParameterOperand(PTypeSpace.BOOLEAN, "p1", true);
87         clone = checkClone(bo, withContext);
88         assertEquals("Bad operand value", bo.getBoolean(), ((BasicOperand) clone).getBoolean());
89
90         bo = new BasicParameterOperand(PTypeSpace.INT, "p2", 12);
91         clone = checkClone(bo, withContext);
92         assertEquals("Bad operand value", bo.getInt(), ((BasicOperand) clone).getInt());
93
94         bo = new BasicParameterOperand(PTypeSpace.STRING, "p3", "azerty");
95         clone = checkClone(bo, withContext);
96         assertEquals("Bad operand value", bo.getString(), ((BasicOperand) clone).getString());
97     }
98
99     public void testLogicalOperatorwithoutContext() throws Exception JavaDoc {
100         testLogicalOperator(false);
101     }
102     public void testLogicalOperatorwithContext() throws Exception JavaDoc {
103         testLogicalOperator(true);
104     }
105     public void testLogicalOperator(boolean withContext) throws Exception JavaDoc {
106         Object JavaDoc clone;
107         Expression e = new And(
108             new Not(new BasicOperand(true)),
109             new Or(
110                 new Equal(new BasicOperand(12), new BasicOperand(13)),
111                 new Greater(
112                     new BasicParameterOperand(PTypeSpace.BOOLEAN, "p1", true),
113                     new Length(
114                         new Concat(
115                             new StringLower(new BasicOperand("TOTO")),
116                             new Like(
117                                 new BasicOperand("hjklm"),
118                                 new BasicOperand("M*"),
119                                 new BasicOperand("k")
120                             )
121                         )
122                     )
123                 )
124             )
125         );
126         clone = checkClone(e, withContext);
127         equals("testLogicalOperator(" + withContext +"):",
128             e, (Expression) clone, logger);
129     }
130
131     public void testArithmeticOperatorwithoutContext() throws Exception JavaDoc {
132         testLogicalOperator(false);
133     }
134     public void testArithmeticOperatorwithContext() throws Exception JavaDoc {
135         testLogicalOperator(true);
136     }
137     public void testArithmeticOperator(boolean withContext) throws Exception JavaDoc {
138         Object JavaDoc clone;
139         Expression e = new Mult(
140                 new Mod(
141                     new BasicOperand(24),
142                     new BasicOperand(24)
143                 ),
144                 new DivideBy(
145                     new Plus(new Abs(new BasicOperand(-12)), new BasicOperand(13)),
146                     new BasicParameterOperand(PTypeSpace.INT, "p1", 45)
147                 )
148         );
149         clone = checkClone(e, withContext);
150         equals("testArithmeticOperator(" + withContext +"):",
151             e, (Expression) clone, logger);
152     }
153 }
154
Popular Tags