Skip to content

Commit

Permalink
#18 integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maddingo committed Jan 13, 2017
1 parent 376fd45 commit 6d0752b
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 14 deletions.
34 changes: 34 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>nio-fs</artifactId>
<groupId>no.maddin.niofs</groupId>
<version>2.0.1-SNAPSHOT</version>
</parent>

<artifactId>integration-tests</artifactId>

<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>no.maddin.niofs</groupId>
<artifactId>nio-fs-sftp</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
31 changes: 31 additions & 0 deletions integration-tests/src/test/java/PathServerOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.hamcrest.Matcher;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;

import static org.hamcrest.Matchers.is;

@RunWith(Parameterized.class)
public class PathServerOperations {

private final String uriString;
private final Matcher<?> expected;

@Parameterized.Parameters(name="{index} {0} {1}")
public static java.util.List<Object[]> data() {
return Arrays.asList(
new Object[] {"sftp://localhost/", is(true)},
new Object[] {"sftp://localhost/testfile/", is(true)}
);
}

public PathServerOperations(String uriString, Matcher<?> expected) {
this.uriString = uriString;
this.expected = expected;
}

private void startSftpServer() {

}
}
71 changes: 71 additions & 0 deletions integration-tests/src/test/java/PathsGetUriIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import no.maddin.niofs.sftp.SFTPPath;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

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

import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

@RunWith(Parameterized.class)
public class PathsGetUriIT {

@Rule
public ExpectedException exception = ExpectedException.none();

private final String uriString;
private final Matcher<Path> testGetUriExpected;
private final Matcher<? extends Throwable> exceptionExpectation;

@Parameterized.Parameters(name="{index} {0} {1}")
public static java.util.List<Object[]> data() {
return Arrays.asList(
new Object[] {
"sftp://localhost/",
null,
instanceOf(SFTPPath.class)
},
new Object[] {
"sftp://localhost/testfile/",
null,
instanceOf(SFTPPath.class)
},
new Object[] {
"sftp://localhost/../testfile/",
null,
instanceOf(SFTPPath.class)
},
new Object[] {
"sftp:/localhost/../testfile/",
instanceOf(IllegalArgumentException.class),
instanceOf(SFTPPath.class)
}

);
}

public PathsGetUriIT(String uriString, Matcher<? extends Throwable> exceptionExpectation, Matcher<Path> testGetUriExpected) {
this.uriString = uriString;
this.exceptionExpectation = exceptionExpectation;
this.testGetUriExpected = testGetUriExpected;
}

@Test
public void getURI() throws Exception {

if (exceptionExpectation != null) {
exception.expect(exceptionExpectation);
}
Path p = Paths.get(URI.create(this.uriString));

assertThat(p, is(testGetUriExpected));
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>smb</module>
<module>test-util</module>
<module>util</module>
<module>integration-tests</module>
</modules>

<ciManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.spi.FileSystemProvider;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* FileSystemProvider for Secure FTP.
Expand Down Expand Up @@ -72,6 +70,9 @@ public Path getPath(URI uri) {
*/
private SFTPHost getSFTPHost(URI uri, boolean create) throws URISyntaxException {
String host = uri.getHost();
if (host == null) {
throw new IllegalArgumentException(uri.toString());
}
int port = uri.getPort();
if (port == -1) {
port = DEFAULT_PORT;
Expand Down Expand Up @@ -121,8 +122,13 @@ public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOExcept

sftp.connect();

// Implementation might not support recursive creation of directories
for (String subPath : ((SFTPPath) dir).getParts()) {
List<String> parts = ((SFTPPath) dir).getParts();
// remove the first part if it is the root directory (empty string)
if (!parts.isEmpty() && parts.get(0).equals("")) {
parts = parts.subList(1, parts.size()-1);
}
// Implementation might not support recursive creation of directories
for (String subPath : parts) {
try {
sftp.mkdir(subPath);
} catch(SftpException e) {
Expand Down
5 changes: 3 additions & 2 deletions sftp/src/main/java/no/maddin/niofs/sftp/SFTPPath.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package no.maddin.niofs.sftp;

import sun.nio.fs.UnixFileSystemProvider;

import java.io.File;
import java.io.IOException;
import java.net.URI;
Expand All @@ -28,6 +26,9 @@ public class SFTPPath implements Path {
}

private List<String> splitParts(String path) {
if (path == null) {
return Collections.emptyList();
}
String[] parts = path.split(PATH_SEP, -1);
return Arrays.asList(parts);
}
Expand Down
12 changes: 5 additions & 7 deletions sftp/src/test/java/no/maddin/niofs/sftp/PartsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ public class PartsTest {
public static List<Object[]> data() {

return Arrays.asList(
new Object[] {".", Arrays.asList(".")},
new Object[] {"", Arrays.asList("")},
new Object[] {".", Collections.singletonList(".")},
new Object[] {"", Collections.singletonList("")},
new Object[] {null, Collections.emptyList()},
new Object[] {"/", Arrays.asList("", "")},
new Object[] {"./", Arrays.asList(".", "")},
new Object[] {"/~", Arrays.asList("", "~")},
new Object[] {"aa", Arrays.asList("aa")},
new Object[] {"aa", Collections.singletonList("aa")},
new Object[] {"/aa/bb", Arrays.asList("", "aa", "bb")},
new Object[] {"/aa/../bb", Arrays.asList("", "aa", "..", "bb")},
new Object[] {"/aa/../bb.txt", Arrays.asList("", "aa", "..", "bb.txt")},
new Object[] {"../", Arrays.asList("..", "")}
);
}
Expand All @@ -48,9 +50,5 @@ public void splitDot() {
List<String> parts = path.getParts();

assertThat(parts, is(equalTo(this.result)));
// assertThat(parts, hasSize(1));
// assertThat(parts, hasItem(equalTo(".")));
}


}

0 comments on commit 6d0752b

Please sign in to comment.