/*
 * Copyright 2017, OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.opencensus.tags;

import static com.google.common.truth.Truth.assertThat;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link Tags}. */
@RunWith(JUnit4.class)
public class TagsTest {
  @Rule public ExpectedException thrown = ExpectedException.none();

  @Test
  public void loadTagsComponent_UsesProvidedClassLoader() {
    final RuntimeException toThrow = new RuntimeException("UseClassLoader");
    thrown.expect(RuntimeException.class);
    thrown.expectMessage("UseClassLoader");
    Tags.loadTagsComponent(
        new ClassLoader() {
          @Override
          public Class<?> loadClass(String name) {
            throw toThrow;
          }
        });
  }

  @Test
  public void loadTagsComponent_IgnoresMissingClasses() {
    ClassLoader classLoader =
        new ClassLoader() {
          @Override
          public Class<?> loadClass(String name) throws ClassNotFoundException {
            throw new ClassNotFoundException();
          }
        };
    assertThat(Tags.loadTagsComponent(classLoader).getClass().getName())
        .isEqualTo("io.opencensus.tags.NoopTags$NoopTagsComponent");
  }

  @Test
  public void getState() {
    assertThat(Tags.getState()).isEqualTo(TaggingState.DISABLED);
  }

  @Test
  @SuppressWarnings("deprecation")
  public void setState_IgnoresInput() {
    Tags.setState(TaggingState.ENABLED);
    assertThat(Tags.getState()).isEqualTo(TaggingState.DISABLED);
  }

  @Test(expected = NullPointerException.class)
  @SuppressWarnings("deprecation")
  public void setState_DisallowsNull() {
    Tags.setState(null);
  }

  @Test
  public void defaultTagger() {
    assertThat(Tags.getTagger()).isEqualTo(NoopTags.getNoopTagger());
  }

  @Test
  public void defaultTagContextSerializer() {
    assertThat(Tags.getTagPropagationComponent())
        .isEqualTo(NoopTags.getNoopTagPropagationComponent());
  }
}

@@ -1854,111 +1854,206 @@
 %0A%0A  
-@Test%0A  public void getState() %7B%0A    assertThat(Tags.getState()).isEqualTo(TaggingState.DISABLED);%0A  %7D%0A
+// There is only one test that modifies tagging state in the Tags class, since the state is%0A  // global, and it could affect other tests. NoopTagsTest has more thorough tests for tagging%0A  // state.
 %0A  @
@@ -2106,24 +2106,55 @@
 ic void 
+testState() %7B%0A    // Test that 
 setState
 _Ignores
@@ -2149,25 +2149,27 @@
 tate
-_I
+ i
 gnores
-Input() %7B
+ its input.
 %0A   
@@ -2276,43 +2276,98 @@
 D);%0A
+%0A
   
-%7D%0A%0A  @Test(expected = NullPointer
+  // Test that setState cannot be called after getState.%0A    thrown.expect(IllegalState
 Exce
@@ -2382,84 +2382,80 @@
 ass)
+;
 %0A  
-@SuppressWarnings(%22deprecation%22)%0A  public void
+  thrown.expectMessage(%22State was already read, cannot
  set
-S
+ s
 tate
-_DisallowsNull() %7B
+.%22);
 %0A   
@@ -2473,12 +2473,28 @@
 ate(
-null
+TaggingState.ENABLED
 );%0A 

Combine tests for Tags.getState and Tags.setState.