KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > FunctionsTest


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge;
12
13 import java.util.*;
14 import org.mmbase.util.*;
15 import org.mmbase.util.functions.*;
16 import org.mmbase.tests.*;
17 import org.mmbase.bridge.util.CollectionNodeList;
18
19 /**
20  *
21  * @author Simon Groenewolt (simon@submarine.nl)
22  * @author Michiel Meeuwissen
23  * @since $Id: FunctionsTest.java,v 1.10 2006/01/14 19:12:08 michiel Exp $
24  * @since MMBase-1.8
25  */

26 public class FunctionsTest extends BridgeTest {
27
28
29     public FunctionsTest(String JavaDoc name) {
30         super(name);
31     }
32
33     public void testPatternFunction() {
34         Cloud cloud = getCloud();
35         NodeManager nm = cloud.getNodeManager("datatypes");
36         Node node = nm.createNode();
37         node.commit();
38         assertTrue(node.getFunctionValue("test", null).toString().equals("[" + node.getNumber() + "]"));
39     }
40
41     public void testNodeManagerFunction() {
42         Cloud cloud = getCloud();
43         NodeManager nm = cloud.getNodeManager("datatypes");
44         Function function = nm.getFunction("aaa");
45         Parameters params = function.createParameters();
46         params.set("parameter2", new Integer JavaDoc(5));
47         Object JavaDoc result = function.getFunctionValue(params);
48         assertTrue("No instance of Integer but " + result.getClass(), result instanceof Integer JavaDoc);
49         Integer JavaDoc i = (Integer JavaDoc) result;
50         assertTrue(i.intValue() == 15);
51         // can also be called on a node.
52
Node node = nm.createNode();
53         node.commit();
54         assertTrue(node.getFunctionValue("aaa", params).toInt() == 15);
55     }
56
57     public void testNodeManagerFunction2() {
58         Cloud cloud = getCloud();
59         NodeManager nm = cloud.getNodeManager("news");
60         Node newsNode = nm.createNode();
61         newsNode.commit();
62         Function function = nm.getFunction("latest");
63         Parameters params = function.createParameters();
64         // remote clouds are not serializable. It is not needed anyway. Cloud parameters are
65
// implicit, when using bridge.
66
// params.set(Parameter.CLOUD, cloud);
67
params.set("max", new Integer JavaDoc(1));
68         NodeList nl = (NodeList) function.getFunctionValue(params);
69         assertTrue(nl.getNode(0).getNumber() == newsNode.getNumber());
70     }
71
72     public void testNodeManagerFunction3() {
73         Cloud cloud = getCloud();
74         NodeManager nm = cloud.getNodeManager("mmservers");
75         Function function = nm.getFunction("uptime");
76         Parameters params = function.createParameters();
77         Long JavaDoc value = (Long JavaDoc) function.getFunctionValue(params);
78         assertTrue(value.longValue() >= 0);
79     }
80
81     public void testNodeFunctionWithNodeResult() {
82         Cloud cloud = getCloud();
83         NodeManager nm = cloud.getNodeManager("datatypes");
84         Node node1 = nm.createNode();
85         node1.commit();
86         Node node2 = nm.createNode();
87         node2.commit();
88         Node successorOfNode1 = node1.getFunctionValue("successor", null).toNode();
89         assertTrue(successorOfNode1.equals(node2));
90
91     }
92
93     public void testNodeFunctionWithNodeResult1() {
94         Cloud cloud = getCloud();
95         NodeManager nm = cloud.getNodeManager("datatypes");
96         Node node1 = nm.createNode();
97         node1.commit();
98         Function function = node1.getFunction("nodeFunction1");
99         Parameters params = function.createParameters();
100         params.set("parameter1", "hoi");
101         Node n = node1.getFunctionValue("nodeFunction1", params).toNode();
102         assertTrue(n.getStringValue("bloe").equals("hoi"));
103         n = (Node) function.getFunctionValue(params);
104         assertTrue(n.getStringValue("bloe").equals("hoi"));
105     }
106
107     public void testNodeFunctionWithNodeResult2() {
108         Cloud cloud = getCloud();
109         NodeManager nm = cloud.getNodeManager("datatypes");
110         Node node1 = nm.createNode();
111         node1.commit();
112         Function function = node1.getFunction("nodeFunction2");
113         Parameters params = function.createParameters();
114         params.set("parameter1", "hoi");
115         Node n = node1.getFunctionValue("nodeFunction2", params).toNode();
116         assertTrue(n.getStringValue("bloe").equals("hoi"));
117         n = (Node) function.getFunctionValue(params);
118         assertTrue(n.getStringValue("bloe").equals("hoi"));
119     }
120
121     public void testNodeFunctionWithNodeListResult() {
122         Cloud cloud = getCloud();
123         NodeManager nm = cloud.getNodeManager("datatypes");
124         Node node1 = nm.createNode();
125         node1.commit();
126         Function function = node1.getFunction("nodeListFunction");
127         Parameters params = function.createParameters();
128         params.set("parameter1", "hoi");
129         NodeList nl = new CollectionNodeList((Collection) node1.getFunctionValue("nodeListFunction", params).get(), cloud);
130         NodeIterator i = nl.nodeIterator();
131         while (i.hasNext()) {
132             Node n = i.nextNode();
133             assertTrue(n.getStringValue("bloe").equals("hoi"));
134         }
135         nl = new CollectionNodeList((Collection) function.getFunctionValue(params), cloud);
136         i = nl.nodeIterator();
137         while (i.hasNext()) {
138             Node n = i.nextNode();
139             assertTrue(n.getStringValue("bloe").equals("hoi"));
140         }
141     }
142     public void testNodeFunctionWithNodeListResult1() {
143         Cloud cloud = getCloud();
144         NodeManager nm = cloud.getNodeManager("datatypes");
145         Node node1 = nm.createNode();
146         node1.commit();
147         Function function = node1.getFunction("nodeListFunction1");
148         Parameters params = function.createParameters();
149         params.set("parameter1", "hoi");
150         NodeList nl = (NodeList) node1.getFunctionValue("nodeListFunction1", params).get();
151         NodeIterator i = nl.nodeIterator();
152         while (i.hasNext()) {
153             Node n = i.nextNode();
154             assertTrue(n.getStringValue("bloe").equals("hoi"));
155         }
156         nl = (NodeList) function.getFunctionValue(params);
157         i = nl.nodeIterator();
158         while (i.hasNext()) {
159             Node n = i.nextNode();
160             assertTrue(n.getStringValue("bloe").equals("hoi"));
161         }
162     }
163
164     /**
165      * test a variety of functionset possibilities
166      * XXX really not complete yet
167      */

168     public void testFunctionSets() {
169
170         Function testfunc1 = FunctionSets.getFunction("utils", "randomLong");
171         assertTrue("function 'randomLong' not found (functionset 'utils')", testfunc1 != null);
172         // long randomLong = testfunc1.getFunctionValue(null);
173
Function testfunc2 = FunctionSets.getFunction("testfunctions", "testBoolean");
174         assertTrue("function 'testBoolean' not found (functionset 'testfunctions')", testfunc2 != null);
175
176         Parameters params2 = testfunc2.createParameters();
177         params2.set("inBoolean", Boolean.valueOf(true));
178
179
180         Object JavaDoc result = testfunc2.getFunctionValue(params2);
181         assertTrue("Expected return value of type 'Boolean', but got: '" + result + "'", result instanceof java.lang.Boolean JavaDoc);
182         assertTrue("function 'testBoolean' didn't return true as was expected", ((Boolean JavaDoc) result).booleanValue());
183     }
184 }
185
Popular Tags