From 8c91dd4d8b4cc6300c7fe68db415444580426532 Mon Sep 17 00:00:00 2001 From: halibobo1205 Date: Mon, 8 Aug 2022 18:43:36 +0800 Subject: [PATCH] test(db-mv):add test for db mv func. --- .../java/org/tron/plugins/DbMoveTest.java | 157 ++++++++++++++++++ .../src/test/resources/config-duplicate.conf | 22 +++ plugins/src/test/resources/config.conf | 22 +++ 3 files changed, 201 insertions(+) create mode 100644 plugins/src/test/java/org/tron/plugins/DbMoveTest.java create mode 100644 plugins/src/test/resources/config-duplicate.conf create mode 100644 plugins/src/test/resources/config.conf diff --git a/plugins/src/test/java/org/tron/plugins/DbMoveTest.java b/plugins/src/test/java/org/tron/plugins/DbMoveTest.java new file mode 100644 index 00000000000..6bbac663c2c --- /dev/null +++ b/plugins/src/test/java/org/tron/plugins/DbMoveTest.java @@ -0,0 +1,157 @@ +package org.tron.plugins; + +import static org.iq80.leveldb.impl.Iq80DBFactory.factory; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Paths; +import java.util.Properties; +import java.util.UUID; +import lombok.extern.slf4j.Slf4j; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import picocli.CommandLine; + +@Slf4j +public class DbMoveTest { + + private static final String OUTPUT_DIRECTORY = "output-directory-toolkit"; + private static final String OUTPUT_DIRECTORY_DATABASE = + Paths.get(OUTPUT_DIRECTORY,"ori","database").toString(); + private static final String ENGINE = "ENGINE"; + private static final String LEVELDB = "LEVELDB"; + private static final String ACCOUNT = "account"; + private static final String TRANS = "trans"; + private static final String MARKET = "market_pair_price_to_order"; + private static final String ENGINE_FILE = "engine.properties"; + + + @BeforeClass + public static void init() throws IOException { + File file = new File(OUTPUT_DIRECTORY_DATABASE, ACCOUNT); + factory.open(file, ArchiveManifest.newDefaultLevelDbOptions()).close(); + writeProperty(file + File.separator + ENGINE_FILE, ENGINE, LEVELDB); + + file = new File(OUTPUT_DIRECTORY_DATABASE, MARKET); + factory.open(file, ArchiveManifest.newDefaultLevelDbOptions()).close(); + writeProperty(file + File.separator + ENGINE_FILE, ENGINE, LEVELDB); + + file = new File(OUTPUT_DIRECTORY_DATABASE, TRANS); + factory.open(file, ArchiveManifest.newDefaultLevelDbOptions()).close(); + writeProperty(file + File.separator + ENGINE_FILE, ENGINE, LEVELDB); + + } + + @AfterClass + public static void destroy() { + deleteDir(new File(OUTPUT_DIRECTORY)); + } + + private static void writeProperty(String filename, String key, String value) throws IOException { + File file = new File(filename); + if (!file.exists()) { + file.createNewFile(); + } + + try (FileInputStream fis = new FileInputStream(file); + OutputStream out = new FileOutputStream(file); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, + StandardCharsets.UTF_8))) { + BufferedReader bf = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8)); + Properties properties = new Properties(); + properties.load(bf); + properties.setProperty(key, value); + properties.store(bw, "Generated by the application. PLEASE DO NOT EDIT! "); + } catch (Exception e) { + logger.warn("{}", e); + } + } + + /** + * delete directory. + */ + private static boolean deleteDir(File dir) { + if (dir.isDirectory()) { + String[] children = dir.list(); + assert children != null; + for (String child : children) { + boolean success = deleteDir(new File(dir, child)); + if (!success) { + logger.warn("can't delete dir:" + dir); + return false; + } + } + } + return dir.delete(); + } + + private static String getConfig(String config) { + URL path = DbMoveTest.class.getClassLoader().getResource(config); + return path == null ? null : path.getPath(); + } + + @Test + public void testMv() { + String[] args = new String[] {"db", "mv", "-d", + Paths.get(OUTPUT_DIRECTORY,"ori").toString(), "-c", + getConfig("config.conf")}; + CommandLine cli = new CommandLine(new Toolkit()); + Assert.assertEquals(0, cli.execute(args)); + Assert.assertEquals(2, cli.execute(args)); + } + + @Test + public void testDuplicate() { + String[] args = new String[] {"db", "mv", "-d", + Paths.get(OUTPUT_DIRECTORY,"ori").toString(), "-c", + getConfig("config-duplicate.conf")}; + CommandLine cli = new CommandLine(new Toolkit()); + Assert.assertEquals(2, cli.execute(args)); + } + + @Test + public void testHelp() { + String[] args = new String[] {}; + CommandLine cli = new CommandLine(new Toolkit()); + Assert.assertEquals(0, cli.execute(args)); + } + + @Test + public void testDicNotExist() { + String[] args = new String[] {"db", "mv", "-d", "dicNotExist"}; + CommandLine cli = new CommandLine(new Toolkit()); + Assert.assertEquals(2, cli.execute(args)); + } + + @Test + public void testConfNotExist() { + String[] args = new String[] {"db", "mv", "-d", + Paths.get(OUTPUT_DIRECTORY,"ori").toString(), "-c", + "config.conf"}; + CommandLine cli = new CommandLine(new Toolkit()); + Assert.assertEquals(2, cli.execute(args)); + } + + @Test + public void testEmpty() { + File file = new File(OUTPUT_DIRECTORY_DATABASE + File.separator + UUID.randomUUID()); + file.mkdirs(); + file.deleteOnExit(); + String[] args = new String[] {"db", "mv", "-d", file.toString(), "-c", + getConfig("config.conf")}; + CommandLine cli = new CommandLine(new Toolkit()); + + Assert.assertEquals(2, cli.execute(args)); + } +} diff --git a/plugins/src/test/resources/config-duplicate.conf b/plugins/src/test/resources/config-duplicate.conf new file mode 100644 index 00000000000..758312ad05e --- /dev/null +++ b/plugins/src/test/resources/config-duplicate.conf @@ -0,0 +1,22 @@ + +storage { + # Directory for storing persistent data + db.version = 2, + db.engine = "LEVELDB", + db.sync = false, + db.directory = "database", + index.directory = "index", + transHistory.switch = "on", + properties = [ + { + name = "trans", + path = "output-directory-toolkit/dest", + }, + { + name = "trans", + path = "output-directory-toolkit/dest2", + }, + ] +} + + diff --git a/plugins/src/test/resources/config.conf b/plugins/src/test/resources/config.conf new file mode 100644 index 00000000000..be16d44c420 --- /dev/null +++ b/plugins/src/test/resources/config.conf @@ -0,0 +1,22 @@ + +storage { + # Directory for storing persistent data + db.version = 2, + db.engine = "LEVELDB", + db.sync = false, + db.directory = "database", + index.directory = "index", + transHistory.switch = "on", + properties = [ + { + name = "account", + path = "output-directory-toolkit/dest", + }, + { + name = "market_pair_price_to_order", + path = "output-directory-toolkit/dest", + }, + ] +} + +