Skip to content

Commit

Permalink
#378: Refactored MarkdownImporter removed legacy ID. Broke Spec-Objec…
Browse files Browse the repository at this point in the history
…t-Importer.
  • Loading branch information
redcatbear committed Mar 21, 2024
1 parent 7a8ee0b commit 1fab988
Show file tree
Hide file tree
Showing 30 changed files with 851 additions and 936 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,12 @@ public class SpecificationItemId implements Comparable<SpecificationItemId>
+ ITEM_NAME_PATTERN //
+ REVISION_SEPARATOR //
+ ITEM_REVISION_PATTERN;
// [impl->dsn~md.eb-markdown-id~1]
private static final String LEGACY_ID = LEGACY_ID_NAME + ", *v" //
+ ITEM_REVISION_PATTERN;
private static final int ARTIFACT_TYPE_MATCHING_GROUP = 1;
private static final int NAME_MATCHING_GROUP = 2;
private static final int REVISION_MATCHING_GROUP = 3;
/** Regexp pattern for item IDs: {@code <type>~<name>~<revision>} */
public static final Pattern ID_PATTERN = Pattern.compile(ID);
private static final Pattern LEGACY_NAME_PATTERN = Pattern.compile(LEGACY_ID_NAME);
/** Regexp pattern for legacy item IDs: {@code <name>, v<revision>}. */
public static final Pattern LEGACY_ID_PATTERN = Pattern.compile(LEGACY_ID);

private final String name;
private final int revision;
Expand Down Expand Up @@ -301,13 +296,11 @@ public SpecificationItemId build()
if (this.id == null)
{
validateFields();
cleanUpLegacyIds();
}
else
{
parseId();
}

return new SpecificationItemId(this.name, this.artifactType, this.revision);
}

Expand All @@ -319,60 +312,17 @@ private void validateFields()
}
}

private void cleanUpLegacyIds()
{
if (this.artifactType == null || this.artifactType.isEmpty())
{
inferArtifactType();
}

removeSuperfluousArtifactPrefix();
}

private void inferArtifactType()
{
final Matcher matcher = LEGACY_NAME_PATTERN.matcher(this.name);
if (matcher.matches())
{
this.artifactType = matcher.group(ARTIFACT_TYPE_MATCHING_GROUP);
}
else
{
LOG.warning(() -> "Name '" + this.name + "' does not match legacy name pattern '"
+ LEGACY_NAME_PATTERN + "': using artifact type '" + UNKNOWN_ARTIFACT_TYPE
+ "'.");
this.artifactType = UNKNOWN_ARTIFACT_TYPE;
}
}

private void removeSuperfluousArtifactPrefix()
{
final Matcher matcher = LEGACY_NAME_PATTERN.matcher(this.name);
if (matcher.matches())
{
this.name = matcher.group(NAME_MATCHING_GROUP);
}
}

