/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * 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.theoryinpractice.testng.model;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.theoryinpractice.testng.configuration.TestNGConfiguration;
import com.theoryinpractice.testng.configuration.TestNGRunnableState;
import org.testng.remote.strprotocol.*;

import java.net.SocketTimeoutException;

/**
 * The client side of the RemoteTestRunner. Handles the marshaling of the different messages.
 */
public class IDEARemoteTestRunnerClient extends AbstractRemoteTestRunnerClient
{

  private TestNGRemoteListener myListener;
  private int myPort;

  public synchronized void startListening(TestNGConfiguration config) {
      final IMessageSender messageSender = TestNGRunnableState.supportSerializationProtocol(config)
                                           ? new SerializedMessageSender("localhost", myPort)
                                           : new StringMessageSender("localhost", myPort);
      final ServerConnection srvConnection = new ServerConnection(messageSender) {
        @Override
        protected void handleThrowable(Throwable cause) {
          cause.printStackTrace();
        }
      };
      new Thread() {
        @Override
        public void run() {
          try {
            messageSender.initReceiver();
          }
          catch (SocketTimeoutException e) {
            e.printStackTrace();
          }
          startListening(new IRemoteSuiteListener[]{myListener},
                         new IRemoteTestListener[]{myListener},
                         srvConnection
          );
        }
      }.start();
    }

    @Override
    protected void notifyStart(final GenericMessage genericMessage) {
        ApplicationManager.getApplication().invokeAndWait(new Runnable()
        {
            public void run() {
                for (final IRemoteSuiteListener listener : m_suiteListeners) {
                    listener.onInitialization(genericMessage);
                }
            }
        }, ModalityState.NON_MODAL);
    }

    @Override
    protected void notifySuiteEvents(final SuiteMessage suiteMessage) {
        ApplicationManager.getApplication().invokeAndWait(new Runnable()
        {
            public void run() {
                for (final IRemoteSuiteListener listener : m_suiteListeners) {
                    if (suiteMessage.isMessageOnStart()) {
                        listener.onStart(suiteMessage);
                    } else {
                        listener.onFinish(suiteMessage);
                    }
                }
            }
        }, ModalityState.NON_MODAL);
    }

    @Override
    protected void notifyTestEvents(final TestMessage testMessage) {
        ApplicationManager.getApplication().invokeAndWait(new Runnable()
        {
            public void run() {
                for (final IRemoteTestListener listener : m_testListeners) {
                    if (testMessage.isMessageOnStart()) {
                        listener.onStart(testMessage);
                    } else {
                        listener.onFinish(testMessage);
                    }
                }
            }
        }, ModalityState.NON_MODAL);
    }

    @Override
    protected void notifyResultEvents(final TestResultMessage testResultMessage) {
        ApplicationManager.getApplication().invokeAndWait(new Runnable()
        {
            public void run() {
                for (final IRemoteTestListener listener : m_testListeners) {
                    switch (testResultMessage.getResult()) {
                        case MessageHelper.TEST_STARTED:
                            listener.onTestStart(testResultMessage);
                            break;
                        case MessageHelper.PASSED_TEST:
                            listener.onTestSuccess(testResultMessage);
                            break;
                        case MessageHelper.FAILED_TEST:
                            listener.onTestFailure(testResultMessage);
                            break;
                        case MessageHelper.SKIPPED_TEST:
                            listener.onTestSkipped(testResultMessage);
                            break;
                        case MessageHelper.FAILED_ON_PERCENTAGE_TEST:
                            listener.onTestFailedButWithinSuccessPercentage(testResultMessage);
                            break;
                    }
                }
            }
        }, ModalityState.NON_MODAL);
    }

  public void prepareListening(TestNGRemoteListener listener, int port) {
    myListener = listener;
    myPort = port;
  }

}

@@ -1872,79 +1872,8 @@
  %7D;%0A
-      new Thread() %7B%0A        @Override%0A        public void run() %7B%0A    
     
@@ -1888,20 +1888,16 @@
         
-    
 messageS
@@ -1920,36 +1920,32 @@
 er();%0A      
-    
 %7D%0A      
     catch (S
@@ -1932,20 +1932,16 @@
 %7D%0A      
- 
-   
 catch (S
@@ -1975,20 +1975,16 @@
         
-    
 e.printS
@@ -1998,34 +1998,26 @@
 ce();%0A      
-  
-  %7D%0A    
+%7D%0A
       startL
@@ -2086,20 +2086,16 @@
         
-   
- 
 new IRem
@@ -2146,20 +2146,16 @@
         
-    
 srvConne
@@ -2163,46 +2163,8 @@
 tion
-%0A          );%0A        %7D%0A      %7D.start(
 );%0A 
@@ -2295,39 +2295,37 @@
 ication().invoke
-AndWait
+Later
 (new Runnable()%0A
@@ -2712,39 +2712,37 @@
 ication().invoke
-AndWait
+Later
 (new Runnable()%0A
@@ -3286,39 +3286,37 @@
 ication().invoke
-AndWait
+Later
 (new Runnable()%0A
@@ -3881,15 +3881,13 @@
 voke
-AndWait
+Later
 (new

update to support testng >= 6.1 (IDEA-73123)