/*
 * Copyright 2017 ThoughtWorks, Inc.
 *
 * 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 com.thoughtworks.go.domain.materials.tfs;

import com.thoughtworks.go.util.NestedJarClassLoader;
import com.thoughtworks.go.util.command.UrlArgument;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URISyntaxException;

import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;

public class TfsSDKCommandBuilderTest {
    private TfsSDKCommandBuilder builder;
    private final String DOMAIN = "CORPORATE";
    private final String USERNAME = "userName";
    private final String PASSWORD = "password";
    private final String computedWorkspaceName = "boo-yaa-goo-moo-foo";
    private NestedJarClassLoader mockSdkLoader;

    @Before
    public void setup() throws IOException, URISyntaxException {
        mockSdkLoader = mock(NestedJarClassLoader.class);
        builder = new TfsSDKCommandBuilder(mockSdkLoader);
    }

    @Test
    public void shouldLoadTheCorrectImplementationOfSDKCommandViaTheNestedClassLoader() throws Exception {
        when(mockSdkLoader.loadClass("com.thoughtworks.go.tfssdk.TfsSDKCommandTCLAdapter")).thenThrow(new RuntimeException());
        try {
            builder.buildTFSSDKCommand(null,new UrlArgument("url"), DOMAIN, USERNAME, PASSWORD, computedWorkspaceName, "$/project");
            fail("should have failed to load class as we are not wiring any dependencies");
        } catch (Exception e) {
            //Do not worry about load class failing. We're only asserting that load class is invoked with the right FQN for TFSSDKCommand
        }
        verify(mockSdkLoader, times(1)).loadClass("com.thoughtworks.go.tfssdk.TfsSDKCommandTCLAdapter");
    }
}

@@ -16,9 +16,9 @@
  201
-7
+8
  Tho
@@ -651,62 +651,8 @@
 s;%0A%0A
-import com.thoughtworks.go.util.NestedJarClassLoader;%0A
 impo
@@ -814,16 +814,60 @@
 ption;%0A%0A
+import static org.hamcrest.CoreMatchers.is;%0A
 import s
@@ -889,20 +889,26 @@
 .Assert.
-fail
+assertThat
 ;%0Aimport
@@ -923,25 +923,25 @@
 org.
-mockito.Mockito.*
+junit.Assert.fail
 ;%0A%0Ap
@@ -1248,25 +1248,16 @@
 private 
-NestedJar
 ClassLoa
@@ -1274,16 +1274,75 @@
 kLoader;
+%0A    private boolean invoked;%0A    private String className;
 %0A%0A    @B
@@ -1424,56 +1424,407 @@
     
-mockSdkLoader = mock(NestedJarClassLoader.class)
+// we intentionally don't use mockito to mock this%0A        // see https://bugs.openjdk.java.net/browse/JDK-8202758%0A        mockSdkLoader = new ClassLoader() %7B%0A            @Override%0A            public Class%3C?%3E loadClass(String name) throws ClassNotFoundException %7B%0A                invoked = true;%0A                className = name;%0A                throw new RuntimeException();%0A            %7D%0A        %7D
 ;%0A  
@@ -2008,135 +2008,8 @@
 n %7B%0A
-        when(mockSdkLoader.loadClass(%22com.thoughtworks.go.tfssdk.TfsSDKCommandTCLAdapter%22)).thenThrow(new RuntimeException());%0A
     
@@ -2435,48 +2435,70 @@
     
-verify(mockSdkLoader, times(1)).loadClas
+assertThat(invoked, is(true));%0A        assertThat(className, i
 s(%22c
@@ -2548,16 +2548,17 @@
 dapter%22)
+)
 ;%0A    %7D%0A

Fix failing test on java 10/11. Fixes #5174