Moved tests to spock

This commit is contained in:
Jeroen van Erp
2018-11-27 11:27:45 +01:00
parent e14fb2f695
commit 00cd335f47
4 changed files with 126 additions and 159 deletions

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.sftp
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
class PathHelperSpec extends Specification {
@Shared
def pathHelper = new PathHelper(new PathHelper.Canonicalizer() {
/**
* Very basic, it does not try to canonicalize relative bits in the middle of a path.
*/
@Override
String canonicalize(String path)
throws IOException {
if ("" == path || "." == path || "./" == path)
return "/home/me"
if (".." == path || "../" == path)
return "/home"
return path
}
}, "/")
@Unroll
def "should correctly componentize path \"#input\""() {
given:
def components = pathHelper.getComponents(input)
expect:
components.getName() == name
components.getParent() == parent
components.getPath() == path
where:
input || name | path | parent
"" || "me" | "/home/me" | "/home"
"/" || "/" | "/" | ""
"." || "me" | "/home/me" | "/home"
".." || "home" | "/home" | "/"
"somefile" || "somefile" | "somefile" | ""
"dir1/dir2" || "dir2" | "dir1/dir2" | "dir1"
"/home/me/../somedir/somefile" || "somefile" | "/home/me/../somedir/somefile" | "/home/me/../somedir"
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.sftp
import spock.lang.Specification
import spock.lang.Unroll
class ResponseStatusCodeSpec extends Specification {
@Unroll
def "status #status should have status code #code"() {
expect:
code == status.getCode()
where:
status || code
Response.StatusCode.UNKNOWN || -1
Response.StatusCode.OK || 0
Response.StatusCode.EOF || 1
Response.StatusCode.NO_SUCH_FILE || 2
Response.StatusCode.PERMISSION_DENIED || 3
Response.StatusCode.FAILURE || 4
Response.StatusCode.BAD_MESSAGE || 5
Response.StatusCode.NO_CONNECTION || 6
Response.StatusCode.CONNECITON_LOST || 7
Response.StatusCode.OP_UNSUPPORTED || 8
Response.StatusCode.INVALID_HANDLE || 9
Response.StatusCode.NO_SUCH_PATH || 10
Response.StatusCode.FILE_ALREADY_EXISTS || 11
Response.StatusCode.WRITE_PROTECT || 12
Response.StatusCode.NO_MEDIA || 13
Response.StatusCode.NO_SPACE_ON_FILESYSTEM || 14
Response.StatusCode.QUOTA_EXCEEDED || 15
Response.StatusCode.UNKNOWN_PRINCIPAL || 16
Response.StatusCode.LOCK_CONFLICT || 17
Response.StatusCode.DIR_NOT_EMPTY || 18
Response.StatusCode.NOT_A_DIRECTORY || 19
Response.StatusCode.INVALID_FILENAME || 20
Response.StatusCode.LINK_LOOP || 21
Response.StatusCode.CANNOT_DELETE || 22
Response.StatusCode.INVALID_PARAMETER || 23
Response.StatusCode.FILE_IS_A_DIRECTORY || 24
Response.StatusCode.BYTE_RANGE_LOCK_CONFLICT || 25
Response.StatusCode.BYTE_RANGE_LOCK_REFUSED || 26
Response.StatusCode.DELETE_PENDING || 27
Response.StatusCode.FILE_CORRUPT || 28
Response.StatusCode.OWNER_INVALID || 29
Response.StatusCode.GROUP_INVALID || 30
Response.StatusCode.NO_MATCHING_BYTE_RANGE_LOCK || 31
}
}

View File

@@ -1,98 +0,0 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.sftp;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.Assert.assertEquals;
public class PathHelperTest {
private final PathHelper helper = new PathHelper(new PathHelper.Canonicalizer() {
/**
* Very basic, it does not try to canonicalize relative bits in the middle of a path.
*/
@Override
public String canonicalize(String path)
throws IOException {
if ("".equals(path) || ".".equals(path) || "./".equals(path))
return "/home/me";
if ("..".equals(path) || "../".equals(path))
return "/home";
return path;
}
}, "/");
@Test
public void empty()
throws IOException {
final PathComponents comp = helper.getComponents("");
assertEquals("me", comp.getName());
assertEquals("/home", comp.getParent());
}
@Test
public void root()
throws IOException {
final PathComponents comp = helper.getComponents("/");
assertEquals("/", comp.getName());
assertEquals("", comp.getParent());
}
@Test
public void dot()
throws IOException {
final PathComponents comp = helper.getComponents(".");
assertEquals("me", comp.getName());
assertEquals("/home", comp.getParent());
}
@Test
public void dotDot()
throws IOException {
final PathComponents comp = helper.getComponents("..");
assertEquals("home", comp.getName());
assertEquals("/", comp.getParent());
}
@Test
public void fileInHomeDir()
throws IOException {
final PathComponents comp = helper.getComponents("somefile");
assertEquals("somefile", comp.getName());
assertEquals("somefile", comp.getPath());
assertEquals("", comp.getParent());
}
@Test
public void pathInHomeDir() throws IOException {
final PathComponents comp = helper.getComponents("dir1/dir2");
assertEquals("dir2", comp.getName());
assertEquals("dir1/dir2", comp.getPath());
assertEquals("dir1", comp.getParent());
}
@Test
public void fileSomeLevelsDeep()
throws IOException {
final PathComponents comp = helper.getComponents("/home/me/../somedir/somefile");
assertEquals("somefile", comp.getName());
assertEquals("/home/me/../somedir", comp.getParent());
}
}

View File

@@ -1,61 +0,0 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.sftp;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ResponseStatusCodeTest {
@Test
public void shouldReturnProperNumericCodesForStatusCode() {
assertEquals(-1, Response.StatusCode.UNKNOWN.getCode());
assertEquals(0, Response.StatusCode.OK.getCode());
assertEquals(1, Response.StatusCode.EOF.getCode());
assertEquals(2, Response.StatusCode.NO_SUCH_FILE.getCode());
assertEquals(3, Response.StatusCode.PERMISSION_DENIED.getCode());
assertEquals(4, Response.StatusCode.FAILURE.getCode());
assertEquals(5, Response.StatusCode.BAD_MESSAGE.getCode());
assertEquals(6, Response.StatusCode.NO_CONNECTION.getCode());
assertEquals(7, Response.StatusCode.CONNECITON_LOST.getCode());
assertEquals(8, Response.StatusCode.OP_UNSUPPORTED.getCode());
assertEquals(9, Response.StatusCode.INVALID_HANDLE.getCode());
assertEquals(10, Response.StatusCode.NO_SUCH_PATH.getCode());
assertEquals(11, Response.StatusCode.FILE_ALREADY_EXISTS.getCode());
assertEquals(12, Response.StatusCode.WRITE_PROTECT.getCode());
assertEquals(13, Response.StatusCode.NO_MEDIA.getCode());
assertEquals(14, Response.StatusCode.NO_SPACE_ON_FILESYSTEM.getCode());
assertEquals(15, Response.StatusCode.QUOTA_EXCEEDED.getCode());
assertEquals(16, Response.StatusCode.UNKNOWN_PRINCIPAL.getCode());
assertEquals(17, Response.StatusCode.LOCK_CONFLICT.getCode());
assertEquals(18, Response.StatusCode.DIR_NOT_EMPTY.getCode());
assertEquals(19, Response.StatusCode.NOT_A_DIRECTORY.getCode());
assertEquals(20, Response.StatusCode.INVALID_FILENAME.getCode());
assertEquals(21, Response.StatusCode.LINK_LOOP.getCode());
assertEquals(22, Response.StatusCode.CANNOT_DELETE.getCode());
assertEquals(23, Response.StatusCode.INVALID_PARAMETER.getCode());
assertEquals(24, Response.StatusCode.FILE_IS_A_DIRECTORY.getCode());
assertEquals(25, Response.StatusCode.BYTE_RANGE_LOCK_CONFLICT.getCode());
assertEquals(26, Response.StatusCode.BYTE_RANGE_LOCK_REFUSED.getCode());
assertEquals(27, Response.StatusCode.DELETE_PENDING.getCode());
assertEquals(28, Response.StatusCode.FILE_CORRUPT.getCode());
assertEquals(29, Response.StatusCode.OWNER_INVALID.getCode());
assertEquals(30, Response.StatusCode.GROUP_INVALID.getCode());
assertEquals(31, Response.StatusCode.NO_MATCHING_BYTE_RANGE_LOCK.getCode());
}
}