From 00cd335f474fad7d9788d9566fc9707bfb145dd9 Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Tue, 27 Nov 2018 11:27:45 +0100 Subject: [PATCH] Moved tests to spock --- .../schmizz/sshj/sftp/PathHelperSpec.groovy | 62 ++++++++++++ .../sshj/sftp/ResponseStatusCodeSpec.groovy | 64 ++++++++++++ .../net/schmizz/sshj/sftp/PathHelperTest.java | 98 ------------------- .../sshj/sftp/ResponseStatusCodeTest.java | 61 ------------ 4 files changed, 126 insertions(+), 159 deletions(-) create mode 100644 src/test/groovy/net/schmizz/sshj/sftp/PathHelperSpec.groovy create mode 100644 src/test/groovy/net/schmizz/sshj/sftp/ResponseStatusCodeSpec.groovy delete mode 100644 src/test/java/net/schmizz/sshj/sftp/PathHelperTest.java delete mode 100644 src/test/java/net/schmizz/sshj/sftp/ResponseStatusCodeTest.java diff --git a/src/test/groovy/net/schmizz/sshj/sftp/PathHelperSpec.groovy b/src/test/groovy/net/schmizz/sshj/sftp/PathHelperSpec.groovy new file mode 100644 index 00000000..af835ef9 --- /dev/null +++ b/src/test/groovy/net/schmizz/sshj/sftp/PathHelperSpec.groovy @@ -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" + + } +} diff --git a/src/test/groovy/net/schmizz/sshj/sftp/ResponseStatusCodeSpec.groovy b/src/test/groovy/net/schmizz/sshj/sftp/ResponseStatusCodeSpec.groovy new file mode 100644 index 00000000..01e8cc90 --- /dev/null +++ b/src/test/groovy/net/schmizz/sshj/sftp/ResponseStatusCodeSpec.groovy @@ -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 + } +} diff --git a/src/test/java/net/schmizz/sshj/sftp/PathHelperTest.java b/src/test/java/net/schmizz/sshj/sftp/PathHelperTest.java deleted file mode 100644 index 957ad099..00000000 --- a/src/test/java/net/schmizz/sshj/sftp/PathHelperTest.java +++ /dev/null @@ -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()); - } - -} diff --git a/src/test/java/net/schmizz/sshj/sftp/ResponseStatusCodeTest.java b/src/test/java/net/schmizz/sshj/sftp/ResponseStatusCodeTest.java deleted file mode 100644 index 564ee56c..00000000 --- a/src/test/java/net/schmizz/sshj/sftp/ResponseStatusCodeTest.java +++ /dev/null @@ -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()); - } - -}