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
12 changes: 9 additions & 3 deletions gortools/src/main/scala/gorsat/Analysis/ForkWrite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ case class ForkWrite(forkCol: Int,
fullFileName.replace("#{fork}", forkValue).replace("""${fork}""", forkValue)
} else {
if (fullFileName.isEmpty && options.linkFile.nonEmpty) {
val (linkFileMeta, linkFileInfo) = extractLinkMetaInfo(options.linkFileMeta)
val linkSourceRef = new SourceReference(options.linkFile, null, projectContext.getFileReader.getCommonRoot, null, null, true);
// Infer the full file name from the link (and defautl locations)
LinkFile.inferDataFileNameFromLinkFile(
projectContext.getFileReader.resolveDataSource(linkSourceRef).asInstanceOf[StreamSource]);
projectContext.getFileReader.resolveDataSource(linkSourceRef).asInstanceOf[StreamSource], linkFileMeta);
} else {
fullFileName
}
Expand Down Expand Up @@ -380,10 +381,15 @@ case class ForkWrite(forkCol: Int,
linkFileContent = dataSource.getProjectLinkFileContent
}
}
val (linkFileMeta, linkFileInfo) = extractLinkMetaInfo(optLinkFileMeta)
(linkFile, linkFileContent, linkFileMeta, linkFileInfo)
}

