11package org .tron .plugins ;
22
3- import java .io .File ;
43import java .io .IOException ;
5- import java .nio .charset .StandardCharsets ;
64import java .util .UUID ;
7- import org .iq80 .leveldb .DB ;
8- import org .junit .AfterClass ;
95import org .junit .Assert ;
10- import org .junit .BeforeClass ;
116import 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 ;
167import 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
0 commit comments