Custom exceptions and testing
This commit is contained in:
64
.gitignore
vendored
64
.gitignore
vendored
@@ -1,3 +1,61 @@
|
|||||||
/out/production/blockchain/
|
/.idea/.gitignore
|
||||||
/.idea/
|
/build/reports/tests/test/css/base-style.css
|
||||||
/blockchain.iml
|
/build/classes/java/main/model/Block.class
|
||||||
|
/out/production/blockchain_gitea/model/Block.class
|
||||||
|
/build/classes/java/main/model/BlockChain.class
|
||||||
|
/out/production/blockchain_gitea/model/BlockChain.class
|
||||||
|
/blockChain.txt
|
||||||
|
/build/classes/java/test/BlockChainTest.class
|
||||||
|
/build/reports/tests/test/classes/BlockChainTest.html
|
||||||
|
/.gradle/buildOutputCleanup/buildOutputCleanup.lock
|
||||||
|
/.gradle/buildOutputCleanup/cache.properties
|
||||||
|
/.gradle/checksums/checksums.lock
|
||||||
|
/.idea/compiler.xml
|
||||||
|
/build/reports/tests/test/packages/default-package.html
|
||||||
|
/.gradle/7.1/dependencies-accessors/dependencies-accessors.lock
|
||||||
|
/.gradle/7.1/executionHistory/executionHistory.lock
|
||||||
|
/.gradle/7.1/fileHashes/fileHashes.lock
|
||||||
|
/build/classes/java/main/util/FileManagement.class
|
||||||
|
/out/production/blockchain_gitea/util/FileManagement.class
|
||||||
|
/.gradle/7.1/dependencies-accessors/gc.properties
|
||||||
|
/.gradle/7.1/gc.properties
|
||||||
|
/.gradle/vcs-1/gc.properties
|
||||||
|
/.idea/gradle.xml
|
||||||
|
/gradle/wrapper/gradle-wrapper.jar
|
||||||
|
/gradle/wrapper/gradle-wrapper.properties
|
||||||
|
/gradlew
|
||||||
|
/gradlew.bat
|
||||||
|
/build/classes/java/main/util/HashFunction.class
|
||||||
|
/out/production/blockchain_gitea/util/HashFunction.class
|
||||||
|
/build/reports/tests/test/index.html
|
||||||
|
/.idea/sonarlint/issuestore/index.pb
|
||||||
|
/.idea/jarRepositories.xml
|
||||||
|
/.gradle/7.1/fileChanges/last-build.bin
|
||||||
|
/build/classes/java/main/Main.class
|
||||||
|
/out/production/blockchain_gitea/Main.class
|
||||||
|
/build/classes/java/main/model/Miner.class
|
||||||
|
/out/production/blockchain_gitea/model/Miner.class
|
||||||
|
/.idea/misc.xml
|
||||||
|
/build/classes/java/main/exceptions/NoSelfTransactionException.class
|
||||||
|
/build/classes/java/main/exceptions/NotEnoughCoinsException.class
|
||||||
|
/out/production/blockchain_gitea/exceptions/NotEnoughCoinsException.class
|
||||||
|
/build/test-results/test/binary/output.bin
|
||||||
|
/build/test-results/test/binary/output.bin.idx
|
||||||
|
/build/tmp/compileJava/previous-compilation-data.bin
|
||||||
|
/build/tmp/compileTestJava/previous-compilation-data.bin
|
||||||
|
/KeyPair/privateKey
|
||||||
|
/.idea/inspectionProfiles/Project_Default.xml
|
||||||
|
/KeyPair/publicKey
|
||||||
|
/build/reports/tests/test/js/report.js
|
||||||
|
/build/test-results/test/binary/results.bin
|
||||||
|
/.idea/runConfigurations.xml
|
||||||
|
/build/classes/java/main/util/Security.class
|
||||||
|
/out/production/blockchain_gitea/util/Security.class
|
||||||
|
/build/reports/tests/test/css/style.css
|
||||||
|
/build/test-results/test/TEST-BlockChainTest.xml
|
||||||
|
/build/classes/java/main/model/Transaction.class
|
||||||
|
/out/production/blockchain_gitea/model/Transaction.class
|
||||||
|
/build/classes/java/main/util/TransactionsGenerator.class
|
||||||
|
/out/production/blockchain_gitea/util/TransactionsGenerator.class
|
||||||
|
/.idea/uiDesigner.xml
|
||||||
|
/.idea/vcs.xml
|
||||||
|
|||||||
9
src/main/java/exceptions/NoSelfTransactionException.java
Normal file
9
src/main/java/exceptions/NoSelfTransactionException.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package exceptions;
|
||||||
|
|
||||||
|
public class NoSelfTransactionException extends RuntimeException {
|
||||||
|
|
||||||
|
public NoSelfTransactionException(final String username) {
|
||||||
|
super(username + " tried to send coins to oneself");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package model;
|
package model;
|
||||||
|
|
||||||
|
import exceptions.NoSelfTransactionException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
@@ -94,7 +95,9 @@ public class BlockChain implements Serializable {
|
|||||||
InvalidKeySpecException,
|
InvalidKeySpecException,
|
||||||
NotEnoughCoinsException
|
NotEnoughCoinsException
|
||||||
{
|
{
|
||||||
if (usersCoins.get(sender) <= 0 || usersCoins.get(sender) < amount) {
|
if (sender.equals(receiver)) {
|
||||||
|
throw new NoSelfTransactionException(sender);
|
||||||
|
} else if (usersCoins.get(sender) <= 0 || usersCoins.get(sender) < amount) {
|
||||||
throw new NotEnoughCoinsException(sender);
|
throw new NotEnoughCoinsException(sender);
|
||||||
} else {
|
} else {
|
||||||
transferCoins(sender, amount, receiver);
|
transferCoins(sender, amount, receiver);
|
||||||
|
|||||||
84
src/test/java/BlockChainTest.java
Normal file
84
src/test/java/BlockChainTest.java
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import exceptions.NoSelfTransactionException;
|
||||||
|
import exceptions.NotEnoughCoinsException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SignatureException;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
import model.BlockChain;
|
||||||
|
import model.Miner;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class BlockChainTest {
|
||||||
|
|
||||||
|
private final int nThreads = Runtime.getRuntime().availableProcessors();
|
||||||
|
private List<Miner> miners;
|
||||||
|
private BlockChain blockChain;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
blockChain = new BlockChain();
|
||||||
|
blockChain.acceptUser("tester1", 100);
|
||||||
|
blockChain.acceptUser("tester2", 100);
|
||||||
|
miners = IntStream.range(0, 5)
|
||||||
|
.mapToObj(minerId -> new Miner(minerId, blockChain))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void block_chain_correct_test()
|
||||||
|
throws InterruptedException, NoSuchAlgorithmException, SignatureException, IOException, InvalidKeySpecException, InvalidKeyException {
|
||||||
|
|
||||||
|
final var minerExecutor = Executors.newFixedThreadPool(nThreads);
|
||||||
|
|
||||||
|
for (final Miner miner: miners){
|
||||||
|
blockChain.acceptTransaction(blockChain.getNextTransactionId(), "tester1", 10, "tester2");
|
||||||
|
blockChain.acceptTransaction(blockChain.getNextTransactionId(), "tester2", 10, "tester1");
|
||||||
|
minerExecutor.execute(miner);
|
||||||
|
}
|
||||||
|
|
||||||
|
minerExecutor.shutdown();
|
||||||
|
|
||||||
|
if (!minerExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
|
||||||
|
minerExecutor.shutdownNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(blockChain.validateBlockchain());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void block_chain_not_enough_coins_test() {
|
||||||
|
final NotEnoughCoinsException thrown = assertThrows(
|
||||||
|
NotEnoughCoinsException.class,
|
||||||
|
() -> blockChain.acceptTransaction(
|
||||||
|
1,
|
||||||
|
"tester1",
|
||||||
|
110,
|
||||||
|
"tester2")
|
||||||
|
);
|
||||||
|
assertTrue(thrown.getMessage().contains("tester1 has not enough coins to perform the transaction"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void block_chain_not_self_transaction_test() {
|
||||||
|
final NoSelfTransactionException thrown = assertThrows(
|
||||||
|
NoSelfTransactionException.class,
|
||||||
|
() -> blockChain.acceptTransaction(
|
||||||
|
1,
|
||||||
|
"tester1",
|
||||||
|
10,
|
||||||
|
"tester1")
|
||||||
|
);
|
||||||
|
assertTrue(thrown.getMessage().contains("tried to send coins to oneself"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user