diff --git a/common/src/main/java/apoc/ApocConfig.java b/common/src/main/java/apoc/ApocConfig.java index 1b7fa22d7..040439a0e 100644 --- a/common/src/main/java/apoc/ApocConfig.java +++ b/common/src/main/java/apoc/ApocConfig.java @@ -44,7 +44,6 @@ import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.stream.Stream; import org.apache.commons.configuration2.CombinedConfiguration; import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.EnvironmentConfiguration; @@ -189,32 +188,7 @@ public void init() { } protected String determineNeo4jConfFolder() { - return neo4jConfig.get(configuration_directory).resolve(Config.DEFAULT_CONFIG_DIR_NAME).toString(); -// String defaultPath =neo4jConfig.get(configuration_directory).toString(); //resolve(Config.DEFAULT_CONFIG_DIR_NAME).toString(); -// neo4jConfig -// .get(neo4j_home) -// .resolve(Config.DEFAULT_CONFIG_DIR_NAME) -// .toString(); -// String command = System.getProperty(SUN_JAVA_COMMAND); -// if (command == null) { -// log.warn( -// "system property %s is not set, assuming %s as conf dir. This might cause `apoc.conf` not getting loaded.", -// SUN_JAVA_COMMAND, defaultPath); -// return defaultPath; -// } else { -// final String neo4jConfFolder = Stream.of(command.split("--")) -// .map(String::trim) -// .filter(s -> s.startsWith(CONFIG_DIR)) -// .map(s -> s.substring(CONFIG_DIR.length())) -// .findFirst() -// .orElse(defaultPath); -// if (defaultPath.equals(neo4jConfFolder)) { -// log.info("cannot determine conf folder from sys property %s, assuming %s", command, defaultPath); -// } else { -// log.info("from system properties: NEO4J_CONF=%s", neo4jConfFolder); -// } -// return neo4jConfFolder; -// } + return neo4jConfig.get(configuration_directory).toString(); } /** diff --git a/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java b/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java index 95715c529..885aac6b2 100644 --- a/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java +++ b/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java @@ -19,7 +19,6 @@ package apoc; import static apoc.ApocConfig.APOC_MAX_DECOMPRESSION_RATIO; -import static apoc.ApocConfig.SUN_JAVA_COMMAND; import static java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE; import static java.nio.file.attribute.PosixFilePermission.GROUP_READ; import static java.nio.file.attribute.PosixFilePermission.GROUP_WRITE; @@ -46,7 +45,6 @@ import java.util.Collections; import java.util.Set; import org.assertj.core.api.Assertions; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.neo4j.configuration.Config; @@ -75,16 +73,16 @@ public void setup() throws Exception { when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.get(GraphDatabaseSettings.allow_file_urls)).thenReturn(false); when(neo4jConfig.expandCommands()).thenReturn(true); -// when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); apocConfigCommandExpansionFile = new File(getClass() .getClassLoader() - .getResource("apoc.conf") + .getResource("apoc-config-command-expansion/apoc.conf") .toURI()); Files.setPosixFilePermissions( apocConfigCommandExpansionFile.toPath(), permittedFilePermissionsForCommandExpansion); - when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of(apocConfigCommandExpansionFile.getParent())); + when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)) + .thenReturn(Path.of(apocConfigCommandExpansionFile.getParent())); GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class); DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class); @@ -92,29 +90,14 @@ public void setup() throws Exception { new ApocConfig(neo4jConfig, new SimpleLogService(logProvider), registry, databaseManagementService); } - @After - public void cleanup() { - System.clearProperty(SUN_JAVA_COMMAND); - System.clearProperty(APOC_MAX_DECOMPRESSION_RATIO); - System.clearProperty("command.expansion"); - } - private void setApocConfigFilePermissions(Set forbidden) throws Exception { Files.setPosixFilePermissions( apocConfigCommandExpansionFile.toPath(), Sets.union(permittedFilePermissionsForCommandExpansion, forbidden)); } -// private void setApocConfigSystemProperty() { -// System.setProperty( -// SUN_JAVA_COMMAND, -// "com.neo4j.server.enterprise.CommercialEntryPoint --home-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02 --config-dir=" -// + apocConfigCommandExpansionFile.getParent()); -// } - @Test public void testApocConfWithExpandCommands() { -// setApocConfigSystemProperty(); apocConfig.init(); assertEquals("expanded value", apocConfig.getConfig().getString("command.expansion")); @@ -124,7 +107,6 @@ public void testApocConfWithExpandCommands() { public void testApocConfWithInvalidExpandCommands() throws Exception { String invalidExpandLine = "command.expansion.3=$(fakeCommand 3 + 3)"; addLineToApocConfig(invalidExpandLine); -// setApocConfigSystemProperty(); RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init); String expectedMessage = @@ -138,7 +120,6 @@ public void testApocConfWithInvalidExpandCommands() throws Exception { public void testApocConfWithWrongFilePermissions() throws Exception { for (PosixFilePermission filePermission : forbiddenFilePermissionsForCommandExpansion) { setApocConfigFilePermissions(Set.of(filePermission)); -// setApocConfigSystemProperty(); RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init); String expectedMessage = "does not have the correct file permissions to evaluate commands."; @@ -156,17 +137,14 @@ public void testApocConfWithoutExpandCommands() { when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap()); when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.expandCommands()).thenReturn(false); -// when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); -// when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); - when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of(apocConfigCommandExpansionFile.getParent())); + when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)) + .thenReturn(Path.of(apocConfigCommandExpansionFile.getParent())); GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class); DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class); ApocConfig apocConfig = new ApocConfig(neo4jConfig, new SimpleLogService(logProvider), registry, databaseManagementService); -// setApocConfigSystemProperty(); - RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init); String expectedMessage = "$(echo \"expanded value\") is a command, but config is not explicitly told to expand it. (Missing --expand-commands argument?)"; @@ -182,9 +160,8 @@ public void testMaxDecompressionRatioValidation() { when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap()); when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.expandCommands()).thenReturn(false); - when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); -// when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); - when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of(apocConfigCommandExpansionFile.getParent())); + when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)) + .thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0/conf")); GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class); DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class); diff --git a/common/src/test/java/apoc/ApocConfigTest.java b/common/src/test/java/apoc/ApocConfigTest.java index b3ac92917..bfd9c0028 100644 --- a/common/src/test/java/apoc/ApocConfigTest.java +++ b/common/src/test/java/apoc/ApocConfigTest.java @@ -18,7 +18,6 @@ */ package apoc; -import static apoc.ApocConfig.SUN_JAVA_COMMAND; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -50,11 +49,11 @@ public void setup() throws Exception { when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap()); when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.get(GraphDatabaseSettings.allow_file_urls)).thenReturn(false); - when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); - when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); apocConfigFile = new File(getClass().getClassLoader().getResource("apoc.conf").toURI()); + when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)) + .thenReturn(Path.of(apocConfigFile.getParent())); GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class); DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class); @@ -62,42 +61,15 @@ public void setup() throws Exception { new ApocConfig(neo4jConfig, new SimpleLogService(logProvider), registry, databaseManagementService); } - private void setApocConfigSystemProperty() { - System.setProperty( - SUN_JAVA_COMMAND, - "com.neo4j.server.enterprise.CommercialEntryPoint --home-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02 --config-dir=" - + apocConfigFile.getParent()); - } - @Test public void testDetermineNeo4jConfFolderDefault() { - System.setProperty(SUN_JAVA_COMMAND, ""); - assertEquals("C:/neo4j/neo4j-enterprise-5.x.0/conf", apocConfig.determineNeo4jConfFolder()); - } - - @Test - public void testDetermineNeo4jConfFolder() { - System.setProperty( - SUN_JAVA_COMMAND, - "com.neo4j.server.enterprise.CommercialEntryPoint --home-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02 --config-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02/conf"); - - assertEquals("/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02/conf", apocConfig.determineNeo4jConfFolder()); + assertEquals(apocConfigFile.getParent(), apocConfig.determineNeo4jConfFolder()); } @Test public void testApocConfFileBeingLoaded() { - setApocConfigSystemProperty(); apocConfig.init(); assertEquals("bar", apocConfig.getConfig().getString("foo")); } - - @Test - public void testDetermineNeo4jConfFolderWithWhitespaces() { - System.setProperty( - SUN_JAVA_COMMAND, - "com.neo4j.server.enterprise.CommercialEntryPoint --config-dir=/home/stefan/neo4j enterprise-4.0.0-alpha09mr02/conf --home-dir=/home/stefan/neo4j enterprise-4.0.0-alpha09mr02"); - - assertEquals("/home/stefan/neo4j enterprise-4.0.0-alpha09mr02/conf", apocConfig.determineNeo4jConfFolder()); - } } diff --git a/common/src/test/resources/conf/apoc.conf b/common/src/test/resources/apoc-config-command-expansion/apoc.conf similarity index 100% rename from common/src/test/resources/conf/apoc.conf rename to common/src/test/resources/apoc-config-command-expansion/apoc.conf