summaryrefslogtreecommitdiff
path: root/platform/testRunner/src/com/intellij/execution/testframework/stacktrace/DiffHyperlink.java
blob: f8ff62996eca565746d02edc5fb548deba0f7baf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * 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.
 */

/*
 * User: anna
 * Date: 15-Aug-2007
 */
package com.intellij.execution.testframework.stacktrace;

import com.intellij.execution.ExecutionBundle;
import com.intellij.execution.filters.HyperlinkInfo;
import com.intellij.execution.testframework.AbstractTestProxy;
import com.intellij.execution.testframework.Printable;
import com.intellij.execution.testframework.Printer;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diff.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.File;

public class DiffHyperlink implements Printable {
  private static final String NEW_LINE = "\n";

  protected final String myExpected;
  protected final String myActual;
  protected final String myFilePath;
  private boolean myPrintOneLine;
  private final HyperlinkInfo myDiffHyperlink = new DiffHyperlinkInfo();


  public DiffHyperlink(final String expected, final String actual, final String filePath) {
    this(expected, actual, filePath, true);
  }

  public DiffHyperlink(final String expected,
                       final String actual,
                       final String filePath,
                       boolean printOneLine) {
    myExpected = expected;
    myActual = actual;
    myFilePath = filePath == null ? null : filePath.replace(File.separatorChar, '/');
    myPrintOneLine = printOneLine;
  }

  public void openDiff(Project project) {
    openMultiDiff(project, null);
  }

  public void openMultiDiff(final Project project,
                            final AbstractTestProxy.AssertEqualsDiffChain chain) {
    final SimpleDiffRequest diffData = createRequest(project, chain, myFilePath, myExpected, myActual);
    DiffManager.getInstance().getIdeaDiffTool().show(diffData);
  }

  private SimpleDiffRequest createRequest(final Project project,
                                          final AbstractTestProxy.AssertEqualsDiffChain chain,
                                          String filePath, String expected, String actual) {
    String expectedTitle = ExecutionBundle.message("diff.content.expected.title");
    final DiffContent expectedContent;
    final VirtualFile vFile;
    if (filePath != null && (vFile = LocalFileSystem.getInstance().findFileByPath(filePath)) != null) {
      expectedContent = DiffContent.fromFile(project, vFile);
      expectedTitle += " (" + vFile.getPresentableUrl() + ")";
    } else {
      expectedContent = new SimpleContent(expected);
    }
    final SimpleDiffRequest diffData = new SimpleDiffRequest(project, getTitle());
    if (chain != null) {
      diffData.setToolbarAddons(new DiffRequest.ToolbarAddons() {
        @Override
        public void customize(DiffToolbar toolbar) {
          toolbar.addAction(new NextPrevAction("Compare Previous Failure", AllIcons.Actions.Prevfile, chain) {
            {
              registerCustomShortcutSet(ActionManager.getInstance().getAction("PreviousTab").getShortcutSet(), null);
            }

            @Override
            protected AbstractTestProxy.AssertEqualsMultiDiffViewProvider getNextId() {
              return chain.getPrevious();
            }
          });
          toolbar.addAction(new NextPrevAction("Compare Next Failure", AllIcons.Actions.Nextfile, chain) {
            {
              registerCustomShortcutSet(ActionManager.getInstance().getAction("NextTab").getShortcutSet(), null);
            }

            @Override
            protected AbstractTestProxy.AssertEqualsMultiDiffViewProvider getNextId() {
              return chain.getNext();
            }
          });
        }
      });
    }
    diffData.setContents(expectedContent, new SimpleContent(actual));
    diffData.setContentTitles(expectedTitle, ExecutionBundle.message("diff.content.actual.title"));
    diffData.addHint(DiffTool.HINT_SHOW_FRAME);
    diffData.addHint(DiffTool.HINT_DO_NOT_IGNORE_WHITESPACES);
    diffData.setGroupKey("#com.intellij.execution.junit2.states.ComparisonFailureState$DiffDialog");
    return diffData;
  }

  abstract class NextPrevAction extends AnAction {

    private final AbstractTestProxy.AssertEqualsDiffChain myChain;

    public NextPrevAction(@Nullable String text, @Nullable Icon icon,
                          final AbstractTestProxy.AssertEqualsDiffChain chain) {
      super(text, text, icon);
      myChain = chain;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
      final DiffViewer viewer = e.getData(PlatformDataKeys.DIFF_VIEWER);
      final Project project = e.getData(CommonDataKeys.PROJECT);
      final AbstractTestProxy.AssertEqualsMultiDiffViewProvider nextProvider = getNextId();
      myChain.setCurrent(nextProvider);
      final SimpleDiffRequest nextRequest =
        createRequest(project, myChain, nextProvider.getFilePath(), nextProvider.getExpected(), nextProvider.getActual());
      viewer.setDiffRequest(nextRequest);
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
      final DiffViewer viewer = e.getData(PlatformDataKeys.DIFF_VIEWER);
      final Project project = e.getData(CommonDataKeys.PROJECT);
      e.getPresentation().setEnabled(project != null && viewer != null);
    }

    protected abstract AbstractTestProxy.AssertEqualsMultiDiffViewProvider getNextId();
  }

  protected String getTitle() {
    return ExecutionBundle.message("strings.equal.failed.dialog.title");
  }

  public String getLeft() {
    return myExpected;
  }

  public String getRight() {
    return myActual;
  }

  public String getFilePath() {
    return myFilePath;
  }

  public void printOn(final Printer printer) {
    if (!hasMoreThanOneLine(myActual) && !hasMoreThanOneLine(myExpected) && myPrintOneLine) {
      printer.print(NEW_LINE, ConsoleViewContentType.ERROR_OUTPUT);
      printer.print(ExecutionBundle.message("diff.content.expected.for.file.title"), ConsoleViewContentType.SYSTEM_OUTPUT);
      printer.print(myExpected + NEW_LINE, ConsoleViewContentType.ERROR_OUTPUT);
      printer.print(ExecutionBundle.message("junit.actual.text.label"), ConsoleViewContentType.SYSTEM_OUTPUT);
      printer.print(myActual + NEW_LINE, ConsoleViewContentType.ERROR_OUTPUT);
    }
    printer.print(" ", ConsoleViewContentType.ERROR_OUTPUT);
    printer.printHyperlink(ExecutionBundle.message("junit.click.to.see.diff.link"), myDiffHyperlink);
    printer.print(NEW_LINE, ConsoleViewContentType.ERROR_OUTPUT);
  }

  private static boolean hasMoreThanOneLine(final String string) {
    return string.indexOf('\n') != -1 || string.indexOf('\r') != -1;
  }

  public class DiffHyperlinkInfo implements HyperlinkInfo {
    public void navigate(final Project project) {
      openDiff(project);
    }

    public DiffHyperlink getPrintable() {
      return DiffHyperlink.this;
    }
  }
}