From 641e4295a1f9c79bde10d13c072924722c0e3052 Mon Sep 17 00:00:00 2001 From: Dmitry Baev Date: Mon, 16 Feb 2015 16:03:05 +0300 Subject: [PATCH] fix NPE when convert invalid results (non-exist names) --- .../DefaultTestCaseConverter.groovy | 23 +++-- .../DefaultTestCaseConverterTest.groovy | 87 +++++++++++++++++++ 2 files changed, 102 insertions(+), 8 deletions(-) diff --git a/allure-report-data/src/main/groovy/ru/yandex/qatools/allure/data/converters/DefaultTestCaseConverter.groovy b/allure-report-data/src/main/groovy/ru/yandex/qatools/allure/data/converters/DefaultTestCaseConverter.groovy index aa5844f5..22e7f6e4 100644 --- a/allure-report-data/src/main/groovy/ru/yandex/qatools/allure/data/converters/DefaultTestCaseConverter.groovy +++ b/allure-report-data/src/main/groovy/ru/yandex/qatools/allure/data/converters/DefaultTestCaseConverter.groovy @@ -23,6 +23,10 @@ import static ru.yandex.qatools.allure.data.utils.TextUtils.generateUid */ class DefaultTestCaseConverter implements TestCaseConverter { + public static final String UNKNOWN_STEP_NAME = "UnknownStepName" + public static final String UNKNOWN_TEST_SUITE = "UnknownTestSuite" + public static final String UNKNOWN_TEST_CASE = "UnknownTestCase" + def suiteUids = [:].withDefault { generateUid(); } @@ -48,9 +52,9 @@ class DefaultTestCaseConverter implements TestCaseConverter { use([PluginUtils, SummaryCategory]) { result.uid = generateUid(); - if (!result.title) { - result.title = TextUtils.humanize(result.name); - } + result.name = source.name ?: UNKNOWN_TEST_CASE; + result.title = result.title ?: TextUtils.humanize(result.name); + result.description = source.convertedDescription; result.time = source.time; @@ -61,10 +65,14 @@ class DefaultTestCaseConverter implements TestCaseConverter { result.severity = source.severity; result.testId = source.testId result.issues = source.issues; + + def suiteName = source.suiteName ?: UNKNOWN_TEST_SUITE; + def suiteTitle = source.suiteTitle ?: TextUtils.humanize(suiteName); + result.suite = new AllureTestSuiteInfo( uid: suiteUids[source.suiteName], - name: source.suiteName, - title: source.suiteTitle ?: source.suiteName ? TextUtils.humanize(source.suiteName) : "Unknown" + name: suiteName, + title: suiteTitle ); } @@ -79,9 +87,8 @@ class DefaultTestCaseConverter implements TestCaseConverter { def source = context.source; use([PluginUtils, SummaryCategory]) { - if (!result.title && result.name) { - result.title = TextUtils.humanize(result.name); - } + result.name = result.name ?: UNKNOWN_STEP_NAME; + result.title = result.title ?: TextUtils.humanize(result.name); result.time = source.time; diff --git a/allure-report-data/src/test/groovy/ru/yandex/qatools/allure/data/converters/DefaultTestCaseConverterTest.groovy b/allure-report-data/src/test/groovy/ru/yandex/qatools/allure/data/converters/DefaultTestCaseConverterTest.groovy index 9423a174..9c00d345 100644 --- a/allure-report-data/src/test/groovy/ru/yandex/qatools/allure/data/converters/DefaultTestCaseConverterTest.groovy +++ b/allure-report-data/src/test/groovy/ru/yandex/qatools/allure/data/converters/DefaultTestCaseConverterTest.groovy @@ -2,6 +2,7 @@ package ru.yandex.qatools.allure.data.converters import org.junit.Test import ru.yandex.qatools.allure.data.io.TestCaseReader +import ru.yandex.qatools.allure.data.utils.TextUtils import ru.yandex.qatools.allure.model.Attachment import ru.yandex.qatools.allure.model.Description import ru.yandex.qatools.allure.model.DescriptionType @@ -11,6 +12,8 @@ import ru.yandex.qatools.allure.model.Step import ru.yandex.qatools.allure.model.TestCaseResult import static ru.yandex.qatools.allure.config.AllureModelUtils.createSeverityLabel +import static ru.yandex.qatools.allure.data.converters.DefaultTestCaseConverter.UNKNOWN_STEP_NAME +import static ru.yandex.qatools.allure.data.converters.DefaultTestCaseConverter.UNKNOWN_TEST_CASE import static ru.yandex.qatools.allure.model.SeverityLevel.CRITICAL import static ru.yandex.qatools.allure.model.SeverityLevel.NORMAL import static ru.yandex.qatools.allure.model.Status.PASSED @@ -296,6 +299,90 @@ class DefaultTestCaseConverterTest { assert modify.attachments[0].uid } + @Test + void shouldConvertTestCaseWithoutNameAndTitle() { + def origin = new TestCaseResult() + def modify = converter.convert(origin) + + assert modify.name + assert modify.name == UNKNOWN_TEST_CASE + + assert modify.title + assert modify.title == TextUtils.humanize(UNKNOWN_TEST_CASE) + } + + @Test + void shouldConvertTestCaseWithoutName() { + def origin = new TestCaseResult(title: "some title") + def modify = converter.convert(origin) + + assert modify.name + assert modify.name == UNKNOWN_TEST_CASE + + assert modify.title + assert modify.title == "some title" + } + + @Test + void shouldConvertTestCaseWithoutTitle() { + def origin = new TestCaseResult(name: "someName") + def modify = converter.convert(origin) + + assert modify.name + assert modify.name == "someName" + + assert modify.title + assert modify.title == "Some name" + } + + @Test + void shouldConvertStepWithoutNameAndTitle() { + def origin = new TestCaseResult( + name: "name", + steps: [new Step()] + ) + def modify = converter.convert(origin) + + assert modify.steps + assert modify.steps.size() == 1 + assert modify.steps[0].name + assert modify.steps[0].name == UNKNOWN_STEP_NAME + assert modify.steps[0].title + assert modify.steps[0].title == TextUtils.humanize(UNKNOWN_STEP_NAME) + } + + @Test + void shouldConvertStepWithoutName() { + def origin = new TestCaseResult( + name: "name", + steps: [new Step(title: "some title")] + ) + def modify = converter.convert(origin) + + assert modify.steps + assert modify.steps.size() == 1 + assert modify.steps[0].name + assert modify.steps[0].name == UNKNOWN_STEP_NAME + assert modify.steps[0].title + assert modify.steps[0].title == "some title" + } + + @Test + void shouldConvertStepWithoutTitle() { + def origin = new TestCaseResult( + name: "name", + steps: [new Step(name: "someName")] + ) + def modify = converter.convert(origin) + + assert modify.steps + assert modify.steps.size() == 1 + assert modify.steps[0].name + assert modify.steps[0].name == "someName" + assert modify.steps[0].title + assert modify.steps[0].title == "Some name" + } + static def checkLabel(List