Home | History | Annotate | Download | only in stats
      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.stats;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import org.junit.Rule;
     22 import org.junit.Test;
     23 import org.junit.rules.ExpectedException;
     24 import org.junit.runner.RunWith;
     25 import org.junit.runners.JUnit4;
     26 
     27 /** Tests for {@link Stats}. */
     28 @RunWith(JUnit4.class)
     29 public final class StatsTest {
     30   @Rule public ExpectedException thrown = ExpectedException.none();
     31 
     32   @Test
     33   public void loadStatsManager_UsesProvidedClassLoader() {
     34     final RuntimeException toThrow = new RuntimeException("UseClassLoader");
     35     thrown.expect(RuntimeException.class);
     36     thrown.expectMessage("UseClassLoader");
     37     Stats.loadStatsComponent(
     38         new ClassLoader() {
     39           @Override
     40           public Class<?> loadClass(String name) {
     41             throw toThrow;
     42           }
     43         });
     44   }
     45 
     46   @Test
     47   public void loadStatsManager_IgnoresMissingClasses() {
     48     ClassLoader classLoader =
     49         new ClassLoader() {
     50           @Override
     51           public Class<?> loadClass(String name) throws ClassNotFoundException {
     52             throw new ClassNotFoundException();
     53           }
     54         };
     55 
     56     assertThat(Stats.loadStatsComponent(classLoader).getClass().getName())
     57         .isEqualTo("io.opencensus.stats.NoopStats$NoopStatsComponent");
     58   }
     59 
     60   @Test
     61   public void defaultValues() {
     62     assertThat(Stats.getStatsRecorder()).isEqualTo(NoopStats.getNoopStatsRecorder());
     63     assertThat(Stats.getViewManager()).isInstanceOf(NoopStats.newNoopViewManager().getClass());
     64   }
     65 
     66   @Test
     67   public void getState() {
     68     assertThat(Stats.getState()).isEqualTo(StatsCollectionState.DISABLED);
     69   }
     70 
     71   @Test
     72   @SuppressWarnings("deprecation")
     73   public void setState_IgnoresInput() {
     74     Stats.setState(StatsCollectionState.ENABLED);
     75     assertThat(Stats.getState()).isEqualTo(StatsCollectionState.DISABLED);
     76   }
     77 
     78   @Test
     79   @SuppressWarnings("deprecation")
     80   public void setState_DisallowsNull() {
     81     thrown.expect(NullPointerException.class);
     82     thrown.expectMessage("state");
     83     Stats.setState(null);
     84   }
     85 }
     86