13604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes/* 23604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes * Copyright (C) 2010 The Android Open Source Project 3f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 43604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 53604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes * you may not use this file except in compliance with the License. 63604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes * You may obtain a copy of the License at 7f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 83604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 103604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes * Unless required by applicable law or agreed to in writing, software 113604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 123604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes * See the License for the specific language governing permissions and 143604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes * limitations under the License. 153604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes */ 163604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes 174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.util; 183604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes 194557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.util.Iterator; 20ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughesimport java.util.ServiceConfigurationError; 214557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.util.ServiceLoader; 223604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes 233604384c5f53c83383ce85f838901e46b0105e5eElliott Hughespublic class ServiceLoaderTest extends junit.framework.TestCase { 24ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public static interface UnimplementedInterface { } 25ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_noImplementations() { 26ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertFalse(ServiceLoader.load(UnimplementedInterface.class).iterator().hasNext()); 27ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 28ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 29ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public static class Impl1 implements ServiceLoaderTestInterface { } 30ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public static class Impl2 implements ServiceLoaderTestInterface { } 31ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_implementations() { 32ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ServiceLoader<ServiceLoaderTestInterface> loader = ServiceLoader.load(ServiceLoaderTestInterface.class); 33ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes Iterator<ServiceLoaderTestInterface> it = loader.iterator(); 34ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(it.hasNext()); 35ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(it.next() instanceof Impl1); 36ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(it.hasNext()); 37ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(it.next() instanceof Impl2); 38ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertFalse(it.hasNext()); 39ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 40ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 41ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Something like "does.not.Exist", that is a well-formed class name, but just doesn't exist. 42ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_missingRegisteredClass() throws Exception { 43ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 44ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ServiceLoader.load(ServiceLoaderTestInterfaceMissingClass.class).iterator().next(); 45ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 46ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (ServiceConfigurationError expected) { 47ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(expected.getCause() instanceof ClassNotFoundException); 48ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 49ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 50ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes 51ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Something like "java.lang.String", that is a class that exists, 52ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // but doesn't implement the interface. 53ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_wrongTypeRegisteredClass() throws Exception { 54ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 55ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ServiceLoader.load(ServiceLoaderTestInterfaceWrongType.class).iterator().next(); 56ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 57ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (ServiceConfigurationError expected) { 58ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes assertTrue(expected.getCause() instanceof ClassCastException); 593604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes } 60ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 613604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes 62ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes // Something like "This is not a class name!" that's just a parse error. 63ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes public void test_invalidRegisteredClass() throws Exception { 64ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes try { 65ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes ServiceLoader.load(ServiceLoaderTestInterfaceParseError.class).iterator().next(); 66ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes fail(); 67ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } catch (ServiceConfigurationError expected) { 683604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes } 69ea1caf4ccc5c2255f384c0774aa9e055763a6a41Elliott Hughes } 703604384c5f53c83383ce85f838901e46b0105e5eElliott Hughes} 71