Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.Arguments.argumentSet;
import static org.mockito.Mockito.*;

import java.io.BufferedReader;
Expand All @@ -19,25 +20,73 @@
import org.itsallcode.openfasttrace.importer.xmlparser.XmlParserFactory;
import org.itsallcode.openfasttrace.testutil.importer.input.StreamInput;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.FieldSource;

class TestSpecobjectImporter
{
private static final String PSEUDO_FILENAME = "pseudo_filename";
private static final Location STANDARD_LOCATION = Location.create(PSEUDO_FILENAME, 2);

@Test
void testImportOfMinimalSpecObject()
static List<Arguments> idImportChecks = List.of(
argumentSet("minimal import", """
<specobjects doctype="req">
<specobject>
<id>minimal</id>
<version>1</version>
</specobject>
</specobjects>""",
"req~minimal~1"),

argumentSet("strip superfluous artifact type", """
<specobjects doctype="impl">
<specobject>
<id>impl:strip_duplicate_prefix</id>
<version>0</version>
</specobject>
</specobjects>""",
"impl~strip_duplicate_prefix~0"),

argumentSet("ignore unknown XML elements", """
<specobjects doctype="feat">
<specobject>
<id>ignore-selected-xml-elements</id>
<version>12345</version>
<creationdate>1970-01-01</creationdate>
<source>john doe</source>
</specobject>
</specobjects>""",
"feat~ignore-selected-xml-elements~12345"),

argumentSet("fullfilled-by is ignored", """
<specobjects doctype="req">
<specobject>
<id>with-fulfilled-by</id>
<version>1</version>
<fulfilledby>
<ffbObj>
<ffbType>impl</ffbType>
<ffbId>ffb-a</ffbId>
<ffbVersion>1</ffbVersion>
</ffbObj>
<ffbObj>
<ffbType>utest</ffbType>
<ffbId>ffb-b</ffbId>
<ffbVersion>2</ffbVersion>
</ffbObj>
</fulfilledby>
</specobject>
</specobjects>""", "req~with-fulfilled-by~1"));

@ParameterizedTest
@FieldSource("idImportChecks")
void testImportSeesTheCorrectId(final String input, final String expectedId)
{
final ImportEventListener listenerMock = importFromString("""
<specobjects doctype="req">
<specobject>
<id>minimal</id>
<version>1</version>
</specobject>
</specobjects>""");
final ImportEventListener listenerMock = importFromString(input);
verify(listenerMock).beginSpecificationItem();
verify(listenerMock).setLocation(STANDARD_LOCATION);
verify(listenerMock).setId(SpecificationItemId.parseId("req~minimal~1"));
verify(listenerMock).setLocation(TestSpecobjectImporter.STANDARD_LOCATION);
verify(listenerMock).setId(SpecificationItemId.parseId(expectedId));
verify(listenerMock).endSpecificationItem();
verifyNoMoreInteractions(listenerMock);
}
Expand Down Expand Up @@ -103,26 +152,6 @@ void testImportOnlyShortDescription()
verifyNoMoreInteractions(listenerMock);
}

@Test
void testSelectedElementsAreIgnoredDuringImport()
{
final ImportEventListener listenerMock = importFromString("""
<specobjects doctype="feat">
<specobject>
<id>ignore-selected-xml-elements</id>
<version>12345</version>
<creationdate>1970-01-01</creationdate>
<source>john doe</source>
</specobject>
</specobjects>""");
verify(listenerMock).beginSpecificationItem();
verify(listenerMock).setLocation(STANDARD_LOCATION);
verify(listenerMock)
.setId(SpecificationItemId.parseId("feat~ignore-selected-xml-elements~12345"));
verify(listenerMock).endSpecificationItem();
verifyNoMoreInteractions(listenerMock);
}

