/*
 * Copyright 2017 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * 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.opentracing.contrib.agent;

import static org.junit.Assert.*;

import java.lang.reflect.Field;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import io.opentracing.NoopTracerFactory;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.mock.MockSpan;
import io.opentracing.mock.MockTracer;
import io.opentracing.util.GlobalTracer;

public class OpenTracingHelperTest {

    // Approach used in opentracing-util GlobalTracerTest to reset the global tracer
    private static void _setGlobal(Tracer tracer) {
        try {
            Field globalTracerField = GlobalTracer.class.getDeclaredField("tracer");
            globalTracerField.setAccessible(true);
            globalTracerField.set(null, tracer);
            globalTracerField.setAccessible(false);
        } catch (Exception e) {
            throw new RuntimeException("Error reflecting globalTracer: " + e.getMessage(), e);
        }
    }

    @Before
    @After
    public void clearGlobalTracer() {
        _setGlobal(NoopTracerFactory.create());
    }

    @Test
    public void testActivateDeactivateSpan() {
        OpenTracingHelper helper = new OpenTracingHelper(null);

        Span span = new MockTracer().buildSpan("Test").start();

        assertNull(helper.currentSpan());

        helper.activateSpan(span);

        assertEquals(span, helper.currentSpan());

        helper.deactivateCurrentSpan();

        assertNull(helper.currentSpan());
    }

    @Test
    public void testAssociateSpan() {
        OpenTracingHelper helper = new OpenTracingHelper(null);

        Object obj = new Object();
        Span span = new MockTracer().buildSpan("Test").start();

        helper.associateSpan(obj, span);

        assertEquals(span, helper.retrieveSpan(obj));
    }

    @Test
    public void testState() {
        OpenTracingHelper helper = new OpenTracingHelper(null);

        Object obj = new Object();

        helper.setState(obj, 5);

        assertEquals(5, helper.getState(obj));
    }

    @Test
    public void testGetTracerResolved() {
        OpenTracingHelper helper = new OpenTracingHelper(null);
        Tracer tracer = helper.getTracer();

        assertNotNull(tracer);

        try {
            tracer.buildSpan("Test");
            fail("Service loadable DummyTracer was not used");
        } catch (DummyTracer.DummyCalled e) {
            // Ignore
        }
    }

    @Test
    public void testGetTracerExisting() {
        GlobalTracer.register(new MockTracer());

        OpenTracingHelper helper = new OpenTracingHelper(null);
        Tracer tracer = helper.getTracer();

        assertNotNull(tracer);

        assertTrue(tracer.buildSpan("Test").start() instanceof MockSpan);
    }

}

@@ -741,17 +741,160 @@
 .Assert.
-*
+assertEquals;%0Aimport static org.junit.Assert.assertNotNull;%0Aimport static org.junit.Assert.assertNull;%0Aimport static org.junit.Assert.assertTrue
 ;%0A%0Aimpor
@@ -2850,32 +2850,72 @@
     %7D%0A%0A    @Test
+(expected=DummyTracer.DummyCalled.class)
 %0A    public void
@@ -3082,34 +3082,16 @@
 acer);%0A%0A
-        try %7B%0A    
         
@@ -3120,149 +3120,8 @@
 %22);%0A
-            fail(%22Service loadable DummyTracer was not used%22);%0A        %7D catch (DummyTracer.DummyCalled e) %7B%0A            // Ignore%0A        %7D%0A
     

Update test following review