View Javadoc
1   package org.codehaus.plexus.classworlds.realm;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase;
20  import org.codehaus.plexus.classworlds.ClassWorld;
21  
22  /**
23   * @author <a href="bwalding@jakarta.org">Ben Walding</a>
24   */
25  public class EntryTest
26      extends AbstractClassWorldsTestCase
27  {
28  
29      /**
30       * Constructor for EntryTest.
31       *
32       * @param name
33       */
34      public EntryTest( String name )
35      {
36          super( name );
37      }
38  
39      public void testCompareTo()
40          throws Exception
41      {
42          ClassWorld cw = new ClassWorld();
43          ClassRealm r = cw.newRealm( "test1" );
44  
45          Entry entry1 = new Entry( r, "org.test" );
46          Entry entry2 = new Entry( r, "org.test.impl" );
47  
48          assertTrue( "org.test > org.test.impl", entry1.compareTo( entry2 ) > 0 );
49      }
50  
51      /**
52       * Tests the equality is realm independant
53       *
54       * @throws Exception
55       */
56      public void testEquals()
57          throws Exception
58      {
59          ClassWorld cw = new ClassWorld();
60          ClassRealm r1 = cw.newRealm( "test1" );
61          ClassRealm r2 = cw.newRealm( "test2" );
62  
63          Entry entry1 = new Entry( r1, "org.test" );
64          Entry entry2 = new Entry( r2, "org.test" );
65  
66          assertTrue( "entry1 == entry2", entry1.equals( entry2 ) );
67          assertTrue( "entry1.hashCode() == entry2.hashCode()", entry1.hashCode() == entry2.hashCode() );
68      }
69  
70      public void testMatchesClassByPackageImport()
71          throws Exception
72      {
73          ClassWorld cw = new ClassWorld();
74          ClassRealm r = cw.newRealm( "test1" );
75  
76          Entry entry = new Entry( r, "org.test" );
77  
78          assertTrue( entry.matches( "org.test.MyClass" ) );
79          assertTrue( entry.matches( "org.test.MyClass$NestedClass" ) );
80          assertTrue( entry.matches( "org.test.MyClassUtils" ) );
81          assertTrue( entry.matches( "org.test.impl.MyClass" ) );
82          assertFalse( entry.matches( "org.tests.AnotherClass" ) );
83      }
84  
85      public void testMatchesClassByClassImport()
86          throws Exception
87      {
88          ClassWorld cw = new ClassWorld();
89          ClassRealm r = cw.newRealm( "test1" );
90  
91          Entry entry = new Entry( r, "org.test.MyClass" );
92  
93          assertTrue( entry.matches( "org.test.MyClass" ) );
94          assertTrue( entry.matches( "org.test.MyClass$NestedClass" ) );
95          assertFalse( entry.matches( "org.test.MyClassUtils" ) );
96          assertFalse( entry.matches( "org.test.AnotherClass" ) );
97      }
98  
99      public void testMatchesResourceByPackageImport()
100         throws Exception
101     {
102         ClassWorld cw = new ClassWorld();
103         ClassRealm r = cw.newRealm( "test1" );
104 
105         Entry entry = new Entry( r, "org.test" );
106 
107         assertTrue( entry.matches( "org/test/MyClass.class" ) );
108         assertTrue( entry.matches( "org/test/MyClass$NestedClass.class" ) );
109         assertTrue( entry.matches( "org/test/MyClasses.properties" ) );
110         assertTrue( entry.matches( "org/test/impl/MyClass.class" ) );
111         assertFalse( entry.matches( "org/tests/AnotherClass.class" ) );
112     }
113 
114     public void testMatchesResourceByClassImport()
115         throws Exception
116     {
117         ClassWorld cw = new ClassWorld();
118         ClassRealm r = cw.newRealm( "test1" );
119 
120         Entry entry = new Entry( r, "org.test.MyClass" );
121 
122         assertTrue( entry.matches( "org/test/MyClass.class" ) );
123         assertTrue( entry.matches( "org/test/MyClass$NestedClass.class" ) );
124         assertFalse( entry.matches( "org/test/MyClass.properties" ) );
125         assertFalse( entry.matches( "org/test/AnotherClass" ) );
126     }
127 
128     public void testMatchesAllImport()
129         throws Exception
130     {
131         ClassWorld cw = new ClassWorld();
132         ClassRealm r = cw.newRealm( "test1" );
133 
134         Entry entry = new Entry( r, "" );
135 
136         assertTrue( entry.matches( "org.test.MyClass" ) );
137         assertTrue( entry.matches( "org.test.MyClass$NestedClass" ) );
138         assertTrue( entry.matches( "org/test/MyClass.class" ) );
139         assertTrue( entry.matches( "org/test/MyClass.properties" ) );
140     }
141 
142     public void testMatchesResourceByResourceImport()
143         throws Exception
144     {
145         ClassWorld cw = new ClassWorld();
146         ClassRealm r = cw.newRealm( "test1" );
147 
148         Entry entry1 = new Entry( r, "some.properties" );
149 
150         assertTrue( entry1.matches( "some.properties" ) );
151         assertFalse( entry1.matches( "other.properties" ) );
152 
153         Entry entry2 = new Entry( r, "org/test/some.properties" );
154 
155         assertTrue( entry2.matches( "org/test/some.properties" ) );
156         assertFalse( entry2.matches( "org/test/other.properties" ) );
157     }
158 
159     public void testMatchesClassByExactPackageImport()
160         throws Exception
161     {
162         ClassWorld cw = new ClassWorld();
163         ClassRealm r = cw.newRealm( "test1" );
164 
165         Entry entry = new Entry( r, "org.test.*" );
166 
167         assertTrue( entry.matches( "org.test.MyClass" ) );
168         assertTrue( entry.matches( "org.test.MyClass$NestedClass" ) );
169         assertTrue( entry.matches( "org.test.MyClassUtils" ) );
170         assertFalse( entry.matches( "org.test.impl.MyClass" ) );
171         assertFalse( entry.matches( "org.tests.AnotherClass" ) );
172     }
173 
174     public void testMatchesResourceByExactPackageImport()
175         throws Exception
176     {
177         ClassWorld cw = new ClassWorld();
178         ClassRealm r = cw.newRealm( "test1" );
179 
180         Entry entry = new Entry( r, "org.test.*" );
181 
182         assertTrue( entry.matches( "org/test/MyClass.class" ) );
183         assertTrue( entry.matches( "org/test/MyClass$NestedClass.class" ) );
184         assertTrue( entry.matches( "org/test/MyClasses.properties" ) );
185         assertFalse( entry.matches( "org/test/impl/MyClass.class" ) );
186         assertFalse( entry.matches( "org/tests/AnotherClass.class" ) );
187     }
188 
189 }