1   /*
2    * @(#) ContextTest.java Oct 23, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.naming;
11  
12  import javax.naming.Context;
13  import javax.naming.NameParser;
14  
15  import junit.framework.Assert;
16  import junit.framework.TestCase;
17  
18  /***
19   * Tests the context
20   * <p>
21   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
22   * University of Technology </a>, the Netherlands. <br>
23   * See for project information <a href="http://www.simulation.tudelft.nl">
24   * www.simulation.tudelft.nl </a> <br>
25   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
26   * License (GPL) </a>, no warranty <br>
27   * 
28   * @author Niels Lang
29   * @version 1.2 2004-03-24
30   * @since 1.2
31   */
32  public class ContextTest extends TestCase
33  {
34  	/*** TEST_METHOD_NAME refers to the name of the test method */
35  	public static final String TEST_METHOD_NAME = "test";
36  
37  	/*** eventList is the eventList on which the test is fired */
38  	private Context context = null;
39  
40  	/***
41  	 * constructs a new EventListTest.
42  	 * 
43  	 * @param context is the context to test
44  	 */
45  	public ContextTest(final Context context)
46  	{
47  		this(ContextTest.TEST_METHOD_NAME, context);
48  	}
49  
50  	/***
51  	 * constructs a new EventListTest
52  	 * 
53  	 * @param arg0 the name of the test method
54  	 * @param context is the context on which the test is fired
55  	 */
56  	public ContextTest(final String arg0, final Context context)
57  	{
58  		super(arg0);
59  		this.context = context;
60  	}
61  
62  	/***
63  	 * tests the TreeMapEventListOld
64  	 */
65  	public void test()
66  	{
67  		try
68  		{
69  			this.context.createSubcontext("level_1");
70  			this.context.createSubcontext("level_1/level_2");
71  			this.context.bind("/level_1/level_2/test", "HelloWorld");
72  
73  			Context lev2 = ((Context) ((Context) this.context.lookup("level_1"))
74  					.lookup("level_2"));
75  			lev2.createSubcontext("./level_3");
76  			Assert.assertTrue(lev2.lookup("test").equals("HelloWorld"));
77  			Assert.assertTrue(lev2.lookup("./test").equals("HelloWorld"));
78  
79  			//Checking empty name
80  			NameParser parser = lev2.getNameParser("");
81  			Assert.assertEquals(parser.parse("").size(), 0);
82  
83  			// Binding @ absolute adress
84  			lev2.bind("/level_1/test_2", "HelloWorld_2");
85  			lev2.createSubcontext("level_21");
86  			lev2.bind("level_21/test_3", "HelloWorld_3");
87  			Assert
88  					.assertEquals(((Context) lev2.lookup("level_3"))
89  							.lookup("/level_1/level_2/level_21/test_3"),
90  							"HelloWorld_3");
91  		} catch (Exception exception)
92  		{
93  			Assert.fail(exception.getMessage());
94  		}
95  	}
96  }