GraphFactory { public static Graph open(final Configuration configuration) { if (null == configuration) throw Graph.Exceptions.argumentCanNotBeNull("configuration"); final String clazz = configuration.getString(Graph.GRAPH, null); if (null == clazz) throw new RuntimeException(String.format("Configuration must contain a valid '%s' setting", Graph.GRAPH)); final Class<?> graphClass; try { graphClass = Class.forName(clazz); } catch (final ClassNotFoundException e) { throw new RuntimeException(String.format("GraphFactory could not find [%s] - Ensure that the jar is in the classpath", clazz)); } final GraphFactoryClass factoryAnnotation = graphClass.getAnnotation(GraphFactoryClass.class); final Class<?> factoryClass = factoryAnnotation != null ? factoryAnnotation.value() : graphClass; return open(configuration, factoryClass); } private  GraphFactory(); static Graph open(final Configuration configuration); static Graph open(final String configurationFile); static Graph open(final Map configuration);  }
@Test public void shouldThrowExceptionOnNullConfiguration() { try { GraphFactory.open((Configuration) null); fail("Should have thrown an exception since configuration is null"); } catch (Exception ex) { final Exception expected = Graph.Exceptions.argumentCanNotBeNull("configuration"); assertEquals(expected.getClass(), ex.getClass()); assertEquals(expected.getMessage(), ex.getMessage()); } }
@Test public void shouldThrowExceptionOnNullMapConfiguration() { try { GraphFactory.open((Map) null); fail("Should have thrown an exception since configuration is null"); } catch (Exception ex) { final Exception expected = Graph.Exceptions.argumentCanNotBeNull("configuration"); assertEquals(expected.getClass(), ex.getClass()); assertEquals(expected.getMessage(), ex.getMessage()); } }
@Test public void shouldThrowExceptionOnNullFileConfiguration() { try { GraphFactory.open((String) null); fail("Should have thrown an exception since configuration is null"); } catch (Exception ex) { final Exception expected = Graph.Exceptions.argumentCanNotBeNull("configurationFile"); assertEquals(expected.getClass(), ex.getClass()); assertEquals(expected.getMessage(), ex.getMessage()); } }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionOnIfConfigurationFileIsNotAnActualFile() { GraphFactory.open(TestSupport.makeTestDataDirectory(GraphFactoryTest.class, "path")); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionIfGraphKeyIsNotPresentInConfiguration() { final Configuration conf = new BaseConfiguration(); GraphFactory.open(conf); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionIfGraphKeyIsNotPresentInMapConfig() { final Map<String,Object> conf = new HashMap<>(); GraphFactory.open(conf); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionIfGraphKeyIsNotValidClassInConfiguration() { final Configuration conf = new BaseConfiguration(); conf.setProperty(Graph.GRAPH, "not.a.real.class.that.is.a.SomeGraph"); GraphFactory.open(conf); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionIfGraphKeyIsNotValidClassInMapConfig() { final Map<String,Object> conf = new HashMap<>(); conf.put(Graph.GRAPH, "not.a.real.class.that.is.a.SomeGraph"); GraphFactory.open(conf); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionIfGraphDoesNotHaveOpenMethodViaConfiguration() { final Configuration conf = new BaseConfiguration(); conf.setProperty(Graph.GRAPH, MockGraphWithoutOpen.class.getName()); GraphFactory.open(conf); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionIfGraphDoesNotHaveOpenMethodViaMapConfig() { final Map<String,Object> conf = new HashMap<>(); conf.put(Graph.GRAPH, MockGraphWithoutOpen.class.getName()); GraphFactory.open(conf); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionIfGraphThrowsWhileConfiguringViaConfiguration() { final Configuration conf = new BaseConfiguration(); conf.setProperty(Graph.GRAPH, MockGraph.class.getName()); conf.setProperty("throw", "it"); GraphFactory.open(conf); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionIfGraphThrowsWhileConfiguringViaMapConfig() { final Map<String,Object> conf = new HashMap<>(); conf.put(Graph.GRAPH, MockGraph.class.getName()); conf.put("throw", "it"); GraphFactory.open(conf); }
@Test public void shouldOpenViaConfiguration() { final Configuration conf = new BaseConfiguration(); conf.setProperty(Graph.GRAPH, MockGraph.class.getName()); conf.setProperty("keep", "it"); GraphFactory.open(conf); final MockGraph g = (MockGraph) GraphFactory.open(conf); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenViaMapConfig() { final Map<String,Object> conf = new HashMap<>(); conf.put(Graph.GRAPH, MockGraph.class.getName()); conf.put("keep", "it"); final MockGraph g = (MockGraph) GraphFactory.open(conf); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenViaPropertiesFileConfig() throws IOException { final File confFile = TestSupport.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph.properties", ".properties"); final MockGraph g = (MockGraph) GraphFactory.open(Storage.toPath(confFile)); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenViaPropertiesFileConfigAsDefault() throws IOException { final File confFile = TestSupport.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph.properties", ".notrecognized"); final MockGraph g = (MockGraph) GraphFactory.open(Storage.toPath(confFile)); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenViaXmlFileConfig() throws IOException { final File confFile = TestSupport.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph.xml", ".xml"); final MockGraph g = (MockGraph) GraphFactory.open(Storage.toPath(confFile)); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenViaYamlFileConfig() throws IOException { final File confFile = TestSupport.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph.yaml", ".yaml"); final MockGraph g = (MockGraph) GraphFactory.open(Storage.toPath(confFile)); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenViaYmlFileConfig() throws IOException { final File confFile = TestSupport.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph.yaml", ".yml"); final MockGraph g = (MockGraph) GraphFactory.open(Storage.toPath(confFile)); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowOnInvalidConfigTypeViaFileConfig() throws IOException { final File confFile = TestSupport.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph-busted.yaml", ".yaml"); final MockGraph g = (MockGraph) GraphFactory.open(Storage.toPath(confFile)); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenWithFactoryViaConcreteClass() { final Configuration conf = new BaseConfiguration(); conf.setProperty(Graph.GRAPH, MockGraphWithFactory.class.getName()); GraphFactory.open(conf); final MockGraphWithFactory g = (MockGraphWithFactory) GraphFactory.open(conf); assertEquals(conf, g.getConf()); }
@Test public void shouldOpenWithFactoryViaConcreteInterface() { final Configuration conf = new BaseConfiguration(); conf.setProperty(Graph.GRAPH, MockGraphInterface.class.getName()); GraphFactory.open(conf); final MockGraphInterface g = (MockGraphInterface) GraphFactory.open(conf); assertEquals(conf, g.getConf()); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionIfFactoryDoesNotHaveOpenMethodViaConfiguration() { final Configuration conf = new BaseConfiguration(); conf.setProperty(Graph.GRAPH, MockGraphWithFactoryWithoutOpen.class.getName()); GraphFactory.open(conf); }
@Test public void shouldCreateAnEmptyGraphInstance() { final Configuration conf = new BaseConfiguration(); conf.setProperty(Graph.GRAPH, EmptyGraph.class.getName()); final Graph graph = GraphFactory.open(conf); assertSame(EmptyGraph.instance(), graph); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionOnIfConfigurationFileIsNotAnActualFile() { GraphFactory.open(TestHelper.makeTestDataPath(GraphFactoryTest.class, "path").getAbsolutePath()); }
@Test public void shouldOpenViaPropertiesFileConfig() throws IOException { final File confFile = TestHelper.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph.properties", ".properties"); final MockGraph g = (MockGraph) GraphFactory.open(confFile.getAbsolutePath()); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenViaPropertiesFileConfigAsDefault() throws IOException { final File confFile = TestHelper.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph.properties", ".notrecognized"); final MockGraph g = (MockGraph) GraphFactory.open(confFile.getAbsolutePath()); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenViaXmlFileConfig() throws IOException { final File confFile = TestHelper.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph.xml", ".xml"); final MockGraph g = (MockGraph) GraphFactory.open(confFile.getAbsolutePath()); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenViaYamlFileConfig() throws IOException { final File confFile = TestHelper.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph.yaml", ".yaml"); final MockGraph g = (MockGraph) GraphFactory.open(confFile.getAbsolutePath()); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test public void shouldOpenViaYmlFileConfig() throws IOException { final File confFile = TestHelper.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph.yaml", ".yml"); final MockGraph g = (MockGraph) GraphFactory.open(confFile.getAbsolutePath()); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowOnInvalidConfigTypeViaFileConfig() throws IOException { final File confFile = TestHelper.generateTempFileFromResource(GraphFactoryTest.class, "mockgraph-busted.yaml", ".yaml"); final MockGraph g = (MockGraph) GraphFactory.open(confFile.getAbsolutePath()); assertEquals(MockGraph.class.getName(), g.getConf().getString(Graph.GRAPH)); assertEquals("it", g.getConf().getString("keep")); }