Skip to content

Commit 0ba6ea2

Browse files
committed
test:improve unit test
1 parent c9c67ec commit 0ba6ea2

2 files changed

Lines changed: 148 additions & 216 deletions

File tree

Lines changed: 148 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,148 @@
1-
/*
2-
* Copyright (c) [2016] [ <ether.camp> ] This file is part of the ethereumJ library.
3-
*
4-
* The ethereumJ library is free software: you can redistribute it and/or modify it under the terms
5-
* of the GNU Lesser General Public License as published by the Free Software Foundation, either
6-
* version 3 of the License, or (at your option) any later version.
7-
*
8-
* The ethereumJ library is distributed in the hope that it will be useful, but WITHOUT ANY
9-
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10-
* PURPOSE. See the GNU Lesser General Public License for more details.
11-
*
12-
* You should have received a copy of the GNU Lesser General Public License along with the ethereumJ
13-
* library. If not, see <http://www.gnu.org/licenses/>.
14-
*/
15-
16-
package org.tron.common.storage.leveldb;
17-
18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertFalse;
20-
import static org.junit.Assert.assertNotNull;
21-
22-
import org.junit.Before;
23-
import org.junit.Test;
24-
import org.tron.common.utils.ByteArray;
25-
import org.tron.core.Constant;
26-
import org.tron.core.config.Configuration;
27-
import org.tron.core.config.args.Args;
28-
29-
public class LevelDbDataSourceImplTest {
30-
31-
private LevelDbDataSourceImpl dataSource;
32-
33-
@Before
34-
public void setup() {
35-
Args.setParam(new String[]{}, Configuration.getByPath(Constant.TEST_CONF));
36-
dataSource = new LevelDbDataSourceImpl(Args.getInstance().getOutputDirectory(), "test");
37-
}
38-
39-
@Test
40-
public void testGet() {
41-
dataSource.initDB();
42-
String key1 = "000134yyyhy";
43-
byte[] key = key1.getBytes();
44-
byte[] value = dataSource.getData(key);
45-
String s = ByteArray.toStr(value);
46-
dataSource.closeDB();
47-
System.out.println(s);
48-
}
49-
50-
@Test
51-
public void testPut() {
52-
dataSource.initDB();
53-
String key1 = "000134yyyhy";
54-
byte[] key = key1.getBytes();
55-
56-
String value1 = "50000";
57-
byte[] value = value1.getBytes();
58-
59-
dataSource.putData(key, value);
60-
61-
assertNotNull(dataSource.getData(key));
62-
assertEquals(1, dataSource.allKeys().size());
63-
64-
dataSource.closeDB();
65-
}
66-
67-
@Test
68-
public void testRest() {
69-
dataSource.resetDb();
70-
dataSource.closeDB();
71-
}
72-
73-
@Test(timeout = 100)
74-
public void testLockReleased() {
75-
dataSource.initDB();
76-
// normal close
77-
dataSource.closeDB();
78-
// closing already closed db.
79-
dataSource.closeDB();
80-
// closing again to make sure the lock is free. If not test will hang.
81-
dataSource.closeDB();
82-
83-
assertFalse("Database is still alive after closing.", dataSource.isAlive());
84-
}
85-
86-
}
1+
/*
2+
* Copyright (c) [2016] [ <ether.camp> ]
3+
* This file is part of the ethereumJ library.
4+
*
5+
* The ethereumJ library is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* The ethereumJ library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
package org.tron.common.storage.leveldb;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertFalse;
23+
import static org.junit.Assert.assertNotNull;
24+
import static org.junit.Assert.assertNull;
25+
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
import org.tron.common.utils.ByteArray;
33+
import org.tron.core.Constant;
34+
import org.tron.core.config.Configuration;
35+
import org.tron.core.config.args.Args;
36+
37+
public class LevelDbDataSourceImplTest {
38+
39+
private static final Logger logger = LoggerFactory.getLogger("Test");
40+
41+
LevelDbDataSourceImpl dataSourceTest;
42+
43+
@Before
44+
public void initDb() {
45+
Args.setParam(new String[]{}, Configuration.getByPath(Constant.TEST_CONF));
46+
dataSourceTest = new LevelDbDataSourceImpl(Args.getInstance().getOutputDirectory(), "test");
47+
}
48+
49+
@Test
50+
public void testPutGet() {
51+
dataSourceTest.resetDb();
52+
String key1 = "2c0937534dd1b3832d05d865e8e6f2bf23218300b33a992740d45ccab7d4f519";
53+
byte[] key = key1.getBytes();
54+
dataSourceTest.initDB();
55+
String value1 = "50000";
56+
byte[] value = value1.getBytes();
57+
58+
dataSourceTest.putData(key, value);
59+
60+
assertNotNull(dataSourceTest.getData(key));
61+
assertEquals(1, dataSourceTest.allKeys().size());
62+
assertEquals("50000", ByteArray.toStr(dataSourceTest.getData(key1.getBytes())));
63+
64+
}
65+
66+
@Test
67+
public void testReset() {
68+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
69+
Args.getInstance().getOutputDirectory(), "test_reset");
70+
dataSource.resetDb();
71+
assertEquals(0, dataSource.allKeys().size());
72+
dataSource.closeDB();
73+
}
74+
75+
@Test
76+
public void testupdateByBatchInner() {
77+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
78+
Args.getInstance().getOutputDirectory(), "test_updateByBatch");
79+
dataSource.initDB();
80+
dataSource.resetDb();
81+
String key1 = "431cd8c8d5abe5cb5944b0889b32482d85772fbb98987b10fbb7f17110757350";
82+
String value1 = "50000";
83+
String key2 = "431cd8c8d5abe5cb5944b0889b32482d85772fbb98987b10fbb7f17110757351";
84+
String value2 = "10000";
85+
86+
Map<byte[], byte[]> rows = new HashMap<>();
87+
rows.put(key1.getBytes(), value1.getBytes());
88+
rows.put(key2.getBytes(), value2.getBytes());
89+
90+
dataSource.updateByBatch(rows);
91+
92+
assertEquals("50000", ByteArray.toStr(dataSource.getData(key1.getBytes())));
93+
assertEquals("10000", ByteArray.toStr(dataSource.getData(key2.getBytes())));
94+
assertEquals(2, dataSource.allKeys().size());
95+
}
96+
97+
@Test
98+
public void testdeleteData() {
99+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
100+
Args.getInstance().getOutputDirectory(), "test_delete");
101+
dataSource.initDB();
102+
String key1 = "431cd8c8d5abe5cb5944b0889b32482d85772fbb98987b10fbb7f17110757350";
103+
byte[] key = key1.getBytes();
104+
dataSource.deleteData(key);
105+
byte[] value = dataSource.getData(key);
106+
String s = ByteArray.toStr(value);
107+
assertNull(s);
108+
109+
}
110+
111+
@Test
112+
public void testallKeys() {
113+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
114+
Args.getInstance().getOutputDirectory(), "test_find_key");
115+
dataSource.initDB();
116+
dataSource.resetDb();
117+
118+
String key1 = "431cd8c8d5abe5cb5944b0889b32482d85772fbb98987b10fbb7f17110757321";
119+
byte[] key = key1.getBytes();
120+
121+
String value1 = "50000";
122+
byte[] value = value1.getBytes();
123+
124+
dataSource.putData(key, value);
125+
String key3 = "431cd8c8d5abe5cb5944b0889b32482d85772fbb98987b10fbb7f17110757091";
126+
byte[] key2 = key3.getBytes();
127+
128+
String value3 = "30000";
129+
byte[] value2 = value3.getBytes();
130+
131+
dataSource.putData(key2, value2);
132+
assertEquals(2, dataSource.allKeys().size());
133+
dataSource.resetDb();
134+
}
135+
136+
@Test(timeout = 100)
137+
public void testLockReleased() {
138+
dataSourceTest.initDB();
139+
// normal close
140+
dataSourceTest.closeDB();
141+
// closing already closed db.
142+
dataSourceTest.closeDB();
143+
// closing again to make sure the lock is free. If not test will hang.
144+
dataSourceTest.closeDB();
145+
146+
assertFalse("Database is still alive after closing.", dataSourceTest.isAlive());
147+
}
148+
}

‎src/test/java/org/tron/storage/leveldb/LevelDbDataSourceImplTest.java‎

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)