View Javadoc

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