1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.osgi.config;
17
18 import junit.framework.TestCase;
19
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.InvalidSyntaxException;
22 import org.osgi.framework.ServiceReference;
23 import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
24 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
25 import org.springframework.context.support.GenericApplicationContext;
26 import org.springframework.core.io.ClassPathResource;
27 import org.springframework.osgi.context.support.BundleContextAwareProcessor;
28 import org.springframework.osgi.mock.MockBundleContext;
29
30
31
32
33
34 public class OsgiSingleReferenceParserWithInvalidFilesTest extends TestCase {
35 private GenericApplicationContext appContext;
36
37 protected void setUp() throws Exception {
38 BundleContext bundleContext = new MockBundleContext() {
39
40 public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
41 return new ServiceReference[0];
42 }
43 };
44
45 appContext = new GenericApplicationContext();
46 appContext.getBeanFactory().addBeanPostProcessor(new BundleContextAwareProcessor(bundleContext));
47 appContext.setClassLoader(getClass().getClassLoader());
48 }
49
50 protected void tearDown() throws Exception {
51 appContext.close();
52 appContext = null;
53 }
54
55 private void readCtxFromResource(String resourceName) {
56 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
57 reader.loadBeanDefinitions(new ClassPathResource(resourceName, getClass()));
58 appContext.refresh();
59 }
60
61 private void expectException(String resourceName) {
62 try {
63 readCtxFromResource(resourceName);
64 fail("should have thrown parsing exception, invalid resource " + resourceName);
65 }
66 catch (BeanDefinitionParsingException ex) {
67
68 }
69 }
70
71 public void testInlineInterfaceAndNestedInterfaces() throws Exception {
72 expectException("osgiSingleReferenceInvalidInterface.xml");
73 }
74
75 public void testListenerWithNestedDefinitionAndInlinedRefVariant1() throws Exception {
76 expectException("osgiSingleReferenceWithInvalidListener1.xml");
77 }
78 public void testListenerWithNestedDefinitionAndInlinedRefVariant2() throws Exception {
79 expectException("osgiSingleReferenceWithInvalidListener2.xml");
80 }
81
82 }