DATAGRAPH-175: refactored hello-worlds-aspects example to make it easier to interprete
This commit is contained in:
@@ -3,37 +3,28 @@ package org.springframework.data.neo4j.examples.hellograph;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* Hello world(s)!
|
||||
* <p/>
|
||||
* An example application for exploring Spring Data Neo4j.
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext applicationContext =
|
||||
new ClassPathXmlApplicationContext("/spring/helloWorldContext.xml");
|
||||
|
||||
public static void main( String[] args )
|
||||
{
|
||||
|
||||
ConfigurableApplicationContext applicationContext =
|
||||
new ClassPathXmlApplicationContext( "/spring/helloWorldContext.xml");
|
||||
MyWorldRepository galaxy = applicationContext.getBean(WorldRepositoryImpl.class);
|
||||
galaxy.makeSureGalaxyIsNotEmpty();
|
||||
|
||||
System.out.println("Trying to find the Earth by its name:");
|
||||
World earth = galaxy.findWorldNamed("Earth");
|
||||
System.out.printf("Found Earth: %s\n", earth);
|
||||
|
||||
WorldRepositoryImpl galaxy = applicationContext.getBean(WorldRepositoryImpl.class);
|
||||
|
||||
Iterable<World> 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<World> worldsBeyond = galaxy.exploreWorldsBeyond( homeWorld );
|
||||
for (World world : worldsBeyond) {
|
||||
System.out.println( "found worlds beyond: " + world );
|
||||
System.out.println("Retrieveing the list of worlds that can be reached from the Earth:");
|
||||
for(World world : earth.getReachableWorlds()) {
|
||||
System.out.printf("Can travel between %s and %s\n", earth, world);
|
||||
}
|
||||
|
||||
System.out.println("Here's the list of all worlds in the galaxy:");
|
||||
for(World world : galaxy.findAllWorlds()) {
|
||||
System.out.printf("There's a world: %s\n", world);
|
||||
}
|
||||
|
||||
applicationContext.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,22 +2,11 @@ package org.springframework.data.neo4j.examples.hellograph;
|
||||
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author mh
|
||||
* @since 01.04.11
|
||||
*/
|
||||
public interface MyWorldRepository {
|
||||
@Transactional
|
||||
Collection<World> makeSomeWorlds();
|
||||
|
||||
@Transactional
|
||||
World world(String name, int moons);
|
||||
|
||||
void makeSureGalaxyIsNotEmpty();
|
||||
|
||||
World findWorldNamed(String name);
|
||||
|
||||
Iterable<World> findWorldsWithMoons(int moonCount);
|
||||
|
||||
Iterable<World> exploreWorldsBeyond(World homeWorld);
|
||||
|
||||
Iterable<World> findAllWorlds();
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
@@ -8,56 +8,42 @@ import org.springframework.data.neo4j.annotation.RelatedTo;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A Spring Data Neo4j enhanced World entity.
|
||||
* <p/>
|
||||
* 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<World> 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<World> getReachableWorlds() {
|
||||
return reachableByRocket;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String, Integer> countMoons(Iterable<World> worlds) {
|
||||
Map<String, Integer> moons = new HashMap<String, Integer>();
|
||||
for (World world : worlds) {
|
||||
moons.put(world.getName(), world.getMoons());
|
||||
}
|
||||
return moons;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<World>, NamedIndexRepository<World> {
|
||||
public interface WorldRepository extends MyWorldRepository, GraphRepository<World> {
|
||||
}
|
||||
|
||||
@@ -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<World> makeSomeWorlds() {
|
||||
ArrayList<World> newWorlds = new ArrayList<World>();
|
||||
|
||||
// solar worlds
|
||||
newWorlds.add(world("Mercury", 0));
|
||||
newWorlds.add(world("Venus", 0));
|
||||
World earth = world("Earth", 1);
|
||||
newWorlds.add(earth);
|
||||
World mars = world("Mars", 2);
|
||||
public void makeSureGalaxyIsNotEmpty() {
|
||||
// Solar worlds
|
||||
resolveWorld("Mercury", 0);
|
||||
resolveWorld("Venus", 0);
|
||||
|
||||
World earth = resolveWorld("Earth", 1);
|
||||
World mars = resolveWorld("Mars", 2);
|
||||
mars.addRocketRouteTo(earth);
|
||||
newWorlds.add(mars);
|
||||
newWorlds.add(world("Jupiter", 63));
|
||||
newWorlds.add(world("Saturn", 62));
|
||||
newWorlds.add(world("Uranus", 27));
|
||||
newWorlds.add(world("Neptune", 13));
|
||||
|
||||
resolveWorld("Jupiter", 63);
|
||||
resolveWorld("Saturn", 62);
|
||||
resolveWorld("Uranus", 27);
|
||||
resolveWorld("Neptune", 13);
|
||||
|
||||
// Norse worlds
|
||||
newWorlds.add(world("Alfheimr", 0));
|
||||
newWorlds.add(world("Midgard", 1));
|
||||
newWorlds.add(world("Muspellheim", 2));
|
||||
newWorlds.add(world("Asgard", 63));
|
||||
newWorlds.add(world("Hel", 62));
|
||||
|
||||
return newWorlds;
|
||||
resolveWorld("Alfheimr", 0);
|
||||
resolveWorld("Midgard", 1);
|
||||
resolveWorld("Muspellheim", 2);
|
||||
resolveWorld("Asgard", 63);
|
||||
resolveWorld("Hel", 62);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public World world(String name, int moons) {
|
||||
|
||||
private World resolveWorld(String name, int moons) {
|
||||
World createdWorld = findWorldNamed(name);
|
||||
if (createdWorld == null) {
|
||||
createdWorld = new World(name, moons).persist();
|
||||
@@ -62,16 +47,9 @@ public class WorldRepositoryImpl implements MyWorldRepository {
|
||||
public World findWorldNamed(String name) {
|
||||
return worldRepository.findByPropertyValue("name", name);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterable<World> findWorldsWithMoons(int moonCount) {
|
||||
return worldRepository.findAllByPropertyValue("moon-index", "moons", moonCount);
|
||||
public Iterable<World> findAllWorlds() {
|
||||
return worldRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<World> exploreWorldsBeyond(World homeWorld) {
|
||||
TraversalDescription traversal = Traversal.description().relationships(withName(REACHABLE_BY_ROCKET), Direction.OUTGOING);
|
||||
return worldRepository.findAllByTraversal(homeWorld, traversal);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String,Integer> result = counter.countMoons(asList(new World("earth", 1)));
|
||||
assertEquals("earth has one moon",(Integer)1,result.get("earth"));
|
||||
}
|
||||
}
|
||||
@@ -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<World> foundWorlds = galaxy.findAll();
|
||||
World mine = foundWorlds.iterator().next();
|
||||
assertEquals(myWorld.getName(), mine.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPopulateGalaxyWithWorlds()
|
||||
{
|
||||
Iterable<World> 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<World> madeWorlds = galaxy.makeSomeWorlds();
|
||||
Iterable<World> 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 ) );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user