@Test public void testGenerateNodeIdFromPropsGeneratesRandomWhenFileIsNotPresentAndNoFallback() { exception.expect(RuntimeException.class); exception.expectMessage("Can't read 'nodeId.properties' and no fallback method has been set"); GenerateNodeIdStrategy fileNodeIdGeneratorStrategy = new GetNodeIdFromPropsFile("database-test"); fileNodeIdGeneratorStrategy.getNodePrivateKey(); }
@Override public String getNodePrivateKey() { Properties props = new Properties(); File file = new File(databaseDir, "nodeId.properties"); if (file.canRead()) { try (Reader r = new FileReader(file)) { props.load(r); return props.getProperty("nodeIdPrivateKey"); } catch (IOException e) { throw new RuntimeException("Error reading 'nodeId.properties' file", e); } } else { if (fallbackGenerateNodeIdStrategy != null) { return fallbackGenerateNodeIdStrategy.getNodePrivateKey(); } else { throw new RuntimeException("Can't read 'nodeId.properties' and no fallback method has been set"); } } }
GetNodeIdFromPropsFile implements GenerateNodeIdStrategy { @Override public String getNodePrivateKey() { Properties props = new Properties(); File file = new File(databaseDir, "nodeId.properties"); if (file.canRead()) { try (Reader r = new FileReader(file)) { props.load(r); return props.getProperty("nodeIdPrivateKey"); } catch (IOException e) { throw new RuntimeException("Error reading 'nodeId.properties' file", e); } } else { if (fallbackGenerateNodeIdStrategy != null) { return fallbackGenerateNodeIdStrategy.getNodePrivateKey(); } else { throw new RuntimeException("Can't read 'nodeId.properties' and no fallback method has been set"); } } } }
GetNodeIdFromPropsFile implements GenerateNodeIdStrategy { @Override public String getNodePrivateKey() { Properties props = new Properties(); File file = new File(databaseDir, "nodeId.properties"); if (file.canRead()) { try (Reader r = new FileReader(file)) { props.load(r); return props.getProperty("nodeIdPrivateKey"); } catch (IOException e) { throw new RuntimeException("Error reading 'nodeId.properties' file", e); } } else { if (fallbackGenerateNodeIdStrategy != null) { return fallbackGenerateNodeIdStrategy.getNodePrivateKey(); } else { throw new RuntimeException("Can't read 'nodeId.properties' and no fallback method has been set"); } } } GetNodeIdFromPropsFile(String databaseDir); }
GetNodeIdFromPropsFile implements GenerateNodeIdStrategy { @Override public String getNodePrivateKey() { Properties props = new Properties(); File file = new File(databaseDir, "nodeId.properties"); if (file.canRead()) { try (Reader r = new FileReader(file)) { props.load(r); return props.getProperty("nodeIdPrivateKey"); } catch (IOException e) { throw new RuntimeException("Error reading 'nodeId.properties' file", e); } } else { if (fallbackGenerateNodeIdStrategy != null) { return fallbackGenerateNodeIdStrategy.getNodePrivateKey(); } else { throw new RuntimeException("Can't read 'nodeId.properties' and no fallback method has been set"); } } } GetNodeIdFromPropsFile(String databaseDir); @Override String getNodePrivateKey(); GenerateNodeIdStrategy withFallback(GenerateNodeIdStrategy generateNodeIdStrategy); }
GetNodeIdFromPropsFile implements GenerateNodeIdStrategy { @Override public String getNodePrivateKey() { Properties props = new Properties(); File file = new File(databaseDir, "nodeId.properties"); if (file.canRead()) { try (Reader r = new FileReader(file)) { props.load(r); return props.getProperty("nodeIdPrivateKey"); } catch (IOException e) { throw new RuntimeException("Error reading 'nodeId.properties' file", e); } } else { if (fallbackGenerateNodeIdStrategy != null) { return fallbackGenerateNodeIdStrategy.getNodePrivateKey(); } else { throw new RuntimeException("Can't read 'nodeId.properties' and no fallback method has been set"); } } } GetNodeIdFromPropsFile(String databaseDir); @Override String getNodePrivateKey(); GenerateNodeIdStrategy withFallback(GenerateNodeIdStrategy generateNodeIdStrategy);  }