/*
 * Bach - Java Shell Builder
 * Copyright (C) 2019 Christian Stein
 *
 * 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
 *
 *     https://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 it;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ServiceLoader;
import java.util.spi.ToolProvider;
import org.junit.jupiter.api.Test;

class ToolProviderTests {

  @Test
  void findBachUsingServiceLoader() {
    var bach =
        ServiceLoader.load(ToolProvider.class, getClass().getClassLoader()).stream()
            .map(ServiceLoader.Provider::get)
            .filter(provider -> provider.name().equals("bach"))
            .findFirst();
    assertTrue(bach.isPresent(), "Service 'bach' not found!");
  }

  @Test
  void providerIsMemberOfNamedModule() {
    var bach = ToolProvider.findFirst("bach").orElseThrow();
    assertEquals("de.sormuras.bach", bach.getClass().getModule().getName());
  }

  @Test
  void runVersionYieldsZero() {
    var bach = ToolProvider.findFirst("bach").orElseThrow();
    var out = new PrintWriter(new StringWriter());
    var err = new PrintWriter(new StringWriter());
    assertEquals(0, bach.run(out, err, "version"));
  }

  @Test
  void runUnsupportedArgumentYieldsOne() {
    var bach = ToolProvider.findFirst("bach").orElseThrow();
    var out = new PrintWriter(new StringWriter());
    var err = new PrintWriter(new StringWriter());
    assertEquals(1, bach.run(out, err, "?"));
  }
}

@@ -810,32 +810,59 @@
 o.StringWriter;%0A
+import java.util.Optional;%0A
 import java.util
@@ -981,49 +981,174 @@
 %0A%0A  
-@Test%0A  void findBachUsingService
+private Optional%3CToolProvider%3E findBach() %7B%0A    var currentClassLoader = getClass().getClassLoader();%0A    if (currentClassLoader == ClassLoader.getSystemClass
 Loader()
  %7B%0A 
@@ -1147,34 +1147,74 @@
 er()
+)
  %7B%0A    
-var 
+  return ToolProvider.findFirst(%22
 bach
- =
+%22);
 %0A    
+%7D%0A
     
+return 
 Serv
@@ -1248,29 +1248,22 @@
 .class, 
-getClass().ge
+curren
 tClassLo
@@ -1266,18 +1266,16 @@
 ssLoader
-()
 ).stream
@@ -1409,24 +1409,75 @@
 indFirst();%0A
+  %7D%0A%0A  @Test%0A  void findBachUsingServiceLoader() %7B%0A
     assertTr
@@ -1479,20 +1479,26 @@
 ertTrue(
-b
+findB
 ach
+()
 .isPrese
@@ -1591,69 +1591,8 @@
 ) %7B%0A
-    var bach = ToolProvider.findFirst(%22bach%22).orElseThrow();%0A
     
@@ -1624,20 +1624,40 @@
 .bach%22, 
-bach
+findBach().orElseThrow()
 .getClas
@@ -1733,69 +1733,8 @@
 ) %7B%0A
-    var bach = ToolProvider.findFirst(%22bach%22).orElseThrow();%0A
     
@@ -1851,20 +1851,40 @@
 uals(0, 
-bach
+findBach().orElseThrow()
 .run(out
@@ -1963,69 +1963,8 @@
 ) %7B%0A
-    var bach = ToolProvider.findFirst(%22bach%22).orElseThrow();%0A
     
@@ -2081,20 +2081,40 @@
 uals(1, 
-bach
+findBach().orElseThrow()
 .run(out

Use current class loader instance