Skip to content

Commit 40804fc

Browse files
authored
Merge pull request #75 from bia-technologies/feature/74
fix: добавлена обработка ситуации с пустым отчетом
2 parents beae9d0 + 73094e8 commit 40804fc

File tree

10 files changed

+72
-27
lines changed

10 files changed

+72
-27
lines changed

viewer/src/main/java/ru/biatech/edt/junit/BasicElementLabels.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import org.eclipse.osgi.util.TextProcessor;
2020

21-
import java.io.File;
21+
import java.nio.file.Path;
2222

2323

2424
/**
@@ -49,8 +49,8 @@ private static String markLTR(String string, String delimiters) {
4949
* @param file the file
5050
* @return the label of the file path to be used in the UI.
5151
*/
52-
public static String getPathLabel(File file) {
53-
return markLTR(file.getAbsolutePath(), "/\\:."); //$NON-NLS-1$
52+
public static String getPathLabel(Path file) {
53+
return markLTR(file.toAbsolutePath().toString(), "/\\:."); //$NON-NLS-1$
5454
}
5555

5656
/**

viewer/src/main/java/ru/biatech/edt/junit/model/Session.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public class Session extends Report<TestSuiteElement> implements ITestRunSession
132132
public Session() {
133133
testRunnerKind = ITestKind.NULL; //TODO
134134
startTime = System.currentTimeMillis();
135+
testsuite = new TestSuiteElement[0];
135136
reset();
136137
}
137138

@@ -153,6 +154,10 @@ public ITestSuiteElement getParent() {
153154

154155
@Override
155156
public TestStatus getStatus() {
157+
if (getTestsuite().length == 0) {
158+
return TestStatus.NOT_RUN;
159+
}
160+
156161
var status = TestStatus.OK;
157162
for (var item : getTestsuite()) {
158163
status = TestStatus.combineStatus(status, item.getStatus());
@@ -322,6 +327,8 @@ public String toString() {
322327
* Заполняет необходимые поля. Вызывается после полного заполнения.
323328
*/
324329
void init() {
330+
reset();
331+
325332
for (var suite : getTestsuite()) {
326333
suite.init();
327334
errorCount += suite.getErrors();

viewer/src/main/java/ru/biatech/edt/junit/model/SessionsManager.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
import ru.biatech.edt.junit.model.report.ReportLoader;
3838
import ru.biatech.edt.junit.ui.UIMessages;
3939

40-
import java.io.File;
4140
import java.io.IOException;
4241
import java.nio.file.Files;
42+
import java.nio.file.Path;
4343
import java.text.MessageFormat;
4444
import java.util.ArrayList;
4545
import java.util.LinkedList;
@@ -66,10 +66,17 @@ public SessionsManager() {
6666
private LifecycleListener lifecycleListener;
6767
private Session activeSession;
6868

69+
private static void dropFile(Path path) {
70+
try {
71+
Files.deleteIfExists(path);
72+
} catch (IOException e) {
73+
TestViewerPlugin.log().logError("Не удалось удалить файл: " + path, e);
74+
}
75+
}
76+
6977
public void importSession(LifecycleItem item) {
7078
log().debug(UIMessages.JUnitModel_LoadReport);
7179

72-
try {
7380
var launch = item.getMainLaunch();
7481

7582
var configuration = launch.getLaunchConfiguration();
@@ -83,20 +90,22 @@ public void importSession(LifecycleItem item) {
8390
}
8491

8592
log().debug("Импорт отчета о тестировании: " + reportPath.toAbsolutePath());
93+
try {
8694
Session session;
8795
if (activeSession != null && activeSession.isRunning()) {
88-
importActiveSession(reportPath.toFile());
96+
importActiveSession(reportPath);
8997
} else {
90-
session = importSession(reportPath.toFile());
98+
session = importSession(reportPath);
9199
if (session == null) {
92100
log().logError("Session is null after import.");
93101
return;
94102
}
95103
session.setLaunch(item.getTestLaunch());
96104
}
97-
Files.deleteIfExists(reportPath);
98-
} catch (CoreException | IOException e) {
105+
} catch (CoreException e) {
99106
log().logError(UIMessages.JUnitModel_UnknownErrorOnReportLoad, e);
107+
} finally {
108+
dropFile(reportPath);
100109
}
101110
}
102111

@@ -107,7 +116,7 @@ public void importSession(LifecycleItem item) {
107116
* @return the imported test run session
108117
* @throws CoreException if the import failed
109118
*/
110-
public Session importSession(File file) throws CoreException {
119+
public Session importSession(Path file) throws CoreException {
111120
Session session;
112121
try {
113122
log().debug("Загрузку отчета в новую сессию");
@@ -124,7 +133,7 @@ public Session importSession(File file) throws CoreException {
124133
return session;
125134
}
126135

127-
public void importActiveSession(File file) {
136+
public void importActiveSession(Path file) {
128137
log().debug("Загрузку отчета в активную сессию");
129138
ReportLoader.loadInto(file, activeSession);
130139
appendSession(activeSession);
@@ -140,6 +149,9 @@ public void importSession(TestSuiteElement[] data, ILaunch launch) {
140149

141150
private void appendSession(Session session) {
142151
session.init();
152+
if (session.getTestsuite().length == 0) {
153+
log().logError("Отчет пуст");
154+
}
143155
instance.addSession(session);
144156

145157
// TODO: Генерировать событие и отображать панель оттуда
@@ -172,7 +184,7 @@ public void stop() {
172184
var swapFiles = historyDirectory.listFiles();
173185
if (swapFiles != null) {
174186
for (var swapFile : swapFiles) {
175-
swapFile.delete();
187+
dropFile(swapFile.toPath());
176188
}
177189
}
178190
}

viewer/src/main/java/ru/biatech/edt/junit/model/TestSuiteElement.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@
1717
package ru.biatech.edt.junit.model;
1818

1919
import lombok.Getter;
20-
import lombok.NoArgsConstructor;
2120
import ru.biatech.edt.junit.model.report.ErrorInfo;
2221
import ru.biatech.edt.junit.model.report.TestSuite;
2322

2423
import java.util.Arrays;
2524
import java.util.stream.Stream;
2625

27-
@NoArgsConstructor
2826
public class TestSuiteElement extends TestSuite<TestCaseElement> implements ITestSuiteElement {
2927
@Getter
3028
private TestStatus status;
3129
private TestStatus childrenStatus;
3230

31+
public TestSuiteElement() {
32+
testcase = new TestCaseElement[0];
33+
}
34+
3335
@Override
3436
public ITestSuiteElement getParent() {
3537
return null;
@@ -93,7 +95,10 @@ void init() {
9395
failures = 0;
9496
errors = 0;
9597
skipped = 0;
96-
tests = testcase.length;
98+
tests = getTestcase().length;
99+
status = TestStatus.NOT_RUN;
100+
childrenStatus = TestStatus.OK;
101+
97102
for (var test : getTestcase()) {
98103
test.init(this);
99104
switch (test.getStatus()) {

viewer/src/main/java/ru/biatech/edt/junit/model/report/Report.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public class Report<T extends TestSuite<?>> {
1111
* Properties (e.g., environment settings) set during test execution
1212
*/
1313
@JacksonXmlElementWrapper(useWrapping = true, localName = "properties")
14-
Property[] property;
15-
T[] testsuite;
14+
protected Property[] property;
15+
protected T[] testsuite;
1616
}

viewer/src/main/java/ru/biatech/edt/junit/model/report/ReportLoader.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@
44
import lombok.experimental.UtilityClass;
55
import ru.biatech.edt.junit.Serializer;
66

7-
import java.io.File;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
89

910
@UtilityClass
1011
public class ReportLoader {
1112

1213
@SneakyThrows
13-
public <T> T load(File path, Class<T> reportClass) {
14-
return Serializer.getXmlMapper().readValue(path, reportClass);
14+
public <T> T load(Path path, Class<T> reportClass) {
15+
try (var stream = Files.newInputStream(path)) {
16+
return Serializer.getXmlMapper().readValue(stream, reportClass);
17+
}
1518
}
1619

1720
@SneakyThrows
18-
public <T> void loadInto(File path, T object) {
19-
Serializer.getXmlMapper().readerForUpdating(object).readValue(path);
21+
public <T> void loadInto(Path path, T object) {
22+
try (var stream = Files.newInputStream(path)) {
23+
Serializer.getXmlMapper().readerForUpdating(object).readValue(stream);
24+
}
2025
}
2126

2227
}

viewer/src/main/java/ru/biatech/edt/junit/ui/report/TestRunnerViewPart.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,13 @@ private void resetViewIcon() {
340340
}
341341

342342
private void updateViewIcon() {
343-
if (session == null || session.getStartedCount() == 0) {
343+
if (session == null) {
344344
fViewImage = fOriginalViewImage;
345-
} else if (hasErrorsOrFailures()) {
345+
} else if (session.getTestsuite().length == 0) {
346+
fViewImage = imageProvider.getInactiveLogo();
347+
} else if (session.getErrorCount() != 0) {
348+
fViewImage = imageProvider.getTestRunErrorIcon();
349+
} else if (session.getFailureCount() != 0) {
346350
fViewImage = imageProvider.getTestRunFailIcon();
347351
} else {
348352
fViewImage = imageProvider.getTestRunOKIcon();

viewer/src/main/java/ru/biatech/edt/junit/ui/report/actions/ImportTestRunSessionAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import ru.biatech.edt.junit.ui.UIMessages;
2727
import ru.biatech.edt.junit.ui.report.TestRunnerViewPart;
2828

29-
import java.io.File;
29+
import java.nio.file.Path;
3030

3131
/**
3232
* Команда загрузки отчета о тестировании из файла
@@ -54,7 +54,7 @@ public void run() {
5454
}
5555

5656
//TODO: MULTI: getFileNames()
57-
File file = new File(path);
57+
var file = Path.of(path);
5858

5959
try {
6060
SessionsManager.getInstance().importSession(file);

viewer/src/main/java/ru/biatech/edt/junit/ui/viewsupport/ImageProvider.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import org.eclipse.jface.resource.ImageDescriptor;
2121
import org.eclipse.jface.viewers.DecorationOverlayIcon;
2222
import org.eclipse.jface.viewers.IDecoration;
23+
import org.eclipse.swt.SWT;
2324
import org.eclipse.swt.graphics.Image;
25+
import org.eclipse.swt.widgets.Display;
2426
import org.eclipse.ui.ISharedImages;
2527
import org.eclipse.ui.IWorkbench;
2628
import org.eclipse.ui.PlatformUI;
@@ -91,10 +93,14 @@ public class ImageProvider {
9193
@Getter(lazy = true)
9294
private final Image logo = createManagedImage(LOGO);
9395
@Getter(lazy = true)
96+
private final Image inactiveLogo = createGrayManagedImage(getLogo());
97+
@Getter(lazy = true)
9498
private final Image testRunOKIcon = createOverlayIcon(getLogo(), OVERLAY_SUCCESS);
9599
@Getter(lazy = true)
96100
private final Image testRunFailIcon = createOverlayIcon(getLogo(), OVERLAY_FAILED);
97101
@Getter(lazy = true)
102+
private final Image testRunErrorIcon = createOverlayIcon(getLogo(), OVERLAY_ERROR);
103+
@Getter(lazy = true)
98104
private final Image testIcon = createManagedImage(TEST_ICON);
99105
@Getter(lazy = true)
100106
private final Image testOkIcon = createManagedImage(TEST_OK_ICON);
@@ -176,6 +182,12 @@ private Image createManagedImage(String path) {
176182
return createManagedImage(getImageDescriptor(path));
177183
}
178184

185+
private Image createGrayManagedImage(Image base) {
186+
var image = new Image(Display.getDefault(), base, SWT.IMAGE_GRAY);
187+
imagesToDispose.add(image);
188+
return image;
189+
}
190+
179191
private Image createOverlayIcon(Image base, String second) {
180192
return createOverlayIcon(base, getImageDescriptor(second));
181193
}

viewer/src/main/java/ru/biatech/edt/junit/yaxunit/remote/RemoteLauncherImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void onMessageReceive(WebSocket socket, Message<?> message) {
9292
}
9393

9494
private void handleReportFile(ReportFileMessage message) {
95-
SessionsManager.getInstance().importActiveSession(message.getData().getReportFile().toFile());
95+
SessionsManager.getInstance().importActiveSession(message.getData().getReportFile());
9696
}
9797

9898
private void handleReport(ReportMessage message) {

0 commit comments

Comments
 (0)