logging for bad situations was improved
[fedora-idea.git] / smRunner / src / com / intellij / execution / testframework / sm / runner / TestSuiteStack.java
blob8a181e89ab8693adb90aca0b26b88404a10d1212
1 package com.intellij.execution.testframework.sm.runner;
3 import com.intellij.openapi.diagnostic.Logger;
4 import org.jetbrains.annotations.NonNls;
5 import org.jetbrains.annotations.NotNull;
6 import org.jetbrains.annotations.Nullable;
8 import java.util.EmptyStackException;
9 import java.util.Stack;
11 /**
12 * @author Roman Chernyatchik
14 public class TestSuiteStack {
15 private static final Logger LOG = Logger.getInstance(TestSuiteStack.class.getName());
17 @NonNls private static final String EMPTY = "empty";
19 private final Stack<SMTestProxy> myStack = new Stack<SMTestProxy>();
21 public void pushSuite(@NotNull final SMTestProxy suite) {
22 myStack.push(suite);
25 /**
26 * @return Top element of non stack or null for empty stack
28 @Nullable
29 public SMTestProxy getCurrentSuite() {
30 if (getStackSize() != 0) {
31 return myStack.peek();
33 return null;
36 /**
37 * Pop element form stack and checks consistency
38 * @param suiteName Predictable name of top suite in stack
40 @NotNull
41 public SMTestProxy popSuite(final String suiteName) throws EmptyStackException {
42 if (myStack.isEmpty()) {
43 LOG.assertTrue(false, "Pop error: Test runner try to close test suite which has been already closed or wasn't started at all. Unexpected suite name [" + suiteName + "]");
44 return null;
46 final SMTestProxy currentSuite = myStack.pop();
48 if (!suiteName.equals(currentSuite.getName())) {
49 LOG.assertTrue(false, "Pop error: Unexpected closing suite. Expected [" + suiteName + "] but [" + currentSuite.getName() + "] was found. Rest of stack: " + getSuitePathPresentation());
50 return null;
53 return currentSuite;
56 public final boolean isEmpty() {
57 return getStackSize() == 0;
60 protected int getStackSize() {
61 return myStack.size();
64 protected String[] getSuitePath() {
65 final int stackSize = getStackSize();
66 final String[] names = new String[stackSize];
67 for (int i = 0; i < stackSize; i++) {
68 names[i] = myStack.get(i).getName();
70 return names;
73 protected String getSuitePathPresentation() {
74 final String[] names = getSuitePath();
75 if (names.length == 0) {
76 return EMPTY;
79 final StringBuilder builder = new StringBuilder();
80 final String lastName = names[names.length - 1];
81 for (String name : names) {
82 builder.append('[').append(name).append(']');
83 //Here we can use != instead of !equals()
84 //noinspection StringEquality
85 if (name != lastName) {
86 builder.append("->");
89 return builder.toString();
92 public void clear() {
93 myStack.clear();