-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(genesis): allows external genesis file
test(genesis): adds file loader tests squashme: better catch handling
- Loading branch information
Showing
3 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
rskj-core/src/main/java/org/ethereum/core/genesis/GenesisLoaderException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.ethereum.core.genesis; | ||
|
||
public class GenesisLoaderException extends RuntimeException { | ||
GenesisLoaderException(String message, Throwable error) { | ||
super(message, error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
rskj-core/src/test/java/org/ethereum/core/genesis/GenesisLoaderImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.ethereum.core.genesis; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
class GenesisLoaderImplTest { | ||
private final String GENESIS_FILE_NAME = "temp_genesis.json"; | ||
|
||
private final String RESOURCES_GENESIS_FILE_PATH = Objects.requireNonNull(GenesisLoaderImpl.class.getResource("/genesis")).getPath() + "/" + GENESIS_FILE_NAME; | ||
|
||
@SuppressWarnings("ResultOfMethodCallIgnored") | ||
private boolean isStreamReadable(InputStream stream) { | ||
try { | ||
stream.read(); | ||
return true; | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
} | ||
|
||
@BeforeEach | ||
public void createTempFiles() throws IOException { | ||
boolean created = (new File(RESOURCES_GENESIS_FILE_PATH)).createNewFile(); | ||
Assertions.assertTrue(created); | ||
} | ||
|
||
@AfterEach | ||
public void cleanUpFiles() { | ||
boolean deleted = (new File(RESOURCES_GENESIS_FILE_PATH)).delete(); | ||
Assertions.assertTrue(deleted); | ||
} | ||
|
||
@Test | ||
void loadGenesisFile_fromResourcesDir() { | ||
InputStream genesisFileStream = GenesisLoaderImpl.loadGenesisFile(GENESIS_FILE_NAME); | ||
|
||
Assertions.assertTrue(isStreamReadable(genesisFileStream)); | ||
} | ||
|
||
@Test | ||
void loadGenesisFile_missingFile_inResourcesDir() { | ||
String genesisFilePath = new File("non-existent-file.json").getPath(); | ||
|
||
Exception e = Assertions.assertThrows(GenesisLoaderException.class, () -> GenesisLoaderImpl.loadGenesisFile(genesisFilePath)); | ||
Assertions.assertEquals("Cannot open genesis block configuration file", e.getMessage()); | ||
} | ||
|
||
@Test | ||
void loadGenesisFile_fromSystem(@TempDir Path tempGenesisDir) throws IOException { | ||
File genesisFile = new File(tempGenesisDir + "/" + GENESIS_FILE_NAME); | ||
Assertions.assertTrue(genesisFile.createNewFile()); | ||
InputStream genesisFileStream = GenesisLoaderImpl.loadGenesisFile(genesisFile.getPath()); | ||
|
||
Assertions.assertTrue(isStreamReadable(genesisFileStream)); | ||
} | ||
|
||
@Test | ||
void loadGenesisFile_missingFile_inSystem(@TempDir Path tempGenesisDir) { | ||
String genesisFilePath = new File(tempGenesisDir + "/non-existent-file.json").getPath(); | ||
|
||
Exception e = Assertions.assertThrows(GenesisLoaderException.class, () -> GenesisLoaderImpl.loadGenesisFile(genesisFilePath)); | ||
Assertions.assertEquals("Cannot open genesis block configuration file", e.getMessage()); | ||
} | ||
} |