Skip to content

Commit b54912a

Browse files
authored
test(plugins): add test for plugins (#5482)
1 parent 9f8cebd commit b54912a

8 files changed

Lines changed: 372 additions & 78 deletions

File tree

plugins/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ dependencies {
2929
testCompile group: 'junit', name: 'junit', version: '4.13.2'
3030
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.13.0'
3131
testCompile group: 'org.hamcrest', name: 'hamcrest-junit', version: '1.0.0.1'
32+
testCompile project(":framework")
33+
testCompile project(":framework").sourceSets.test.output
3234
compile group: 'info.picocli', name: 'picocli', version: '4.6.3'
3335
compile group: 'com.typesafe', name: 'config', version: '1.3.2'
3436
compile group: 'me.tongfei', name: 'progressbar', version: '0.9.3'
@@ -75,6 +77,7 @@ test {
7577
testLogging {
7678
exceptionFormat = 'full'
7779
}
80+
maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
7881
jacoco {
7982
destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
8083
classDumpDir = file("$buildDir/jacoco/classpathdumps")

plugins/src/test/java/org/tron/plugins/DbConvertTest.java

Lines changed: 14 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,24 @@
11
package org.tron.plugins;
22

3-
import java.io.File;
43
import java.io.IOException;
5-
import java.nio.charset.StandardCharsets;
64
import java.util.UUID;
7-
import org.iq80.leveldb.DB;
8-
import org.junit.AfterClass;
95
import org.junit.Assert;
10-
import org.junit.BeforeClass;
116
import org.junit.Test;
12-
import org.tron.plugins.utils.ByteArray;
13-
import org.tron.plugins.utils.DBUtils;
14-
import org.tron.plugins.utils.FileUtils;
15-
import org.tron.plugins.utils.MarketUtils;
167
import picocli.CommandLine;
178

18-
public class DbConvertTest {
9+
public class DbConvertTest extends DbTest {
1910

20-
21-
private static final String INPUT_DIRECTORY = "output-directory/convert-database/";
22-
private static final String OUTPUT_DIRECTORY = "output-directory/convert-database-dest/";
23-
private static final String ACCOUNT = "account";
24-
private static final String MARKET = DBUtils.MARKET_PAIR_PRICE_TO_ORDER;
25-
CommandLine cli = new CommandLine(new Toolkit());
26-
27-
28-
@BeforeClass
29-
public static void init() throws IOException {
30-
if (new File(INPUT_DIRECTORY).mkdirs()) {
31-
initDB(new File(INPUT_DIRECTORY,ACCOUNT));
32-
initDB(new File(INPUT_DIRECTORY,MARKET));
33-
}
34-
}
35-
36-
private static void initDB(File file) throws IOException {
37-
try (DB db = DBUtils.newLevelDb(file.toPath())) {
38-
if (MARKET.equalsIgnoreCase(file.getName())) {
39-
byte[] sellTokenID1 = ByteArray.fromString("100");
40-
byte[] buyTokenID1 = ByteArray.fromString("200");
41-
byte[] pairPriceKey1 = MarketUtils.createPairPriceKey(
42-
sellTokenID1,
43-
buyTokenID1,
44-
1000L,
45-
2001L
46-
);
47-
byte[] pairPriceKey2 = MarketUtils.createPairPriceKey(
48-
sellTokenID1,
49-
buyTokenID1,
50-
1000L,
51-
2002L
52-
);
53-
byte[] pairPriceKey3 = MarketUtils.createPairPriceKey(
54-
sellTokenID1,
55-
buyTokenID1,
56-
1000L,
57-
2003L
58-
);
59-
60-
61-
//Use out-of-order insertion,key in store should be 1,2,3
62-
db.put(pairPriceKey1, "1".getBytes(StandardCharsets.UTF_8));
63-
db.put(pairPriceKey2, "2".getBytes(StandardCharsets.UTF_8));
64-
db.put(pairPriceKey3, "3".getBytes(StandardCharsets.UTF_8));
65-
} else {
66-
for (int i = 0; i < 100; i++) {
67-
byte[] bytes = UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8);
68-
db.put(bytes, bytes);
69-
}
70-
}
71-
}
72-
}
73-
74-
@AfterClass
75-
public static void destroy() {
76-
FileUtils.deleteDir(new File(INPUT_DIRECTORY));
77-
FileUtils.deleteDir(new File(OUTPUT_DIRECTORY));
11+
@Test
12+
public void testRun() throws IOException {
13+
String[] args = new String[] { "db", "convert", INPUT_DIRECTORY,
14+
temporaryFolder.newFolder().toString() };
15+
Assert.assertEquals(0, cli.execute(args));
7816
}
7917

8018
@Test
81-
public void testRun() {
82-
String[] args = new String[] { "db", "convert", INPUT_DIRECTORY, OUTPUT_DIRECTORY };
19+
public void testRunWithSafe() throws IOException {
20+
String[] args = new String[] { "db", "convert", INPUT_DIRECTORY,
21+
temporaryFolder.newFolder().toString(),"--safe" };
8322
Assert.assertEquals(0, cli.execute(args));
8423
}
8524

@@ -92,18 +31,15 @@ public void testHelp() {
9231

9332
@Test
9433
public void testNotExist() {
95-
String[] args = new String[] {"db", "convert",
96-
OUTPUT_DIRECTORY + File.separator + UUID.randomUUID(),
97-
OUTPUT_DIRECTORY};
34+
String[] args = new String[] {"db", "convert", UUID.randomUUID().toString(),
35+
UUID.randomUUID().toString()};
9836
Assert.assertEquals(404, cli.execute(args));
9937
}
10038

10139
@Test
102-
public void testEmpty() {
103-
File file = new File(OUTPUT_DIRECTORY + File.separator + UUID.randomUUID());
104-
file.mkdirs();
105-
file.deleteOnExit();
106-
String[] args = new String[] {"db", "convert", file.toString(), OUTPUT_DIRECTORY};
40+
public void testEmpty() throws IOException {
41+
String[] args = new String[] {"db", "convert", temporaryFolder.newFolder().toString(),
42+
temporaryFolder.newFolder().toString()};
10743
Assert.assertEquals(0, cli.execute(args));
10844
}
10945

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.tron.plugins;
2+
3+
import java.io.IOException;
4+
import java.util.UUID;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import picocli.CommandLine;
8+
9+
public class DbCopyTest extends DbTest {
10+
11+
@Test
12+
public void testRun() {
13+
String[] args = new String[] { "db", "cp", INPUT_DIRECTORY,
14+
tmpDir + UUID.randomUUID()};
15+
Assert.assertEquals(0, cli.execute(args));
16+
}
17+
18+
@Test
19+
public void testHelp() {
20+
String[] args = new String[] {"db", "cp", "-h"};
21+
CommandLine cli = new CommandLine(new Toolkit());
22+
Assert.assertEquals(0, cli.execute(args));
23+
}
24+
25+
@Test
26+
public void testNotExist() {
27+
String[] args = new String[] {"db", "cp", UUID.randomUUID().toString(),
28+
UUID.randomUUID().toString()};
29+
Assert.assertEquals(404, cli.execute(args));
30+
}
31+
32+
@Test
33+
public void testEmpty() throws IOException {
34+
String[] args = new String[] {"db", "cp", temporaryFolder.newFolder().toString(),
35+
tmpDir + UUID.randomUUID()};
36+
Assert.assertEquals(0, cli.execute(args));
37+
}
38+
39+
@Test
40+
public void testDestIsExist() throws IOException {
41+
String[] args = new String[] {"db", "cp", temporaryFolder.newFile().toString(),
42+
temporaryFolder.newFolder().toString()};
43+
Assert.assertEquals(402, cli.execute(args));
44+
}
45+
46+
@Test
47+
public void testSrcIsFile() throws IOException {
48+
String[] args = new String[] {"db", "cp", temporaryFolder.newFile().toString(),
49+
tmpDir + UUID.randomUUID()};
50+
Assert.assertEquals(403, cli.execute(args));
51+
}
52+
53+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.tron.plugins;
2+
3+
import java.io.IOException;
4+
import org.junit.Test;
5+
6+
7+
public class DbLiteLevelDbTest extends DbLiteTest {
8+
9+
@Test
10+
public void testToolsWithLevelDB() throws InterruptedException, IOException {
11+
testTools("LEVELDB", 1);
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.tron.plugins;
2+
3+
import java.io.IOException;
4+
import org.junit.Test;
5+
6+
public class DbLiteLevelDbV2Test extends DbLiteTest {
7+
8+
@Test
9+
public void testToolsWithLevelDBV2() throws InterruptedException, IOException {
10+
testTools("LEVELDB", 2);
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.tron.plugins;
2+
3+
import java.io.IOException;
4+
import org.junit.Test;
5+
6+
public class DbLiteRocksDbTest extends DbLiteTest {
7+
8+
@Test
9+
public void testToolsWithRocksDB() throws InterruptedException, IOException {
10+
testTools("ROCKSDB", 1);
11+
}
12+
}

0 commit comments

Comments
 (0)