add tests + bugfix
authorEugene.Petrenko <eugene.petrenko@gmail.com>
Thu, 7 Jul 2011 16:45:39 +0000 (20:45 +0400)
committerEugene.Petrenko <eugene.petrenko@gmail.com>
Thu, 7 Jul 2011 16:45:39 +0000 (20:45 +0400)
nuget-agent/src/jetbrains/buildServer/nuget/agent/util/DelegatingBuildProcess.java
nuget-tests/src/jetbrains/buildServer/nuget/tests/util/BuildProcessTestCase.java [new file with mode: 0644]
nuget-tests/src/jetbrains/buildServer/nuget/tests/util/CompositeBuildProcessTest.java
nuget-tests/src/jetbrains/buildServer/nuget/tests/util/DelegatingBuildProcessTest.java [new file with mode: 0644]

index 9e9060eaa910e74b702cc730a6a9014db91db264..a82171794ba807179135e214d63b24333db3a778 100644 (file)
@@ -35,14 +35,6 @@ public class DelegatingBuildProcess extends BuildProcessBase {
     myAction = action;\r
   }\r
 \r
-  @Override\r
-  public final void start() throws RunBuildException {\r
-    super.start();\r
-    if (isInterrupted()) return;\r
-\r
-    myReference.set(myAction.startImpl());\r
-  }\r
-\r
   @Override\r
   protected final void interruptImpl() {\r
     super.interruptImpl();\r
@@ -52,20 +44,17 @@ public class DelegatingBuildProcess extends BuildProcessBase {
 \r
   @Override\r
   protected final BuildFinishedStatus waitForImpl() throws RunBuildException {\r
-    BuildProcess process = myReference.get();\r
-    if (isInterrupted()) return BuildFinishedStatus.INTERRUPTED;\r
-\r
     try {\r
-      if (process != null) {\r
-        process.start();\r
-        return process.waitFor();\r
-      }\r
+      BuildProcess process = myAction.startImpl();\r
+      myReference.set(process);\r
+\r
+      if (isInterrupted()) return BuildFinishedStatus.INTERRUPTED;\r
+      process.start();\r
+      return process.waitFor();\r
     } finally {\r
+      myReference.set(null);\r
       myAction.finishedImpl();\r
     }\r
-\r
-    if (isInterrupted()) return BuildFinishedStatus.INTERRUPTED;\r
-    return BuildFinishedStatus.FINISHED_SUCCESS;\r
   }\r
 \r
   public static interface Action {\r
diff --git a/nuget-tests/src/jetbrains/buildServer/nuget/tests/util/BuildProcessTestCase.java b/nuget-tests/src/jetbrains/buildServer/nuget/tests/util/BuildProcessTestCase.java
new file mode 100644 (file)
index 0000000..850aeae
--- /dev/null
@@ -0,0 +1,144 @@
+/*\r
+ * Copyright 2000-2011 JetBrains s.r.o.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package jetbrains.buildServer.nuget.tests.util;\r
+\r
+import jetbrains.buildServer.BaseTestCase;\r
+import jetbrains.buildServer.RunBuildException;\r
+import jetbrains.buildServer.agent.BuildFinishedStatus;\r
+import jetbrains.buildServer.agent.BuildProcess;\r
+import jetbrains.buildServer.util.StringUtil;\r
+import org.jetbrains.annotations.NotNull;\r
+import org.jetbrains.annotations.Nullable;\r
+import org.testng.Assert;\r
+import org.testng.annotations.BeforeMethod;\r
+import org.testng.annotations.DataProvider;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 07.07.11 20:08\r
+ */\r
+public class BuildProcessTestCase extends BaseTestCase {\r
+  private List<String> myLog;\r
+\r
+  protected synchronized void log(@NotNull String message) {\r
+    myLog.add(message);\r
+  }\r
+\r
+  @BeforeMethod\r
+  @Override\r
+  protected void setUp() throws Exception {\r
+    super.setUp();\r
+    myLog = new ArrayList<String>();\r
+  }\r
+\r
+  @DataProvider(name = "buildFinishStatuses")\r
+  public Object[][] buildStatuses() {\r
+    List<Object[]> list = new ArrayList<Object[]>();\r
+    for (BuildFinishedStatus val : BuildFinishedStatus.values()) {\r
+      list.add(new Object[]{val});\r
+    }\r
+    return list.toArray(new Object[list.size()][]);\r
+  }\r
+\r
+  protected void assertRunSuccessfully(@NotNull BuildProcess proc, @NotNull BuildFinishedStatus result) {\r
+    BuildFinishedStatus status = null;\r
+    try {\r
+      proc.start();\r
+      status = proc.waitFor();\r
+    } catch (RunBuildException e) {\r
+      Assert.fail("Failed with exception " + e);\r
+    }\r
+\r
+    Assert.assertEquals(result, status);\r
+  }\r
+\r
+  protected void assertRunException(@NotNull BuildProcess proc, @NotNull String message) {\r
+    try {\r
+      proc.start();\r
+      proc.waitFor();\r
+      Assert.fail("Exception expected");\r
+    } catch (RunBuildException e) {\r
+      Assert.assertEquals(message, e.getMessage());\r
+    }\r
+  }\r
+\r
+  protected void assertLog(String... gold) {\r
+    String actual = StringUtil.join(myLog, "\n");\r
+    String expected = StringUtil.join(gold, "\n");\r
+    Assert.assertEquals(actual, expected);\r
+  }\r
+\r
+  /**\r
+  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+  * Date: 07.07.11 20:07\r
+  */\r
+  protected class RecordingBuildProcess implements BuildProcess {\r
+    private final String myId;\r
+    private final BuildFinishedStatus myResultStatus;\r
+    private Throwable myStartException;\r
+    private Throwable myFinishException;\r
+\r
+    RecordingBuildProcess(@NotNull String id,\r
+                          @Nullable final BuildFinishedStatus resultStatus) {\r
+      myId = id;\r
+      myResultStatus = resultStatus;\r
+    }\r
+\r
+    public void setStartException(Exception startException) {\r
+      myStartException = startException;\r
+    }\r
+\r
+    public void setFinishException(Exception finishException) {\r
+      myFinishException = finishException;\r
+    }\r
+\r
+    public void start() throws RunBuildException {\r
+      log("start-" + myId);\r
+      throwExceptionIfPossible(myStartException);\r
+    }\r
+\r
+    private void throwExceptionIfPossible(Throwable ex) throws RunBuildException {\r
+      if (ex != null) {\r
+        if (ex instanceof RunBuildException) throw (RunBuildException) ex;\r
+        if (ex instanceof RuntimeException) throw (RuntimeException) ex;\r
+        throw (Error) ex;\r
+      }\r
+    }\r
+\r
+    public boolean isInterrupted() {\r
+      return false;\r
+    }\r
+\r
+    public boolean isFinished() {\r
+      return false;\r
+    }\r
+\r
+    public void interrupt() {\r
+      log("interrupt-" + myId);\r
+    }\r
+\r
+    @NotNull\r
+    public BuildFinishedStatus waitFor() throws RunBuildException {\r
+      log("waitFor-" + myId);\r
+      throwExceptionIfPossible(myFinishException);\r
+      return myResultStatus;\r
+    }\r
+  }\r
+}\r
index aadf0157f596b3c8347d5349ea0c4aab8d358bb4..c438c8c0ff6266f10ddef4b03f460b06cdb29e1a 100644 (file)
 \r
 package jetbrains.buildServer.nuget.tests.util;\r
 \r
-import jetbrains.buildServer.BaseTestCase;\r
 import jetbrains.buildServer.RunBuildException;\r
 import jetbrains.buildServer.agent.BuildFinishedStatus;\r
-import jetbrains.buildServer.agent.BuildProcess;\r
 import jetbrains.buildServer.nuget.agent.util.impl.CompositeBuildProcessImpl;\r
-import jetbrains.buildServer.util.StringUtil;\r
-import org.testng.Assert;\r
 import org.jetbrains.annotations.NotNull;\r
-import org.jetbrains.annotations.Nullable;\r
-import org.testng.annotations.DataProvider;\r
+import org.testng.Assert;\r
 import org.testng.annotations.Test;\r
 \r
 import java.util.ArrayList;\r
-import java.util.Collection;\r
 import java.util.List;\r
 \r
 /**\r
  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
  * Date: 07.07.11 19:04\r
  */\r
-public class CompositeBuildProcessTest extends BaseTestCase {\r
+public class CompositeBuildProcessTest extends BuildProcessTestCase {\r
   @Test\r
   public void test_empty_build_process() {\r
     CompositeBuildProcessImpl i = new CompositeBuildProcessImpl();\r
@@ -46,63 +40,57 @@ public class CompositeBuildProcessTest extends BaseTestCase {
   @Test(dataProvider = "buildFinishStatuses")\r
   public void test_one_build_process(BuildFinishedStatus result) throws RunBuildException {\r
     CompositeBuildProcessImpl i = new CompositeBuildProcessImpl();\r
-    final List<String> log = new ArrayList<String>();\r
-    i.pushBuildProcess(new RecordingBuildProcess("1", result, log));\r
+    i.pushBuildProcess(new RecordingBuildProcess("1", result));\r
     assertRunSuccessfully(i, result == BuildFinishedStatus.INTERRUPTED ? BuildFinishedStatus.FINISHED_SUCCESS : result);\r
 \r
-    assertLog(log, "start-1", "waitFor-1");\r
+    assertLog("start-1", "waitFor-1");\r
   }\r
 \r
   @Test\r
   public void test_stopOnFirstError() throws RunBuildException {\r
     CompositeBuildProcessImpl i = new CompositeBuildProcessImpl();\r
-    final List<String> log = new ArrayList<String>();\r
-    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_FAILED, log));\r
-    i.pushBuildProcess(new RecordingBuildProcess("2", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
+    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_FAILED));\r
+    i.pushBuildProcess(new RecordingBuildProcess("2", BuildFinishedStatus.FINISHED_SUCCESS));\r
 \r
     assertRunSuccessfully(i, BuildFinishedStatus.FINISHED_FAILED);\r
-\r
-    assertLog(log, "start-1", "waitFor-1");\r
+    assertLog("start-1", "waitFor-1");\r
   }\r
 \r
   @Test\r
   public void test_stopOnFirstError2() throws RunBuildException {\r
     CompositeBuildProcessImpl i = new CompositeBuildProcessImpl();\r
-    final List<String> log = new ArrayList<String>();\r
-    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
-    i.pushBuildProcess(new RecordingBuildProcess("2", BuildFinishedStatus.FINISHED_FAILED, log));\r
-    i.pushBuildProcess(new RecordingBuildProcess("3", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
+    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_SUCCESS));\r
+    i.pushBuildProcess(new RecordingBuildProcess("2", BuildFinishedStatus.FINISHED_FAILED));\r
+    i.pushBuildProcess(new RecordingBuildProcess("3", BuildFinishedStatus.FINISHED_SUCCESS));\r
 \r
     assertRunSuccessfully(i, BuildFinishedStatus.FINISHED_FAILED);\r
-    assertLog(log, "start-1", "waitFor-1", "start-2", "waitFor-2");\r
+    assertLog("start-1", "waitFor-1", "start-2", "waitFor-2");\r
   }\r
 \r
   @Test\r
   public void test_stopOnStartException() throws RunBuildException {\r
     CompositeBuildProcessImpl i = new CompositeBuildProcessImpl();\r
-    final List<String> log = new ArrayList<String>();\r
-    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
-    i.pushBuildProcess(new RecordingBuildProcess("2", BuildFinishedStatus.FINISHED_SUCCESS, log) {{\r
+    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_SUCCESS));\r
+    i.pushBuildProcess(new RecordingBuildProcess("2", BuildFinishedStatus.FINISHED_SUCCESS) {{\r
       setStartException(new RunBuildException("aaa"));\r
     }});\r
-    i.pushBuildProcess(new RecordingBuildProcess("3", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
+    i.pushBuildProcess(new RecordingBuildProcess("3", BuildFinishedStatus.FINISHED_SUCCESS));\r
 \r
     assertRunException(i, "aaa");\r
-    assertLog(log, "start-1", "waitFor-1", "start-2");\r
+    assertLog("start-1", "waitFor-1", "start-2");\r
   }\r
 \r
   @Test\r
   public void test_stopOnWaitForException() throws RunBuildException {\r
     CompositeBuildProcessImpl i = new CompositeBuildProcessImpl();\r
-    final List<String> log = new ArrayList<String>();\r
-    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
-    i.pushBuildProcess(new RecordingBuildProcess("2", BuildFinishedStatus.FINISHED_SUCCESS, log) {{\r
+    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_SUCCESS));\r
+    i.pushBuildProcess(new RecordingBuildProcess("2", BuildFinishedStatus.FINISHED_SUCCESS) {{\r
       setFinishException(new RunBuildException("aaa"));\r
     }});\r
-    i.pushBuildProcess(new RecordingBuildProcess("3", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
+    i.pushBuildProcess(new RecordingBuildProcess("3", BuildFinishedStatus.FINISHED_SUCCESS));\r
 \r
     assertRunException(i, "aaa");\r
-    assertLog(log, "start-1", "waitFor-1", "start-2", "waitFor-2");\r
+    assertLog("start-1", "waitFor-1", "start-2", "waitFor-2");\r
   }\r
 \r
   @Test\r
@@ -122,24 +110,23 @@ public class CompositeBuildProcessTest extends BaseTestCase {
   public void test_interruptCalledForFirst() {\r
     final CompositeBuildProcessImpl i = new CompositeBuildProcessImpl();\r
     final List<String> log = new ArrayList<String>();\r
-    i.pushBuildProcess(new RecordingBuildProcess("1",BuildFinishedStatus.FINISHED_SUCCESS, log) {\r
+    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_SUCCESS) {\r
       @Override\r
       public void start() throws RunBuildException {\r
         super.start();\r
         i.interrupt();\r
       }\r
     });\r
-    i.pushBuildProcess(new RecordingBuildProcess("f", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
+    i.pushBuildProcess(new RecordingBuildProcess("f", BuildFinishedStatus.FINISHED_SUCCESS));\r
 \r
     assertRunSuccessfully(i, BuildFinishedStatus.INTERRUPTED);\r
-    assertLog(log, "start-1", "interrupt-1", "waitFor-1");\r
+    assertLog("start-1", "interrupt-1", "waitFor-1");\r
   }\r
 \r
   @Test\r
   public void test_interruptCalledForFirst_WaitFor() {\r
     final CompositeBuildProcessImpl i = new CompositeBuildProcessImpl();\r
-    final List<String> log = new ArrayList<String>();\r
-    i.pushBuildProcess(new RecordingBuildProcess("1",BuildFinishedStatus.FINISHED_SUCCESS, log) {\r
+    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_SUCCESS) {\r
       @NotNull\r
       @Override\r
       public BuildFinishedStatus waitFor() throws RunBuildException {\r
@@ -147,36 +134,34 @@ public class CompositeBuildProcessTest extends BaseTestCase {
         return super.waitFor();\r
       }\r
     });\r
-    i.pushBuildProcess(new RecordingBuildProcess("f", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
+    i.pushBuildProcess(new RecordingBuildProcess("f", BuildFinishedStatus.FINISHED_SUCCESS));\r
 \r
     assertRunSuccessfully(i, BuildFinishedStatus.INTERRUPTED);\r
-    assertLog(log, "start-1", "interrupt-1", "waitFor-1");\r
+    assertLog("start-1", "interrupt-1", "waitFor-1");\r
   }\r
 \r
   @Test\r
   public void test_interruptCalledForTwo() {\r
     final CompositeBuildProcessImpl i = new CompositeBuildProcessImpl();\r
-    final List<String> log = new ArrayList<String>();\r
-    i.pushBuildProcess(new RecordingBuildProcess("0", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
-    i.pushBuildProcess(new RecordingBuildProcess("1",BuildFinishedStatus.FINISHED_SUCCESS, log) {\r
+    i.pushBuildProcess(new RecordingBuildProcess("0", BuildFinishedStatus.FINISHED_SUCCESS));\r
+    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_SUCCESS) {\r
       @Override\r
       public void start() throws RunBuildException {\r
         super.start();\r
         i.interrupt();\r
       }\r
     });\r
-    i.pushBuildProcess(new RecordingBuildProcess("f", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
+    i.pushBuildProcess(new RecordingBuildProcess("f", BuildFinishedStatus.FINISHED_SUCCESS));\r
 \r
     assertRunSuccessfully(i, BuildFinishedStatus.INTERRUPTED);\r
-    assertLog(log, "start-0", "waitFor-0", "start-1", "interrupt-1", "waitFor-1");\r
+    assertLog("start-0", "waitFor-0", "start-1", "interrupt-1", "waitFor-1");\r
   }\r
 \r
   @Test\r
   public void test_interruptCalledForTwo_WaitFor() {\r
     final CompositeBuildProcessImpl i = new CompositeBuildProcessImpl();\r
-    final List<String> log = new ArrayList<String>();\r
-    i.pushBuildProcess(new RecordingBuildProcess("0", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
-    i.pushBuildProcess(new RecordingBuildProcess("1",BuildFinishedStatus.FINISHED_SUCCESS, log) {\r
+    i.pushBuildProcess(new RecordingBuildProcess("0", BuildFinishedStatus.FINISHED_SUCCESS));\r
+    i.pushBuildProcess(new RecordingBuildProcess("1", BuildFinishedStatus.FINISHED_SUCCESS) {\r
       @NotNull\r
       @Override\r
       public BuildFinishedStatus waitFor() throws RunBuildException {\r
@@ -184,104 +169,9 @@ public class CompositeBuildProcessTest extends BaseTestCase {
         return super.waitFor();\r
       }\r
     });\r
-    i.pushBuildProcess(new RecordingBuildProcess("f", BuildFinishedStatus.FINISHED_SUCCESS, log));\r
+    i.pushBuildProcess(new RecordingBuildProcess("f", BuildFinishedStatus.FINISHED_SUCCESS));\r
 \r
     assertRunSuccessfully(i, BuildFinishedStatus.INTERRUPTED);\r
-    assertLog(log, "start-0", "waitFor-0", "start-1", "interrupt-1", "waitFor-1");\r
-  }\r
-\r
-\r
-\r
-  @DataProvider(name = "buildFinishStatuses")\r
-  public Object[][] buildStatuses() {\r
-    List<Object[]> list = new ArrayList<Object[]>();\r
-    for (BuildFinishedStatus val : BuildFinishedStatus.values()) {\r
-      list.add(new Object[]{val});\r
-    }\r
-    return list.toArray(new Object[list.size()][]);\r
-  }\r
-\r
-  private void assertRunSuccessfully(@NotNull BuildProcess proc, @NotNull BuildFinishedStatus result) {\r
-    BuildFinishedStatus status = null;\r
-    try {\r
-      proc.start();\r
-      status = proc.waitFor();\r
-    } catch (RunBuildException e) {\r
-      Assert.fail("Failed with exception " + e);\r
-    }\r
-\r
-    Assert.assertEquals(result, status);\r
-  }\r
-\r
-  private void assertRunException(@NotNull BuildProcess proc, @NotNull String message) {\r
-    try {\r
-      proc.start();\r
-      proc.waitFor();\r
-      Assert.fail("Exception expected");\r
-    } catch (RunBuildException e) {\r
-      Assert.assertEquals(message, e.getMessage());\r
-    }\r
-  }\r
-\r
-  private void assertLog(Collection<String> log, String... gold) {\r
-    String actual = StringUtil.join(log, "\n");\r
-    String expected = StringUtil.join(gold, "\n");\r
-    Assert.assertEquals(actual, expected);\r
-  }\r
-\r
-  private class RecordingBuildProcess implements BuildProcess {\r
-    private final String myId;\r
-    private final List<String> myLog;\r
-    private final BuildFinishedStatus myResultStatus;\r
-    private Throwable myStartException;\r
-    private Throwable myFinishException;\r
-\r
-    private RecordingBuildProcess(@NotNull String id,\r
-                                  @Nullable final BuildFinishedStatus resultStatus,\r
-                                  @NotNull final List<String> log) {\r
-      myId = id;\r
-      myLog = log;\r
-      myResultStatus = resultStatus;\r
-    }\r
-\r
-    public void setStartException(Exception startException) {\r
-      myStartException = startException;\r
-    }\r
-\r
-    public void setFinishException(Exception finishException) {\r
-      myFinishException = finishException;\r
-    }\r
-\r
-    public void start() throws RunBuildException {\r
-      myLog.add("start-" + myId);\r
-      throwExceptionIfPossible(myStartException);\r
-    }\r
-\r
-    private void throwExceptionIfPossible(Throwable ex) throws RunBuildException {\r
-      if (ex != null) {\r
-        if (ex instanceof RunBuildException) throw (RunBuildException) ex;\r
-        if (ex instanceof RuntimeException) throw (RuntimeException) ex;\r
-        throw (Error) ex;\r
-      }\r
-    }\r
-\r
-    public boolean isInterrupted() {\r
-      return false;\r
-    }\r
-\r
-    public boolean isFinished() {\r
-      return false;\r
-    }\r
-\r
-    public void interrupt() {\r
-      myLog.add("interrupt-" + myId);\r
-    }\r
-\r
-    @NotNull\r
-    public BuildFinishedStatus waitFor() throws RunBuildException {\r
-      myLog.add("waitFor-" + myId);\r
-      throwExceptionIfPossible(myFinishException);\r
-      return myResultStatus;\r
-    }\r
+    assertLog("start-0", "waitFor-0", "start-1", "interrupt-1", "waitFor-1");\r
   }\r
 }\r
diff --git a/nuget-tests/src/jetbrains/buildServer/nuget/tests/util/DelegatingBuildProcessTest.java b/nuget-tests/src/jetbrains/buildServer/nuget/tests/util/DelegatingBuildProcessTest.java
new file mode 100644 (file)
index 0000000..8e01d99
--- /dev/null
@@ -0,0 +1,186 @@
+/*\r
+ * Copyright 2000-2011 JetBrains s.r.o.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package jetbrains.buildServer.nuget.tests.util;\r
+\r
+import jetbrains.buildServer.RunBuildException;\r
+import jetbrains.buildServer.agent.BuildFinishedStatus;\r
+import jetbrains.buildServer.agent.BuildProcess;\r
+import jetbrains.buildServer.nuget.agent.util.DelegatingBuildProcess;\r
+import org.jetbrains.annotations.NotNull;\r
+import org.testng.Assert;\r
+import org.testng.annotations.Test;\r
+\r
+import java.util.concurrent.atomic.AtomicReference;\r
+\r
+import static jetbrains.buildServer.agent.BuildFinishedStatus.FINISHED_SUCCESS;\r
+import static jetbrains.buildServer.agent.BuildFinishedStatus.INTERRUPTED;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 07.07.11 20:06\r
+ */\r
+public class DelegatingBuildProcessTest extends BuildProcessTestCase {\r
+\r
+  @Test\r
+  public void test_FailedToCreateDelegate() {\r
+    DelegatingBuildProcess aaa = new DelegatingBuildProcess(new LoggingAction(FINISHED_SUCCESS) {\r
+      @NotNull\r
+      public BuildProcess startImpl() throws RunBuildException {\r
+        super.startImpl();\r
+        throw new RunBuildException("aaa");\r
+      }\r
+    });\r
+\r
+    Assert.assertFalse(aaa.isFinished());\r
+    assertRunException(aaa, "aaa");\r
+    assertLog("start-impl", "finish-impl");\r
+    Assert.assertTrue(aaa.isFinished());\r
+  }\r
+\r
+  @Test\r
+  public void test_interrupted_before_start() {\r
+    DelegatingBuildProcess aaa = new DelegatingBuildProcess(new LoggingAction(FINISHED_SUCCESS));\r
+    aaa.interrupt();\r
+    assertRunSuccessfully(aaa, INTERRUPTED);\r
+    assertLog();\r
+    Assert.assertTrue(aaa.isFinished());\r
+    Assert.assertTrue(aaa.isInterrupted());\r
+  }\r
+\r
+  @Test\r
+  public void test_interrupted_in_startImpl() {\r
+    final AtomicReference<BuildProcess> bp = new AtomicReference<BuildProcess>();\r
+    DelegatingBuildProcess aaa = new DelegatingBuildProcess(new LoggingAction(FINISHED_SUCCESS){\r
+      @NotNull\r
+      @Override\r
+      public BuildProcess startImpl() throws RunBuildException {\r
+        bp.get().interrupt();\r
+        return super.startImpl();\r
+      }\r
+    });\r
+    bp.set(aaa);\r
+\r
+    assertRunSuccessfully(aaa, INTERRUPTED);\r
+    assertLog("start-impl","finish-impl");\r
+    Assert.assertTrue(aaa.isFinished());\r
+    Assert.assertTrue(aaa.isInterrupted());\r
+  }\r
+\r
+  @Test\r
+  public void test_interrupted_in_process_start() {\r
+    final AtomicReference<BuildProcess> bp = new AtomicReference<BuildProcess>();\r
+    DelegatingBuildProcess aaa = new DelegatingBuildProcess(new LoggingActionBase(){\r
+      @Override\r
+      protected RecordingBuildProcess createSub() {\r
+        return new RecordingBuildProcess("i", BuildFinishedStatus.FINISHED_SUCCESS){\r
+          @Override\r
+          public void start() throws RunBuildException {\r
+            super.start();\r
+            bp.get().interrupt();\r
+          }\r
+        };\r
+      }\r
+    });\r
+    bp.set(aaa);\r
+\r
+    assertRunSuccessfully(aaa, INTERRUPTED);\r
+    assertLog("start-impl","start-i","interrupt-i","waitFor-i","finish-impl");\r
+    Assert.assertTrue(aaa.isFinished());\r
+    Assert.assertTrue(aaa.isInterrupted());\r
+  }\r
+\r
+  @Test\r
+  public void test_interrupted_in_process_finish() {\r
+    final AtomicReference<BuildProcess> bp = new AtomicReference<BuildProcess>();\r
+    DelegatingBuildProcess aaa = new DelegatingBuildProcess(new LoggingActionBase(){\r
+      @Override\r
+      protected RecordingBuildProcess createSub() {\r
+        return new RecordingBuildProcess("i", BuildFinishedStatus.FINISHED_SUCCESS){\r
+          @NotNull\r
+          @Override\r
+          public BuildFinishedStatus waitFor() throws RunBuildException {\r
+            bp.get().interrupt();\r
+            return super.waitFor();\r
+          }\r
+        };\r
+      }\r
+    });\r
+    bp.set(aaa);\r
+\r
+    assertRunSuccessfully(aaa, INTERRUPTED);\r
+    assertLog("start-impl","start-i","interrupt-i","waitFor-i","finish-impl");\r
+    Assert.assertTrue(aaa.isFinished());\r
+    Assert.assertTrue(aaa.isInterrupted());\r
+  }\r
+\r
+  @Test\r
+  public void test_interrupted_in_process_finishImpl() {\r
+    final AtomicReference<BuildProcess> bp = new AtomicReference<BuildProcess>();\r
+    DelegatingBuildProcess aaa = new DelegatingBuildProcess(new LoggingAction(BuildFinishedStatus.FINISHED_SUCCESS){\r
+      @Override\r
+      public void finishedImpl() {\r
+        bp.get().interrupt();\r
+        super.finishedImpl();\r
+      }\r
+    });\r
+    bp.set(aaa);\r
+\r
+    assertRunSuccessfully(aaa, INTERRUPTED);\r
+    assertLog("start-impl","start-i","waitFor-i","finish-impl");\r
+    Assert.assertTrue(aaa.isFinished());\r
+    Assert.assertTrue(aaa.isInterrupted());\r
+  }\r
+\r
+  @Test(dataProvider = "buildFinishStatuses")\r
+  public void test_with_sub_action(@NotNull BuildFinishedStatus status) {\r
+    DelegatingBuildProcess aaa = new DelegatingBuildProcess(new LoggingAction(status));\r
+    assertRunSuccessfully(aaa, status);\r
+    assertLog("start-impl", "start-i", "waitFor-i", "finish-impl");\r
+    Assert.assertTrue(aaa.isFinished());\r
+  }\r
+\r
+  private class LoggingAction extends LoggingActionBase {\r
+    private final BuildFinishedStatus myStatus;\r
+\r
+    private LoggingAction(BuildFinishedStatus status) {\r
+      myStatus = status;\r
+    }\r
+\r
+    @Override\r
+    protected RecordingBuildProcess createSub() {\r
+      return new RecordingBuildProcess("i", myStatus);\r
+    }\r
+  }\r
+\r
+  /**\r
+   * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+   * Date: 07.07.11 20:30\r
+   */\r
+  private abstract class LoggingActionBase implements DelegatingBuildProcess.Action {\r
+    @NotNull\r
+    public BuildProcess startImpl() throws RunBuildException {\r
+      log("start-impl");\r
+      return createSub();\r
+    }\r
+\r
+    protected abstract RecordingBuildProcess createSub();\r
+\r
+    public void finishedImpl() {\r
+      log("finish-impl");\r
+    }\r
+  }\r
+}\r