-
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.
test(genesis): adds file loader tests
- Loading branch information
Showing
2 changed files
with
79 additions
and
13 deletions.
There are no files selected for viewing
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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
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 { | ||
Check notice Code scanning / CodeQL Unused classes and interfaces Note test
Unused class: GenesisLoaderImplTest is not referenced within this codebase. If not used as an external API it should be removed.
|
||
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); | ||
assert genesisFileStream != null; | ||
|
||
Assertions.assertTrue(isStreamReadable(genesisFileStream)); | ||
} | ||
|
||
@Test | ||
void loadGenesisFile_missingFile_inResourcesDir() { | ||
File genesisFile = new File("non-existent-file.json"); | ||
|
||
Assertions.assertNull(GenesisLoaderImpl.loadGenesisFile(genesisFile.getPath())); | ||
} | ||
|
||
@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()); | ||
assert genesisFileStream != null; | ||
|
||
Assertions.assertTrue(isStreamReadable(genesisFileStream)); | ||
} | ||
|
||
@Test | ||
void loadGenesisFile_missingFile_inSystem(@TempDir Path tempGenesisDir) { | ||
File genesisFile = new File(tempGenesisDir + "/non-existent-file.json"); | ||
|
||
Assertions.assertNull(GenesisLoaderImpl.loadGenesisFile(genesisFile.getPath())); | ||
} | ||
|
||
} |