/*
 * Copyright 2017 The OpenTracing 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.opentracing.contrib.tracerresolver;

import io.opentracing.Tracer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class TracerResolverTest {
    private static final File TRACERRESOLVER_SERVICE_FILE =
            new File("target/test-classes/META-INF/services/" + TracerResolver.class.getName());
    private static final File TRACER_SERVICE_FILE =
            new File("target/test-classes/META-INF/services/" + Tracer.class.getName());

    @Before
    public void prepareClasspath() throws IOException {
        writeServiceFile(TRACER_SERVICE_FILE, Mocks.FallbackTracer.class.getName());
    }

    @After
    public void restoreClasspath() throws IOException {
        if (TRACER_SERVICE_FILE.isFile()) TRACER_SERVICE_FILE.delete();
        if (TRACERRESOLVER_SERVICE_FILE.isFile()) TRACERRESOLVER_SERVICE_FILE.delete();
    }

    @Before
    @After
    public void resetTracerResolver() {
        TracerResolver.reload();
    }

    @Test
    public void testResolveFallback() {
        assertThat(TracerResolver.resolveTracer(), is(instanceOf(Mocks.FallbackTracer.class)));
    }

    @Test
    public void testResolveTracer() throws IOException {
        writeServiceFile(TRACERRESOLVER_SERVICE_FILE, Mocks.MockTracerResolver.class.getName());
        assertThat(TracerResolver.resolveTracer(), is(instanceOf(Mocks.ResolvedTracer.class)));
    }

    @Test
    public void testFallbackWhenResolvingNull() throws IOException {
        writeServiceFile(TRACERRESOLVER_SERVICE_FILE, Mocks.NullTracerResolver.class.getName());
        assertThat(TracerResolver.resolveTracer(), is(instanceOf(Mocks.FallbackTracer.class)));
    }

    @Test
    public void testNothingRegistered() throws IOException {
        TRACER_SERVICE_FILE.delete();
        assertThat(TracerResolver.resolveTracer(), is(nullValue()));
    }

    @Test
    public void testResolvingNullWithoutFallback() throws IOException {
        TRACER_SERVICE_FILE.delete();
        writeServiceFile(TRACERRESOLVER_SERVICE_FILE, Mocks.NullTracerResolver.class.getName());
        assertThat(TracerResolver.resolveTracer(), is(nullValue()));
    }

    private static void writeServiceFile(File serviceFile, String content) throws IOException {
        serviceFile.getParentFile().mkdirs();
        if (serviceFile.isFile()) serviceFile.delete();
        PrintWriter writer = new PrintWriter(new FileWriter(serviceFile));
        try {
            writer.println(content);
        } finally {
            writer.close();
        }
    }

}

@@ -1286,30 +1286,29 @@
 e());%0A%0A    @
-Before
+After
 %0A    public 
@@ -1316,183 +1316,25 @@
 oid 
-prepareClasspath() throws IOException %7B%0A        writeServiceFile(TRACER_SERVICE_FILE, Mocks.FallbackTracer.class.getName());%0A    %7D%0A%0A    @After%0A    public void restoreClasspath
+cleanServiceFiles
 () t
@@ -1671,17 +1671,121 @@
 lback() 
-%7B
+throws IOException %7B%0A        writeServiceFile(TRACER_SERVICE_FILE, Mocks.FallbackTracer.class.getName());
 %0A       
@@ -2181,33 +2181,25 @@
 test
-FallbackWhenResolvingNull
+NothingRegistered
 () t
@@ -2222,105 +2222,8 @@
 n %7B%0A
-        writeServiceFile(TRACERRESOLVER_SERVICE_FILE, Mocks.NullTracerResolver.class.getName());%0A
     
@@ -2276,45 +2276,18 @@
  is(
-instanceOf(Mocks.FallbackTracer.class
+nullValue(
 )));
@@ -2324,33 +2324,41 @@
 oid test
-NothingRegistered
+FallbackWhenResolvingNull
 () throw
@@ -2373,32 +2373,49 @@
 ption %7B%0A        
+writeServiceFile(
 TRACER_SERVICE_F
@@ -2413,33 +2413,160 @@
 SERVICE_FILE
-.delet
+, Mocks.FallbackTracer.class.getName());%0A        writeServiceFile(TRACERRESOLVER_SERVICE_FILE, Mocks.NullTracerResolver.class.getNam
 e()
+)
 ;%0A        as
@@ -2605,34 +2605,61 @@
 racer(), is(
-nullValue(
+instanceOf(Mocks.FallbackTracer.class
 )));%0A    %7D%0A%0A
@@ -2744,46 +2744,8 @@
 n %7B%0A
-        TRACER_SERVICE_FILE.delete();%0A
     

Make it explicit per test case which service files are created.