KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > service > ServiceDescriptorRegistryTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.service;
18
19 import java.util.Collection JavaDoc;
20
21 import javax.transaction.UserTransaction JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.alfresco.service.ServiceDescriptor;
26 import org.alfresco.service.ServiceRegistry;
27 import org.alfresco.service.cmr.coci.CheckOutCheckInService;
28 import org.alfresco.service.cmr.dictionary.DictionaryService;
29 import org.alfresco.service.cmr.lock.LockService;
30 import org.alfresco.service.cmr.repository.ContentService;
31 import org.alfresco.service.cmr.repository.CopyService;
32 import org.alfresco.service.cmr.repository.MimetypeService;
33 import org.alfresco.service.cmr.repository.NodeService;
34 import org.alfresco.service.cmr.search.SearchService;
35 import org.alfresco.service.cmr.version.VersionService;
36 import org.alfresco.service.namespace.QName;
37 import org.alfresco.service.transaction.TransactionService;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.context.support.ClassPathXmlApplicationContext;
40
41 public class ServiceDescriptorRegistryTest extends TestCase
42 {
43     
44     private ApplicationContext factory = null;
45     
46     private static String JavaDoc TEST_NAMESPACE = "http://www.alfresco.org/test/serviceregistrytest";
47     private static QName invalidService = QName.createQName(TEST_NAMESPACE, "invalid");
48     private static QName service1 = QName.createQName(TEST_NAMESPACE, "service1");
49     private static QName service2 = QName.createQName(TEST_NAMESPACE, "service2");
50     private static QName service3 = QName.createQName(TEST_NAMESPACE, "service3");
51
52     
53     public void setUp()
54     {
55         factory = new ClassPathXmlApplicationContext("org/alfresco/repo/service/testregistry.xml");
56     }
57     
58     public void testDescriptor()
59     {
60         ServiceRegistry registry = (ServiceRegistry)factory.getBean("serviceRegistry");
61         
62         Collection JavaDoc services = registry.getServices();
63         assertNotNull(services);
64         assertEquals(3, services.size());
65         
66         assertTrue(registry.isServiceProvided(service1));
67         assertFalse(registry.isServiceProvided(invalidService));
68
69         ServiceDescriptor invalid = registry.getServiceDescriptor(invalidService);
70         assertNull(invalid);
71         ServiceDescriptor desc1 = registry.getServiceDescriptor(service1);
72         assertNotNull(desc1);
73         assertEquals(service1, desc1.getQualifiedName());
74         assertEquals("Test Service 1", desc1.getDescription());
75         assertEquals(TestServiceInterface.class, desc1.getInterface());
76         ServiceDescriptor desc2 = registry.getServiceDescriptor(service2);
77         assertNotNull(desc2);
78         assertEquals(service2, desc2.getQualifiedName());
79         assertEquals("Test Service 2", desc2.getDescription());
80         assertEquals(TestServiceInterface.class, desc2.getInterface());
81     }
82
83     
84     public void testService()
85     {
86         ServiceRegistry registry = (ServiceRegistry)factory.getBean("serviceRegistry");
87         
88         TestServiceInterface theService1 = (TestServiceInterface)registry.getService(service1);
89         assertNotNull(service1);
90         assertEquals("Test1:service1", theService1.test("service1"));
91         TestServiceInterface theService2 = (TestServiceInterface)registry.getService(service2);
92         assertNotNull(service2);
93         assertEquals("Test2:service2", theService2.test("service2"));
94     }
95     
96
97     public void testStores()
98     {
99         ServiceRegistry registry = (ServiceRegistry)factory.getBean("serviceRegistry");
100         
101         ServiceDescriptor desc3 = registry.getServiceDescriptor(service3);
102         assertNotNull(desc3);
103         StoreRedirector theService3 = (StoreRedirector)registry.getService(service3);
104         assertNotNull(service3);
105         
106         Collection JavaDoc<String JavaDoc> descStores = desc3.getSupportedStoreProtocols();
107         assertTrue(descStores.contains("Type1"));
108         assertTrue(descStores.contains("Type2"));
109         assertFalse(descStores.contains("Invalid"));
110         
111         Collection JavaDoc<String JavaDoc> serviceStores = theService3.getSupportedStoreProtocols();
112         for (String JavaDoc store: descStores)
113         {
114             assertTrue(serviceStores.contains(store));
115         }
116     }
117     
118     
119     public void testAppContext()
120     {
121         ApplicationContext appContext = new ClassPathXmlApplicationContext("alfresco/application-context.xml");
122
123         ServiceRegistry registry = (ServiceRegistry)appContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
124         assertNotNull(registry);
125         NodeService s1 = registry.getNodeService();
126         assertNotNull(s1);
127         CheckOutCheckInService s2 = registry.getCheckOutCheckInService();
128         assertNotNull(s2);
129         ContentService s3 = registry.getContentService();
130         assertNotNull(s3);
131         CopyService s4 = registry.getCopyService();
132         assertNotNull(s4);
133         DictionaryService s5 = registry.getDictionaryService();
134         assertNotNull(s5);
135         LockService s6 = registry.getLockService();
136         assertNotNull(s6);
137         MimetypeService s7 = registry.getMimetypeService();
138         assertNotNull(s7);
139         SearchService s8 = registry.getSearchService();
140         assertNotNull(s8);
141         TransactionService transactionService = registry.getTransactionService();
142         UserTransaction JavaDoc s9 = transactionService.getUserTransaction();
143         assertNotNull(s9);
144         UserTransaction JavaDoc s10 = transactionService.getUserTransaction();
145         assertNotNull(s10);
146         assertFalse(s9.equals(s10));
147         VersionService s11 = registry.getVersionService();
148         assertNotNull(s11);
149     }
150     
151     
152     public interface TestServiceInterface
153     {
154         public String JavaDoc test(String JavaDoc arg);
155     }
156
157     public static abstract class Component implements TestServiceInterface
158     {
159         private String JavaDoc type;
160         
161         private Component(String JavaDoc type)
162         {
163             this.type = type;
164         }
165         
166         public String JavaDoc test(String JavaDoc arg)
167         {
168             return type + ":" + arg;
169         }
170     }
171     
172     public static class Test1Component extends Component
173     {
174         private Test1Component()
175         {
176             super("Test1");
177         }
178     }
179
180     public static class Test2Component extends Component
181     {
182         private Test2Component()
183         {
184             super("Test2");
185         }
186     }
187     
188 }
189
Popular Tags