/platform/smRunner/src/com/intellij/execution/testframework/sm/runner/states/TestFailedState.java

https://bitbucket.org/nbargnesi/idea · Java · 118 lines · 83 code · 16 blank · 19 comment · 8 complexity · a9768c29dade2567d7e316fca10dccd7 MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.execution.testframework.sm.runner.states;
  17. import com.intellij.execution.testframework.CompositePrintable;
  18. import com.intellij.execution.testframework.Printer;
  19. import com.intellij.execution.ui.ConsoleViewContentType;
  20. import com.intellij.openapi.util.text.StringUtil;
  21. import com.intellij.util.containers.ContainerUtilRt;
  22. import org.jetbrains.annotations.NotNull;
  23. import org.jetbrains.annotations.Nullable;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. /**
  27. * @author Roman Chernyatchik
  28. */
  29. public class TestFailedState extends AbstractState {
  30. private final List<String> myPresentationText;
  31. public TestFailedState(@Nullable final String localizedMessage,
  32. @Nullable final String stackTrace)
  33. {
  34. myPresentationText = ContainerUtilRt.newArrayList(buildErrorPresentationText(localizedMessage, stackTrace));
  35. }
  36. public void addError(@Nullable String localizedMessage, @Nullable String stackTrace, Printer printer) {
  37. final String msg = buildErrorPresentationText(localizedMessage, stackTrace);
  38. if (msg != null) {
  39. myPresentationText.add(msg);
  40. if (printer != null) {
  41. printError(printer, Arrays.asList(msg), false);
  42. }
  43. }
  44. }
  45. @Nullable
  46. public static String buildErrorPresentationText(@Nullable final String localizedMessage,
  47. @Nullable final String stackTrace)
  48. {
  49. final String text = (StringUtil.isEmptyOrSpaces(localizedMessage) ? "" : localizedMessage + CompositePrintable.NEW_LINE) +
  50. (StringUtil.isEmptyOrSpaces(stackTrace) ? "" : stackTrace + CompositePrintable.NEW_LINE);
  51. return StringUtil.isEmptyOrSpaces(text) ? null : text;
  52. }
  53. public static void printError(@NotNull final Printer printer,
  54. @NotNull final List<String> errorPresentationText)
  55. {
  56. printError(printer, errorPresentationText, true);
  57. }
  58. private static void printError(@NotNull final Printer printer,
  59. @NotNull final List<String> errorPresentationText,
  60. final boolean setMark)
  61. {
  62. boolean addMark = setMark;
  63. for (final String errorText : errorPresentationText) {
  64. if (errorText != null) {
  65. printer.print(CompositePrintable.NEW_LINE, ConsoleViewContentType.ERROR_OUTPUT);
  66. if (addMark) {
  67. printer.mark();
  68. addMark = false;
  69. }
  70. printer.print(errorText, ConsoleViewContentType.ERROR_OUTPUT);
  71. }
  72. }
  73. }
  74. @Override
  75. public void printOn(final Printer printer) {
  76. super.printOn(printer);
  77. printError(printer, myPresentationText);
  78. }
  79. public boolean isDefect() {
  80. return true;
  81. }
  82. public boolean wasLaunched() {
  83. return true;
  84. }
  85. public boolean isFinal() {
  86. return true;
  87. }
  88. public boolean isInProgress() {
  89. return false;
  90. }
  91. public boolean wasTerminated() {
  92. return false;
  93. }
  94. public Magnitude getMagnitude() {
  95. return Magnitude.FAILED_INDEX;
  96. }
  97. @Override
  98. public String toString() {
  99. //noinspection HardCodedStringLiteral
  100. return "TEST FAILED";
  101. }
  102. }