View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.util;
20  
21  import org.apache.myfaces.trinidad.util.Service;
22  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  
27  
28  public class ServiceTest extends TestCase
29  {
30    public static final Test suite()
31    {
32      return new TestSuite(ServiceTest.class);
33    }
34    
35    public static void main(String[] args) throws Throwable
36    {
37      junit.textui.TestRunner.run(suite());
38    }
39  
40    public ServiceTest(
41      String testName)
42    {
43      super(testName);
44    }
45  
46    public void testDirect()
47    {
48      Base b = new Base();
49      Base b1 = new Base1();
50      Base b2 = new Base2();
51  
52      assertEquals(b, Service.getService(b, Base.class));
53      assertNull(Service.getService(b, Interface1.class));
54      assertNull(Service.getService(b, Interface2.class));
55  
56      assertEquals(b1, Service.getService(b1, Base.class));
57      assertEquals(b1, Service.getService(b1, Interface1.class));
58      assertNull(Service.getService(b1, Interface2.class));
59  
60      assertEquals(b2, Service.getService(b2, Base.class));
61      assertEquals(b2, Service.getService(b2, Interface1.class));
62      assertEquals(b2, Service.getService(b2, Interface2.class));
63    }
64  
65    @SuppressWarnings("cast")
66    public void testProvider()
67    {
68      Base b = new UsesProvider();
69      Base b2 = new UsesProvider2();
70  
71      assertEquals(b, Service.getService(b, Base.class));
72      assert(Service.getService(b, Interface1.class) != null);
73      assert(Service.getService(b, Interface1.class) instanceof Interface1);
74      assertNull(Service.getService(b, Interface2.class));
75  
76      assertEquals(b2, Service.getService(b2, Base.class));
77      assert(Service.getService(b2, Interface1.class) != null);
78      assert(Service.getService(b2, Interface1.class) instanceof Interface1);
79      assertEquals(b2, Service.getService(b2, Interface2.class));
80    }
81  
82    
83  
84    static public class Base
85    {
86    }
87  
88    static public interface Interface1
89    {
90    }
91  
92    static public interface Interface2
93    {
94    }
95  
96    static public class Base1 extends Base implements Interface1
97    {
98    }
99  
100   static public class Base2 extends Base1 implements Interface2
101   {
102   }
103 
104   static public class UsesProvider extends Base implements Service.Provider
105   {
106     @SuppressWarnings("unchecked")
107     public <T> T getService(Class<T> serviceClass)
108     {
109       if (serviceClass == Interface1.class)
110         return (T)new Interface1(){};
111         
112       return null;
113     }    
114   }
115 
116   static public class UsesProvider2 extends UsesProvider implements Interface2
117   {
118   }
119 }