private void parseId()
{
final Matcher matcher = ID_PATTERN.matcher(this.id);
if (matcher.matches())
{
setIdPartsFromMatches(matcher);
}
else
{
final Matcher legacyMatcher = LEGACY_ID_PATTERN.matcher(this.id);
if (legacyMatcher.matches())
{
setIdPartsFromMatches(legacyMatcher);
}
else
{
throw new IllegalStateException("String \"" + this.id
+ "\" cannot be parsed to a specification item ID");
}
else {
throw new IllegalArgumentException(
"Invalid specification item ID format: \"" + this.id + "\". "
+ "Format must be <arifact-type>~<requirement-name-or-number>~<revision>.");
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hamcrest.Matchers.equalTo;
import static org.itsallcode.openfasttrace.api.core.SpecificationItemId.createId;
import static org.itsallcode.openfasttrace.api.core.SpecificationItemId.parseId;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.itsallcode.openfasttrace.api.core.SpecificationItemId.Builder;
Expand All @@ -19,61 +20,36 @@
// [utest->dsn~specification-item-id~1]
class TestSpecificationItemId
{
private static final int REVISION = 1;
private static final String NAME = "foo";
private static final String ARTIFACT_TYPE_FEATURE = "feat";

@Test
void testCreateId()
@ParameterizedTest(name = "Parsing of ID ''{0}'' succeeds")
@CsvSource(value =
{
final SpecificationItemId id = createId(ARTIFACT_TYPE_FEATURE, NAME, REVISION);
assertThat(id, equalTo(new Builder().artifactType(ARTIFACT_TYPE_FEATURE).name(NAME)
.revision(REVISION).build()));
}

@Test
void testCreateIdWithoutRevision()
"type~name~42, type, name, 42",
"type~name-with-dash~42, type, name-with-dash, 42",
"type~name.with.dot~42, type, name.with.dot, 42",
"type~name-trailing-~42, type, name-trailing-, 42",
"foobar~utf-8.compatible.äöü~333, foobar, utf-8.compatible.äöü, 333"
})
void parsingValidIdsSucceeds(final String id, final String expectedType, final String expectedName,
final int expectedRevision)
{
final SpecificationItemId id = createId(ARTIFACT_TYPE_FEATURE, NAME);
assertThat(id, equalTo(new Builder().artifactType(ARTIFACT_TYPE_FEATURE).name(NAME)
.revision(Integer.MIN_VALUE).build()));
final SpecificationItemId parsedId = parseId(id);
assertAll(
() -> assertThat(parsedId.getArtifactType(), equalTo(expectedType)),
() -> assertThat(parsedId.getName(), equalTo(expectedName)),
() -> assertThat(parsedId.getRevision(), equalTo(expectedRevision)));
}

// This is the wildcard revision test
@Test
void testParseId_singleDigitRevision()
{
final SpecificationItemId id = parseId("feat~foo~1");
assertThat(id.getArtifactType(), equalTo(ARTIFACT_TYPE_FEATURE));
assertThat(id.getName(), equalTo(NAME));
assertThat(id.getRevision(), equalTo(1));
}


@Test
void testParseId_multipleFragmentName()
{
final SpecificationItemId id = parseId("feat~foo.bar_zoo.baz-narf~1");
assertThat(id.getArtifactType(), equalTo(ARTIFACT_TYPE_FEATURE));
assertThat(id.getName(), equalTo("foo.bar_zoo.baz-narf"));
assertThat(id.getRevision(), equalTo(1));
}

@Test
void testParseId_umlautName()
{
final SpecificationItemId id = parseId("feat~änderung~1");
assertThat(id.getArtifactType(), equalTo(ARTIFACT_TYPE_FEATURE));
assertThat(id.getName(), equalTo("änderung"));
assertThat(id.getRevision(), equalTo(1));
}

@Test
void testParseId_multipleDigitRevision()
void testCreateIdWithoutRevision()
{
final SpecificationItemId id = parseId("feat~foo~999");
assertThat(id.getArtifactType(), equalTo(ARTIFACT_TYPE_FEATURE));
assertThat(id.getName(), equalTo(NAME));
assertThat(id.getRevision(), equalTo(999));
final SpecificationItemId id = createId("impl", "wildcard-feature");
assertAll(() -> assertThat(id, equalTo(new Builder().artifactType("impl").name("wildcard-feature")
.revision(Integer.MIN_VALUE).build())),
() -> assertThat(SpecificationItemId.REVISION_WILDCARD, equalTo(Integer.MIN_VALUE)));
}

@Test
Expand All @@ -85,21 +61,30 @@ void testParseId_IllegalNumberFormat()
"Error parsing version number from specification item ID: \"feat~foo~999999999999999999999999999999999999999\""));
}

@ParameterizedTest
@CsvSource(
{ "feat.foo~1", "foo~1", "req~foo", "req1~foo~1", "req.r~foo~1", "req~1foo~1", "req~.foo~1", "req~foo~-1",
@CsvSource({
"feat.foo~1",
"foo~1",
"req~foo",
"req1~foo~1",
"req.r~foo~1",
"req~1foo~1",
"req~.foo~1",
"req~foo.~1",
"req~foo~-1",
// Wildcard revision:
"feat~foo~-2147483648" })
"feat~foo~-2147483648",
// Obsolet Elektrobit-style specification item ID
"feat:foo, v1"
})
@ParameterizedTest(name = "Parsing of id ''{0}'' fails")
void testParseId_mustFailForIllegalIds(final String illegalId)
{

final IllegalStateException exception = assertThrows(IllegalStateException.class,
() -> parseId(illegalId));
final Throwable exception = assertThrows(IllegalArgumentException.class, () -> parseId(illegalId));
assertThat(exception.getMessage(),
equalTo("String \"" + illegalId + "\" cannot be parsed to a specification item ID"));
equalTo("Invalid specification item ID format: \"" + illegalId + "\". "
+ "Format must be <arifact-type>~<requirement-name-or-number>~<revision>."));
}


@Test
void testToRevisionWildcard()
{
Expand All @@ -121,16 +106,7 @@ void testToStringRevisionWildcard()
{
final Builder builder = new Builder();
builder.artifactType("dsn").name("dummy").revisionWildcard();
assertThat(builder.build().toString(),
equalTo("dsn~dummy~" + SpecificationItemId.REVISION_WILDCARD));
}

void testCreate_WithRevisionWildcard()
{
final SpecificationItemId id = createId(ARTIFACT_TYPE_FEATURE, NAME);
assertThat(id.getArtifactType(), equalTo(ARTIFACT_TYPE_FEATURE));
assertThat(id.getName(), equalTo(NAME));
assertThat(id.getRevision(), equalTo(SpecificationItemId.REVISION_WILDCARD));
assertThat(builder.build().toString(), equalTo("dsn~dummy~" + SpecificationItemId.REVISION_WILDCARD));
}

@Test
Expand Down

This file was deleted.

Loading

0 comments on commit 1fab988

Please sign in to comment.