Skip to content

Commit

Permalink
test(genesis): adds file loader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajpiar committed Mar 27, 2024
1 parent d705707 commit accb029
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -237,21 +234,16 @@ private void setupPrecompiledContractsStorage(Repository repository) {

public static InputStream loadGenesisFile(String fileName) {
InputStream inputStream = GenesisLoaderImpl.class.getResourceAsStream("/genesis/" + fileName);

if (inputStream != null) {
return inputStream;
}

Path filePath = Paths.get(fileName);
if (Files.exists(filePath)) {
try {
return new FileInputStream(filePath.toFile());
} catch (FileNotFoundException e) {
logger.error(e.getLocalizedMessage());
}
try {
return Files.newInputStream(Paths.get(fileName));
} catch (Exception e) {
logger.error("Could not load file due to:", e);
return null;
}

return null;
}

public static void loadGenesisInitalState(Repository repository, Genesis genesis) {
Expand Down
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()));
}

}

0 comments on commit accb029

Please sign in to comment.