/*
 * Copyright 2014 Cask, 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 co.cask.cdap.reactor.client.common;

import co.cask.cdap.SingleNodeMain;
import co.cask.cdap.common.conf.CConfiguration;
import co.cask.cdap.common.conf.Constants;
import org.apache.hadoop.conf.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 */
public class SingleNodeTestBase {

  private static final Logger LOG = LoggerFactory.getLogger(SingleNodeTestBase.class);

  @ClassRule
  public static TemporaryFolder tmpFolder = new TemporaryFolder();

  private static SingleNodeMain singleNodeMain;

  @BeforeClass
  public static void setUpClass() throws Throwable {
    if (singleNodeMain != null) {
      try {
        CConfiguration cConf = CConfiguration.create();
        cConf.set(Constants.CFG_LOCAL_DATA_DIR, tmpFolder.newFolder().getAbsolutePath());

        // Start singlenode without UI
        singleNodeMain = SingleNodeMain.createSingleNodeMain(true, null, cConf, new Configuration());
        singleNodeMain.startUp();
      } catch (Throwable e) {
        LOG.error("Failed to start singlenode", e);
        if (singleNodeMain != null) {
          singleNodeMain.shutDown();
        }
        throw e;
      }
    }
  }

  @AfterClass
  public static void tearDownClass() {
    if (singleNodeMain != null) {
      singleNodeMain.shutDown();
      singleNodeMain = null;
    }
  }
}

@@ -1248,16 +1248,201 @@
 odeMain;
+%0A  /**%0A   * Index of the current test being run.%0A   * TODO: Hack to handle when SingleNodeTestBase is used as a suite and part of a suite.%0A   */%0A  private static int testStackIndex = 0;
 %0A%0A  @Bef
@@ -1503,16 +1503,38 @@
 wable %7B%0A
+    testStackIndex++;%0A
     if (
@@ -1540,33 +1540,33 @@
 (singleNodeMain 
-!
+=
 = null) %7B%0A      
@@ -2145,24 +2145,46 @@
 wnClass() %7B%0A
+    testStackIndex--;%0A
     if (sing
@@ -2193,32 +2193,55 @@
 NodeMain != null
+ && testStackIndex == 0
 ) %7B%0A      single

Fix bug in SingleNodeTestBase