Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/w 10673625 #193

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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 @@ -6,8 +6,9 @@
*/
package org.mule.extension.file.common.api.command;

import static java.lang.String.format;

import static org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage;
import static java.lang.String.format;
import static org.slf4j.LoggerFactory.getLogger;

import org.mule.extension.file.common.api.FileConnectorConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.net.URISyntaxException;
import java.util.regex.PatternSyntaxException;


/**
* Class for creating and handling URIs.
*
Expand Down Expand Up @@ -50,7 +51,7 @@ public static URI createUri(String basePath, String filePath) {
String fullPath;
try {
if (filePath.length() > 0) {
if (isAbsolute(filePath)) {
if (isAbsolute(filePath) || basePath.isEmpty()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what will happen if the filePath is not an absolute path and the basePath is empty? will the URI constructor throw an exception?

fullPath = filePath;
} else {
fullPath = addSeparator(basePath) + filePath;
Expand All @@ -73,7 +74,7 @@ public static URI createUri(String basePath, String filePath) {
* Adds a separator at the end of the given path. If the path already ends with the separator, then this method does nothing.
*/
private static String addSeparator(String path) {
return (path.endsWith(SEPARATOR) || path.length() == 1) ? path : path + SEPARATOR;
return (path.endsWith(SEPARATOR)) ? path : path + SEPARATOR;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
*/
package org.mule.test.extension.file.common;

import static org.mule.extension.file.common.api.util.UriUtils.createUri;
import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.mule.extension.file.common.api.util.UriUtils.createUri;

import org.mule.extension.file.common.api.AbstractFileAttributes;

import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Test;

public class AbstractFileAttributesTestCase {

private static final String TEST_FILENAME = "test.txt";
private static String path;

@Test
Expand Down Expand Up @@ -135,13 +135,117 @@ public void bothConstructorAssignEqualFileNamesForParentDirectoryWithTrailingSla
assertFileName(path);
}

@Test
public void bothConstructorAssignAnEmptyBasePath() {
ConcreteFileAttributes uriAttributes = new ConcreteFileAttributes(createUri("", TEST_FILENAME));
assertThat(uriAttributes.getPath(), equalTo(TEST_FILENAME));
}

@Test
public void bothConstructorAssignAnEmptyFilePath() {
path = "/test";
ConcreteFileAttributes uriAttributes = new ConcreteFileAttributes(createUri(path, ""));
assertThat(uriAttributes.getPath(), equalTo(path));
}

@Test
public void bothConstructorAssignAnEmptyFilePathWithoutDash() {
ConcreteFileAttributes uriAttributes = new ConcreteFileAttributes(createUri(TEST_FILENAME, ""));
assertThat(uriAttributes.getPath(), equalTo(TEST_FILENAME));
}

private void assertFileName(String path) {
ConcreteFileAttributes pathAttributes = new ConcreteFileAttributes(Paths.get(path));
ConcreteFileAttributes uriAttributes = new ConcreteFileAttributes(createUri(path));

assertThat(pathAttributes.getName(), equalTo(uriAttributes.getName()));
}

@Test
public void bothConstructorWithBasePathAssignInSpecificFolderWithSeparator2() {
ConcreteFileAttributes pathAttributes = new ConcreteFileAttributes(Paths.get("/myFile.txt"));
// Basepath parameter will be ignored when tne filepath is absolute (when it start with slash)
ConcreteFileAttributes uriAttributes = new ConcreteFileAttributes(createUri("/root/", "/myFile.txt"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case I would expect the result as "/root/myfile.txt", the condition to check if the path is absolute checks only that the filePath started with a "/" but that doesn't mean that the path is absolute right? in Unix I would expect a "~/" to determine that the path is absolute and in Windows I would expect a "X:"

assertThat(pathAttributes.getName(), equalTo(uriAttributes.getName()));
}

@Test
public void bothConstructorWithBasePathInHome() {
assertBasePathAndFileName("~", "myFile.txt");
}

@Test
public void bothConstructorWithBasePathAssignInCurrent() {
assertBasePathAndFileName(".", "myFile.txt");
}

@Test
public void bothConstructorWithBasePathAssignInSpecificFolder() {
assertBasePathAndFileName("/root", "myFile.txt");
}

@Test
public void bothConstructorWithBasePathAssignInSpecificFolderWithSeparator() {
assertBasePathAndFileName("/root/", "myFile.txt");
}

@Test
public void bothConstructorWithBasePathAssignInSpecificFolderWithoutSeparators() {
assertBasePathAndFileName("root", "myFile.txt");
}

@Test
public void bothConstructorWithBasePathAssignInSpecificFoldersWithoutSeparators() {
assertBasePathAndFileName("root/test", "myFile.txt");
}

@Test
public void bothConstructorWithBasePathAssignInRoot() {
assertBasePathAndFileName("/", "myFile.txt");
}

@Test
public void bothConstructorWithBasePathAssignInParent() {
assertBasePathAndFileName("..", "myFile.txt");
}

@Test
public void bothConstructorWithBasePathAssignInParentSpeficFolder() {
assertBasePathAndFileName("/root", "../myFile.txt");
}

@Test
public void bothConstructorWithBasePathAEmpty() {
assertBasePathAndFileName("", "myFile.txt");
}

@Test
public void bothConstructorWithBasePathAEmptyAndSeparator() {
assertBasePathAndFileName("", "/myFile.txt");
}

@Test
public void bothConstructorWithBasePathAEmptyAndRelativePathFile() {
assertBasePathAndFileName("", "~/myFile.txt");
}

@Test
public void bothConstructorWithBasePathAEmptyAndFilePathEmpty() {
assertBasePathAndFileName("", "");
}

@Test
public void bothConstructorWithBasePathWithSpaceAndFilePathEmpty() {
assertBasePathAndFileName(" ", "");
}

private void assertBasePathAndFileName(String basePath, String filePath) {
ConcreteFileAttributes pathAttributes = new ConcreteFileAttributes(Paths.get(basePath, filePath));
ConcreteFileAttributes uriAttributes = new ConcreteFileAttributes(createUri(basePath, filePath));
assertThat(pathAttributes.getName(), equalTo(uriAttributes.getName()));
assertThat(pathAttributes.getPath(), equalTo(uriAttributes.getPath()));
}

private class ConcreteFileAttributes extends AbstractFileAttributes {

public ConcreteFileAttributes(Path path) {
Expand Down