• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.trace;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import io.opencensus.trace.config.TraceConfig;
22 import io.opencensus.trace.export.ExportComponent;
23 import io.opencensus.trace.propagation.PropagationComponent;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 /** Unit tests for {@link Tracing}. */
31 @RunWith(JUnit4.class)
32 public class TracingTest {
33   @Rule public ExpectedException thrown = ExpectedException.none();
34 
35   @Test
loadTraceComponent_UsesProvidedClassLoader()36   public void loadTraceComponent_UsesProvidedClassLoader() {
37     final RuntimeException toThrow = new RuntimeException("UseClassLoader");
38     thrown.expect(RuntimeException.class);
39     thrown.expectMessage("UseClassLoader");
40     Tracing.loadTraceComponent(
41         new ClassLoader() {
42           @Override
43           public Class<?> loadClass(String name) {
44             throw toThrow;
45           }
46         });
47   }
48 
49   @Test
loadTraceComponent_IgnoresMissingClasses()50   public void loadTraceComponent_IgnoresMissingClasses() {
51     ClassLoader classLoader =
52         new ClassLoader() {
53           @Override
54           public Class<?> loadClass(String name) throws ClassNotFoundException {
55             throw new ClassNotFoundException();
56           }
57         };
58     assertThat(Tracing.loadTraceComponent(classLoader).getClass().getName())
59         .isEqualTo("io.opencensus.trace.TraceComponent$NoopTraceComponent");
60   }
61 
62   @Test
defaultTracer()63   public void defaultTracer() {
64     assertThat(Tracing.getTracer()).isSameAs(Tracer.getNoopTracer());
65   }
66 
67   @Test
defaultBinaryPropagationHandler()68   public void defaultBinaryPropagationHandler() {
69     assertThat(Tracing.getPropagationComponent())
70         .isSameAs(PropagationComponent.getNoopPropagationComponent());
71   }
72 
73   @Test
defaultTraceExporter()74   public void defaultTraceExporter() {
75     assertThat(Tracing.getExportComponent())
76         .isInstanceOf(ExportComponent.newNoopExportComponent().getClass());
77   }
78 
79   @Test
defaultTraceConfig()80   public void defaultTraceConfig() {
81     assertThat(Tracing.getTraceConfig()).isSameAs(TraceConfig.getNoopTraceConfig());
82   }
83 }
84