DATAGRAPH-175: refactored hello-worlds example to make it easier to interprete

This commit is contained in:
loki2302
2012-05-15 12:42:10 -07:00
parent 919ac2661d
commit 2fd22d77f6
10 changed files with 64 additions and 367 deletions

View File

@@ -3,34 +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);
galaxy.makeSomeWorlds();
World homeWorld = galaxy.findWorldNamed( "Earth" );
System.out.println( "At home on: " + homeWorld );
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();
}
}

View File

@@ -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();
}

View File

@@ -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";
}

View File

@@ -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.
* <p/>
* 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<World> reachableByRocket = new HashSet<World>();
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<World> getReachableWorlds() {
return reachableByRocket;
}
@Override

View File

@@ -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;
}
}

View File

@@ -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> {
}

View File

@@ -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<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);
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 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);
}
}

View File

@@ -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<String,Integer> result = counter.countMoons(asList(new World("earth", 1)));
assertEquals("earth has one moon",(Integer)1,result.get("earth"));
}
}

View File

@@ -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<World> worlds = galaxy.makeSomeWorlds();
assertNotNull( worlds );
}
@Test
public void shouldHaveCorrectNumberOfWorlds()
{
galaxy.makeSomeWorlds();
assertEquals(13, (long) galaxy.count());
}
@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" );
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 ) );
}
}

View File

@@ -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());
}
}