001 /* 002 * Copyright 2005,2009 Ivan SZKIBA 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.ini4j.spi; 017 018 import org.ini4j.Ini4jCase; 019 020 import org.ini4j.test.Helper; 021 022 import static org.junit.Assert.assertEquals; 023 import static org.junit.Assert.assertNull; 024 import static org.junit.Assert.assertTrue; 025 026 import org.junit.Test; 027 028 public class ServiceFinderTest extends Ini4jCase 029 { 030 static final String DUMMY = "dummy"; 031 static final String DUMMY_SERVICE = "org.ini4j.Dummy"; 032 static final String BAD_CONFIG_SERVICE = "org.ini4j.BadConfig"; 033 static final String EMPTY_CONFIG_SERVICE = "org.ini4j.EmptyConfig"; 034 static final String DUMMY_IMPL = "DummyImpl"; 035 036 @Test public void testFindService() throws Exception 037 { 038 boolean flag = false; 039 040 System.setProperty(IniParser.class.getName(), Helper.class.getName()); 041 try 042 { 043 ServiceFinder.findService(IniParser.class); 044 } 045 catch (IllegalArgumentException x) 046 { 047 flag = true; 048 } 049 050 // System.clearProperty(IniParser.SERVICE_ID); missing in 1.4 051 System.getProperties().remove(IniParser.class.getName()); 052 assertTrue(flag); 053 } 054 055 @Test public void testFindServiceClass() throws Exception 056 { 057 boolean flag = false; 058 059 System.setProperty(IniParser.class.getName(), DUMMY); 060 try 061 { 062 ServiceFinder.findServiceClass(IniParser.class); 063 } 064 catch (IllegalArgumentException x) 065 { 066 flag = true; 067 } 068 069 // System.clearProperty(IniParser.SERVICE_ID); missing in 1.4 070 System.getProperties().remove(IniParser.class.getName()); 071 assertTrue(flag); 072 } 073 074 @Test public void testFindServiceClassName() throws Exception 075 { 076 System.setProperty(IniParser.class.getName(), DUMMY); 077 assertEquals(DUMMY, ServiceFinder.findServiceClassName(IniParser.class.getName())); 078 079 // System.clearProperty(IniParser.SERVICE_ID); missing in 1.4 080 System.getProperties().remove(IniParser.class.getName()); 081 assertNull(ServiceFinder.findServiceClassName(IniParser.class.getName())); 082 assertEquals(DUMMY_IMPL, ServiceFinder.findServiceClassName(DUMMY_SERVICE)); 083 assertNull(DUMMY, ServiceFinder.findServiceClassName(BAD_CONFIG_SERVICE)); 084 assertNull(DUMMY, ServiceFinder.findServiceClassName(EMPTY_CONFIG_SERVICE)); 085 } 086 }