001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.scxml2.env.javascript;
019
020import java.util.AbstractMap;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.commons.scxml2.Context;
025import org.junit.Assert;
026import org.junit.Before;
027import org.junit.Test;
028
029import javax.script.Bindings;
030import javax.script.SimpleBindings;
031
032/**
033 * JUnit 3 test case for the JSBinding implementation that imports
034 * SCXML context variables into a JavaScript bindings. Includes tests
035 * for:
036 * <ul>
037 * <li> constructor
038 * </ul>
039 */
040public class JSBindingsTest {
041    // TEST CONSTANTS
042
043    private static final Map.Entry<String,Object> KOALA   = new AbstractMap.SimpleEntry<String,Object>("bear","koala");
044    private static final Map.Entry<String,Object> GRIZZLY = new AbstractMap.SimpleEntry<String,Object>("bear","grizzly");
045    private static final Map.Entry<String,Object> FELIX   = new AbstractMap.SimpleEntry<String,Object>("cat", "felix");
046    private static final Map.Entry<String,Object> ROVER   = new AbstractMap.SimpleEntry<String,Object>("dog", "rover");
047
048    // TEST VARIABLES
049
050    // TEST SETUP
051
052    /**
053     * Creates and initializes an SCXML data model in the context.
054     */
055    @Before
056    public void setUp() throws Exception {
057    }
058
059    // CLASS METHODS
060
061    /**
062     * Stand-alone test runtime.
063     */
064    public static void main(String args[]) {
065        String[] testCaseName = {JSBindingsTest.class.getName()};
066        junit.textui.TestRunner.main(testCaseName);
067    }
068
069    // INSTANCE METHOD TESTS
070
071    /**
072     * Tests implementation of JSBindings constructor.
073     */    
074    @Test
075    public void testConstructor() {
076        Assert.assertNotNull(new JSBindings(new JSContext(),new SimpleBindings()));
077    }
078
079    /**
080     * Test implementation of JSBindings constructor with invalid SCXML context.
081     */    
082    @Test
083    public void testInvalidContextConstructor() {
084        try {
085             Assert.assertNotNull(new JSBindings(null,new SimpleBindings()));
086             Assert.fail("JSBindings constructor accepted invalid SCXML context");
087
088        } catch (IllegalArgumentException x) {
089             // expected, ignore
090        }
091    }
092
093    /**
094     * Test implementation of JSBindings constructor with invalid Javascript bindings.
095     */    
096    @Test
097    public void testInvalidBindingsConstructor() {
098        try {
099             Assert.assertNotNull(new JSBindings(new JSContext(),null));
100             Assert.fail("JSBindings constructor accepted invalid Javascript bindings");
101
102        } catch (IllegalArgumentException x) {
103             // expected, ignore
104        }
105    }
106
107    /**
108     * Tests the <code>containsKey</code> method with items in the SCXML context as well as the
109     * JavaScript Bindings.
110     *
111     */    
112    @Test
113    public void testContainsKey() {
114        Context    context  = new JSContext     ();
115        Bindings   bindings = new SimpleBindings();
116        JSBindings jsx      = new JSBindings    (context,bindings);
117
118        Assert.assertFalse("Invalid SCXML context",      context.has         ("bear"));
119        Assert.assertFalse("Invalid Javascript bindings",bindings.containsKey("bear"));
120        Assert.assertFalse("Invalid JSbindings",         jsx.containsKey     ("bear"));
121
122        context.set("bear","koala");
123        Assert.assertTrue ("Invalid SCXML context",      context.has         ("bear"));
124        Assert.assertFalse("Invalid Javascript bindings",bindings.containsKey("bear"));
125        Assert.assertTrue ("Invalid JSbindings",         jsx.containsKey     ("bear"));
126
127        context.reset();
128        bindings.put ("bear","grizzly");
129        Assert.assertFalse  ("Invalid SCXML context",      context.has         ("bear"));
130        Assert.assertTrue   ("Invalid Javascript bindings",bindings.containsKey("bear"));
131        Assert.assertTrue   ("Invalid JSbindings",         jsx.containsKey     ("bear"));
132
133        context.set ("bear","koala");
134        bindings.put("bear","grizzly");
135        Assert.assertTrue  ("Invalid SCXML context",      context.has         ("bear"));
136        Assert.assertTrue  ("Invalid Javascript bindings",bindings.containsKey("bear"));
137        Assert.assertTrue  ("Invalid JSbindings",         jsx.containsKey     ("bear"));
138    }
139
140    /**
141     * Tests the <code>keySet</code> method with items in the SCXML context as well as the
142     * JavaScript Bindings.
143     *
144     */    
145    @Test
146    public void testKeySet() {
147        Context    context  = new JSContext     ();
148        Bindings   bindings = new SimpleBindings();
149        JSBindings jsx      = new JSBindings    (context,bindings);
150
151        Assert.assertFalse ("Invalid SCXML context",      context.has          ("bear"));
152        Assert.assertFalse ("Invalid Javascript bindings",bindings.containsKey ("bear"));
153        Assert.assertFalse ("Invalid JSbindings",         jsx.keySet().contains("bear"));
154
155        context.set   ("bear","koala");
156        bindings.clear();
157        Assert.assertTrue    ("Invalid SCXML context",      context.has          ("bear"));
158        Assert.assertFalse   ("Invalid Javascript bindings",bindings.containsKey ("bear"));
159        Assert.assertTrue    ("Invalid JSbindings",         jsx.keySet().contains("bear"));
160
161        context.reset ();
162        bindings.clear();
163        bindings.put  ("bear","grizzly");
164        Assert.assertFalse   ("Invalid SCXML context",      context.has          ("bear"));
165        Assert.assertTrue    ("Invalid Javascript bindings",bindings.containsKey ("bear"));
166        Assert.assertTrue    ("Invalid JSbindings",         jsx.keySet().contains("bear"));
167
168        context.reset ();
169        bindings.clear();
170        context.set  ("cat","felix");
171        bindings.put ("dog","rover");
172        Assert.assertFalse  ("Invalid SCXML context",      context.has          ("bear"));
173        Assert.assertFalse  ("Invalid Javascript bindings",bindings.containsKey ("bear"));
174        Assert.assertTrue   ("Invalid SCXML context",      context.has          ("cat"));
175        Assert.assertTrue   ("Invalid Javascript bindings",bindings.containsKey ("dog"));
176        Assert.assertTrue   ("Invalid JSbindings",         jsx.keySet().contains("cat"));
177        Assert.assertTrue   ("Invalid JSbindings",         jsx.keySet().contains("dog"));
178    }
179
180    /**
181     * Tests the <code>size</code> method with items in the SCXML context as well as the
182     * JavaScript Bindings.
183     *
184     */
185   
186    @Test
187    public void testSize() {
188       Context    context  = new JSContext     ();
189       Bindings   bindings = new SimpleBindings();
190       JSBindings jsx      = new JSBindings    (context,bindings);
191
192       Assert.assertFalse ("Invalid SCXML context",      context.has          ("bear"));
193       Assert.assertFalse ("Invalid Javascript bindings",bindings.containsKey ("bear"));
194       Assert.assertEquals("Invalid JSbindings",0,jsx.size());
195
196       context.set   ("bear","koala");
197       bindings.clear();
198       Assert.assertTrue    ("Invalid SCXML context",      context.has          ("bear"));
199       Assert.assertFalse   ("Invalid Javascript bindings",bindings.containsKey ("bear"));
200       Assert.assertEquals  ("Invalid JSbindings",1,jsx.size());
201
202       context.reset ();
203       bindings.clear();
204       bindings.put  ("bear","grizzly");
205       Assert.assertFalse   ("Invalid SCXML context",      context.has          ("bear"));
206       Assert.assertTrue    ("Invalid Javascript bindings",bindings.containsKey ("bear"));
207       Assert.assertEquals  ("Invalid JSbindings",1,jsx.size());
208
209       context.reset ();
210       bindings.clear();
211       context.set   ("bear","koala");
212       bindings.put  ("bear","grizzly");
213       Assert.assertTrue    ("Invalid SCXML context",      context.has          ("bear"));
214       Assert.assertTrue    ("Invalid Javascript bindings",bindings.containsKey ("bear"));
215       Assert.assertEquals  ("Invalid JSbindings",1,jsx.size());
216
217       context.reset ();
218       bindings.clear();
219       context.set  ("cat","felix");
220       bindings.put ("dog","rover");
221       Assert.assertFalse  ("Invalid SCXML context",      context.has          ("bear"));
222       Assert.assertFalse  ("Invalid Javascript bindings",bindings.containsKey ("bear"));
223       Assert.assertTrue   ("Invalid SCXML context",      context.has          ("cat"));
224       Assert.assertTrue   ("Invalid Javascript bindings",bindings.containsKey ("dog"));
225       Assert.assertEquals ("Invalid JSbindings",2,jsx.size());
226    }
227
228    /**
229     * Tests the <code>containsValue</code> method with items in the SCXML context as well as the
230     * JavaScript Bindings.
231     *
232     */    
233    @Test
234    public void testContainsValue() {
235        Context    context  = new JSContext     ();
236        Bindings   bindings = new SimpleBindings();
237        JSBindings jsx      = new JSBindings    (context,bindings);
238
239        Assert.assertFalse("Invalid SCXML context",      context.getVars().containsValue("koala"));
240        Assert.assertFalse("Invalid Javascript bindings",bindings.containsValue("koala"));
241        Assert.assertFalse("Invalid JSbindings",         jsx.containsValue     ("koala"));
242
243        context.reset ();
244        bindings.clear();
245        context.set   ("bear","koala");
246        Assert.assertTrue    ("Invalid SCXML context",      context.getVars().containsValue("koala"));
247        Assert.assertFalse   ("Invalid Javascript bindings",bindings.containsValue("koala"));
248        Assert.assertTrue    ("Invalid JSbindings",         jsx.containsValue     ("koala"));
249
250        context.reset ();
251        bindings.clear();
252        bindings.put  ("bear","grizzly");
253        Assert.assertFalse   ("Invalid SCXML context",      context.getVars().containsValue("grizzly"));
254        Assert.assertTrue    ("Invalid Javascript bindings",bindings.containsValue("grizzly"));
255        Assert.assertTrue    ("Invalid JSbindings",         jsx.containsValue     ("grizzly"));
256
257        context.reset ();
258        bindings.clear();
259        context.set   ("bear","koala");
260        bindings.put  ("bear","grizzly");
261        Assert.assertTrue    ("Invalid SCXML context",      context.getVars().containsValue("koala"));
262        Assert.assertTrue    ("Invalid Javascript bindings",bindings.containsValue("grizzly"));
263        Assert.assertTrue    ("Invalid JSbindings",         jsx.containsValue     ("koala"));
264        Assert.assertTrue    ("Invalid JSbindings",         jsx.containsValue     ("grizzly"));
265    }
266
267    /**
268     * Tests the <code>entrySet</code> method with items in the SCXML context as well as the
269     * JavaScript Bindings.
270     *
271     */    
272    @Test
273    public void testEntrySet() {
274        Context    context  = new JSContext     ();
275        Bindings   bindings = new SimpleBindings();
276        JSBindings jsx      = new JSBindings    (context,bindings);
277
278        Assert.assertEquals("Invalid SCXML context",      0,context.getVars().entrySet().size());
279        Assert.assertEquals("Invalid Javascript bindings",0,bindings.entrySet().size());
280        Assert.assertEquals("Invalid JSbindings",         0,jsx.entrySet().size());
281
282        context.reset ();
283        bindings.clear();
284        context.set   ("bear","koala");
285        Assert.assertEquals  ("Invalid SCXML context",      1,context.getVars().entrySet().size());
286        Assert.assertTrue    ("Invalid SCXML context",      context.getVars().entrySet().contains(KOALA));
287        Assert.assertEquals  ("Invalid Javascript bindings",0,bindings.entrySet().size());
288        Assert.assertFalse   ("Invalid Javascript bindings",bindings.entrySet().contains(KOALA));
289        Assert.assertEquals  ("Invalid JSBindings",         1,jsx.entrySet().size());
290        Assert.assertTrue    ("Invalid JSbindings",         jsx.entrySet().contains(KOALA));
291
292        context.reset ();
293        bindings.clear();
294        bindings.put  ("bear","grizzly");
295        Assert.assertEquals  ("Invalid SCXML context",      0,context.getVars().entrySet().size());
296        Assert.assertFalse   ("Invalid SCXML context",      context.getVars().entrySet().contains(GRIZZLY));
297        Assert.assertEquals  ("Invalid Javascript bindings",1,bindings.entrySet().size());
298        Assert.assertTrue    ("Invalid Javascript bindings",bindings.entrySet().contains(GRIZZLY));
299        Assert.assertEquals  ("Invalid JSBindings",         1,jsx.entrySet().size());
300        Assert.assertTrue    ("Invalid JSbindings",         jsx.entrySet().contains(GRIZZLY));
301
302        context.reset ();
303        bindings.clear();
304        context.set   ("bear","koala");
305        bindings.put  ("bear","grizzly");
306        Assert.assertEquals  ("Invalid SCXML context",      1,context.getVars().entrySet().size());
307        Assert.assertTrue    ("Invalid SCXML context",      context.getVars().entrySet().contains(KOALA));
308        Assert.assertEquals  ("Invalid Javascript bindings",1,bindings.entrySet().size());
309        Assert.assertTrue    ("Invalid Javascript bindings",bindings.entrySet().contains(GRIZZLY));
310        Assert.assertEquals  ("Invalid JSBindings",         1,jsx.entrySet().size());
311        Assert.assertFalse   ("Invalid JSbindings",         jsx.entrySet().contains(KOALA));
312        Assert.assertTrue    ("Invalid JSbindings",         jsx.entrySet().contains(GRIZZLY));
313
314        context.reset ();
315        bindings.clear();
316        context.set   ("cat","felix");
317        bindings.put  ("dog","rover");
318        Assert.assertEquals  ("Invalid SCXML context",      1,context.getVars().entrySet().size());
319        Assert.assertTrue    ("Invalid SCXML context",      context.getVars().entrySet().contains(FELIX));
320        Assert.assertEquals  ("Invalid Javascript bindings",1,bindings.entrySet().size());
321        Assert.assertTrue    ("Invalid Javascript bindings",bindings.entrySet().contains(ROVER));
322        Assert.assertEquals  ("Invalid JSBindings",         2,jsx.entrySet().size());
323        Assert.assertTrue    ("Invalid JSbindings",         jsx.entrySet().contains(FELIX));
324        Assert.assertTrue    ("Invalid JSbindings",         jsx.entrySet().contains(ROVER));
325    }
326
327    /**
328     * Tests the <code>values</code> method with items in the SCXML context as well as the
329     * JavaScript Bindings.
330     *
331     */    
332    @Test
333    public void testValues() {
334        Context    context  = new JSContext     ();
335        Bindings   bindings = new SimpleBindings();
336        JSBindings jsx      = new JSBindings    (context,bindings);
337
338        Assert.assertEquals("Invalid SCXML context",      0,context.getVars().values().size());
339        Assert.assertEquals("Invalid Javascript bindings",0,bindings.values().size());
340        Assert.assertEquals("Invalid JSbindings",         0,jsx.values().size());
341
342        context.reset ();
343        bindings.clear();
344        context.set   ("bear","koala");
345        Assert.assertEquals  ("Invalid SCXML context",      1,context.getVars().values().size());
346        Assert.assertTrue    ("Invalid SCXML context",      context.getVars().values().contains(KOALA.getValue()));
347        Assert.assertEquals  ("Invalid Javascript bindings",0,bindings.values().size());
348        Assert.assertFalse   ("Invalid Javascript bindings",bindings.values().contains(KOALA.getValue()));
349        Assert.assertEquals  ("Invalid JSBindings",         1,jsx.values().size());
350        Assert.assertTrue    ("Invalid JSbindings",         jsx.values().contains(KOALA.getValue()));
351
352        context.reset ();
353        bindings.clear();
354        bindings.put  ("bear","grizzly");
355        Assert.assertEquals  ("Invalid SCXML context",      0,context.getVars().values().size());
356        Assert.assertFalse   ("Invalid SCXML context",      context.getVars().values().contains(GRIZZLY.getValue()));
357        Assert.assertEquals  ("Invalid Javascript bindings",1,bindings.values().size());
358        Assert.assertTrue    ("Invalid Javascript bindings",bindings.values().contains(GRIZZLY.getValue()));
359        Assert.assertEquals  ("Invalid JSBindings",         1,jsx.values().size());
360        Assert.assertTrue    ("Invalid JSbindings",         jsx.values().contains(GRIZZLY.getValue()));
361
362        context.reset ();
363        bindings.clear();
364        context.set   ("bear","koala");
365        bindings.put  ("bear","grizzly");
366        Assert.assertEquals  ("Invalid SCXML context",      1,context.getVars().values().size());
367        Assert.assertTrue    ("Invalid SCXML context",      context.getVars().values().contains(KOALA.getValue()));
368        Assert.assertEquals  ("Invalid Javascript bindings",1,bindings.values().size());
369        Assert.assertTrue    ("Invalid Javascript bindings",bindings.values().contains(GRIZZLY.getValue()));
370        Assert.assertEquals  ("Invalid JSBindings",         1,jsx.values().size());
371        Assert.assertFalse   ("Invalid JSbindings",         jsx.values().contains(KOALA.getValue()));
372        Assert.assertTrue    ("Invalid JSbindings",         jsx.values().contains(GRIZZLY.getValue()));
373
374        context.reset ();
375        bindings.clear();
376        context.set   ("cat","felix");
377        bindings.put  ("dog","rover");
378        Assert.assertEquals  ("Invalid SCXML context",      1,context.getVars().values().size());
379        Assert.assertTrue    ("Invalid SCXML context",      context.getVars().values().contains(FELIX.getValue()));
380        Assert.assertEquals  ("Invalid Javascript bindings",1,bindings.values().size());
381        Assert.assertTrue    ("Invalid Javascript bindings",bindings.values().contains(ROVER.getValue()));
382        Assert.assertEquals  ("Invalid JSBindings",         2,jsx.values().size());
383        Assert.assertTrue    ("Invalid JSbindings",         jsx.values().contains(FELIX.getValue()));
384        Assert.assertTrue    ("Invalid JSbindings",         jsx.values().contains(ROVER.getValue()));
385    }
386
387    /**
388     * Tests the <code>isEmpty</code> method with items in the SCXML context as well as the
389     * JavaScript Bindings.
390     *
391     */    
392    @Test
393    public void testIsEmpty() {
394        Context    context  = new JSContext     ();
395        Bindings   bindings = new SimpleBindings();
396        JSBindings jsx      = new JSBindings    (context,bindings);
397
398        Assert.assertTrue("Invalid SCXML context",      context.getVars().isEmpty());
399        Assert.assertTrue("Invalid Javascript bindings",bindings.isEmpty());
400        Assert.assertTrue("Invalid JSbindings",         jsx.isEmpty());
401
402        context.set   ("bear","koala");
403        bindings.clear();
404        Assert.assertFalse   ("Invalid SCXML context",      context.getVars().isEmpty());
405        Assert.assertTrue    ("Invalid Javascript bindings",bindings.isEmpty());
406        Assert.assertFalse   ("Invalid JSbindings",         jsx.isEmpty());
407
408        context.reset ();
409        bindings.clear();
410        bindings.put  ("bear","grizzly");
411        Assert.assertTrue    ("Invalid SCXML context",      context.getVars().isEmpty());
412        Assert.assertFalse   ("Invalid Javascript bindings",bindings.isEmpty());
413        Assert.assertFalse   ("Invalid JSbindings",         jsx.isEmpty());
414
415        context.reset ();
416        bindings.clear();
417        context.set   ("bear","koala");
418        bindings.put  ("bear","grizzly");
419        Assert.assertFalse   ("Invalid SCXML context",      context.getVars().isEmpty());
420        Assert.assertFalse   ("Invalid Javascript bindings",bindings.isEmpty());
421        Assert.assertFalse   ("Invalid JSbindings",         jsx.isEmpty());
422
423        context.reset ();
424        bindings.clear();
425        context.set   ("cat","felix");
426        bindings.put  ("dog","rover");
427        Assert.assertFalse   ("Invalid SCXML context",      context.getVars().isEmpty());
428        Assert.assertFalse   ("Invalid Javascript bindings",bindings.isEmpty());
429        Assert.assertFalse   ("Invalid JSbindings",         jsx.isEmpty());
430    }
431
432    /**
433     * Tests the <code>get</code> method with items in the SCXML context as well as the
434     * JavaScript Bindings.
435     *
436     */    
437    @Test
438    public void testGet() {
439        Context    context  = new JSContext     ();
440        Bindings   bindings = new SimpleBindings();
441        JSBindings jsx      = new JSBindings    (context,bindings);
442
443        Assert.assertNull("Invalid SCXML context",      context.get ("bear"));
444        Assert.assertNull("Invalid Javascript bindings",bindings.get("bear"));
445        Assert.assertNull("Invalid JSbindings",         jsx.get     ("bear"));
446
447        context.reset ();
448        bindings.clear();
449        context.set   ("bear","koala");
450        Assert.assertNotNull ("Invalid SCXML context",        context.get ("bear"));
451        Assert.assertEquals  ("Invalid SCXML context","koala",context.get ("bear"));
452        Assert.assertNull    ("Invalid Javascript bindings",  bindings.get("bear"));
453        Assert.assertNotNull ("Invalid JSbindings",           jsx.get     ("bear"));
454        Assert.assertEquals  ("Invalid JSbindings","koala",   jsx.get     ("bear"));
455
456        context.reset ();
457        bindings.clear();
458        bindings.put  ("bear","grizzly");
459        Assert.assertNull    ("Invalid SCXML context",                context.get ("bear"));
460        Assert.assertNotNull ("Invalid Javascript bindings",          bindings.get("bear"));
461        Assert.assertEquals  ("Invalid Javascript bindings","grizzly",bindings.get("bear"));
462        Assert.assertNotNull ("Invalid JSbindings",                   jsx.get     ("bear"));
463        Assert.assertEquals  ("Invalid JSbindings","grizzly",         jsx.get     ("bear"));
464
465        context.reset ();
466        bindings.clear();
467        context.set   ("bear","koala");
468        bindings.put  ("bear","grizzly");
469        Assert.assertNotNull ("Invalid SCXML context",        context.get ("bear"));
470        Assert.assertEquals  ("Invalid SCXML context","koala",context.get ("bear"));
471        Assert.assertNotNull ("Invalid Javascript bindings",          bindings.get("bear"));
472        Assert.assertEquals  ("Invalid Javascript bindings","grizzly",bindings.get("bear"));
473        Assert.assertNotNull ("Invalid JSbindings",                   jsx.get     ("bear"));
474        Assert.assertEquals  ("Invalid JSbindings","grizzly",         jsx.get     ("bear"));
475
476        context.reset ();
477        bindings.clear();
478        context.set   ("cat","felix");
479        bindings.put  ("dog","rover");
480        Assert.assertNotNull ("Invalid SCXML context",              context.get ("cat"));
481        Assert.assertEquals  ("Invalid SCXML context","felix",      context.get ("cat"));
482        Assert.assertNotNull ("Invalid Javascript bindings",        bindings.get("dog"));
483        Assert.assertEquals  ("Invalid Javascript bindings","rover",bindings.get("dog"));
484        Assert.assertNotNull ("Invalid JSbindings",                 jsx.get     ("cat"));
485        Assert.assertEquals  ("Invalid JSbindings","felix",         jsx.get     ("cat"));
486        Assert.assertNotNull ("Invalid JSbindings",                 jsx.get     ("dog"));
487        Assert.assertEquals  ("Invalid JSbindings","rover",         jsx.get     ("dog"));
488    }
489
490    /**
491     * Tests the <code>put</code> method with items in the SCXML context as well as the
492     * JavaScript Bindings.
493     *
494     */    
495    @Test
496    public void testPut() {
497        Context    context  = new JSContext     ();
498        Bindings   bindings = new SimpleBindings();
499        JSBindings jsx      = new JSBindings    (context,bindings);
500
501        Assert.assertNull("Invalid SCXML context",      context.get ("bear"));
502        Assert.assertNull("Invalid Javascript bindings",bindings.get("bear"));
503        Assert.assertNull("Invalid JSbindings",         jsx.get     ("bear"));
504
505        jsx.put       ("bear","koala");
506        Assert.assertNotNull ("Invalid SCXML context",        context.get ("bear"));
507        Assert.assertEquals  ("Invalid SCXML context","koala",context.get("bear"));
508        Assert.assertNotNull ("Invalid JSbindings",           jsx.get ("bear"));
509        Assert.assertNull    ("Invalid Javascript bindings",  bindings.get("bear"));
510    }
511
512    /**
513     * Tests the <code>putAll</code> method with items in the SCXML context as well as the
514     * JavaScript Bindings.
515     *
516     */    
517    @Test
518    public void testPutAll() {
519        Context            context  = new JSContext     ();
520        Bindings           bindings = new SimpleBindings();
521        JSBindings         jsx      = new JSBindings    (context,bindings);
522        Map<String,Object> vars     = new HashMap<String, Object>();
523
524        vars.put("bear","grizzly");
525        vars.put("cat","felix");
526        vars.put("dog","rover");
527
528        Assert.assertNull("Invalid SCXML context",      context.get ("bear"));
529        Assert.assertNull("Invalid SCXML context",      context.get ("cat"));
530        Assert.assertNull("Invalid SCXML context",      context.get ("dog"));
531
532        Assert.assertNull("Invalid Javascript bindings",bindings.get("bear"));
533        Assert.assertNull("Invalid Javascript bindings",bindings.get("cat"));
534        Assert.assertNull("Invalid Javascript bindings",bindings.get("dog"));
535
536        Assert.assertNull("Invalid JSbindings",         jsx.get     ("bear"));
537        Assert.assertNull("Invalid JSbindings",         jsx.get     ("cat"));
538        Assert.assertNull("Invalid JSbindings",         jsx.get     ("dog"));
539
540        context.set("bear","koala");
541        jsx.putAll (vars);
542
543        Assert.assertNotNull ("Invalid SCXML context",        context.get ("bear"));
544        Assert.assertNull    ("Invalid SCXML context",        context.get ("cat"));
545        Assert.assertNull    ("Invalid SCXML context",        context.get ("dog"));
546        Assert.assertEquals  ("Invalid SCXML context","koala",context.get ("bear"));
547        Assert.assertEquals  ("Invalid SCXML context",1,      context.getVars().size());
548
549        Assert.assertNotNull ("Invalid Javascript bindings",          bindings.get("bear"));
550        Assert.assertNotNull ("Invalid Javascript bindings",          bindings.get("cat"));
551        Assert.assertNotNull ("Invalid Javascript bindings",          bindings.get("dog"));
552        Assert.assertEquals  ("Invalid Javascript bindings","grizzly",bindings.get("bear"));
553        Assert.assertEquals  ("Invalid Javascript bindings","felix",  bindings.get("cat"));
554        Assert.assertEquals  ("Invalid Javascript bindings","rover",  bindings.get("dog"));
555        Assert.assertEquals  ("Invalid Javascript bindings",3,        bindings.size());
556    }
557
558    /**
559     * Tests the <code>remove</code> method with items in the SCXML context as well as the
560     * JavaScript Bindings.
561     *
562     */    
563    @Test
564    public void testRemove() {
565        Context            context  = new JSContext     ();
566        Bindings           bindings = new SimpleBindings();
567        JSBindings         jsx      = new JSBindings    (context,bindings);
568
569        context.set ("bear","koala");
570        bindings.put("bear","grizzly");
571        bindings.put("cat", "felix");
572        bindings.put("dog", "rover");
573
574        Assert.assertNotNull("Invalid SCXML context",        context.get("bear"));
575        Assert.assertEquals ("Invalid SCXML context","koala",context.get("bear"));
576        Assert.assertEquals ("Invalid SCXML context",1,      context.getVars().size());
577
578        Assert.assertNotNull("Invalid Javascript bindings",          bindings.get("bear"));
579        Assert.assertNotNull("Invalid Javascript bindings",          bindings.get("cat"));
580        Assert.assertNotNull("Invalid Javascript bindings",          bindings.get("dog"));
581        Assert.assertEquals ("Invalid Javascript bindings","grizzly",bindings.get("bear"));
582        Assert.assertEquals ("Invalid Javascript bindings","felix",  bindings.get("cat"));
583        Assert.assertEquals ("Invalid Javascript bindings","rover",  bindings.get("dog"));
584        Assert.assertEquals ("Invalid Javascript bindings",3,        bindings.size());
585
586        jsx.remove("cat");
587
588        Assert.assertNotNull("Invalid SCXML context",                context.get("bear"));
589        Assert.assertEquals ("Invalid SCXML context","koala",        context.get("bear"));
590        Assert.assertEquals ("Invalid SCXML context",1,              context.getVars().size());
591        Assert.assertNotNull("Invalid Javascript bindings",          bindings.get("bear"));
592        Assert.assertNull   ("Invalid Javascript bindings",          bindings.get("cat"));
593        Assert.assertNotNull("Invalid Javascript bindings",          bindings.get("dog"));
594        Assert.assertEquals ("Invalid Javascript bindings","grizzly",bindings.get("bear"));
595        Assert.assertEquals ("Invalid Javascript bindings","rover",  bindings.get("dog"));
596        Assert.assertEquals ("Invalid Javascript bindings",2,        bindings.size());
597
598        jsx.remove("dog");
599
600        Assert.assertNotNull("Invalid SCXML context",               context.get("bear"));
601        Assert.assertEquals ("Invalid SCXML context","koala",        context.get("bear"));
602        Assert.assertEquals ("Invalid SCXML context",1,              context.getVars().size());
603        Assert.assertNotNull("Invalid Javascript bindings",          bindings.get("bear"));
604        Assert.assertNull   ("Invalid Javascript bindings",          bindings.get("cat"));
605        Assert.assertNull   ("Invalid Javascript bindings",          bindings.get("dog"));
606        Assert.assertEquals ("Invalid Javascript bindings","grizzly",bindings.get("bear"));
607        Assert.assertEquals ("Invalid Javascript bindings",1,        bindings.size());
608
609        jsx.remove("bear");
610
611        Assert.assertNotNull("Invalid SCXML context",       context.get("bear"));
612        Assert.assertEquals("Invalid SCXML context","koala",context.get("bear"));
613        Assert.assertEquals("Invalid SCXML context",1,      context.getVars().size());
614        Assert.assertNull  ("Invalid Javascript bindings",  bindings.get("bear"));
615        Assert.assertNull  ("Invalid Javascript bindings",  bindings.get("cat"));
616        Assert.assertNull  ("Invalid Javascript bindings",  bindings.get("dog"));
617        Assert.assertEquals("Invalid Javascript bindings",0,bindings.size());
618
619        jsx.remove("bear");
620
621        Assert.assertNull  ("Invalid SCXML context",        context.get("bear"));
622        Assert.assertEquals("Invalid SCXML context",0,      context.getVars().size());
623        Assert.assertNull  ("Invalid Javascript bindings",  bindings.get("bear"));
624        Assert.assertNull  ("Invalid Javascript bindings",  bindings.get("cat"));
625        Assert.assertNull  ("Invalid Javascript bindings",  bindings.get("dog"));
626        Assert.assertEquals("Invalid Javascript bindings",0,bindings.size());
627    }
628
629    /**
630     * Tests the <code>clear</code> method with items in the SCXML context as well as the
631     * JavaScript Bindings.
632     *
633     */    
634    @Test
635    public void testClear() {
636        Context            context  = new JSContext     ();
637        Bindings           bindings = new SimpleBindings();
638        JSBindings         jsx      = new JSBindings    (context,bindings);
639
640        context.set ("bear","koala");
641        bindings.put("bear","grizzly");
642        bindings.put("cat", "felix");
643        bindings.put("dog", "rover");
644
645        Assert.assertNotNull("Invalid SCXML context",        context.get("bear"));
646        Assert.assertEquals ("Invalid SCXML context","koala",context.get("bear"));
647        Assert.assertEquals ("Invalid SCXML context",1,      context.getVars().size());
648
649        Assert.assertNotNull("Invalid Javascript bindings",          bindings.get("bear"));
650        Assert.assertNotNull("Invalid Javascript bindings",          bindings.get("cat"));
651        Assert.assertNotNull("Invalid Javascript bindings",          bindings.get("dog"));
652        Assert.assertEquals ("Invalid Javascript bindings","grizzly",bindings.get("bear"));
653        Assert.assertEquals ("Invalid Javascript bindings","felix",  bindings.get("cat"));
654        Assert.assertEquals ("Invalid Javascript bindings","rover",  bindings.get("dog"));
655        Assert.assertEquals ("Invalid Javascript bindings",3,        bindings.size());
656
657        jsx.clear();
658
659        Assert.assertNotNull("Invalid SCXML context",       context.get("bear"));
660        Assert.assertEquals("Invalid SCXML context","koala",context.get("bear"));
661        Assert.assertEquals("Invalid SCXML context",1,      context.getVars().size());
662        Assert.assertNull  ("Invalid Javascript bindings",  bindings.get("bear"));
663        Assert.assertNull  ("Invalid Javascript bindings",  bindings.get("cat"));
664        Assert.assertNull  ("Invalid Javascript bindings",  bindings.get("dog"));
665        Assert.assertEquals("Invalid Javascript bindings",0,bindings.size());
666    }
667
668}
669