/*************************GO-LICENSE-START*********************************
 * Copyright 2014 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.
 *************************GO-LICENSE-END***********************************/

package com.thoughtworks.go.domain.materials.perforce;

import com.thoughtworks.go.config.materials.perforce.P4Material;
import com.thoughtworks.go.helper.P4TestRepo;
import com.thoughtworks.go.util.command.CommandLineException;
import com.thoughtworks.go.util.command.ConsoleResult;

public class P4Fixture {
    private P4TestRepo repo;

    public void start() {
    }

    public void setRepo(P4TestRepo repo) {
        this.repo = repo;
    }

    public void stop(P4Client p4) {
        stopP4d(p4);
        repo.onTearDown();
    }

    private void stopP4d(P4Client p4) {
        try {
            p4.admin("stop");
            ConsoleResult consoleResult = p4.checkConnection();
            while (!consoleResult.failed()) {
                try {
                    //Wait for the server to shutdown
                    Thread.sleep(100);
                } catch (InterruptedException ignored) {
                }
            }
        } catch (CommandLineException expected) {
            if (isKnownWindowsError(expected)) {
                return;
            }
            if (expected.getResult().errorAsString().contains("Connection refused")) {
                return;
            }
            throw expected;
        }
    }

    private boolean isKnownWindowsError(CommandLineException expected) {
        // Stopping p4d on windows returns the following failure:
        return expected.getResult().errorAsString().contains("WSAECONNRESET") || expected.getResult().errorAsString().contains("WSAECONNREFUSED");
    }

    public String port() {
        return repo.serverAndPort();
    }

    public P4Client createClient() throws Exception {
        return repo.createClient();
    }

    public P4Client createClient(String name, String view) throws Exception {
        return repo.createClientWith(name, view);
    }

    public P4Material material(String view) {
        return repo.material(view);

    }

    public P4Material material(String view, String dest) {
        P4Material p4Material = material(view);
        p4Material.setFolder(dest);

        return p4Material;
    }
}

@@ -1745,407 +1745,270 @@
     
-if (isKnownWindowsError(expected)) %7B%0A                return;%0A            %7D%0A            if (expected.getResult().errorAsString().contains(%22Connection refused%22)) %7B%0A                return;%0A            %7D%0A            throw expected;%0A        %7D%0A    %7D%0A%0A    private boolean isKnownWindowsError(CommandLineException expected) %7B%0A        // Stopping p4d on windows returns the following failure:%0A        return 
+// Stopping p4d on windows returns the following failure:%0A            if (expected.getResult().errorAsString().contains(%22WSAECONNRESET%22) %7C%7C expected.getResult().errorAsString().contains(%22WSAECONNREFUSED%22)) %7B%0A                return;%0A            %7D%0A            if (
 expe
@@ -2046,37 +2046,42 @@
 ).contains(%22
-WSAECONNRESET
+Connection refused
 %22) %7C%7C expect
@@ -2125,26 +2125,105 @@
 ns(%22
-WSAECONNREFUSED%22);
+Connection reset%22)) %7B%0A                return;%0A            %7D%0A            throw expected;%0A        %7D
 %0A   

Fix specs on windows