001: /*
002: * IzPack - Copyright 2001-2006 Julien Ponge, All Rights Reserved.
003: *
004: * http://www.izforge.com/izpack/ http://izpack.codehaus.org/
005: *
006: * Copyright 2007 Dennis Reil
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
009: * in compliance with the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software distributed under the License
014: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
015: * or implied. See the License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package com.izforge.izpack.installer;
019:
020: import junit.framework.TestCase;
021: import net.n3.nanoxml.XMLElement;
022:
023: import com.izforge.izpack.rules.RulesEngine;
024:
025: /**
026: * @author Dennis Reil, <Dennis.Reil@reddot.de>
027: *
028: */
029: public class ConditionTest extends TestCase {
030:
031: public final static String RDE_VCS_REVISION = "$Revision: $";
032:
033: public final static String RDE_VCS_NAME = "$Name: $";
034:
035: protected static InstallData idata = new InstallData();
036:
037: protected RulesEngine rules;
038:
039: /**
040: * @param arg0
041: */
042: public ConditionTest(String arg0) {
043: super (arg0);
044: }
045:
046: /* (non-Javadoc)
047: * @see junit.framework.TestCase#setUp()
048: */
049: protected void setUp() throws Exception {
050: super .setUp();
051: XMLElement conditionspec = new XMLElement();
052: conditionspec.setName("conditions");
053:
054: conditionspec.addChild(this .createVariableCondition(
055: "test.true", "TEST", "true"));
056: conditionspec.addChild(this .createRefCondition("test.true2",
057: "test.true"));
058: //conditionspec.addChild(createNotCondition("test.not.true", createVariableCondition("test.true", "TEST", "true")));
059: conditionspec.addChild(createNotCondition("test.not.true",
060: createRefCondition("", "test.true")));
061: rules = new RulesEngine(conditionspec, idata);
062: }
063:
064: /* (non-Javadoc)
065: * @see junit.framework.TestCase#tearDown()
066: */
067: protected void tearDown() throws Exception {
068: super .tearDown();
069: if (idata != null) {
070: idata.variables.clear();
071: }
072: }
073:
074: protected XMLElement createNotCondition(String id,
075: XMLElement condition) {
076: XMLElement not = new XMLElement();
077: not.setName("condition");
078: not.setAttribute("type", "not");
079: not.setAttribute("id", id);
080: not.addChild(condition);
081:
082: return not;
083: }
084:
085: protected XMLElement createVariableCondition(String id,
086: String variable, String expvalue) {
087: XMLElement variablecondition = new XMLElement();
088: variablecondition.setName("condition");
089: variablecondition.setAttribute("type", "variable");
090: variablecondition.setAttribute("id", id);
091:
092: XMLElement name = new XMLElement();
093: name.setName("name");
094: name.setContent(variable);
095:
096: XMLElement value = new XMLElement();
097: value.setName("value");
098: value.setContent(expvalue);
099:
100: variablecondition.addChild(name);
101: variablecondition.addChild(value);
102:
103: return variablecondition;
104: }
105:
106: protected XMLElement createRefCondition(String id, String refid) {
107: XMLElement refcondition = new XMLElement();
108: refcondition.setName("condition");
109: refcondition.setAttribute("type", "ref");
110: refcondition.setAttribute("refid", refid);
111: refcondition.setAttribute("id", id);
112:
113: return refcondition;
114: }
115:
116: public void testNotCondition() {
117: assertNull(RulesEngine.getCondition("test.not"));
118: assertNotNull(RulesEngine.getCondition("test.not.true"));
119: assertTrue(rules.isConditionTrue("test.not.true",
120: idata.variables));
121:
122: assertNotNull(RulesEngine.getCondition("!test.not.true"));
123:
124: assertFalse(rules.isConditionTrue("!test.not.true",
125: idata.variables));
126: }
127:
128: public void testVariableCondition() {
129:
130: assertNotNull(RulesEngine.getCondition("test.true"));
131: assertNotNull(RulesEngine.getCondition("test.true2"));
132:
133: assertFalse(rules.isConditionTrue("test.true", idata.variables));
134: assertFalse(rules
135: .isConditionTrue("test.true2", idata.variables));
136:
137: idata.setVariable("TEST", "true");
138:
139: assertTrue(rules.isConditionTrue("test.true", idata.variables));
140: assertTrue(rules.isConditionTrue("test.true2", idata.variables));
141:
142: assertFalse(rules
143: .isConditionTrue("!test.true", idata.variables));
144: assertFalse(rules.isConditionTrue("!test.true2",
145: idata.variables));
146:
147: assertTrue(rules.isConditionTrue("test.true+test.true2",
148: idata.variables));
149: assertTrue(rules.isConditionTrue("test.true2+test.true",
150: idata.variables));
151:
152: assertFalse(rules.isConditionTrue("!test.true2+test.true",
153: idata.variables));
154:
155: assertTrue(rules.isConditionTrue("test.true2|test.true",
156: idata.variables));
157:
158: assertFalse(rules.isConditionTrue("test.true2\\test.true",
159: idata.variables));
160: }
161: }
|