Skip to content

Commit

Permalink
Updated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
morazow committed Oct 13, 2021
1 parent 795d7d2 commit 85afa74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/main/scala/com/exasol/common/file/FileChecker.scala
@@ -1,5 +1,6 @@
package com.exasol.common.file

import java.io.File
import java.io.IOException
import java.io.UncheckedIOException
import java.nio.file.Files
Expand Down Expand Up @@ -28,12 +29,11 @@ abstract class FileChecker {
*/
final def isRegularFile(filePath: String): Boolean = {
val path = Paths.get(filePath)
checkStartsWithPath(path)
checkStartsWithPath(path.toFile())
Files.isRegularFile(path)
}

private[this] def checkStartsWithPath(path: Path): Unit = {
val file = path.toFile()
protected[file] final def checkStartsWithPath(file: File): Unit =
try {
val absolutePath = file.getCanonicalPath()
if (!absolutePath.startsWith(getLocationPrefix())) {
Expand All @@ -50,12 +50,11 @@ abstract class FileChecker {
throw new UncheckedIOException(
ExaError
.messageBuilder("E-IEUCS-13")
.message("Failed to open path {{PATH}}.", path)
.message("Failed to open path {{PATH}}.", file.getAbsolutePath())
.mitigation("Please make sure that file exists and starts with location {{PREFIX}}", getLocationPrefix())
.toString(),
exception
)
}
}

}
16 changes: 15 additions & 1 deletion src/test/scala/com/exasol/common/file/FileCheckerTest.scala
@@ -1,12 +1,16 @@
package com.exasol.common.file

import java.io.File
import java.io.IOException
import java.io.UncheckedIOException
import java.nio.file.Files

import org.mockito.Mockito.when
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.mockito.MockitoSugar

class FileCheckerTest extends AnyFunSuite with Matchers {
class FileCheckerTest extends AnyFunSuite with Matchers with MockitoSugar {

test("temporary file checker checks correct path") {
val fileParent = "/tmp/bfsdefault/bucket1"
Expand All @@ -31,6 +35,16 @@ class FileCheckerTest extends AnyFunSuite with Matchers {
assert(message.contains("Please make sure that file path start with '/buckets'."))
}

test("file checker throw ioexception") {
val file = mock[File]
when(file.getAbsolutePath()).thenReturn("test/path")
when(file.getCanonicalPath()).thenThrow(new IOException())
val thrown = intercept[UncheckedIOException] {
new BucketFSFileChecker().checkStartsWithPath(file)
}
assert(thrown.getMessage().startsWith("E-IEUCS-13: Failed to open path 'test/path'."))
}

class TemporaryFileChecker extends FileChecker {
override final def getLocationPrefix(): String = "/tmp"
}
Expand Down

0 comments on commit 85afa74

Please sign in to comment.