1 /* 2 * Copyright 2016-17, OpenCensus Authors 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 17 package io.opencensus.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import java.util.ServiceConfigurationError; 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 import org.junit.runners.JUnit4; 25 26 /** Tests for {@link Provider}. */ 27 @RunWith(JUnit4.class) 28 public class ProviderTest { 29 static class GoodClass { GoodClass()30 public GoodClass() {} 31 } 32 33 static class PrivateConstructorClass { PrivateConstructorClass()34 private PrivateConstructorClass() {} 35 } 36 37 static class NoDefaultConstructorClass { NoDefaultConstructorClass(int arg)38 public NoDefaultConstructorClass(int arg) {} 39 } 40 41 private static class PrivateClass {} 42 43 static interface MyInterface {} 44 45 static class MyInterfaceImpl implements MyInterface { MyInterfaceImpl()46 public MyInterfaceImpl() {} 47 } 48 49 @Test(expected = ServiceConfigurationError.class) createInstance_ThrowsErrorWhenClassIsPrivate()50 public void createInstance_ThrowsErrorWhenClassIsPrivate() throws ClassNotFoundException { 51 Provider.createInstance( 52 Class.forName( 53 "io.opencensus.internal.ProviderTest$PrivateClass", 54 true, 55 ProviderTest.class.getClassLoader()), 56 PrivateClass.class); 57 } 58 59 @Test(expected = ServiceConfigurationError.class) createInstance_ThrowsErrorWhenClassHasPrivateConstructor()60 public void createInstance_ThrowsErrorWhenClassHasPrivateConstructor() 61 throws ClassNotFoundException { 62 Provider.createInstance( 63 Class.forName( 64 "io.opencensus.internal.ProviderTest$PrivateConstructorClass", 65 true, 66 ProviderTest.class.getClassLoader()), 67 PrivateConstructorClass.class); 68 } 69 70 @Test(expected = ServiceConfigurationError.class) createInstance_ThrowsErrorWhenClassDoesNotHaveDefaultConstructor()71 public void createInstance_ThrowsErrorWhenClassDoesNotHaveDefaultConstructor() 72 throws ClassNotFoundException { 73 Provider.createInstance( 74 Class.forName( 75 "io.opencensus.internal.ProviderTest$NoDefaultConstructorClass", 76 true, 77 ProviderTest.class.getClassLoader()), 78 NoDefaultConstructorClass.class); 79 } 80 81 @Test(expected = ServiceConfigurationError.class) createInstance_ThrowsErrorWhenClassIsNotASubclass()82 public void createInstance_ThrowsErrorWhenClassIsNotASubclass() throws ClassNotFoundException { 83 Provider.createInstance( 84 Class.forName( 85 "io.opencensus.internal.ProviderTest$GoodClass", 86 true, 87 ProviderTest.class.getClassLoader()), 88 MyInterface.class); 89 } 90 91 @Test createInstance_GoodClass()92 public void createInstance_GoodClass() throws ClassNotFoundException { 93 assertThat( 94 Provider.createInstance( 95 Class.forName( 96 "io.opencensus.internal.ProviderTest$GoodClass", 97 true, 98 ProviderTest.class.getClassLoader()), 99 GoodClass.class)) 100 .isNotNull(); 101 } 102 103 @Test createInstance_GoodSubclass()104 public void createInstance_GoodSubclass() throws ClassNotFoundException { 105 assertThat( 106 Provider.createInstance( 107 Class.forName( 108 "io.opencensus.internal.ProviderTest$MyInterfaceImpl", 109 true, 110 ProviderTest.class.getClassLoader()), 111 MyInterface.class)) 112 .isNotNull(); 113 } 114 } 115