Skip to content
Merged
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
157 changes: 157 additions & 0 deletions plugins/src/test/java/org/tron/plugins/DbMoveTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
22 changes: 22 additions & 0 deletions plugins/src/test/resources/config-duplicate.conf
Original file line number Diff line number Diff line change
@@ -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",
},
]
}


22 changes: 22 additions & 0 deletions plugins/src/test/resources/config.conf
Original file line number Diff line number Diff line change
@@ -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",
},
]
}