1   /*
2    * Copyright (C) The DNA Group. All rights reserved.
3    *
4    * This software is published under the terms of the DNA
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.dna.impl;
9   
10  import junit.framework.TestCase;
11  
12  import org.codehaus.dna.MissingResourceException;
13  import org.codehaus.dna.impl.DefaultResourceLocator;
14  
15  public class DefaultResourceLocatorTestCase
16      extends TestCase
17  {
18      public void testLookupMissingResourceWithNoParent()
19          throws Exception
20      {
21          final DefaultResourceLocator locator = new DefaultResourceLocator();
22          assertEquals( "locator.contains(rez) post to insert",
23                        false,
24                        locator.contains( "rez" ) );
25  
26          try
27          {
28              locator.lookup( "rez" );
29          }
30          catch( MissingResourceException e )
31          {
32              return;
33          }
34          fail( "Expected to fail looking up missing resource" );
35      }
36  
37      public void testLookupMissingResourceWithParent()
38          throws Exception
39      {
40          final DefaultResourceLocator parent = new DefaultResourceLocator();
41          final DefaultResourceLocator locator = new DefaultResourceLocator( parent );
42          assertEquals( "locator.contains(rez) post to insert",
43                        false,
44                        locator.contains( "rez" ) );
45  
46          try
47          {
48              locator.lookup( "rez" );
49          }
50          catch( MissingResourceException e )
51          {
52              return;
53          }
54          fail( "Expected to fail looking up missing resource" );
55      }
56  
57      public void testLookupResourceInLocalLocator()
58          throws Exception
59      {
60          final Object resource = new Object();
61          final DefaultResourceLocator locator = new DefaultResourceLocator();
62          locator.put( "rez", resource );
63          assertEquals( "locator.contains(rez) post to insert",
64                        true,
65                        locator.contains( "rez" ) );
66  
67          final Object result = locator.lookup( "rez" );
68          assertEquals( "locator.contains(rez) == resource",
69                        resource, result );
70      }
71  
72      public void testLookupResourceInParentLocator()
73          throws Exception
74      {
75          final Object resource = new Object();
76          final DefaultResourceLocator parent = new DefaultResourceLocator();
77          final DefaultResourceLocator locator = new DefaultResourceLocator( parent );
78          parent.put( "rez", resource );
79          assertEquals( "locator.contains(rez) post to insert",
80                        true,
81                        locator.contains( "rez" ) );
82  
83          final Object result = locator.lookup( "rez" );
84          assertEquals( "locator.contains(rez) == resource",
85                        resource, result );
86      }
87  
88      public void testPutWithNullKey()
89          throws Exception
90      {
91          final DefaultResourceLocator locator = new DefaultResourceLocator();
92          try
93          {
94              locator.put( null, new Object() );
95          }
96          catch( NullPointerException e )
97          {
98              assertEquals( "key", e.getMessage() );
99              return;
100         }
101         fail( "Expect to fail to put resource with null key" );
102     }
103 
104     public void testPutWithNullResource()
105         throws Exception
106     {
107         final DefaultResourceLocator locator = new DefaultResourceLocator();
108         try
109         {
110             locator.put( "rez", null );
111         }
112         catch( NullPointerException e )
113         {
114             assertEquals( "resource", e.getMessage() );
115             return;
116         }
117         fail( "Expect to fail to put resource with null resource" );
118     }
119 }