1   /*
2    * $Id: SpiUtilsTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  import org.mule.tck.testmodels.fruit.Apple;
15  import org.mule.tck.testmodels.fruit.Banana;
16  import org.mule.tck.testmodels.fruit.Fruit;
17  
18  import java.io.InputStream;
19  import java.util.Properties;
20  
21  public class SpiUtilsTestCase extends AbstractMuleTestCase
22  {
23  
24      public void testDiscoverDefault() throws Exception
25      {
26          Class c = SpiUtils.findService(Fruit.class, Banana.class.getName(), getClass());
27          assertNotNull(c);
28          assertEquals(Banana.class.getName(), c.getName());
29      }
30  
31      public void testDiscoverNotFound() throws Exception
32      {
33          Class c = SpiUtils.findService(Fruit.class, getClass());
34          assertNull(c);
35      }
36  
37      public void testDiscoverFromProperty() throws Exception
38      {
39          System.setProperty(Fruit.class.getName(), Apple.class.getName());
40          Class c = SpiUtils.findService(Fruit.class, getClass());
41          assertNotNull(c);
42          assertEquals(Apple.class.getName(), c.getName());
43          Properties p = System.getProperties();
44          p.remove(Fruit.class.getName());
45          System.setProperties(p);
46      }
47  
48      public void testDiscoverFromPropertyFile() throws Exception
49      {
50          InputStream is = IOUtils.getResourceAsStream("test-spi.properties", getClass());
51          assertNotNull("Test resource not found.", is);
52          Properties p = new Properties();
53          p.load(is);
54          assertNotNull(p);
55          Class c = SpiUtils.findService(Fruit.class, p, getClass());
56          assertNotNull(c);
57          assertEquals(Banana.class.getName(), c.getName());
58      }
59  
60      public void testDiscoverFromResource() throws Exception
61      {
62          Class c = SpiUtils.findService(Fruit.class, "test-spi.properties", Apple.class.getName(), getClass());
63          assertNotNull(c);
64          assertEquals(Banana.class.getName(), c.getName());
65      }
66  
67  }