Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

[WIP] Unit tests #31

Closed
wants to merge 5 commits into from
Closed
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 @@ -32,7 +32,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock

import org.fusesource.leveldbjni.JniDBFactory.factory
import org.iq80.leveldb._
import org.tron.utils.FileUtil
import org.tron.utils.FileUtils

import scala.collection.mutable
import scala.concurrent.Future
Expand Down Expand Up @@ -92,7 +92,7 @@ class LevelDbDataSourceImpl(dbFolder: File, name: String = "default") extends Da

def resetDB() = Future.successful {
close()
FileUtil.recursiveDelete(new File(dbFolder.getAbsolutePath, name).getAbsolutePath)
FileUtils.recursiveDelete(new File(dbFolder.getAbsolutePath, name).getAbsolutePath)
buildDb() match {
case Right(newDb) =>
db = Some(newDb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ import java.nio.file.attribute.BasicFileAttributes
import java.util


object FileUtil {
object FileUtils {

def recursiveList(path: String): util.List[String] = {
val paths = Paths.get(path)
def getRelativeDirectory(): File = {
new File(".").getCanonicalFile
}

require(paths.toFile.exists(), "path must exist")
def recursiveList(file: File): util.List[String] = {
recursiveList(file.getAbsolutePath)
}

def recursiveList(path: String): util.List[String] = {
val file = new File(path)
require(file.exists(), "path must exist")

val files = new util.ArrayList[String]

Files.walkFileTree(paths, new FileVisitor[Path]() {
Files.walkFileTree(file.toPath, new FileVisitor[Path]() {

def preVisitDirectory(dir: Path, attrs: BasicFileAttributes) = FileVisitResult.CONTINUE

Expand All @@ -45,12 +52,16 @@ object FileUtil {
files
}

def recursiveDelete(fileName: String): Boolean = {
val file = new File(fileName)
def recursiveDelete(file: File): Boolean = {
recursiveDelete(file.getAbsolutePath)
}

def recursiveDelete(path: String): Boolean = {
val file = new File(path)
if (file.exists) { // check if the file is a directory
if (file.isDirectory) if (file.list.length > 0) {
for (s <- file.list) { // call deletion of file individually
recursiveDelete(fileName + System.getProperty("file.separator") + s)
recursiveDelete(s"$path${System.getProperty("file.separator")}$s")
}
}
file.setWritable(true)
Expand Down
19 changes: 19 additions & 0 deletions tron-protocol/src/test/scala/org/tron/utils/FileUtilsSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.tron.utils

import org.specs2.mutable._

class FileUtilsSpec extends Specification {
"FileUtils Spec" should {
"getRelativePath" in {
val directory = FileUtils.getRelativeDirectory()
directory.exists() must beTrue
directory.isDirectory must beTrue
}
"recursiveList" in {
FileUtils.recursiveList("") must throwAn[IllegalArgumentException]
}
"recursiveDelete" in {
1 mustEqual 1
}
}
}
2 changes: 0 additions & 2 deletions tron-protocol/src/test/scala/org/tron/wallet/WalletSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ class WalletSpec extends Specification {
"Wallet Spec" should {

"Generate new address" in {

val wallet = Wallet()

wallet.address.hex must not beEmpty
}
}
Expand Down