1   /**
2    * Copyright 2005,2009 Ivan SZKIBA
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.ini4j;
17  
18  import static org.junit.Assert.*;
19  
20  import org.junit.Test;
21  
22  public class ServiceFinderTest
23  {
24      static final String DUMMY = "dummy";
25      static final String DUMMY_SERVICE = "org.ini4j.Dummy";
26      static final String BAD_CONFIG_SERVICE = "org.ini4j.BadConfig";
27      static final String EMPTY_CONFIG_SERVICE = "org.ini4j.EmptyConfig";
28      static final String DUMMY_IMPL = "DummyImpl";
29  
30      @Test public void testFindService() throws Exception
31      {
32          boolean flag = false;
33  
34          System.setProperty(IniParser.class.getName(), Helper.class.getName());
35          try
36          {
37              ServiceFinder.findService(IniParser.class);
38          }
39          catch (IllegalArgumentException x)
40          {
41              flag = true;
42          }
43  
44          // System.clearProperty(IniParser.SERVICE_ID); missing in 1.4
45          System.getProperties().remove(IniParser.class.getName());
46          assertTrue(flag);
47      }
48  
49      /**
50       * Test of findServiceClass method.
51       *
52       * @throws Exception on error
53       */
54      @Test public void testFindServiceClass() throws Exception
55      {
56          boolean flag = false;
57  
58          System.setProperty(IniParser.class.getName(), DUMMY);
59          try
60          {
61              ServiceFinder.findServiceClass(IniParser.class);
62          }
63          catch (IllegalArgumentException x)
64          {
65              flag = true;
66          }
67  
68          // System.clearProperty(IniParser.SERVICE_ID); missing in 1.4
69          System.getProperties().remove(IniParser.class.getName());
70          assertTrue(flag);
71      }
72  
73      /**
74       * Test of findServiceClassName method.
75       *
76       * @throws Exception on error
77       */
78      @Test public void testFindServiceClassName() throws Exception
79      {
80          boolean flag = false;
81  
82          try
83          {
84              ServiceFinder.findServiceClassName(IniParser.class.getName(), null);
85          }
86          catch (IllegalArgumentException x)
87          {
88              flag = true;
89          }
90  
91          assertTrue(flag);
92          System.setProperty(IniParser.class.getName(), DUMMY);
93          assertEquals(DUMMY, ServiceFinder.findServiceClassName(IniParser.class.getName(), IniParser.class.getName()));
94  
95          // System.clearProperty(IniParser.SERVICE_ID); missing in 1.4
96          System.getProperties().remove(IniParser.class.getName());
97          assertEquals(IniParser.class.getName(), ServiceFinder.findServiceClassName(IniParser.class.getName(), IniParser.class.getName()));
98          assertEquals(DUMMY_IMPL, ServiceFinder.findServiceClassName(DUMMY_SERVICE, ""));
99          assertEquals(DUMMY, ServiceFinder.findServiceClassName(BAD_CONFIG_SERVICE, DUMMY));
100         assertEquals(DUMMY, ServiceFinder.findServiceClassName(EMPTY_CONFIG_SERVICE, DUMMY));
101     }
102 }