@Test
void testStripSuperfluousArtifactPrefixFromName()
{
Expand All @@ -146,13 +175,13 @@ void testTakeOverLocationFromImportedFile()
final String expectedFileName = "/home/johndoe/openfasttrace/examples/specobject.xml";
final int expectedLine = 42;
final ImportEventListener listenerMock = importFromString("<specobjects doctype=\"utest\">\n" //
+ " <specobject>\n" //
+ " <id>takeOverLocation</id>\n" //
+ " <version>99999999</version>\n" //
+ " <sourcefile>" + expectedFileName + "</sourcefile>" //
+ " <sourceline>" + expectedLine + "</sourceline>" // "
+ " </specobject>\n" //
+ "</specobjects>");
+ " <specobject>\n" //
+ " <id>takeOverLocation</id>\n" //
+ " <version>99999999</version>\n" //
+ " <sourcefile>" + expectedFileName + "</sourcefile>" //
+ " <sourceline>" + expectedLine + "</sourceline>" // "
+ " </specobject>\n" //
+ "</specobjects>");
verify(listenerMock).beginSpecificationItem();
verify(listenerMock).setLocation(Location.create(expectedFileName, expectedLine));
verify(listenerMock).setId(SpecificationItemId.parseId("utest~takeOverLocation~99999999"));
Expand Down Expand Up @@ -229,35 +258,6 @@ void testImportWithDependencies()
verifyNoMoreInteractions(listenerMock);
}

@Test
void testImportFulfilledByIsIgnored()
{
final ImportEventListener listenerMock = importFromString("""
<specobjects doctype="req">
<specobject>
<id>with-fulfilled-by</id>
<version>1</version>
<fulfilledby>
<ffbObj>
<ffbType>impl</ffbType>
<ffbId>ffb-a</ffbId>
<ffbVersion>1</ffbVersion>
</ffbObj>
<ffbObj>
<ffbType>utest</ffbType>
<ffbId>ffb-b</ffbId>
<ffbVersion>2</ffbVersion>
</ffbObj>
</fulfilledby>
</specobject>
</specobjects>""");
verify(listenerMock).beginSpecificationItem();
verify(listenerMock).setLocation(STANDARD_LOCATION);
verify(listenerMock).setId(SpecificationItemId.parseId("req~with-fulfilled-by~1"));
verify(listenerMock).endSpecificationItem();
verifyNoMoreInteractions(listenerMock);
}

@Test
void testImportProvidesCoverage()
{
Expand Down Expand Up @@ -290,7 +290,7 @@ void testImportProvidesCoverage()
@Test
void testCustomTagsWithoutNamespacesLogsWarning()
{
try( final RecordingLogHandler logHandler = new RecordingLogHandler() )
try (final RecordingLogHandler logHandler = new RecordingLogHandler())
{
importFromString("""
<specdocument>\
Expand All @@ -314,7 +314,7 @@ void testCustomTagsWithoutNamespacesLogsWarning()
@Test
void testCustomTagsWithNamespacesLogsNoWarning()
{
try( final RecordingLogHandler logHandler = new RecordingLogHandler() )
try (final RecordingLogHandler logHandler = new RecordingLogHandler())
{
importFromString("""
<specdocument xmlns:x="http://extension">
Expand All @@ -336,7 +336,7 @@ void testCustomTagsWithNamespacesLogsNoWarning()
@Test
void testCustomTagsWithNamespacesPlusOftNamespaceLogsNoWarning()
{
try( final RecordingLogHandler logHandler = new RecordingLogHandler() )
try (final RecordingLogHandler logHandler = new RecordingLogHandler())
{
importFromString("""
<specdocument xmlns="https://github.com/itsallcode/openfasttrace"
Expand Down Expand Up @@ -368,7 +368,8 @@ public RecordingLogHandler()
rootLogger.addHandler(this);
}

@Override public void publish(final LogRecord logRecord)
@Override
public void publish(final LogRecord logRecord)
{
logRecords.add(logRecord);
super.publish(logRecord);
Expand All @@ -379,7 +380,8 @@ public Stream<LogRecord> getLogRecords()
return logRecords.stream();
}

@Override public void close()
@Override
public void close()
{
Arrays.stream(rootLogger.getHandlers())
.filter((RecordingLogHandler.class::isInstance))
Expand Down
Loading