/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
java-blockchain/src/test/java/com/baeldung/blockchain/BlockchainUnitTest.java
68 строк
2 KB
Eric Martin
Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2
01 ноя 2019, 04:43
01 ноя 2019, 04:43
3225470
Код
Авторство
О чём код?
package com.baeldung.blockchain; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class BlockchainUnitTest { public static List<Block> blockchain = new ArrayList<Block>(); public static int prefix = 4; public static String prefixString = new String(new char[prefix]).replace('\0', '0'); @BeforeClass public static void setUp() { Block genesisBlock = new Block("The is the Genesis Block.", "0", new Date().getTime()); genesisBlock.mineBlock(prefix); blockchain.add(genesisBlock); Block firstBlock = new Block("The is the First Block.", genesisBlock.getHash(), new Date().getTime()); firstBlock.mineBlock(prefix); blockchain.add(firstBlock); } @Test public void givenBlockchain_whenNewBlockAdded_thenSuccess() { Block newBlock = new Block("The is a New Block.", blockchain.get(blockchain.size() - 1) .getHash(), new Date().getTime()); newBlock.mineBlock(prefix); assertTrue(newBlock.getHash() .substring(0, prefix) .equals(prefixString)); blockchain.add(newBlock); } @Test public void givenBlockchain_whenValidated_thenSuccess() { boolean flag = true; for (int i = 0; i < blockchain.size(); i++) { String previousHash = i == 0 ? "0" : blockchain.get(i - 1) .getHash(); flag = blockchain.get(i) .getHash() .equals(blockchain.get(i) .calculateBlockHash()) && previousHash.equals(blockchain.get(i) .getPreviousHash()) && blockchain.get(i) .getHash() .substring(0, prefix) .equals(prefixString); if (!flag) break; } assertTrue(flag); } @AfterClass public static void tearDown() { blockchain.clear(); } }