From 36ce5baf26a584d6059108f2f2ffcbd7f284d361 Mon Sep 17 00:00:00 2001 From: loki2302 Date: Tue, 15 May 2012 12:24:20 -0700 Subject: [PATCH] DATAGRAPH-175: updated refactored hello-worlds-aspects example to make it easier to interprete --- .../data/neo4j/examples/hellograph/App.java | 45 +++--- .../hellograph/MyWorldRepository.java | 19 +-- .../hellograph/RelationshipTypes.java | 3 +- .../data/neo4j/examples/hellograph/World.java | 38 ++---- .../examples/hellograph/WorldCounter.java | 20 --- .../examples/hellograph/WorldRepository.java | 3 +- .../hellograph/WorldRepositoryImpl.java | 70 ++++------ .../examples/hellograph/WorldCounterTest.java | 45 ------ .../hellograph/WorldRepositoryTest.java | 129 ------------------ .../neo4j/examples/hellograph/WorldTest.java | 51 ------- 10 files changed, 60 insertions(+), 363 deletions(-) delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldCounter.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldCounterTest.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryTest.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldTest.java diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java index 16f3f3449..11fe901cb 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java @@ -3,37 +3,28 @@ package org.springframework.data.neo4j.examples.hellograph; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; -/** - * Hello world(s)! - *

- * An example application for exploring Spring Data Neo4j. - */ -public class App -{ +public class App { + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = + new ClassPathXmlApplicationContext("/spring/helloWorldContext.xml"); - public static void main( String[] args ) - { - - ConfigurableApplicationContext applicationContext = - new ClassPathXmlApplicationContext( "/spring/helloWorldContext.xml"); + MyWorldRepository galaxy = applicationContext.getBean(WorldRepositoryImpl.class); + galaxy.makeSureGalaxyIsNotEmpty(); + + System.out.println("Trying to find the Earth by its name:"); + World earth = galaxy.findWorldNamed("Earth"); + System.out.printf("Found Earth: %s\n", earth); - WorldRepositoryImpl galaxy = applicationContext.getBean(WorldRepositoryImpl.class); - - Iterable worlds = galaxy.makeSomeWorlds(); - - World homeWorld = worlds.iterator().next(); - System.out.println("At home on: " + homeWorld); - - World foundHomeWorld = galaxy.findWorldNamed( homeWorld.getName() ); - System.out.println( "found home world: " + foundHomeWorld ); - - Iterable worldsBeyond = galaxy.exploreWorldsBeyond( homeWorld ); - for (World world : worldsBeyond) { - System.out.println( "found worlds beyond: " + world ); + System.out.println("Retrieveing the list of worlds that can be reached from the Earth:"); + for(World world : earth.getReachableWorlds()) { + System.out.printf("Can travel between %s and %s\n", earth, world); + } + + System.out.println("Here's the list of all worlds in the galaxy:"); + for(World world : galaxy.findAllWorlds()) { + System.out.printf("There's a world: %s\n", world); } applicationContext.close(); - } - } diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java index ead3954a6..1c17e10c1 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java @@ -2,22 +2,11 @@ package org.springframework.data.neo4j.examples.hellograph; import org.springframework.transaction.annotation.Transactional; -import java.util.Collection; - -/** - * @author mh - * @since 01.04.11 - */ public interface MyWorldRepository { @Transactional - Collection makeSomeWorlds(); - - @Transactional - World world(String name, int moons); - + void makeSureGalaxyIsNotEmpty(); + World findWorldNamed(String name); - - Iterable findWorldsWithMoons(int moonCount); - - Iterable exploreWorldsBeyond(World homeWorld); + + Iterable findAllWorlds(); } diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java index 6f66f5f37..0477649d4 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java @@ -1,6 +1,5 @@ package org.springframework.data.neo4j.examples.hellograph; -public abstract class RelationshipTypes -{ +public abstract class RelationshipTypes { public static final String REACHABLE_BY_ROCKET = "REACHABLE_BY_ROCKET"; } diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java index 7f34379e5..0c228907f 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java @@ -8,56 +8,42 @@ import org.springframework.data.neo4j.annotation.RelatedTo; import java.util.Set; -/** - * A Spring Data Neo4j enhanced World entity. - *

- * This is the initial POJO in the Universe. - */ @NodeEntity -public class World -{ +public class World { @Indexed private String name; - @Indexed(indexName = "moon-index") private int moons; - @RelatedTo(type = "REACHABLE_BY_ROCKET", elementClass = World.class, direction = Direction.BOTH) + @RelatedTo(type = RelationshipTypes.REACHABLE_BY_ROCKET, elementClass = World.class, direction = Direction.BOTH) private Set reachableByRocket; - public World( String name, int moons ) - { + public World(String name, int moons) { this.name = name; this.moons = moons; } - public World() - { + public World() { } - public String getName() - { + public String getName() { return name; } - public int getMoons() - { + public int getMoons() { return moons; } @Override - public String toString() - { - return String.format("World{name='%s, moons=%d}", name, moons); + public String toString() { + return String.format("World{name='%s', moons=%d}", name, moons); } - public void addRocketRouteTo( World otherWorld ) - { - relateTo( otherWorld, RelationshipTypes.REACHABLE_BY_ROCKET ); + public void addRocketRouteTo(World otherWorld) { + relateTo(otherWorld, RelationshipTypes.REACHABLE_BY_ROCKET); } - public boolean canBeReachedFrom( World otherWorld ) - { - return reachableByRocket.contains( otherWorld ); + public Iterable getReachableWorlds() { + return reachableByRocket; } } diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldCounter.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldCounter.java deleted file mode 100644 index 7ff6e2e91..000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldCounter.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import java.util.HashMap; -import java.util.Map; - -/** - * @author mh - * @since 17.02.11 - */ -public class WorldCounter { - - public Map countMoons(Iterable worlds) { - Map moons = new HashMap(); - for (World world : worlds) { - moons.put(world.getName(), world.getMoons()); - } - return moons; - } - -} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java index f529c4d67..b693f5c8a 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java @@ -1,11 +1,10 @@ package org.springframework.data.neo4j.examples.hellograph; import org.springframework.data.neo4j.repository.GraphRepository; -import org.springframework.data.neo4j.repository.NamedIndexRepository; /** * @author mh * @since 01.04.11 */ -public interface WorldRepository extends MyWorldRepository, GraphRepository, NamedIndexRepository { +public interface WorldRepository extends MyWorldRepository, GraphRepository { } diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java index 286db0346..5ff279f6c 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java @@ -1,56 +1,41 @@ package org.springframework.data.neo4j.examples.hellograph; -import org.neo4j.graphdb.Direction; -import org.neo4j.graphdb.traversal.TraversalDescription; -import org.neo4j.kernel.Traversal; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.Collection; - -import static org.neo4j.graphdb.DynamicRelationshipType.withName; -import static org.springframework.data.neo4j.examples.hellograph.RelationshipTypes.REACHABLE_BY_ROCKET; - /** * Spring Data Neo4j backed application context for Worlds. */ public class WorldRepositoryImpl implements MyWorldRepository { - @Autowired private WorldRepository worldRepository; + @Autowired + private WorldRepository worldRepository; @Override @Transactional - public Collection makeSomeWorlds() { - ArrayList newWorlds = new ArrayList(); - - // solar worlds - newWorlds.add(world("Mercury", 0)); - newWorlds.add(world("Venus", 0)); - World earth = world("Earth", 1); - newWorlds.add(earth); - World mars = world("Mars", 2); + public void makeSureGalaxyIsNotEmpty() { + // Solar worlds + resolveWorld("Mercury", 0); + resolveWorld("Venus", 0); + + World earth = resolveWorld("Earth", 1); + World mars = resolveWorld("Mars", 2); mars.addRocketRouteTo(earth); - newWorlds.add(mars); - newWorlds.add(world("Jupiter", 63)); - newWorlds.add(world("Saturn", 62)); - newWorlds.add(world("Uranus", 27)); - newWorlds.add(world("Neptune", 13)); + + resolveWorld("Jupiter", 63); + resolveWorld("Saturn", 62); + resolveWorld("Uranus", 27); + resolveWorld("Neptune", 13); // Norse worlds - newWorlds.add(world("Alfheimr", 0)); - newWorlds.add(world("Midgard", 1)); - newWorlds.add(world("Muspellheim", 2)); - newWorlds.add(world("Asgard", 63)); - newWorlds.add(world("Hel", 62)); - - return newWorlds; + resolveWorld("Alfheimr", 0); + resolveWorld("Midgard", 1); + resolveWorld("Muspellheim", 2); + resolveWorld("Asgard", 63); + resolveWorld("Hel", 62); } - - - @Override - @Transactional - public World world(String name, int moons) { + + private World resolveWorld(String name, int moons) { World createdWorld = findWorldNamed(name); if (createdWorld == null) { createdWorld = new World(name, moons).persist(); @@ -62,16 +47,9 @@ public class WorldRepositoryImpl implements MyWorldRepository { public World findWorldNamed(String name) { return worldRepository.findByPropertyValue("name", name); } - + @Override - public Iterable findWorldsWithMoons(int moonCount) { - return worldRepository.findAllByPropertyValue("moon-index", "moons", moonCount); + public Iterable findAllWorlds() { + return worldRepository.findAll(); } - - @Override - public Iterable exploreWorldsBeyond(World homeWorld) { - TraversalDescription traversal = Traversal.description().relationships(withName(REACHABLE_BY_ROCKET), Direction.OUTGOING); - return worldRepository.findAllByTraversal(homeWorld, traversal); - } - } diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldCounterTest.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldCounterTest.java deleted file mode 100644 index 8b2ddf6ad..000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldCounterTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.neo4j.support.Neo4jTemplate; -import org.springframework.data.neo4j.support.node.Neo4jHelper; -import org.springframework.test.annotation.Rollback; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.transaction.BeforeTransaction; -import org.springframework.transaction.annotation.Transactional; - -import java.util.Map; - -import static java.util.Arrays.asList; -import static org.junit.Assert.assertEquals; - -/** - * @author mh - * @since 17.02.11 - * Added to check for some aspectj-snapshot build errors. - */ -@ContextConfiguration(locations = "classpath:spring/helloWorldContext.xml") -@RunWith(SpringJUnit4ClassRunner.class) -@Transactional -public class WorldCounterTest { - - @Autowired - private Neo4jTemplate template; - - @Rollback(false) - @BeforeTransaction - public void clearDatabase() - { - Neo4jHelper.cleanDb(template); - } - - @Test - public void testCountMoons() throws Exception { - WorldCounter counter = new WorldCounter(); - Map result = counter.countMoons(asList(new World("earth", 1))); - assertEquals("earth has one moon",(Integer)1,result.get("earth")); - } -} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryTest.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryTest.java deleted file mode 100644 index 55b70bfc5..000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryTest.java +++ /dev/null @@ -1,129 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.neo4j.aspects.core.NodeBacked; -import org.springframework.data.neo4j.support.Neo4jTemplate; -import org.springframework.data.neo4j.support.node.Neo4jHelper; -import org.springframework.test.annotation.Rollback; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.transaction.BeforeTransaction; -import org.springframework.transaction.annotation.Transactional; - -import java.util.Collection; - -import static junit.framework.Assert.assertEquals; -import static org.hamcrest.CoreMatchers.anyOf; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.*; -import static org.junit.internal.matchers.StringContains.containsString; - -/** - * Exploratory testing of Spring Data Neo4j using - * the WorldRepositoryImpl. - */ -@ContextConfiguration(locations = "classpath:spring/helloWorldContext.xml") -@RunWith(SpringJUnit4ClassRunner.class) -@Transactional -public class WorldRepositoryTest -{ - - @Autowired - private WorldRepository galaxy; - - @Autowired - private Neo4jTemplate template; - - @Rollback(false) - @BeforeTransaction - public void clearDatabase() - { - Neo4jHelper.cleanDb(template); - } - - @Test - public void shouldAllowDirectWorldCreation() - { - assertEquals(0, (long) galaxy.count()); - World myWorld = new World( "mine", 0 ).persist(); - assertEquals(1, (long) galaxy.count()); - Iterable foundWorlds = galaxy.findAll(); - World mine = foundWorlds.iterator().next(); - assertEquals(myWorld.getName(), mine.getName()); - } - - @Test - public void shouldPopulateGalaxyWithWorlds() - { - Iterable worlds = galaxy.makeSomeWorlds(); - assertNotNull( worlds ); - } - - - @Test - public void shouldHaveCorrectNumberOfWorlds() - { - galaxy.makeSomeWorlds(); - assertEquals(13, (long) galaxy.count()); - } - - @Test - public void shouldFindWorldsById() - { - for ( World w : galaxy.makeSomeWorlds() ) - { - assertNotNull(galaxy.findOne(((NodeBacked) w).getNodeId())); - } - } - - @Test - public void shouldFindAllWorlds() - { - Collection madeWorlds = galaxy.makeSomeWorlds(); - Iterable foundWorlds = galaxy.findAll(); - - int countOfFoundWorlds = 0; - for ( World foundWorld : foundWorlds ) - { - assertTrue( madeWorlds.contains( foundWorld ) ); - countOfFoundWorlds++; - } - - assertEquals( madeWorlds.size(), countOfFoundWorlds ); - } - - @Test - public void shouldFindWorldsByName() - { - for ( World w : galaxy.makeSomeWorlds() ) - { - assertNotNull( galaxy.findWorldNamed( w.getName() ) ); - } - } - - @SuppressWarnings("unchecked") - @Test - public void shouldFindWorldsWith1Moon() - { - galaxy.makeSomeWorlds(); - for ( World worldWithOneMoon : galaxy.findWorldsWithMoons( 1 ) ) - { - assertThat( worldWithOneMoon.getName(), is( anyOf( containsString( "Earth" ), containsString( "Midgard" ) ) ) ); - } - } - - @Test - public void shouldReachMarsFromEarth() - { - galaxy.makeSomeWorlds(); - - World earth = galaxy.findWorldNamed( "Earth" ); - World mars = galaxy.findWorldNamed( "Mars" ); - - assertTrue( mars.canBeReachedFrom( earth ) ); - assertTrue( earth.canBeReachedFrom( mars ) ); - } - -} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldTest.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldTest.java deleted file mode 100644 index 571cfd42a..000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.neo4j.support.Neo4jTemplate; -import org.springframework.data.neo4j.support.node.Neo4jHelper; -import org.springframework.test.annotation.Rollback; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.transaction.BeforeTransaction; -import org.springframework.transaction.annotation.Transactional; - -import static org.junit.Assert.assertNull; - -/** - * Exploratory unit-tests for the Spring Data Neo4j annotated World entity. - * - * Since the World is a @NodeEntity, the SpringDataGraph must - * be setup before you can even create instances of the POJO. - */ -@ContextConfiguration(locations = "classpath:spring/helloWorldContext.xml") -@RunWith(SpringJUnit4ClassRunner.class) -@Transactional -public class WorldTest -{ - - @Autowired - private Neo4jTemplate template; - - @Rollback(false) - @BeforeTransaction - public void clearDatabase() - { - Neo4jHelper.cleanDb(template); - } - - @Test - public void shouldBeSimpleToCreateNewEntities() - { - @SuppressWarnings("unused") - World w = new World(); - } - - @Test - public void shouldHaveNullNameUsingDefaultConstructor() - { - World w = new World(); - assertNull(w.getName()); - } -}