From 36ce5baf26a584d6059108f2f2ffcbd7f284d361 Mon Sep 17 00:00:00 2001 From: loki2302 Date: Tue, 15 May 2012 12:24:20 -0700 Subject: [PATCH 1/8] DATAGRAPH-175: updated refactored hello-worlds-aspects example to make it easier to interprete --- .../data/neo4j/examples/hellograph/App.java | 49 +++---- .../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, 62 insertions(+), 365 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 16f3f34499..11fe901cb2 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 @@ 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 static void main( String[] args ) - { - - ConfigurableApplicationContext applicationContext = - new ClassPathXmlApplicationContext( "/spring/helloWorldContext.xml"); - - 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 ); +public class App { + 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); + + 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 ead3954a65..1c17e10c11 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 @@ 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 6f66f5f376..0477649d45 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 7f34379e59..0c228907fb 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 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 7ff6e2e914..0000000000 --- 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 f529c4d671..b693f5c8ab 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 286db03466..5ff279f6c0 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 World world(String name, int moons) { public World findWorldNamed(String name) { return worldRepository.findByPropertyValue("name", name); } - - @Override - public Iterable findWorldsWithMoons(int moonCount) { - return worldRepository.findAllByPropertyValue("moon-index", "moons", moonCount); - } - + @Override - public Iterable exploreWorldsBeyond(World homeWorld) { - TraversalDescription traversal = Traversal.description().relationships(withName(REACHABLE_BY_ROCKET), Direction.OUTGOING); - return worldRepository.findAllByTraversal(homeWorld, traversal); + public Iterable findAllWorlds() { + return worldRepository.findAll(); } - } 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 8b2ddf6ad3..0000000000 --- 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 55b70bfc5d..0000000000 --- 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 571cfd42a9..0000000000 --- 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()); - } -} From c88a564c316b6c539b90f05fa8f8a24b7802548b Mon Sep 17 00:00:00 2001 From: loki2302 Date: Tue, 15 May 2012 12:24:20 -0700 Subject: [PATCH 2/8] DATAGRAPH-175: refactored hello-worlds-aspects example to make it easier to interprete --- .../data/neo4j/examples/hellograph/App.java | 49 +++---- .../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, 62 insertions(+), 365 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 16f3f34499..11fe901cb2 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 @@ 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 static void main( String[] args ) - { - - ConfigurableApplicationContext applicationContext = - new ClassPathXmlApplicationContext( "/spring/helloWorldContext.xml"); - - 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 ); +public class App { + 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); + + 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 ead3954a65..1c17e10c11 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 @@ 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 6f66f5f376..0477649d45 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 7f34379e59..0c228907fb 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 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 7ff6e2e914..0000000000 --- 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 f529c4d671..b693f5c8ab 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 286db03466..5ff279f6c0 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 World world(String name, int moons) { public World findWorldNamed(String name) { return worldRepository.findByPropertyValue("name", name); } - - @Override - public Iterable findWorldsWithMoons(int moonCount) { - return worldRepository.findAllByPropertyValue("moon-index", "moons", moonCount); - } - + @Override - public Iterable exploreWorldsBeyond(World homeWorld) { - TraversalDescription traversal = Traversal.description().relationships(withName(REACHABLE_BY_ROCKET), Direction.OUTGOING); - return worldRepository.findAllByTraversal(homeWorld, traversal); + public Iterable findAllWorlds() { + return worldRepository.findAll(); } - } 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 8b2ddf6ad3..0000000000 --- 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 55b70bfc5d..0000000000 --- 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 571cfd42a9..0000000000 --- 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()); - } -} From 2fd22d77f6342de528a8a1916e68546a99b64afa Mon Sep 17 00:00:00 2001 From: loki2302 Date: Tue, 15 May 2012 12:42:10 -0700 Subject: [PATCH 3/8] DATAGRAPH-175: refactored hello-worlds example to make it easier to interprete --- .../data/neo4j/examples/hellograph/App.java | 46 +++---- .../hellograph/MyWorldRepository.java | 19 +-- .../hellograph/RelationshipTypes.java | 3 +- .../data/neo4j/examples/hellograph/World.java | 41 ++---- .../examples/hellograph/WorldCounter.java | 20 --- .../examples/hellograph/WorldRepository.java | 3 +- .../hellograph/WorldRepositoryImpl.java | 82 ++++-------- .../examples/hellograph/WorldCounterTest.java | 47 ------- .../hellograph/WorldRepositoryTest.java | 121 ------------------ .../neo4j/examples/hellograph/WorldTest.java | 53 -------- 10 files changed, 66 insertions(+), 369 deletions(-) delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldCounter.java delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldCounterTest.java delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryTest.java delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldTest.java diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java index 064d864bdb..11fe901cb2 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java @@ -3,34 +3,28 @@ 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 static void main( String[] args ) - { - - ConfigurableApplicationContext applicationContext = - new ClassPathXmlApplicationContext( "/spring/helloWorldContext.xml"); - - WorldRepositoryImpl galaxy = applicationContext.getBean(WorldRepositoryImpl.class); - - galaxy.makeSomeWorlds(); - - World homeWorld = galaxy.findWorldNamed( "Earth" ); - System.out.println( "At home on: " + homeWorld ); - - Iterable worldsBeyond = galaxy.exploreWorldsBeyond( homeWorld ); - for (World world : worldsBeyond) { - System.out.println( "found worlds beyond: " + world ); +public class App { + 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); + + 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/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java index ead3954a65..1c17e10c11 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java @@ -2,22 +2,11 @@ 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/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java index 6f66f5f376..0477649d45 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java +++ b/spring-data-neo4j-examples/hello-worlds/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/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java index 6e02e1b008..0e54c14285 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java @@ -1,72 +1,55 @@ package org.springframework.data.neo4j.examples.hellograph; - import org.neo4j.graphdb.Direction; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.neo4j.annotation.Fetch; import org.springframework.data.neo4j.annotation.GraphId; import org.springframework.data.neo4j.annotation.Indexed; import org.springframework.data.neo4j.annotation.NodeEntity; import org.springframework.data.neo4j.annotation.RelatedTo; -import org.springframework.data.neo4j.support.Neo4jTemplate; - import java.util.HashSet; import java.util.Set; -/** - * A Spring Data Neo4j enhanced World entity. - *

- * This is the initial POJO in the Universe. - */ @NodeEntity -public class World -{ - @GraphId Long id; +public class World { + @GraphId + Long id; @Indexed private String name; - @Indexed(indexName = "moon-index") private int moons; @Fetch - @RelatedTo(type = "REACHABLE_BY_ROCKET", direction = Direction.BOTH) + @RelatedTo(type = RelationshipTypes.REACHABLE_BY_ROCKET, direction = Direction.BOTH) Set reachableByRocket = new HashSet(); - 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() - { + public String toString() { return String.format("World{name='%s, moons=%d}", name, moons); } - public void addRocketRouteTo( World otherWorld ) - { + public void addRocketRouteTo(World otherWorld) { reachableByRocket.add(otherWorld); } - public boolean canBeReachedFrom( World otherWorld ) - { - return reachableByRocket.contains( otherWorld ); + public Iterable getReachableWorlds() { + return reachableByRocket; } @Override diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldCounter.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldCounter.java deleted file mode 100644 index 7ff6e2e914..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/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/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java index f529c4d671..b693f5c8ab 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java +++ b/spring-data-neo4j-examples/hello-worlds/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/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java index 1abe82a033..3ee1aaeab3 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java @@ -1,65 +1,46 @@ 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); + public void makeSureGalaxyIsNotEmpty() { + // Solar worlds + resolveWorld("Mercury", 0); + resolveWorld("Venus", 0); - World mars = world("Mars", 2); - earth.addRocketRouteTo(mars); - worldRepository.save(earth); + 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) { - - World createdWorld = findWorldNamed(name); - if (createdWorld == null) { - createdWorld = new World(name, moons); - worldRepository.save(createdWorld); - } + + private World resolveWorld(String name, int moons) { + World createdWorld = findWorldNamed(name); + if (createdWorld == null) { + createdWorld = new World(name, moons); + worldRepository.save(createdWorld); + } return createdWorld; } @@ -67,16 +48,9 @@ public World world(String name, int moons) { public World findWorldNamed(String name) { return worldRepository.findByPropertyValue("name", name); } - - @Override - public Iterable findWorldsWithMoons(int moonCount) { - return worldRepository.findAllByPropertyValue("moon-index", "moons", moonCount); - } - + @Override - public Iterable exploreWorldsBeyond(World homeWorld) { - TraversalDescription traversal = Traversal.description().relationships(withName(REACHABLE_BY_ROCKET), Direction.OUTGOING); - return worldRepository.findAllByTraversal(homeWorld, traversal); + public Iterable findAllWorlds() { + return worldRepository.findAll(); } - } diff --git a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldCounterTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldCounterTest.java deleted file mode 100644 index 61d0d35b15..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldCounterTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.junit.Ignore; -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 -@Ignore("TODO ABK") -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/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryTest.java deleted file mode 100644 index e9d0c88839..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryTest.java +++ /dev/null @@ -1,121 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.junit.Ignore; -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.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) template.count(World.class)); - World myWorld = galaxy.save(new World( "mine", 0 )); - assertEquals(1, (long) template.count(World.class)); - World foundWorld = galaxy.findOne(myWorld.id); - assertEquals(myWorld.getName(), foundWorld.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 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" ); - System.err.println(earth.reachableByRocket); - System.err.println(mars.reachableByRocket); - - assertTrue("mars can be reached from earth", mars.canBeReachedFrom( earth ) ); - assertTrue("earth can be reached from mars", earth.canBeReachedFrom( mars ) ); - } - -} diff --git a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldTest.java deleted file mode 100644 index 3c4a856b80..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.junit.Ignore; -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 -@Ignore("TODO ABK") -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()); - } -} From e29477bad04669da7bca497660968194b86e8104 Mon Sep 17 00:00:00 2001 From: agibalov Date: Thu, 17 May 2012 11:02:02 +0400 Subject: [PATCH 4/8] DATAGRAPH-175: added tests, replaced embarassing repository-related classes with GalaxyService --- .../examples/hellograph/GalaxyMapper.java | 27 ++++++++ .../examples/hellograph/GalaxyService.java | 64 ++++++++++++++++++ .../neo4j/examples/hellograph/WorldDto.java | 30 +++++++++ .../hellograph/GalaxyServiceTest.java | 58 +++++++++++++++++ .../examples/hellograph/GalaxyMapper.java | 27 ++++++++ .../examples/hellograph/GalaxyService.java | 65 +++++++++++++++++++ .../neo4j/examples/hellograph/WorldDto.java | 30 +++++++++ .../hellograph/GalaxyServiceTest.java | 58 +++++++++++++++++ 8 files changed, 359 insertions(+) create mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java create mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java create mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java create mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java create mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java create mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java create mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java create mode 100644 spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java new file mode 100644 index 0000000000..97cdb12ce7 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java @@ -0,0 +1,27 @@ +package org.springframework.data.neo4j.examples.hellograph; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Service; + +@Service +public class GalaxyMapper { + public WorldDto worldDtoFromWorld(World world) { + if(world == null) { + return null; + } + + return new WorldDto(world.getId(), world.getName(), world.getMoons()); + } + + public List worldDtosFromWorlds(Iterable worlds) { + List worldDtos = new ArrayList(); + for(World world : worlds) { + WorldDto worldDto = worldDtoFromWorld(world); + worldDtos.add(worldDto); + } + + return worldDtos; + } +} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java new file mode 100644 index 0000000000..c443d89aa5 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java @@ -0,0 +1,64 @@ +package org.springframework.data.neo4j.examples.hellograph; + + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class GalaxyService { + @Autowired + private WorldRepository worldRepository; + + @Autowired + private GalaxyMapper mapper; + + @Transactional + public void makeSureGalaxyIsNotEmpty() { + // Solar worlds + resolveWorld("Mercury", 0); + resolveWorld("Venus", 0); + + World earth = resolveWorld("Earth", 1); + World mars = resolveWorld("Mars", 2); + mars.addRocketRouteTo(earth); + + resolveWorld("Jupiter", 63); + resolveWorld("Saturn", 62); + resolveWorld("Uranus", 27); + resolveWorld("Neptune", 13); + + // Norse worlds + resolveWorld("Alfheimr", 0); + resolveWorld("Midgard", 1); + resolveWorld("Muspellheim", 2); + resolveWorld("Asgard", 63); + resolveWorld("Hel", 62); + } + + private World resolveWorld(String name, int moons) { + World createdWorld = worldRepository.findByPropertyValue("name", name); + if (createdWorld == null) { + createdWorld = new World(name, moons); + createdWorld = worldRepository.save(createdWorld); + } + return createdWorld; + } + + public WorldDto findWorldNamed(String name) { + World world = worldRepository.findByPropertyValue("name", name); + return mapper.worldDtoFromWorld(world); + } + + public List findReachableWorlds(Long worldId) { + World world = worldRepository.findOne(worldId); + return mapper.worldDtosFromWorlds(world.getReachableWorlds()); + } + + public List findAllWorlds() { + Iterable worlds = worldRepository.findAll(); + return mapper.worldDtosFromWorlds(worlds); + } +} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java new file mode 100644 index 0000000000..9860fe2b82 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java @@ -0,0 +1,30 @@ +package org.springframework.data.neo4j.examples.hellograph; + +public class WorldDto { + private final Long id; + private final String name; + private final int moons; + + public WorldDto(Long id, String name, int moons) { + this.id = id; + this.name = name; + this.moons = moons; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public int getMoons() { + return moons; + } + + @Override + public String toString() { + return String.format("WorldDto{id=%d, name='%s', moons=%d}", id, name, moons); + } +} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java new file mode 100644 index 0000000000..a8b9ed43bd --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java @@ -0,0 +1,58 @@ +package org.springframework.data.neo4j.examples.hellograph; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.Before; +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.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml") +@RunWith(SpringJUnit4ClassRunner.class) +public class GalaxyServiceTest { + + @Autowired + private GalaxyService galaxyService; + + @Autowired + private Neo4jTemplate template; + + @Before + public void cleanUpGraph() { + Neo4jHelper.cleanDb(template); + } + + @Test + public void galaxyShouldBeEmptyUntilExplicitlyPopulated() { + assertTrue(galaxyService.findAllWorlds().isEmpty()); + } + + @Test + public void populatedGalaxyShouldHaveEarth() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto earth = galaxyService.findWorldNamed("Earth"); + assertNotNull(earth); + assertEquals("Earth", earth.getName()); + } + + @Test + public void earthShouldHaveOnlyOneReachableWorld() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto earth = galaxyService.findWorldNamed("Earth"); + List reachableWorlds = galaxyService.findReachableWorlds(earth.getId()); + assertEquals(1, reachableWorlds.size()); + } + + @Test + public void populatedGalaxyShouldNotHaveKrypton() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto planetPopsicle = galaxyService.findWorldNamed("Krypton"); + assertNull(planetPopsicle); + } +} diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java new file mode 100644 index 0000000000..97cdb12ce7 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java @@ -0,0 +1,27 @@ +package org.springframework.data.neo4j.examples.hellograph; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Service; + +@Service +public class GalaxyMapper { + public WorldDto worldDtoFromWorld(World world) { + if(world == null) { + return null; + } + + return new WorldDto(world.getId(), world.getName(), world.getMoons()); + } + + public List worldDtosFromWorlds(Iterable worlds) { + List worldDtos = new ArrayList(); + for(World world : worlds) { + WorldDto worldDto = worldDtoFromWorld(world); + worldDtos.add(worldDto); + } + + return worldDtos; + } +} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java new file mode 100644 index 0000000000..a7fa425b75 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java @@ -0,0 +1,65 @@ +package org.springframework.data.neo4j.examples.hellograph; + + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class GalaxyService { + @Autowired + private WorldRepository worldRepository; + + @Autowired + private GalaxyMapper mapper; + + @Transactional + public void makeSureGalaxyIsNotEmpty() { + // Solar worlds + resolveWorld("Mercury", 0); + resolveWorld("Venus", 0); + + World earth = resolveWorld("Earth", 1); + World mars = resolveWorld("Mars", 2); + mars.addRocketRouteTo(earth); + worldRepository.save(mars); + + resolveWorld("Jupiter", 63); + resolveWorld("Saturn", 62); + resolveWorld("Uranus", 27); + resolveWorld("Neptune", 13); + + // Norse worlds + resolveWorld("Alfheimr", 0); + resolveWorld("Midgard", 1); + resolveWorld("Muspellheim", 2); + resolveWorld("Asgard", 63); + resolveWorld("Hel", 62); + } + + private World resolveWorld(String name, int moons) { + World createdWorld = worldRepository.findByPropertyValue("name", name); + if (createdWorld == null) { + createdWorld = new World(name, moons); + createdWorld = worldRepository.save(createdWorld); + } + return createdWorld; + } + + public WorldDto findWorldNamed(String name) { + World world = worldRepository.findByPropertyValue("name", name); + return mapper.worldDtoFromWorld(world); + } + + public List findReachableWorlds(Long worldId) { + World world = worldRepository.findOne(worldId); + return mapper.worldDtosFromWorlds(world.getReachableWorlds()); + } + + public List findAllWorlds() { + Iterable worlds = worldRepository.findAll(); + return mapper.worldDtosFromWorlds(worlds); + } +} diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java new file mode 100644 index 0000000000..9860fe2b82 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java @@ -0,0 +1,30 @@ +package org.springframework.data.neo4j.examples.hellograph; + +public class WorldDto { + private final Long id; + private final String name; + private final int moons; + + public WorldDto(Long id, String name, int moons) { + this.id = id; + this.name = name; + this.moons = moons; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public int getMoons() { + return moons; + } + + @Override + public String toString() { + return String.format("WorldDto{id=%d, name='%s', moons=%d}", id, name, moons); + } +} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java new file mode 100644 index 0000000000..a8b9ed43bd --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java @@ -0,0 +1,58 @@ +package org.springframework.data.neo4j.examples.hellograph; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.Before; +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.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml") +@RunWith(SpringJUnit4ClassRunner.class) +public class GalaxyServiceTest { + + @Autowired + private GalaxyService galaxyService; + + @Autowired + private Neo4jTemplate template; + + @Before + public void cleanUpGraph() { + Neo4jHelper.cleanDb(template); + } + + @Test + public void galaxyShouldBeEmptyUntilExplicitlyPopulated() { + assertTrue(galaxyService.findAllWorlds().isEmpty()); + } + + @Test + public void populatedGalaxyShouldHaveEarth() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto earth = galaxyService.findWorldNamed("Earth"); + assertNotNull(earth); + assertEquals("Earth", earth.getName()); + } + + @Test + public void earthShouldHaveOnlyOneReachableWorld() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto earth = galaxyService.findWorldNamed("Earth"); + List reachableWorlds = galaxyService.findReachableWorlds(earth.getId()); + assertEquals(1, reachableWorlds.size()); + } + + @Test + public void populatedGalaxyShouldNotHaveKrypton() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto planetPopsicle = galaxyService.findWorldNamed("Krypton"); + assertNull(planetPopsicle); + } +} From 1e84418f92f41e4a233e502208e66a12f48a1b8e Mon Sep 17 00:00:00 2001 From: Andrey Date: Sat, 2 Jun 2012 22:02:55 +0400 Subject: [PATCH 5/8] Removed hello-worlds-aspects project, implemented 2 profiles for hello-worlds: aspects and plain. The code doesn't work as expected. Commiting just for back-up --- pom.xml | 1 - .../hello-worlds-aspects/.gitignore | 11 - .../hello-worlds-aspects/README.md | 14 - .../hello-worlds-aspects/build.gradle | 27 - .../hello-worlds-aspects/build.xml | 75 --- .../hello-worlds-aspects/pom.xml | 210 -------- .../settings/install-ivy.xml | 29 -- .../hello-worlds-aspects/settings/ivy.xml | 14 - .../settings/ivysettings.xml | 23 - .../hello-worlds-aspects/settings/path.xml | 30 -- .../data/neo4j/examples/hellograph/App.java | 30 -- .../examples/hellograph/GalaxyMapper.java | 27 - .../examples/hellograph/GalaxyService.java | 64 --- .../hellograph/MyWorldRepository.java | 12 - .../hellograph/RelationshipTypes.java | 5 - .../data/neo4j/examples/hellograph/World.java | 49 -- .../neo4j/examples/hellograph/WorldDto.java | 30 -- .../examples/hellograph/WorldRepository.java | 10 - .../hellograph/WorldRepositoryImpl.java | 55 -- .../src/main/resources/log4j.properties | 7 - .../hellograph/GalaxyServiceTest.java | 58 --- .../hello-worlds/pom.xml | 472 +++++++++++------- .../data/neo4j/examples/hellograph/App.java | 10 +- .../examples/hellograph/GalaxyService.java | 5 +- .../hellograph/MyWorldRepository.java | 12 - .../data/neo4j/examples/hellograph/World.java | 10 +- .../examples/hellograph/WorldRepository.java | 2 +- .../hellograph/WorldRepositoryImpl.java | 56 --- .../spring/aspects}/helloWorldContext.xml | 14 +- .../resources/spring/helloWorldContext.xml | 19 - .../spring/plain/helloWorldContext.xml | 25 + .../hellograph/GalaxyServiceTest.java | 3 +- 32 files changed, 350 insertions(+), 1059 deletions(-) delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/.gitignore delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/README.md delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/build.gradle delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/build.xml delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/pom.xml delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/settings/install-ivy.xml delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/settings/ivy.xml delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/settings/ivysettings.xml delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/settings/path.xml delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/log4j.properties delete mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java rename spring-data-neo4j-examples/{hello-worlds-aspects/src/main/resources/spring => hello-worlds/src/main/resources/spring/aspects}/helloWorldContext.xml (51%) delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext.xml create mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/plain/helloWorldContext.xml diff --git a/pom.xml b/pom.xml index f5d07710e3..1ade39698d 100644 --- a/pom.xml +++ b/pom.xml @@ -156,7 +156,6 @@ examples spring-data-neo4j-examples/hello-worlds - spring-data-neo4j-examples/hello-worlds-aspects spring-data-neo4j-examples/imdb spring-data-neo4j-examples/cineasts spring-data-neo4j-examples/cineasts-aspects diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/.gitignore b/spring-data-neo4j-examples/hello-worlds-aspects/.gitignore deleted file mode 100644 index f092f6270e..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/.gitignore +++ /dev/null @@ -1,11 +0,0 @@ -lib -.ivy -.gradle -target -*.ipr -*.iml -*.iws -.classpath -.project -.settings - diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/README.md b/spring-data-neo4j-examples/hello-worlds-aspects/README.md deleted file mode 100644 index 70d44f7590..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/README.md +++ /dev/null @@ -1,14 +0,0 @@ -Hello Worlds -============ - -A simple Spring Data Neo4j example with just enough code to -do something that works. - -This version of the project takes advantage of the niceties -provided by spring-data-neo4j-aspects, which enhances the -Plain Old Java Objects into Deluxe Aspect Java Objects. - -Build and Run -------------- - -`mvn clean package exec:java` diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/build.gradle b/spring-data-neo4j-examples/hello-worlds-aspects/build.gradle deleted file mode 100644 index 7ffdbf9b75..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/build.gradle +++ /dev/null @@ -1,27 +0,0 @@ -sourceCompatibility = 1.6 -targetCompatibility = 1.6 - -apply plugin: 'idea' -apply plugin: 'eclipse' - - -springVersion = "3.0.7.RELEASE" -springDataNeo4jVersion = "2.0.0.RELEASE" -aspectjVersion = "1.6.12" - -apply from:'https://github.com/SpringSource/spring-data-neo4j/raw/master/build/gradle/springdataneo4j.gradle' - -configurations { - runtime - testCompile -} -repositories { - mavenCentral() - mavenLocal() - mavenRepo urls: "http://maven.springframework.org/release" -} - - -dependencies{ - testCompile group:"junit", name: "junit", version: "4.8" -} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/build.xml b/spring-data-neo4j-examples/hello-worlds-aspects/build.xml deleted file mode 100644 index 60a0d7f3dd..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/build.xml +++ /dev/null @@ -1,75 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/pom.xml b/spring-data-neo4j-examples/hello-worlds-aspects/pom.xml deleted file mode 100644 index a4808f42a2..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/pom.xml +++ /dev/null @@ -1,210 +0,0 @@ - - 4.0.0 - - org.springframework.data.examples - spring-data-neo4j-hello-worlds-aspects - 2.1.0.BUILD-SNAPSHOT - jar - - Spring Data Neo4j hello-worlds Aspects - - - UTF-8 - 3.0.7.RELEASE - 1.6.12 - 1.6.1 - ${project.version} - - - - - spring-maven-release - Spring Maven Release Repository - http://maven.springframework.org/release - - - spring-maven-snapshot - Spring Maven Snapshot Repository - - true - - http://maven.springframework.org/snapshot - - - spring-maven-milestone - Spring Maven Milestone Repository - http://maven.springframework.org/milestone - - - neo4j-release-repository - Neo4j Maven 2 release repository - http://m2.neo4j.org/releases - - true - - - false - - - - neo4j-snapshot-repository - Neo4j Maven 2 snapshot repository - http://m2.neo4j.org/snapshots - - true - - - false - - - - - - - spring-maven-release - Spring Maven Release Repository - http://maven.springframework.org/release - - - spring-maven-milestone - Spring Maven Milestone Repository - http://maven.springframework.org/milestone - - - - - - junit - junit - 4.8.1 - test - - - org.springframework - spring-test - ${spring.version} - test - - - commons-logging - commons-logging - - - - - org.springframework.data - spring-data-neo4j-aspects - ${spring-data-neo4j.version} - - - cglib - cglib-nodep - 2.2 - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.1 - - 1.6 - 1.6 - - - - - - org.codehaus.mojo - aspectj-maven-plugin - 1.2 - - true - - - org.springframework - spring-aspects - - - org.springframework.data - spring-data-neo4j-aspects - - - 1.6 - 1.6 - - - - - compile - test-compile - - - - - - org.aspectj - aspectjrt - ${aspectj.version} - - - org.aspectj - aspectjtools - ${aspectj.version} - - - - - - - org.apache.maven.plugins - maven-eclipse-plugin - 2.8 - - - 1.5 - - org.eclipse.ajdt.ui.ajnature - org.eclipse.jdt.core.javanature - org.springframework.ide.eclipse.core.springnature - - - - - - - org.apache.maven.plugins - maven-idea-plugin - 2.2 - - true - true - - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.2 - - - - exec - - - - - org.springframework.data.neo4j.examples.hellograph.App - - - - - - diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/settings/install-ivy.xml b/spring-data-neo4j-examples/hello-worlds-aspects/settings/install-ivy.xml deleted file mode 100644 index 314d20d9a8..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/settings/install-ivy.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/settings/ivy.xml b/spring-data-neo4j-examples/hello-worlds-aspects/settings/ivy.xml deleted file mode 100644 index 5fb5f95e13..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/settings/ivy.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/settings/ivysettings.xml b/spring-data-neo4j-examples/hello-worlds-aspects/settings/ivysettings.xml deleted file mode 100644 index 704bb0fbc8..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/settings/ivysettings.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/settings/path.xml b/spring-data-neo4j-examples/hello-worlds-aspects/settings/path.xml deleted file mode 100644 index 3d49c706bc..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/settings/path.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 deleted file mode 100644 index 11fe901cb2..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class App { - 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); - - 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/GalaxyMapper.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java deleted file mode 100644 index 97cdb12ce7..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import java.util.ArrayList; -import java.util.List; - -import org.springframework.stereotype.Service; - -@Service -public class GalaxyMapper { - public WorldDto worldDtoFromWorld(World world) { - if(world == null) { - return null; - } - - return new WorldDto(world.getId(), world.getName(), world.getMoons()); - } - - public List worldDtosFromWorlds(Iterable worlds) { - List worldDtos = new ArrayList(); - for(World world : worlds) { - WorldDto worldDto = worldDtoFromWorld(world); - worldDtos.add(worldDto); - } - - return worldDtos; - } -} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java deleted file mode 100644 index c443d89aa5..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -public class GalaxyService { - @Autowired - private WorldRepository worldRepository; - - @Autowired - private GalaxyMapper mapper; - - @Transactional - public void makeSureGalaxyIsNotEmpty() { - // Solar worlds - resolveWorld("Mercury", 0); - resolveWorld("Venus", 0); - - World earth = resolveWorld("Earth", 1); - World mars = resolveWorld("Mars", 2); - mars.addRocketRouteTo(earth); - - resolveWorld("Jupiter", 63); - resolveWorld("Saturn", 62); - resolveWorld("Uranus", 27); - resolveWorld("Neptune", 13); - - // Norse worlds - resolveWorld("Alfheimr", 0); - resolveWorld("Midgard", 1); - resolveWorld("Muspellheim", 2); - resolveWorld("Asgard", 63); - resolveWorld("Hel", 62); - } - - private World resolveWorld(String name, int moons) { - World createdWorld = worldRepository.findByPropertyValue("name", name); - if (createdWorld == null) { - createdWorld = new World(name, moons); - createdWorld = worldRepository.save(createdWorld); - } - return createdWorld; - } - - public WorldDto findWorldNamed(String name) { - World world = worldRepository.findByPropertyValue("name", name); - return mapper.worldDtoFromWorld(world); - } - - public List findReachableWorlds(Long worldId) { - World world = worldRepository.findOne(worldId); - return mapper.worldDtosFromWorlds(world.getReachableWorlds()); - } - - public List findAllWorlds() { - Iterable worlds = worldRepository.findAll(); - return mapper.worldDtosFromWorlds(worlds); - } -} 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 deleted file mode 100644 index 1c17e10c11..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.springframework.transaction.annotation.Transactional; - -public interface MyWorldRepository { - @Transactional - void makeSureGalaxyIsNotEmpty(); - - World findWorldNamed(String name); - - 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 deleted file mode 100644 index 0477649d45..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -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 deleted file mode 100644 index 0c228907fb..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - - -import org.neo4j.graphdb.Direction; -import org.springframework.data.neo4j.annotation.Indexed; -import org.springframework.data.neo4j.annotation.NodeEntity; -import org.springframework.data.neo4j.annotation.RelatedTo; - -import java.util.Set; - -@NodeEntity -public class World { - @Indexed - private String name; - - private int moons; - - @RelatedTo(type = RelationshipTypes.REACHABLE_BY_ROCKET, elementClass = World.class, direction = Direction.BOTH) - private Set reachableByRocket; - - public World(String name, int moons) { - this.name = name; - this.moons = moons; - } - - public World() { - } - - public String getName() { - return name; - } - - public int getMoons() { - return moons; - } - - @Override - 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 Iterable getReachableWorlds() { - return reachableByRocket; - } -} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java deleted file mode 100644 index 9860fe2b82..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -public class WorldDto { - private final Long id; - private final String name; - private final int moons; - - public WorldDto(Long id, String name, int moons) { - this.id = id; - this.name = name; - this.moons = moons; - } - - public Long getId() { - return id; - } - - public String getName() { - return name; - } - - public int getMoons() { - return moons; - } - - @Override - public String toString() { - return String.format("WorldDto{id=%d, name='%s', moons=%d}", id, name, moons); - } -} \ No newline at end of file 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 deleted file mode 100644 index b693f5c8ab..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.springframework.data.neo4j.repository.GraphRepository; - -/** - * @author mh - * @since 01.04.11 - */ -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 deleted file mode 100644 index 5ff279f6c0..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.transaction.annotation.Transactional; - -/** - * Spring Data Neo4j backed application context for Worlds. - */ -public class WorldRepositoryImpl implements MyWorldRepository { - - @Autowired - private WorldRepository worldRepository; - - @Override - @Transactional - public void makeSureGalaxyIsNotEmpty() { - // Solar worlds - resolveWorld("Mercury", 0); - resolveWorld("Venus", 0); - - World earth = resolveWorld("Earth", 1); - World mars = resolveWorld("Mars", 2); - mars.addRocketRouteTo(earth); - - resolveWorld("Jupiter", 63); - resolveWorld("Saturn", 62); - resolveWorld("Uranus", 27); - resolveWorld("Neptune", 13); - - // Norse worlds - resolveWorld("Alfheimr", 0); - resolveWorld("Midgard", 1); - resolveWorld("Muspellheim", 2); - resolveWorld("Asgard", 63); - resolveWorld("Hel", 62); - } - - private World resolveWorld(String name, int moons) { - World createdWorld = findWorldNamed(name); - if (createdWorld == null) { - createdWorld = new World(name, moons).persist(); - } - return createdWorld; - } - - @Override - public World findWorldNamed(String name) { - return worldRepository.findByPropertyValue("name", name); - } - - @Override - public Iterable findAllWorlds() { - return worldRepository.findAll(); - } -} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/log4j.properties b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/log4j.properties deleted file mode 100644 index beb86feab7..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/log4j.properties +++ /dev/null @@ -1,7 +0,0 @@ -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout - -# Print the date in ISO 8601 format -log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n - -log4j.rootLogger=ERROR, stdout diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java deleted file mode 100644 index a8b9ed43bd..0000000000 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.junit.Before; -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.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml") -@RunWith(SpringJUnit4ClassRunner.class) -public class GalaxyServiceTest { - - @Autowired - private GalaxyService galaxyService; - - @Autowired - private Neo4jTemplate template; - - @Before - public void cleanUpGraph() { - Neo4jHelper.cleanDb(template); - } - - @Test - public void galaxyShouldBeEmptyUntilExplicitlyPopulated() { - assertTrue(galaxyService.findAllWorlds().isEmpty()); - } - - @Test - public void populatedGalaxyShouldHaveEarth() { - galaxyService.makeSureGalaxyIsNotEmpty(); - WorldDto earth = galaxyService.findWorldNamed("Earth"); - assertNotNull(earth); - assertEquals("Earth", earth.getName()); - } - - @Test - public void earthShouldHaveOnlyOneReachableWorld() { - galaxyService.makeSureGalaxyIsNotEmpty(); - WorldDto earth = galaxyService.findWorldNamed("Earth"); - List reachableWorlds = galaxyService.findReachableWorlds(earth.getId()); - assertEquals(1, reachableWorlds.size()); - } - - @Test - public void populatedGalaxyShouldNotHaveKrypton() { - galaxyService.makeSureGalaxyIsNotEmpty(); - WorldDto planetPopsicle = galaxyService.findWorldNamed("Krypton"); - assertNull(planetPopsicle); - } -} diff --git a/spring-data-neo4j-examples/hello-worlds/pom.xml b/spring-data-neo4j-examples/hello-worlds/pom.xml index 2b7d289f0b..ec02a14e18 100644 --- a/spring-data-neo4j-examples/hello-worlds/pom.xml +++ b/spring-data-neo4j-examples/hello-worlds/pom.xml @@ -1,178 +1,300 @@ - 4.0.0 - - org.springframework.data.examples - spring-data-neo4j-hello-worlds - 2.1.0.BUILD-SNAPSHOT - jar - - Spring Data Neo4j hello-worlds - - - UTF-8 - 3.0.7.RELEASE - 1.6.12 - 1.6.1 - ${project.version} - - - - - spring-maven-release - Spring Maven Release Repository - http://maven.springframework.org/release - - - spring-maven-snapshot - Spring Maven Snapshot Repository - - true - - http://maven.springframework.org/snapshot - - - spring-maven-milestone - Spring Maven Milestone Repository - http://maven.springframework.org/milestone - - - neo4j-release-repository - Neo4j Maven 2 release repository - http://m2.neo4j.org/releases - - true - - - false - - - - neo4j-snapshot-repository - Neo4j Maven 2 snapshot repository - http://m2.neo4j.org/snapshots - - true - - - false - - - - - - - spring-maven-release - Spring Maven Release Repository - http://maven.springframework.org/release - - - spring-maven-milestone - Spring Maven Milestone Repository - http://maven.springframework.org/milestone - - - - - - junit - junit - 4.8.1 - test - - - org.springframework - spring-test - ${spring.version} - test - - - commons-logging - commons-logging - - - - - org.springframework.data - spring-data-neo4j - ${spring-data-neo4j.version} - - - cglib - cglib-nodep - 2.2 - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.1 - - 1.6 - 1.6 - - - - - - org.apache.maven.plugins - maven-idea-plugin - 2.2 - - true - true - - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.2 - - - - exec - - - - - org.springframework.data.neo4j.examples.hellograph.App - - - - - maven-assembly-plugin - - hello-worlds-${project.version} - false - false - - - org.springframework.data.neo4j.examples.hellograph.App - - - - jar-with-dependencies - - - - - make-assembly - package - - single - - - - - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + org.springframework.data.examples + spring-data-neo4j-hello-worlds + 2.1.0.BUILD-SNAPSHOT + jar + + Spring Data Neo4j hello-worlds + + + UTF-8 + 3.0.7.RELEASE + 1.6.12 + 1.6.1 + ${project.version} + + + + + spring-maven-release + Spring Maven Release Repository + http://maven.springframework.org/release + + + spring-maven-snapshot + Spring Maven Snapshot Repository + + true + + http://maven.springframework.org/snapshot + + + spring-maven-milestone + Spring Maven Milestone Repository + http://maven.springframework.org/milestone + + + neo4j-release-repository + Neo4j Maven 2 release repository + http://m2.neo4j.org/releases + + true + + + false + + + + neo4j-snapshot-repository + Neo4j Maven 2 snapshot repository + http://m2.neo4j.org/snapshots + + true + + + false + + + + + + + spring-maven-release + Spring Maven Release Repository + http://maven.springframework.org/release + + + spring-maven-milestone + Spring Maven Milestone Repository + http://maven.springframework.org/milestone + + + + + + junit + junit + 4.8.1 + test + + + org.springframework + spring-test + ${spring.version} + test + + + commons-logging + commons-logging + + + + + cglib + cglib-nodep + 2.2 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.1 + + 1.6 + 1.6 + + + + + + org.apache.maven.plugins + maven-idea-plugin + 2.2 + + true + true + + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2 + + + + exec + + + + + org.springframework.data.neo4j.examples.hellograph.App + + + + + maven-assembly-plugin + + hello-worlds-${project.version} + false + false + + + org.springframework.data.neo4j.examples.hellograph.App + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + maven-resources-plugin + + + initialize + + resources + + + + + + + + + + + plain + + + org.springframework.data + spring-data-neo4j + ${spring-data-neo4j.version} + + + + + + + ${project.basedir}/src/main/resources/spring/plain + ${project.build.directory}/classes/spring + true + + helloWorldContext.xml + + + + + + + aspects + + true + + + + + org.springframework.data + spring-data-neo4j-aspects + ${spring-data-neo4j.version} + + + + + + + + org.codehaus.mojo + aspectj-maven-plugin + 1.2 + + true + + + org.springframework + spring-aspects + + + org.springframework.data + spring-data-neo4j-aspects + + + 1.6 + 1.6 + + + + + compile + test-compile + + + + + + org.aspectj + aspectjrt + ${aspectj.version} + + + org.aspectj + aspectjtools + ${aspectj.version} + + + + + + + org.apache.maven.plugins + maven-eclipse-plugin + 2.8 + + + 1.5 + + org.eclipse.ajdt.ui.ajnature + org.eclipse.jdt.core.javanature + org.springframework.ide.eclipse.core.springnature + + + + + + + ${project.basedir}/src/main/resources/spring/aspects + ${project.build.directory}/classes/spring + true + + helloWorldContext.xml + + + + + + diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java index 11fe901cb2..25e8b1b9fa 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java @@ -8,20 +8,20 @@ public static void main(String[] args) { ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring/helloWorldContext.xml"); - MyWorldRepository galaxy = applicationContext.getBean(WorldRepositoryImpl.class); - galaxy.makeSureGalaxyIsNotEmpty(); + GalaxyService galaxyService = applicationContext.getBean(GalaxyService.class); + galaxyService.makeSureGalaxyIsNotEmpty(); System.out.println("Trying to find the Earth by its name:"); - World earth = galaxy.findWorldNamed("Earth"); + WorldDto earth = galaxyService.findWorldNamed("Earth"); System.out.printf("Found Earth: %s\n", earth); System.out.println("Retrieveing the list of worlds that can be reached from the Earth:"); - for(World world : earth.getReachableWorlds()) { + for(WorldDto world : galaxyService.findReachableWorlds(earth.getId())) { 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()) { + for(WorldDto world : galaxyService.findAllWorlds()) { System.out.printf("There's a world: %s\n", world); } diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java index a7fa425b75..e3a2967c2a 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java @@ -1,6 +1,5 @@ package org.springframework.data.neo4j.examples.hellograph; - import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -25,6 +24,7 @@ public void makeSureGalaxyIsNotEmpty() { World mars = resolveWorld("Mars", 2); mars.addRocketRouteTo(earth); worldRepository.save(mars); + /*worldRepository.save(earth);*/ resolveWorld("Jupiter", 63); resolveWorld("Saturn", 62); @@ -45,11 +45,12 @@ private World resolveWorld(String name, int moons) { createdWorld = new World(name, moons); createdWorld = worldRepository.save(createdWorld); } + return createdWorld; } public WorldDto findWorldNamed(String name) { - World world = worldRepository.findByPropertyValue("name", name); + World world = worldRepository.findByPropertyValue("name", name); return mapper.worldDtoFromWorld(world); } diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java deleted file mode 100644 index 1c17e10c11..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/MyWorldRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.springframework.transaction.annotation.Transactional; - -public interface MyWorldRepository { - @Transactional - void makeSureGalaxyIsNotEmpty(); - - World findWorldNamed(String name); - - Iterable findAllWorlds(); -} diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java index 0e54c14285..5c125e1d6c 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java @@ -12,7 +12,7 @@ @NodeEntity public class World { @GraphId - Long id; + private Long id; @Indexed private String name; @@ -21,7 +21,7 @@ public class World { @Fetch @RelatedTo(type = RelationshipTypes.REACHABLE_BY_ROCKET, direction = Direction.BOTH) - Set reachableByRocket = new HashSet(); + private Set reachableByRocket = new HashSet(); public World(String name, int moons) { this.name = name; @@ -31,6 +31,10 @@ public World(String name, int moons) { public World() { } + public Long getId() { + return id; + } + public String getName() { return name; } @@ -41,7 +45,7 @@ public int getMoons() { @Override public String toString() { - return String.format("World{name='%s, moons=%d}", name, moons); + return String.format("World{name='%s', moons=%d}", name, moons); } public void addRocketRouteTo(World otherWorld) { diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java index b693f5c8ab..859395eca5 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java @@ -6,5 +6,5 @@ * @author mh * @since 01.04.11 */ -public interface WorldRepository extends MyWorldRepository, GraphRepository { +public interface WorldRepository extends GraphRepository { } diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java deleted file mode 100644 index 3ee1aaeab3..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepositoryImpl.java +++ /dev/null @@ -1,56 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.transaction.annotation.Transactional; - -/** - * Spring Data Neo4j backed application context for Worlds. - */ -public class WorldRepositoryImpl implements MyWorldRepository { - - @Autowired - private WorldRepository worldRepository; - - @Override - @Transactional - public void makeSureGalaxyIsNotEmpty() { - // Solar worlds - resolveWorld("Mercury", 0); - resolveWorld("Venus", 0); - - World earth = resolveWorld("Earth", 1); - World mars = resolveWorld("Mars", 2); - mars.addRocketRouteTo(earth); - - resolveWorld("Jupiter", 63); - resolveWorld("Saturn", 62); - resolveWorld("Uranus", 27); - resolveWorld("Neptune", 13); - - // Norse worlds - resolveWorld("Alfheimr", 0); - resolveWorld("Midgard", 1); - resolveWorld("Muspellheim", 2); - resolveWorld("Asgard", 63); - resolveWorld("Hel", 62); - } - - private World resolveWorld(String name, int moons) { - World createdWorld = findWorldNamed(name); - if (createdWorld == null) { - createdWorld = new World(name, moons); - worldRepository.save(createdWorld); - } - return createdWorld; - } - - @Override - public World findWorldNamed(String name) { - return worldRepository.findByPropertyValue("name", name); - } - - @Override - public Iterable findAllWorlds() { - return worldRepository.findAll(); - } -} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/spring/helloWorldContext.xml b/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/aspects/helloWorldContext.xml similarity index 51% rename from spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/spring/helloWorldContext.xml rename to spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/aspects/helloWorldContext.xml index d925ed2bd9..f4d652b9bf 100644 --- a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/resources/spring/helloWorldContext.xml +++ b/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/aspects/helloWorldContext.xml @@ -1,16 +1,22 @@ - + xsi:schemaLocation=" + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context-3.0.xsd + http://www.springframework.org/schema/data/neo4j + http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd + http://www.springframework.org/schema/tx + http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> + diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext.xml b/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext.xml deleted file mode 100644 index e442c40a55..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/helloWorldContext.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/plain/helloWorldContext.xml b/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/plain/helloWorldContext.xml new file mode 100644 index 0000000000..20647b2996 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/main/resources/spring/plain/helloWorldContext.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + diff --git a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java index a8b9ed43bd..eccdc40a18 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java +++ b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java @@ -34,7 +34,7 @@ public void galaxyShouldBeEmptyUntilExplicitlyPopulated() { } @Test - public void populatedGalaxyShouldHaveEarth() { + public void populatedGalaxyShouldHaveEarth() { galaxyService.makeSureGalaxyIsNotEmpty(); WorldDto earth = galaxyService.findWorldNamed("Earth"); assertNotNull(earth); @@ -55,4 +55,5 @@ public void populatedGalaxyShouldNotHaveKrypton() { WorldDto planetPopsicle = galaxyService.findWorldNamed("Krypton"); assertNull(planetPopsicle); } + } From 7b131f631765d269ba906663695e44de23baff17 Mon Sep 17 00:00:00 2001 From: Andrey Date: Sat, 2 Jun 2012 22:28:08 +0400 Subject: [PATCH 6/8] Fixed the behavior. Now works for both aspects and no-aspects. --- .../data/neo4j/examples/hellograph/GalaxyService.java | 3 +-- .../springframework/data/neo4j/examples/hellograph/World.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java index e3a2967c2a..f3e68837e2 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java @@ -24,7 +24,6 @@ public void makeSureGalaxyIsNotEmpty() { World mars = resolveWorld("Mars", 2); mars.addRocketRouteTo(earth); worldRepository.save(mars); - /*worldRepository.save(earth);*/ resolveWorld("Jupiter", 63); resolveWorld("Saturn", 62); @@ -43,7 +42,7 @@ private World resolveWorld(String name, int moons) { World createdWorld = worldRepository.findByPropertyValue("name", name); if (createdWorld == null) { createdWorld = new World(name, moons); - createdWorld = worldRepository.save(createdWorld); + createdWorld = worldRepository.save(createdWorld); } return createdWorld; diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java index 5c125e1d6c..169573988c 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java @@ -6,7 +6,6 @@ import org.springframework.data.neo4j.annotation.Indexed; import org.springframework.data.neo4j.annotation.NodeEntity; import org.springframework.data.neo4j.annotation.RelatedTo; -import java.util.HashSet; import java.util.Set; @NodeEntity @@ -21,7 +20,7 @@ public class World { @Fetch @RelatedTo(type = RelationshipTypes.REACHABLE_BY_ROCKET, direction = Direction.BOTH) - private Set reachableByRocket = new HashSet(); + private Set reachableByRocket; public World(String name, int moons) { this.name = name; From c0fd916780118f9d0c4006afc34f6f87f62aca75 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 7 Jun 2012 22:07:39 +0400 Subject: [PATCH 7/8] Removed DTO and mapping, reverted tests. --- .../data/neo4j/examples/hellograph/App.java | 30 ---- .../examples/hellograph/GalaxyMapper.java | 27 ---- .../examples/hellograph/GalaxyService.java | 133 +++++++++--------- .../hellograph/RelationshipTypes.java | 5 - .../data/neo4j/examples/hellograph/World.java | 25 ++-- .../neo4j/examples/hellograph/WorldDto.java | 30 ---- .../examples/hellograph/WorldRepository.java | 7 +- .../hellograph/GalaxyServiceTest.java | 59 -------- .../neo4j/examples/hellograph/WorldsTest.java | 119 ++++++++++++++++ 9 files changed, 202 insertions(+), 233 deletions(-) delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java delete mode 100644 spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java create mode 100644 spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldsTest.java diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java deleted file mode 100644 index 25e8b1b9fa..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/App.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class App { - public static void main(String[] args) { - ConfigurableApplicationContext applicationContext = - new ClassPathXmlApplicationContext("/spring/helloWorldContext.xml"); - - GalaxyService galaxyService = applicationContext.getBean(GalaxyService.class); - galaxyService.makeSureGalaxyIsNotEmpty(); - - System.out.println("Trying to find the Earth by its name:"); - WorldDto earth = galaxyService.findWorldNamed("Earth"); - System.out.printf("Found Earth: %s\n", earth); - - System.out.println("Retrieveing the list of worlds that can be reached from the Earth:"); - for(WorldDto world : galaxyService.findReachableWorlds(earth.getId())) { - 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(WorldDto world : galaxyService.findAllWorlds()) { - System.out.printf("There's a world: %s\n", world); - } - - applicationContext.close(); - } -} diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java deleted file mode 100644 index 97cdb12ce7..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import java.util.ArrayList; -import java.util.List; - -import org.springframework.stereotype.Service; - -@Service -public class GalaxyMapper { - public WorldDto worldDtoFromWorld(World world) { - if(world == null) { - return null; - } - - return new WorldDto(world.getId(), world.getName(), world.getMoons()); - } - - public List worldDtosFromWorlds(Iterable worlds) { - List worldDtos = new ArrayList(); - for(World world : worlds) { - WorldDto worldDto = worldDtoFromWorld(world); - worldDtos.add(worldDto); - } - - return worldDtos; - } -} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java index f3e68837e2..41f9cf7e39 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java @@ -1,65 +1,68 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -public class GalaxyService { - @Autowired - private WorldRepository worldRepository; - - @Autowired - private GalaxyMapper mapper; - - @Transactional - public void makeSureGalaxyIsNotEmpty() { - // Solar worlds - resolveWorld("Mercury", 0); - resolveWorld("Venus", 0); - - World earth = resolveWorld("Earth", 1); - World mars = resolveWorld("Mars", 2); - mars.addRocketRouteTo(earth); - worldRepository.save(mars); - - resolveWorld("Jupiter", 63); - resolveWorld("Saturn", 62); - resolveWorld("Uranus", 27); - resolveWorld("Neptune", 13); - - // Norse worlds - resolveWorld("Alfheimr", 0); - resolveWorld("Midgard", 1); - resolveWorld("Muspellheim", 2); - resolveWorld("Asgard", 63); - resolveWorld("Hel", 62); - } - - private World resolveWorld(String name, int moons) { - World createdWorld = worldRepository.findByPropertyValue("name", name); - if (createdWorld == null) { - createdWorld = new World(name, moons); - createdWorld = worldRepository.save(createdWorld); - } - - return createdWorld; - } - - public WorldDto findWorldNamed(String name) { - World world = worldRepository.findByPropertyValue("name", name); - return mapper.worldDtoFromWorld(world); - } - - public List findReachableWorlds(Long worldId) { - World world = worldRepository.findOne(worldId); - return mapper.worldDtosFromWorlds(world.getReachableWorlds()); - } - - public List findAllWorlds() { - Iterable worlds = worldRepository.findAll(); - return mapper.worldDtosFromWorlds(worlds); - } -} +package org.springframework.data.neo4j.examples.hellograph; + +import java.util.ArrayList; +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class GalaxyService { + + @Autowired + private WorldRepository worldRepository; + + public long getNumberOfWorlds() { + return worldRepository.count(); + } + + public World createWorld(String name, int moons) { + return worldRepository.save(new World(name, moons)); + } + + public Iterable getAllWorlds() { + return worldRepository.findAll(); + } + + public World findWorldById(Long id) { + return worldRepository.findOne(id); + } + + public World findWorldByName(String name) { + return worldRepository.findByPropertyValue("name", name); + } + + public Iterable findAllByNumberOfMoons(int numberOfMoons) { + return worldRepository.findAllByPropertyValue("moons", numberOfMoons); + } + + public Collection makeSomeWorlds() { + Collection worlds = new ArrayList(); + + // Solar worlds + worlds.add(createWorld("Mercury", 0)); + worlds.add(createWorld("Venus", 0)); + + World earth = createWorld("Earth", 1); + World mars = createWorld("Mars", 2); + mars.addRocketRouteTo(earth); + worldRepository.save(mars); + worlds.add(earth); + worlds.add(mars); + + worlds.add(createWorld("Jupiter", 63)); + worlds.add(createWorld("Saturn", 62)); + worlds.add(createWorld("Uranus", 27)); + worlds.add(createWorld("Neptune", 13)); + + // Norse worlds + worlds.add(createWorld("Alfheimr", 0)); + worlds.add(createWorld("Midgard", 1)); + worlds.add(createWorld("Muspellheim", 2)); + worlds.add(createWorld("Asgard", 63)); + worlds.add(createWorld("Hel", 62)); + + return worlds; + } + +} diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java deleted file mode 100644 index 0477649d45..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/RelationshipTypes.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -public abstract class RelationshipTypes { - public static final String REACHABLE_BY_ROCKET = "REACHABLE_BY_ROCKET"; -} diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java index 169573988c..a4554a2c99 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/World.java @@ -9,17 +9,20 @@ import java.util.Set; @NodeEntity -public class World { +public class World { + private final static String REACHABLE_BY_ROCKET = "REACHABLE_BY_ROCKET"; + @GraphId private Long id; @Indexed private String name; + @Indexed(indexName = "moon-index") private int moons; @Fetch - @RelatedTo(type = RelationshipTypes.REACHABLE_BY_ROCKET, direction = Direction.BOTH) + @RelatedTo(type = REACHABLE_BY_ROCKET, direction = Direction.BOTH) private Set reachableByRocket; public World(String name, int moons) { @@ -40,19 +43,14 @@ public String getName() { public int getMoons() { return moons; - } - - @Override - public String toString() { - return String.format("World{name='%s', moons=%d}", name, moons); - } + } public void addRocketRouteTo(World otherWorld) { reachableByRocket.add(otherWorld); } - - public Iterable getReachableWorlds() { - return reachableByRocket; + + public boolean canBeReachedFrom(World otherWorld) { + return reachableByRocket.contains(otherWorld); } @Override @@ -69,4 +67,9 @@ public boolean equals(Object obj) { if (id == null) return other.id == null; return id.equals(other.id); } + + @Override + public String toString() { + return String.format("World{name='%s', moons=%d}", name, moons); + } } diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java deleted file mode 100644 index 9860fe2b82..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -public class WorldDto { - private final Long id; - private final String name; - private final int moons; - - public WorldDto(Long id, String name, int moons) { - this.id = id; - this.name = name; - this.moons = moons; - } - - public Long getId() { - return id; - } - - public String getName() { - return name; - } - - public int getMoons() { - return moons; - } - - @Override - public String toString() { - return String.format("WorldDto{id=%d, name='%s', moons=%d}", id, name, moons); - } -} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java index 859395eca5..350acbf345 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldRepository.java @@ -2,9 +2,4 @@ import org.springframework.data.neo4j.repository.GraphRepository; -/** - * @author mh - * @since 01.04.11 - */ -public interface WorldRepository extends GraphRepository { -} +public interface WorldRepository extends GraphRepository {} diff --git a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java deleted file mode 100644 index eccdc40a18..0000000000 --- a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.springframework.data.neo4j.examples.hellograph; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.junit.Before; -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.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml") -@RunWith(SpringJUnit4ClassRunner.class) -public class GalaxyServiceTest { - - @Autowired - private GalaxyService galaxyService; - - @Autowired - private Neo4jTemplate template; - - @Before - public void cleanUpGraph() { - Neo4jHelper.cleanDb(template); - } - - @Test - public void galaxyShouldBeEmptyUntilExplicitlyPopulated() { - assertTrue(galaxyService.findAllWorlds().isEmpty()); - } - - @Test - public void populatedGalaxyShouldHaveEarth() { - galaxyService.makeSureGalaxyIsNotEmpty(); - WorldDto earth = galaxyService.findWorldNamed("Earth"); - assertNotNull(earth); - assertEquals("Earth", earth.getName()); - } - - @Test - public void earthShouldHaveOnlyOneReachableWorld() { - galaxyService.makeSureGalaxyIsNotEmpty(); - WorldDto earth = galaxyService.findWorldNamed("Earth"); - List reachableWorlds = galaxyService.findReachableWorlds(earth.getId()); - assertEquals(1, reachableWorlds.size()); - } - - @Test - public void populatedGalaxyShouldNotHaveKrypton() { - galaxyService.makeSureGalaxyIsNotEmpty(); - WorldDto planetPopsicle = galaxyService.findWorldNamed("Krypton"); - assertNull(planetPopsicle); - } - -} diff --git a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldsTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldsTest.java new file mode 100644 index 0000000000..4013403292 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldsTest.java @@ -0,0 +1,119 @@ +package org.springframework.data.neo4j.examples.hellograph; + +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; + +import java.util.Collection; + +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; + +@ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml") +@RunWith(SpringJUnit4ClassRunner.class) +@Transactional +public class WorldsTest { + + @Autowired + private GalaxyService galaxyService; + + @Autowired + private Neo4jTemplate template; + + @Rollback(false) + @BeforeTransaction + public void cleanUpGraph() { + Neo4jHelper.cleanDb(template); + } + + @Test + public void shouldAllowDirectWorldCreation() { + assertEquals(0, galaxyService.getNumberOfWorlds()); + World myWorld = galaxyService.createWorld("mine", 0); + assertEquals(1, galaxyService.getNumberOfWorlds()); + + Iterable foundWorlds = galaxyService.getAllWorlds(); + World mine = foundWorlds.iterator().next(); + assertEquals(myWorld.getName(), mine.getName()); + } + + @Test + public void shouldHaveCorrectNumberOfWorlds() { + galaxyService.makeSomeWorlds(); + assertEquals(13, galaxyService.getNumberOfWorlds()); + } + + @Test + public void shouldFindWorldsById() { + galaxyService.makeSomeWorlds(); + + for(World world : galaxyService.getAllWorlds()) { + World foundWorld = galaxyService.findWorldById(world.getId()); + assertNotNull(foundWorld); + } + } + + @Test + public void shouldFindWorldsByName() { + galaxyService.makeSomeWorlds(); + + for(World world : galaxyService.getAllWorlds()) { + World foundWorld = galaxyService.findWorldByName(world.getName()); + assertNotNull(foundWorld); + } + } + + @Test + public void shouldReachMarsFromEarth() { + galaxyService.makeSomeWorlds(); + + World earth = galaxyService.findWorldByName("Earth"); + World mars = galaxyService.findWorldByName("Mars"); + + assertTrue(mars.canBeReachedFrom(earth)); + assertTrue(earth.canBeReachedFrom(mars)); + } + + @Test + public void shouldFindAllWorlds() { + Collection madeWorlds = galaxyService.makeSomeWorlds(); + Iterable foundWorlds = galaxyService.getAllWorlds(); + + int countOfFoundWorlds = 0; + for(World foundWorld : foundWorlds) { + assertTrue(madeWorlds.contains(foundWorld)); + countOfFoundWorlds++; + } + + assertEquals(madeWorlds.size(), countOfFoundWorlds); + } + + @SuppressWarnings("unchecked") + @Test + public void shouldFindWorldsWith1Moon() { + galaxyService.makeSomeWorlds(); + + for(World worldWithOneMoon : galaxyService.findAllByNumberOfMoons(1)) { + assertThat( + worldWithOneMoon.getName(), + is(anyOf(containsString("Earth"), containsString("Midgard")))); + } + } + + @Test + public void shouldNotFindKrypton() { + galaxyService.makeSomeWorlds(); + World krypton = galaxyService.findWorldByName("Krypton"); + assertNull(krypton); + } + +} From ab572acc959425113329dbfca954de9c483b78e0 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 7 Jun 2012 22:09:49 +0400 Subject: [PATCH 8/8] Renamed tests class. Done now. --- .../hellograph/{WorldsTest.java => GalaxyServiceTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/{WorldsTest.java => GalaxyServiceTest.java} (96%) diff --git a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldsTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java similarity index 96% rename from spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldsTest.java rename to spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java index 4013403292..76cce92ffb 100644 --- a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/WorldsTest.java +++ b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java @@ -21,7 +21,7 @@ @ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml") @RunWith(SpringJUnit4ClassRunner.class) @Transactional -public class WorldsTest { +public class GalaxyServiceTest { @Autowired private GalaxyService galaxyService;