private def extractLinkMetaInfo(optLinkFileMeta: String) : (String, String) = {
var linkFileMeta = ""
var linkFileInfo = ""

if (linkFile.nonEmpty && !Strings.isNullOrEmpty(optLinkFileMeta)) {
if (!Strings.isNullOrEmpty(optLinkFileMeta)) {
for (s <- CommandParseUtilities.quoteSafeSplit(StringUtils.strip(optLinkFileMeta, "\"\'"), ',')) {
val l = s.trim
if (l.startsWith(LinkFileEntryV1.ENTRY_INFO_KEY)) {
Expand All @@ -394,7 +400,7 @@ case class ForkWrite(forkCol: Int,
}
}

(linkFile, linkFileContent, linkFileMeta, linkFileInfo)
(linkFileMeta, linkFileInfo)
}

private def writeLinkFile(linkFilePath: String, linkFileContent: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ public static String validateAndUpdateLinkFileName(String linkFilePath) {
* Infer the data file name from the link file name.
*
* @param linkSource the link file path with the link extension
* @param linkFileMeta additional link file meta data
* @return the data file path
*/
public static String inferDataFileNameFromLinkFile(StreamSource linkSource) throws IOException {
public static String inferDataFileNameFromLinkFile(StreamSource linkSource, String linkFileMeta) throws IOException {
if (linkSource == null || Strings.isNullOrEmpty(linkSource.getFullPath())) {
throw new IllegalArgumentException("Link file path is null or empty. Can not infer data file name.");
}
Expand All @@ -105,13 +106,12 @@ public static String inferDataFileNameFromLinkFile(StreamSource linkSource) thro
}

var dataFileRootPath = "";

if (linkSource.exists()) {
var link = load(linkSource);
var linkDataFileRootPath = link.getMeta().getProperty(LinkFileMeta.HEADER_CONTENT_LOCATION_MANAGED_KEY);
if (!Strings.isNullOrEmpty(linkDataFileRootPath)) {
dataFileRootPath = linkDataFileRootPath;
}
var link = linkSource.exists()
? load(linkSource).appendMeta(linkFileMeta)
: create(linkSource, linkFileMeta);
var linkDataFileRootPath = link.getMeta().getProperty(LinkFileMeta.HEADER_CONTENT_LOCATION_MANAGED_KEY);
if (!Strings.isNullOrEmpty(linkDataFileRootPath)) {
dataFileRootPath = linkDataFileRootPath;
}

if (Strings.isNullOrEmpty(dataFileRootPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class LinkFileMeta extends BaseMeta {
public static final String HEADER_ENTRIES_COUNT_MAX_KEY = "ENTRIES_COUNT_MAX";
// Max age of entries to keep track of in the link file.
public static final String HEADER_ENTRIES_AGE_MAX_KEY = "ENTRIES_AGE_MAX";
// Determines if the content data location should be managed.
// Path if the managed content data location.
public static final String HEADER_CONTENT_LOCATION_MANAGED_KEY = "CONTENT_LOCATION_MANAGED";
// Should the content lifecycle be managed (data deleted if the link is removed from the link file).
// Should the content lifecycle be managed (data deleted if the link is removed from the link file) (true or false).
public static final String HEADER_CONTENT_LIFECYCLE_MANAGED_KEY = "CONTENT_LIFECYCLE_MANAGED";

public static final String[] DEFAULT_TABLE_HEADER = new String[] {"File", "Timestamp", "MD5", "Serial", "Info"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,41 +168,73 @@ public void testSaveLinkFileV1ToV0() throws IOException {

@Test(expected = IllegalArgumentException.class)
public void testInferDataFileNameFromLinkFile_NullOrEmptyPath() throws Exception {
LinkFile.inferDataFileNameFromLinkFile(new FileSource(""));
LinkFile.inferDataFileNameFromLinkFile(new FileSource(""), null);
}

@Test(expected = IllegalArgumentException.class)
public void testInferDataFileNameFromLinkFile_AbsolutePath() throws Exception {
LinkFile.inferDataFileNameFromLinkFile(new FileSource("/abs/path/x.link"));
LinkFile.inferDataFileNameFromLinkFile(new FileSource("/abs/path/x.link"), null);
}

@Test(expected = IllegalArgumentException.class)
public void testInferDataFileNameFromLinkFile_NoRootConfigured() throws Exception {
environmentVariables.set(GorDriverConfig.GOR_DRIVER_LINK_MANAGED_DATA_FILES_URL, null);
LinkFile.inferDataFileNameFromLinkFile(new FileSource("x.link"));
LinkFile.inferDataFileNameFromLinkFile(new FileSource("x.link"), null);
}

@Test
public void testInferDataFileNameFromLinkFile_FromEnvVariable_WithProject() throws Exception {
String root = "/managed/root";
environmentVariables.set(GorDriverConfig.GOR_DRIVER_LINK_MANAGED_DATA_FILES_URL, root);

String result = LinkFile.inferDataFileNameFromLinkFile(new FileSource(new SourceReference("x.gor.link", null, "/projects/test", -1, null, null, false, false)));
String result = LinkFile.inferDataFileNameFromLinkFile(new FileSource(new SourceReference("x.gor.link", null, "/projects/test", -1, null, null, false, false)), null);
assertNotNull(result);
assertTrue(result.startsWith(root + "/test/x"));
assertFalse(result.endsWith(".gor.link"));
assertNotEquals(result, root + "/test/x.gor.link");
assertTrue(result.matches((root + "/test/x\\..*\\.gor").replace("/", "\\/")));
}

@Test
public void testInferDataFileNameFromLinkFile_FromEnvVariable_WithOutProject() throws Exception {
String root = "/managed/root";
environmentVariables.set(GorDriverConfig.GOR_DRIVER_LINK_MANAGED_DATA_FILES_URL, root);

String result = LinkFile.inferDataFileNameFromLinkFile(new FileSource("x.gor.link"));
String result = LinkFile.inferDataFileNameFromLinkFile(new FileSource("x.gor.link"), null);
assertTrue(result.matches((root + "/x\\..*\\.gor").replace("/", "\\/")));
}

@Test
public void testInferDataFileNameFromLinkFile_FromExiting_File() throws Exception {
String root = "/managed/fromfile";
String linkFilePath = "x.gor.link";
Files.createDirectory(workPath.resolve("test"));
Files.writeString(workPath.resolve("test").resolve(linkFilePath), "## " + LinkFileMeta.HEADER_CONTENT_LOCATION_MANAGED_KEY + " = " + root + "\nsource/y.gorz\n");

String result = LinkFile.inferDataFileNameFromLinkFile(new FileSource(new SourceReference(linkFilePath, null, workPath.resolve("test").toString(), -1, null, null, false, false)), null);
assertNotNull(result);
assertTrue(result.matches((root + "/test/x\\..*\\.gor").replace("/", "\\/")));
}

@Test
public void testInferDataFileNameFromLinkFile_FromMetaParam() throws Exception {
String root = "/managed/fromparam";
String linkFilePath = "x.gor.link";
String linkFileMeta = "## " + LinkFileMeta.HEADER_CONTENT_LOCATION_MANAGED_KEY + " = " + root;

String result = LinkFile.inferDataFileNameFromLinkFile(new FileSource(new SourceReference(linkFilePath)), linkFileMeta);
assertNotNull(result);
assertTrue(result.matches((root + "/x\\..*\\.gor").replace("/", "\\/")));
}

@Test
public void testInferDataFileNameFromLinkFile_FromMetaParam_ExistingFile() throws Exception {
String fileroot = "/managed/fromfile";
String linkFilePath = "x.gor.link";
Files.writeString(workPath.resolve(linkFilePath), "## " + LinkFileMeta.HEADER_CONTENT_LOCATION_MANAGED_KEY + " = " + fileroot + "\nsource/y.gorz\n");

String paramroot = "/managed/fromparam";
String linkFileMeta = "## " + LinkFileMeta.HEADER_CONTENT_LOCATION_MANAGED_KEY + " = " + paramroot;

String result = LinkFile.inferDataFileNameFromLinkFile(new FileSource(new SourceReference(linkFilePath)), linkFileMeta);
assertNotNull(result);
assertTrue(result.startsWith(root + "/x"));
assertFalse(result.endsWith(".gor.link"));
assertNotEquals(result, root + "/x.gor.link");
assertTrue(result.matches((paramroot + "/x\\..*\\.gor").replace("/", "\\/")));
}
}
Loading