1   /*
2    * Copyright 2006-2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author Costin Leau
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  			// service reference already registered
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  			// expected
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  }