Create tests
This commit is contained in:
parent
b791bef037
commit
098e21c3ec
10
DzieCoin.iml
10
DzieCoin.iml
|
@ -14,5 +14,15 @@
|
|||
<orderEntry type="library" name="mysql.connector.java" level="project" />
|
||||
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.30" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.github.davidmoten:commons-csv:1.6.002" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.9.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.9.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.9.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.9.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.9.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.9.2" level="project" />
|
||||
</component>
|
||||
</module>
|
12
pom.xml
12
pom.xml
|
@ -25,5 +25,17 @@
|
|||
<artifactId>commons-csv</artifactId>
|
||||
<version>1.6.002</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -21,7 +21,7 @@ public class NFT extends BlockData {
|
|||
public String getContract() {
|
||||
return contract;
|
||||
}
|
||||
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package pl.mikorosa.dziecoin;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class BlockTest {
|
||||
@Test
|
||||
void mineBlock() {
|
||||
Main.difficulty = 4;
|
||||
|
||||
List<Transaction> transactions = new ArrayList<>();
|
||||
transactions.add(new Transaction("1", "2", 1));
|
||||
|
||||
Block block = new Block("0", 1, transactions);
|
||||
block.mineBlock();
|
||||
|
||||
assertEquals(168149, block.getNonce());
|
||||
assertEquals("0000e4942b486fa4779fadc32a1d0dd483e117aa57bc84f6ff5912f9abefef0e", block.getHash());
|
||||
}
|
||||
|
||||
@Test
|
||||
void alterBlock() {
|
||||
Main.difficulty = 2;
|
||||
|
||||
List<Transaction> transactions = new ArrayList<>();
|
||||
transactions.add(new Transaction("1", "2", 1));
|
||||
|
||||
Block block = new Block("0", 1, transactions);
|
||||
block.mineBlock();
|
||||
|
||||
block.getTransactions().add(new Transaction("2", "1", 1));
|
||||
|
||||
Block.VerifyCodes status = block.verifyBlock();
|
||||
|
||||
assertEquals(Block.VerifyCodes.INVALID_BLOCK_HASH, status);
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidProofOfWork() {
|
||||
Main.difficulty = 2;
|
||||
|
||||
List<Transaction> transactions = new ArrayList<>();
|
||||
transactions.add(new Transaction("1", "2", 1));
|
||||
|
||||
Block block = new Block("0", 1, transactions);
|
||||
block.mineBlock();
|
||||
|
||||
Main.difficulty = 4;
|
||||
|
||||
Block.VerifyCodes status = block.verifyBlock();
|
||||
|
||||
assertEquals(Block.VerifyCodes.INVALID_POW_SIGNATURE, status);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
package pl.mikorosa.dziecoin;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class BlockchainTest {
|
||||
@Test
|
||||
void blockchainIntegrity() {
|
||||
Main.difficulty = 2;
|
||||
Blockchain b = new Blockchain("1", false);
|
||||
|
||||
List<Transaction> transactions1 = new ArrayList<>();
|
||||
transactions1.add(new Transaction("1", "2", 1));
|
||||
|
||||
Block block1 = new Block(b.getLatestBlock().getHash(), 2, transactions1);
|
||||
block1.mineBlock();
|
||||
|
||||
try {
|
||||
b.addBlock(block1);
|
||||
} catch(BlockchainIntegrityException e) {
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
|
||||
List<Transaction> transactions2 = new ArrayList<>();
|
||||
transactions2.add(new Transaction("2", "3", 1));
|
||||
|
||||
Block block2 = new Block(block1.getHash(), 3, transactions2);
|
||||
block2.mineBlock();
|
||||
|
||||
try {
|
||||
b.addBlock(block2);
|
||||
} catch (BlockchainIntegrityException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockchainVerify() {
|
||||
Main.difficulty = 2;
|
||||
Blockchain b = new Blockchain("1", false);
|
||||
|
||||
List<Transaction> transactions1 = new ArrayList<>();
|
||||
transactions1.add(new Transaction("1", "2", 1));
|
||||
|
||||
Block block1 = new Block(b.getLatestBlock().getHash(), 2, transactions1);
|
||||
block1.mineBlock();
|
||||
|
||||
try {
|
||||
b.addBlock(block1);
|
||||
} catch(BlockchainIntegrityException e) {
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
|
||||
List<Transaction> transactions2 = new ArrayList<>();
|
||||
transactions2.add(new Transaction("2", "3", 1));
|
||||
|
||||
Block block2 = new Block(block1.getHash(), 3, transactions2);
|
||||
block2.mineBlock();
|
||||
|
||||
try {
|
||||
b.addBlock(block2);
|
||||
} catch (BlockchainIntegrityException e) {
|
||||
fail();
|
||||
}
|
||||
|
||||
assertTrue(b.validateChain() == Block.VerifyCodes.SUCCESS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockchainIntegrityExceptionThrown() {
|
||||
Main.difficulty = 2;
|
||||
Blockchain b = new Blockchain("1", false);
|
||||
|
||||
List<Transaction> transactions1 = new ArrayList<>();
|
||||
transactions1.add(new Transaction("1", "2", 1));
|
||||
|
||||
Block block1 = new Block(b.getLatestBlock().getHash(), 2, transactions1);
|
||||
block1.mineBlock();
|
||||
|
||||
try {
|
||||
b.addBlock(block1);
|
||||
} catch(BlockchainIntegrityException e) {
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
|
||||
List<Transaction> transactions2 = new ArrayList<>();
|
||||
transactions2.add(new Transaction("2", "3", 1));
|
||||
|
||||
Block block2 = new Block("asdf", 3, transactions2);
|
||||
block2.mineBlock();
|
||||
|
||||
try {
|
||||
b.addBlock(block2);
|
||||
} catch (BlockchainIntegrityException e) {
|
||||
return; // pass
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package pl.mikorosa.dziecoin;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CalculateHashTest {
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
void sha256sum() {
|
||||
String resultingHash = CalculateHash.sha256sum("testing");
|
||||
assertEquals("cf80cd8aed482d5d1527d7dc72fceff84e6326592848447d2dc0b0e87dfc9a90", resultingHash);
|
||||
}
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
void md5sum() {
|
||||
String resultingHash = CalculateHash.md5sum("testing");
|
||||
assertEquals("ae2b1fca515949e5d54fb22b8ed95575", resultingHash);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package pl.mikorosa.dziecoin;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class NFTTest {
|
||||
@Test
|
||||
void NFTContractGen() {
|
||||
NFT nft = new NFT("stevejobs", "test");
|
||||
|
||||
String hash = CalculateHash.md5sum("stevejobstest");
|
||||
|
||||
assertEquals(hash, nft.getContract());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package pl.mikorosa.dziecoin.database;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DatabaseConnectionTest {
|
||||
|
||||
@Test
|
||||
void select() {
|
||||
DatabaseConnection db = new DatabaseConnection();
|
||||
List<Map<String, Object>> rows = db.query("SELECT * FROM test");
|
||||
int numRows = rows.size();
|
||||
|
||||
assertEquals(0, numRows);
|
||||
}
|
||||
|
||||
@Test
|
||||
void insertAndDelete() {
|
||||
DatabaseConnection db = new DatabaseConnection();
|
||||
|
||||
List<Map<String, Object>> before = db.query("SELECT * FROM test");
|
||||
int numRowsBefore = before.size();
|
||||
|
||||
db.update("INSERT INTO test VALUES (null, 'aaa')");
|
||||
|
||||
List<Map<String, Object>> rowsAfter = db.query("SELECT * FROM test");
|
||||
int numRowsAfter = rowsAfter.size();
|
||||
|
||||
assertEquals(numRowsBefore + 1, numRowsAfter);
|
||||
|
||||
db.update("DELETE FROM test");
|
||||
|
||||
List<Map<String, Object>> rowsDeleted = db.query("SELECT * FROM test");
|
||||
int numRowsDeleted = rowsDeleted.size();
|
||||
|
||||
assertEquals(numRowsAfter - 1, numRowsDeleted);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue