001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.embedded;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.EJBLookup;
037: import com.flexive.shared.FxLanguage;
038: import com.flexive.shared.content.FxContent;
039: import com.flexive.shared.content.FxPK;
040: import com.flexive.shared.exceptions.FxApplicationException;
041: import com.flexive.shared.interfaces.ContentEngine;
042: import com.flexive.shared.interfaces.ScriptingEngine;
043: import com.flexive.shared.scripting.FxScriptBinding;
044: import com.flexive.shared.scripting.FxScriptEvent;
045: import com.flexive.shared.scripting.FxScriptInfo;
046: import com.flexive.shared.scripting.FxScriptInfoEdit;
047: import com.flexive.shared.security.ACL;
048: import com.flexive.shared.structure.*;
049: import com.flexive.shared.value.FxString;
050: import static com.flexive.tests.embedded.FxTestUtils.login;
051: import static com.flexive.tests.embedded.FxTestUtils.logout;
052: import org.testng.Assert;
053: import org.testng.annotations.AfterClass;
054: import org.testng.annotations.BeforeClass;
055: import org.testng.annotations.BeforeSuite;
056: import org.testng.annotations.Test;
057:
058: import java.util.ArrayList;
059: import java.util.List;
060:
061: /**
062: * Tests for the scripting engine (grooooooovy)
063: *
064: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
065: */
066: @Test(groups={"ejb","scripting"})
067: public class ScriptingTest {
068: public static boolean allowTearDown = false;
069:
070: ScriptingEngine se;
071: protected static FxScriptInfo loadScript = null;
072: protected static FxScriptInfo removeScript = null;
073:
074: @BeforeSuite(dependsOnGroups={"bootstrap"})
075: public void suiteSetup() throws FxApplicationException {
076: String codeLoad = "println \"[Groovy script]=== Loading Content \"+content.pk+\"(\"+environment.getType(content.getTypeId()).getName()+\") ===\"";
077: String codeRemove = "println \"[Groovy script]=== Before removal of content \"+pk+\"(\"+environment.getType(securityInfo.getTypeId()).getName()+\") ===\"";
078: loadScript = EJBLookup.getScriptingEngine().createScript(
079: FxScriptEvent.AfterContentLoad, "afterLoadTest.gy",
080: "Test script", codeLoad);
081: removeScript = EJBLookup.getScriptingEngine().createScript(
082: FxScriptEvent.BeforeContentRemove,
083: "beforeRemoveTest.gy", "Test script", codeRemove);
084: }
085:
086: public static void suiteShutDown() throws Exception {
087: if (!allowTearDown) //"hack" to prevent this method to be run as normal test
088: return;
089: login(TestUsers.SUPERVISOR);
090: if (loadScript != null)
091: EJBLookup.getScriptingEngine().removeScript(
092: loadScript.getId());
093: if (removeScript != null)
094: EJBLookup.getScriptingEngine().removeScript(
095: removeScript.getId());
096: logout();
097: }
098:
099: @BeforeClass
100: public void setup() throws Exception {
101: se = EJBLookup.getScriptingEngine();
102: login(TestUsers.SUPERVISOR);
103: }
104:
105: @AfterClass
106: public void shutdown() throws Exception {
107: logout();
108: }
109:
110: @Test
111: public void simpleScript() throws Exception {
112: String name = "unittest.gy";
113: String desc = "unit test script";
114: String code = "return \"hello world\";";
115: Object result = "hello world";
116:
117: FxScriptInfo si = se.createScript(FxScriptEvent.Manual, name,
118: desc, code);
119: assert si != null : "No FxScriptInfo returned!";
120: assert si.getId() > 0 : "Invalid script id";
121: assert si.getName().equals(name) : "Invalid name";
122: assert si.getDescription().equals(desc) : "Invalid description";
123: assert si.getEvent().equals(FxScriptEvent.Manual) : "Invalid script type";
124: assert se.runScript(si.getId(), new FxScriptBinding())
125: .getResult().equals(result) : "Invalid result from script!";
126: se.removeScript(si.getId());
127: }
128:
129: @Test
130: public void scriptActivation() throws Exception {
131: FxScriptInfo si = se.createScript(FxScriptEvent.Manual,
132: "manualTestScript.gy", "manualTestScript",
133: "return \"done\";");
134: try {
135: se.runScript(si.getId());
136: FxScriptInfoEdit inactive = si.asEditable();
137: inactive.setActive(false);
138: se.updateScriptInfo(inactive);
139: Assert.assertEquals(se.runScript(si.getId()).getResult(),
140: null, "Inactive scripts must not be runnable!");
141: } finally {
142: se.removeScript(si.getId());
143: }
144: }
145:
146: @Test
147: public void scriptAssignmentMapping() throws Exception {
148: long typeId = EJBLookup.getTypeEngine().save(
149: FxTypeEdit.createNew("AS_SCRIPTING", new FxString(
150: "Assignment scripting test type"), CacheAdmin
151: .getEnvironment().getACL(
152: ACL.Category.STRUCTURE.getDefaultId()),
153: null));
154: EJBLookup.getAssignmentEngine()
155: .createProperty(
156: typeId,
157: FxPropertyEdit.createNew(
158: "A",
159: new FxString(true, FxLanguage.ENGLISH,
160: "A"),
161: new FxString(true, FxLanguage.ENGLISH,
162: "A"),
163: new FxMultiplicity(0, 5),
164: CacheAdmin.getEnvironment().getACL(
165: ACL.Category.STRUCTURE
166: .getDefaultId()),
167: FxDataType.String1024).setMultiLang(
168: false), "/");
169: EJBLookup.getAssignmentEngine()
170: .createProperty(
171: typeId,
172: FxPropertyEdit.createNew(
173: "B",
174: new FxString(true, FxLanguage.ENGLISH,
175: "B"),
176: new FxString(true, FxLanguage.ENGLISH,
177: "B"),
178: new FxMultiplicity(0, 5),
179: CacheAdmin.getEnvironment().getACL(
180: ACL.Category.STRUCTURE
181: .getDefaultId()),
182: FxDataType.String1024).setMultiLang(
183: false), "/");
184: FxType type = CacheAdmin.getEnvironment().getType(typeId);
185: List<FxScriptInfo> scripts = new ArrayList<FxScriptInfo>(10);
186: try {
187: scripts.add(createScript(type.getAssignment("/A"),
188: FxScriptEvent.BeforeAssignmentDataCreate));
189: scripts.add(createScript(type.getAssignment("/A"),
190: FxScriptEvent.AfterAssignmentDataCreate));
191: scripts.add(createScript(type.getAssignment("/A"),
192: FxScriptEvent.BeforeAssignmentDataDelete));
193: scripts.add(createScript(type.getAssignment("/A"),
194: FxScriptEvent.AfterAssignmentDataDelete));
195: scripts.add(createScript(type.getAssignment("/B"),
196: FxScriptEvent.BeforeDataChangeAdd));
197: scripts.add(createScript(type.getAssignment("/B"),
198: FxScriptEvent.BeforeDataChangeUpdate));
199: scripts.add(createScript(type.getAssignment("/B"),
200: FxScriptEvent.BeforeDataChangeDelete));
201: scripts.add(createScript(type.getAssignment("/B"),
202: FxScriptEvent.AfterDataChangeAdd));
203: scripts.add(createScript(type.getAssignment("/B"),
204: FxScriptEvent.AfterDataChangeUpdate));
205: scripts.add(createScript(type.getAssignment("/B"),
206: FxScriptEvent.AfterDataChangeDelete));
207:
208: FxString data = new FxString(false, "data");
209: FxString data2 = new FxString(false, "data2");
210: ContentEngine ce = EJBLookup.getContentEngine();
211: FxContent co = ce.initialize(typeId);
212: FxPK pk = ce.save(co);
213: co = ce.load(pk);
214: co.setValue("/B[1]", data);
215: System.out.println("=>add B[1]");
216: ce.save(co);
217: co = ce.load(pk);
218: FxString tmp = (FxString) co.getValue("/B[1]");
219: tmp.setTranslation(FxLanguage.GERMAN, "data de");
220: System.out.println("=>update B[1]");
221: ce.save(co);
222: co = ce.load(pk);
223: co.setValue("/B[2]", data2);
224: System.out.println("=>add B[2]");
225: ce.save(co);
226: co = ce.load(pk);
227: co.remove("/B[2]");
228: System.out.println("=>delete B[2]");
229: ce.save(co);
230: EJBLookup.getContentEngine().remove(pk);
231: } finally {
232: EJBLookup.getTypeEngine().remove(typeId);
233: for (FxScriptInfo si : scripts)
234: se.removeScript(si.getId());
235: }
236:
237: }
238:
239: private FxScriptInfo createScript(FxAssignment asssignment,
240: FxScriptEvent event) throws FxApplicationException {
241: String data = "unknown";
242: if (event.name().startsWith("BeforeAssignment"))
243: data = "assignment";
244: else if (event.name().startsWith("AfterAssignment"))
245: data = "assignment";
246: else if (event.name().startsWith("BeforeDataChange"))
247: data = "change";
248: else if (event.name().startsWith("AfterDataChange"))
249: data = "change";
250: FxScriptInfo si = se.createScript(event, "Test" + event.name()
251: + ".gy", "", "println \"This is [" + event.name()
252: + "] registered for Assignment ["
253: + asssignment.getXPath() + "] - I am called for ${"
254: + data + "}\"");
255: se.createAssignmentScriptMapping(si.getId(), asssignment
256: .getId(), true, true);
257: return si;
258: }
259: }
|