Skip to content
Merged
Show file tree
Hide file tree
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 @@ -66,6 +66,49 @@ void canReadProtectedRegionContent() throws IOException {
protectedRegion);
}

@Test
void canReadEmptyProtectedRegion() {
// given
String mdslInput = "API description Test\n" + "// ** BEGIN PROTECTED REGION for data types\n"
+ "// ** END PROTECTED REGION for data types\n";

// when
String protectedRegion = new ProtectedRegionReader().getProtectedRegionContent(mdslInput,
ProtectedRegionIdentifier.DATA_TYPE_REGION);

// then
assertEquals("", protectedRegion);
}

@Test
void canReadProtectedRegionWithEmptyLine() {
// given
String mdslInput = "API description Test\n" + "// ** BEGIN PROTECTED REGION for API providers\n" + "\n"
+ "// ** END PROTECTED REGION for API providers\n";

// when
String protectedRegion = new ProtectedRegionReader().getProtectedRegionContent(mdslInput,
ProtectedRegionIdentifier.PROVIDER_REGION);

// then
assertEquals("", protectedRegion);
}

@Test
void canReadProtectedRegionWithWindowsLineEndings() {
// given
String mdslInput = "API description Test\r\n" + "// ** BEGIN PROTECTED REGION for data types\r\n"
+ "data type Parameter1Type P\r\n" + "data type YetAnotherDataType P\r\n"
+ "// ** END PROTECTED REGION for data types\r\n";

// when
String protectedRegion = new ProtectedRegionReader().getProtectedRegionContent(mdslInput,
ProtectedRegionIdentifier.DATA_TYPE_REGION);

// then
assertEquals("data type Parameter1Type P\r\ndata type YetAnotherDataType P", protectedRegion);
}

@Test
void canGetIdentifiersInDataTypeProtectedRegion() throws IOException {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@
public class ProtectedRegionReader {

public String getProtectedRegionContent(String mdslInput, ProtectedRegionIdentifier regionIdentifier) {
String regionStartString = getRegionStartString(regionIdentifier);
if (containsProtectedRegion(mdslInput, regionIdentifier))
return mdslInput.substring(mdslInput.indexOf(regionStartString) + regionStartString.length() + System.lineSeparator().length(),
mdslInput.lastIndexOf(getRegionEndString(regionIdentifier)) - System.lineSeparator().length());
return null;
if (!containsProtectedRegion(mdslInput, regionIdentifier))
return null;

// the content starts after the line break of the start line and ends before the line break of the end line
int startLineEnd = mdslInput.indexOf('\n', mdslInput.indexOf(getRegionStartString(regionIdentifier)));
int endIndex = mdslInput.lastIndexOf(getRegionEndString(regionIdentifier));
if (startLineEnd < 0 || endIndex <= startLineEnd)
return "";

String content = mdslInput.substring(startLineEnd + 1, endIndex);
if (content.endsWith("\n"))
content = content.substring(0, content.length() - 1);
if (content.endsWith("\r"))
content = content.substring(0, content.length() - 1);
return content;
}

public boolean containsProtectedRegion(String mdslInput, ProtectedRegionIdentifier regionIdentifier) {
Expand Down
Loading