[mud4j-commit] SF.net SVN: mud4j: [158] trunk/mud4j-core/src
Status: Pre-Alpha
Brought to you by:
mpurland
From: <mpu...@us...> - 2007-11-01 00:01:02
|
Revision: 158 http://mud4j.svn.sourceforge.net/mud4j/?rev=158&view=rev Author: mpurland Date: 2007-10-31 17:00:59 -0700 (Wed, 31 Oct 2007) Log Message: ----------- Add scheduling service. Add SpringHelper. Rework tile/movement and link system for tiles. Add DaoTest. Modified Paths: -------------- trunk/mud4j-core/src/java/net/sf/mud4j/scheduling/QuartzSchedulingService.java trunk/mud4j-core/src/java/net/sf/mud4j/scheduling/SchedulingService.java trunk/mud4j-core/src/java/net/sf/mud4j/world/Direction.java trunk/mud4j-core/src/java/net/sf/mud4j/world/Room.java trunk/mud4j-core/src/java/net/sf/mud4j/world/TileLocation.java trunk/mud4j-core/src/resources/spring-config.xml trunk/mud4j-core/src/test/unit/net/sf/mud4j/dao/DaoTest.java trunk/mud4j-core/src/test/unit/net/sf/mud4j/test/Mud4jSpringTestBase.java Added Paths: ----------- trunk/mud4j-core/src/java/net/sf/mud4j/skill/ trunk/mud4j-core/src/java/net/sf/mud4j/startup/ trunk/mud4j-core/src/java/net/sf/mud4j/util/SpringHelper.java trunk/mud4j-core/src/java/net/sf/mud4j/world/MoveableException.java trunk/mud4j-core/src/java/net/sf/mud4j/world/link/ trunk/mud4j-core/src/resources/hsqldb/ trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/ trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/DirectionTest.java trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/RoomTest.java trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/TileLocationTest.java Modified: trunk/mud4j-core/src/java/net/sf/mud4j/scheduling/QuartzSchedulingService.java =================================================================== --- trunk/mud4j-core/src/java/net/sf/mud4j/scheduling/QuartzSchedulingService.java 2007-10-14 21:55:55 UTC (rev 157) +++ trunk/mud4j-core/src/java/net/sf/mud4j/scheduling/QuartzSchedulingService.java 2007-11-01 00:00:59 UTC (rev 158) @@ -1,6 +1,7 @@ package net.sf.mud4j.scheduling; import org.quartz.Scheduler; +import org.quartz.SchedulerException; public class QuartzSchedulingService implements SchedulingService { private Scheduler scheduler; @@ -9,4 +10,17 @@ Scheduler scheduler) { this.scheduler = scheduler; } + + public String[] getGroupNames() throws SchedulerException { + return scheduler.getJobGroupNames(); + } + + public String[] getJobNames(String groupName) throws SchedulerException { + return scheduler.getJobNames(groupName); + //scheduler.getJobDetail(jobName, jobGroup) + } + + public void addJob(String jobName, String groupName) { + //scheduler.addJob(jobDetail, replace) + } } Modified: trunk/mud4j-core/src/java/net/sf/mud4j/scheduling/SchedulingService.java =================================================================== --- trunk/mud4j-core/src/java/net/sf/mud4j/scheduling/SchedulingService.java 2007-10-14 21:55:55 UTC (rev 157) +++ trunk/mud4j-core/src/java/net/sf/mud4j/scheduling/SchedulingService.java 2007-11-01 00:00:59 UTC (rev 158) @@ -1,5 +1,7 @@ package net.sf.mud4j.scheduling; public interface SchedulingService { - + public static final String DEFAULT_JOB_GROUP = "mud4j"; + + void addJob(String jobName, String groupName); } \ No newline at end of file Added: trunk/mud4j-core/src/java/net/sf/mud4j/util/SpringHelper.java =================================================================== --- trunk/mud4j-core/src/java/net/sf/mud4j/util/SpringHelper.java (rev 0) +++ trunk/mud4j-core/src/java/net/sf/mud4j/util/SpringHelper.java 2007-11-01 00:00:59 UTC (rev 158) @@ -0,0 +1,56 @@ +package net.sf.mud4j.util; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +import org.apache.commons.dbcp.BasicDataSource; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SpringHelper { + + public static ClassPathXmlApplicationContext getClassPathXmlApplicationContext(boolean saveData) { + ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( + new String[] { "spring-config.xml" }); + + appContext.registerShutdownHook(); + + if (saveData) { + // Add an application listener for when it is closed + appContext.addApplicationListener(new ApplicationListener() { + @Override + public void onApplicationEvent(ApplicationEvent event) { + if (event instanceof ContextClosedEvent) { + ContextClosedEvent contextClosedEvent = (ContextClosedEvent) event; + ApplicationContext appContext = contextClosedEvent.getApplicationContext(); + + // Tell HSQLDB to shutdown and save data + BasicDataSource basicDataSource = (BasicDataSource) appContext.getBean("hsqlDataSource"); + Connection connection; + try { + connection = basicDataSource.getConnection(); + + String sql = "SHUTDOWN"; + Statement stmt = connection.createStatement(); + stmt.executeUpdate(sql); + stmt.close(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + }); + + // Refresh and add the application listener + appContext.refresh(); + } + + return appContext; + } +} Property changes on: trunk/mud4j-core/src/java/net/sf/mud4j/util/SpringHelper.java ___________________________________________________________________ Name: svn:mime-type + text/plain Modified: trunk/mud4j-core/src/java/net/sf/mud4j/world/Direction.java =================================================================== --- trunk/mud4j-core/src/java/net/sf/mud4j/world/Direction.java 2007-10-14 21:55:55 UTC (rev 157) +++ trunk/mud4j-core/src/java/net/sf/mud4j/world/Direction.java 2007-11-01 00:00:59 UTC (rev 158) @@ -20,89 +20,96 @@ * Direction to define different directions for dynamic direction management to * add west, east, north, south, etc... * + * Provides the link between a pair of {@link Location}. + * * @author Matthew Purland */ -public class Direction { +public enum Direction { - public enum DirectionName { +// public enum DirectionName { NORTH("north"), EAST("east"), WEST("west"), - SOUTH("south"); - + SOUTH("south"), + NORTHEAST("northeast"), + NORTHWEST("northwest"), + SOUTHWEST("southwest"), + SOUTHEAST("southeast"); +// private String directionName; - DirectionName(String directionName) { + Direction(String directionName) { this.directionName = directionName; } public String getDirectionName() { return directionName; } - } - - private DirectionName directionName; - - /** - * Direction is from source to target location. Example. If direction is - * east then Source (east to) --> Target - */ - private TileLocation sourceLocation; - private TileLocation targetLocation; - - /** - * Creates a direction with a name such as "north" or "south". - * - * @param directionName Name of the direction. - * @param sourceLocation Source of the location to the target - * @param targetLocation Target of the location from the source - */ - public Direction(DirectionName directionName, TileLocation sourceLocation, - TileLocation targetLocation) { - this.directionName = directionName; - this.sourceLocation = sourceLocation; - this.targetLocation = targetLocation; - } - - /** - * Get name of direction. - */ - public DirectionName getDirectionName() { - return directionName; - } - - /** - * Get the source location. - * - * @return the source location. - */ - public TileLocation getSourceLocation() { - return sourceLocation; - } - - /** - * Get the target location. - * - * @return the target location. - */ - public TileLocation getTargetLocation() { - return targetLocation; - } - - /** - * Move the given placeable to the target location of the direction. - * - * @param placeable Placeable to place within the target location. - * @throws IllegalArgumentException when the given placeable is not in the - * source location. - */ - public void move(Placeable placeable) throws IllegalArgumentException { - if (!placeable.getCurrentLocation().equals(getSourceLocation())) { - throw new IllegalArgumentException("Could not place placeable (" - + placeable - + ") because it is not currently in the source location."); - } - - placeable.place(targetLocation); - } +// } +// +// private DirectionName directionName; +// +// /** +// * Direction is from source to target location. Example. If direction is +// * east then Source (east to) --> Target +// */ +// private TileLocation sourceLocation; +// private TileLocation targetLocation; +// +// /** +// * Creates a direction with a name such as "north" or "south". +// * +// * @param sourceLocation Source of the location to the target +// * @param targetLocation Target of the location from the source +// */ +// public Direction(DirectionName directionName, TileLocation sourceLocation, +// TileLocation targetLocation) { +// this.directionName = directionName; +// this.sourceLocation = sourceLocation; +// this.targetLocation = targetLocation; +// } +// +// /** +// * Get name of direction. +// */ +// public DirectionName getDirectionName() { +// return directionName; +// } +// +// /** +// * Get the source location. +// * +// * @return the source location. +// */ +// public TileLocation getSourceLocation() { +// return sourceLocation; +// } +// +// /** +// * Get the target location. +// * +// * @return the target location. +// */ +// public TileLocation getTargetLocation() { +// return targetLocation; +// } +// +// /** +// * Move the given placeable to the target location of the direction. +// * +// * @param placeable Placeable to place within the target location. +// * @throws MoveableException when the given placeable is not in the +// * source location. +// */ +// public void move(Placeable placeable) throws MoveableException { +// Location placeableCurrentLocation = placeable.getCurrentLocation(); +// +// if (placeableCurrentLocation == null || !placeableCurrentLocation.equals(getSourceLocation())) { +// throw new MoveableException("Could not place placeable (" +// + placeable +// + ") because it is not currently in the source location."); +// } +// +// placeable.place(targetLocation); +// } } Added: trunk/mud4j-core/src/java/net/sf/mud4j/world/MoveableException.java =================================================================== --- trunk/mud4j-core/src/java/net/sf/mud4j/world/MoveableException.java (rev 0) +++ trunk/mud4j-core/src/java/net/sf/mud4j/world/MoveableException.java 2007-11-01 00:00:59 UTC (rev 158) @@ -0,0 +1,7 @@ +package net.sf.mud4j.world; + +public class MoveableException extends Exception { + public MoveableException(String mesg) { + super(mesg); + } +} Property changes on: trunk/mud4j-core/src/java/net/sf/mud4j/world/MoveableException.java ___________________________________________________________________ Name: svn:mime-type + text/plain Modified: trunk/mud4j-core/src/java/net/sf/mud4j/world/Room.java =================================================================== --- trunk/mud4j-core/src/java/net/sf/mud4j/world/Room.java 2007-10-14 21:55:55 UTC (rev 157) +++ trunk/mud4j-core/src/java/net/sf/mud4j/world/Room.java 2007-11-01 00:00:59 UTC (rev 158) @@ -20,6 +20,7 @@ import java.util.List; import net.sf.mud4j.effect.EffectBehavior; +import net.sf.mud4j.world.link.TileLocationLink; /** * Defines rooms interface as a location that events happen within. @@ -81,27 +82,6 @@ /** * {@inheritDoc} */ - public void addDirection(Direction direction) { - directionList.add(direction); - } - - /** - * {@inheritDoc} - */ - public void removeDirection(Direction direction) { - directionList.remove(direction); - } - - /** - * {@inheritDoc} - */ - public List<Direction> getDirections() { - return directionList; - } - - /** - * {@inheritDoc} - */ public void move(Placeable placeable) { placeable.place(this); } Modified: trunk/mud4j-core/src/java/net/sf/mud4j/world/TileLocation.java =================================================================== --- trunk/mud4j-core/src/java/net/sf/mud4j/world/TileLocation.java 2007-10-14 21:55:55 UTC (rev 157) +++ trunk/mud4j-core/src/java/net/sf/mud4j/world/TileLocation.java 2007-11-01 00:00:59 UTC (rev 158) @@ -17,6 +17,8 @@ import java.util.List; +import net.sf.mud4j.world.link.TileLocationLink; + /** * Location that represents a single tile that contains * directions that link to other locations. @@ -24,21 +26,21 @@ * @author Matthew Purland */ public interface TileLocation extends Location { - /** - * Add a direction to the location. - * @param direction Direction to add to the location. - */ - public void addDirection(Direction direction); - - /** - * Remove a direction from the location. - * @param direction to remove from the location. - */ - public void removeDirection(Direction direction); - - /** - * Get list of directions for the location. - * @return Returns the list implementation for the location. - */ - public List<Direction> getDirections(); +// /** +// * Add a link to the location. +// * @param link Link to add to the location. +// */ +// public void addLocationLink(TileLocationLink link); +// +// /** +// * Remove a link from the location. +// * @param link Link to remove from the location. +// */ +// public void removeLocationLink(TileLocationLink link); +// +// /** +// * Get list of links involving this location. +// * @return Returns the list implementation for the location. +// */ +// public List<TileLocationLink> getLinks(); } Modified: trunk/mud4j-core/src/resources/spring-config.xml =================================================================== --- trunk/mud4j-core/src/resources/spring-config.xml 2007-10-14 21:55:55 UTC (rev 157) +++ trunk/mud4j-core/src/resources/spring-config.xml 2007-11-01 00:00:59 UTC (rev 158) @@ -15,7 +15,7 @@ <value>org.hsqldb.jdbcDriver</value> </property> <property name="url"> - <value>jdbc:hsqldb:file:src/resources/hsqldb/data</value> + <value>jdbc:hsqldb:file:src/resources/hsqldb/mud4j;hsqldb.default_table_type=cached</value> </property> <property name="username"> <value>sa</value> @@ -25,12 +25,27 @@ </property> </bean> + <!-- + <bean id="hsqlDatabase" class="org.springmodules.db.hsqldb.ServerBean" lazy-init="false"> + <property name="dataSource"><ref local="hsqlDataSource"/></property> + <property name="serverProperties"> + <props> + <prop key="server.port">9101</prop> + <prop key="server.database.0">src/resources/hsqldb</prop> + <prop key="server.dbname.0">mud4j</prop> + </props> + </property> + </bean> + --> + + <!-- --> + <!-- Database Property --> <bean id="hsqlHibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> - <prop key="hibernate.hbm2ddl.auto">create-drop</prop> + <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop> <prop key="hibernate.query.substitutions">true 'T', false 'F'</prop> @@ -116,7 +131,8 @@ <map> <entry key="timeout" value="5"/> </map> - </property> + </property> + <property name="group" value="mud4j" /> </bean> <!-- Trigger for ticks --> Modified: trunk/mud4j-core/src/test/unit/net/sf/mud4j/dao/DaoTest.java =================================================================== --- trunk/mud4j-core/src/test/unit/net/sf/mud4j/dao/DaoTest.java 2007-10-14 21:55:55 UTC (rev 157) +++ trunk/mud4j-core/src/test/unit/net/sf/mud4j/dao/DaoTest.java 2007-11-01 00:00:59 UTC (rev 158) @@ -1,28 +1,62 @@ package net.sf.mud4j.dao; +import org.springframework.dao.DataAccessException; + import net.sf.mud4j.account.Account; import net.sf.mud4j.test.Mud4jSpringTestBase; public class DaoTest extends Mud4jSpringTestBase { - - public void testAccountDao() { - AccountDao accountDao = (AccountDao) getApplicationContext().getBean("accountDao"); + + public void testAccountDao() { + AccountDao accountDao = (AccountDao) getApplicationContext().getBean( + "accountDao"); assertNotNull("AccountDao should not be null", accountDao); } - + public void testAccountDaoFind() { - AccountDao accountDao = (AccountDao) getApplicationContext().getBean("accountDao"); - + AccountDao accountDao = (AccountDao) getApplicationContext().getBean( + "accountDao"); + Account account = new Account(); account.setUsername("someusername"); account.setPassword("somepassword"); - + Long accountId = accountDao.save(account); - + Account foundAccount = accountDao.findById(accountId); - assertEquals("Account was not found correctly.", accountId, foundAccount.getAccountId()); - + assertEquals("Account was not found correctly.", accountId, + foundAccount.getAccountId()); + Account foundAccountTwo = accountDao.findByUsername("someusername"); - assertEquals("Account was not found correctly.", accountId, foundAccountTwo.getAccountId()); + assertEquals("Account was not found correctly.", accountId, + foundAccountTwo.getAccountId()); } + + /** + * Test when two rows are inserted that findByUsername returns with + * exception. + * + * @todo Username should be unique + */ + public void testAccountDaoFindDataAccessException() { + AccountDao accountDao = (AccountDao) getApplicationContext().getBean( + "accountDao"); + + Account account = new Account(); + account.setUsername("someusername"); + account.setPassword("somepassword"); + + Long accountId = accountDao.save(account); + Long accountId2 = accountDao.save(account); + + boolean exceptionOccurred = false; + try { + Account foundAccount = accountDao.findByUsername("someusername"); + } + catch (DataAccessException ex) { + exceptionOccurred = true; + } + + assertTrue(exceptionOccurred); + } } Modified: trunk/mud4j-core/src/test/unit/net/sf/mud4j/test/Mud4jSpringTestBase.java =================================================================== --- trunk/mud4j-core/src/test/unit/net/sf/mud4j/test/Mud4jSpringTestBase.java 2007-10-14 21:55:55 UTC (rev 157) +++ trunk/mud4j-core/src/test/unit/net/sf/mud4j/test/Mud4jSpringTestBase.java 2007-11-01 00:00:59 UTC (rev 158) @@ -1,7 +1,13 @@ package net.sf.mud4j.test; + +import java.lang.reflect.Field; +import java.util.Properties; + import junit.framework.TestCase; +import net.sf.mud4j.util.SpringHelper; +import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -9,10 +15,38 @@ private ClassPathXmlApplicationContext appContext; public void setUp() { - appContext = new ClassPathXmlApplicationContext( - new String[] { "spring-config.xml" }); + // Save data and schema + boolean saveData = false; + + appContext = SpringHelper.getClassPathXmlApplicationContext(saveData); + + try { + hackHibernateProperties(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } + public void hackHibernateProperties() throws Exception { + //PropertiesFactoryBean hsqlHibernateProperties = (PropertiesFactoryBean) appContext.getBean("hsqlHibernateProperties"); + Properties hsqlHibernateProperties = (Properties) appContext.getBean("hsqlHibernateProperties"); +// // Hack to get properties to remove hbm2ddl from props +// Field f = PropertiesFactoryBean.class.getDeclaredField("localProperties"); +// f.setAccessible(true); +// Properties properties = (Properties) f.get(hsqlHibernateProperties); + //hsqlHibernateProperties.remove("hibernate.hbm2ddl.auto"); + hsqlHibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-update"); + appContext.refresh(); +// hsqlHibernateProperties.setProperties(properties); +// f.setAccessible(false); + } + + public void tearDown() { + // Tearing down the unit test so close the app context + appContext.close(); + } + public ApplicationContext getApplicationContext() { return appContext; } Added: trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/DirectionTest.java =================================================================== --- trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/DirectionTest.java (rev 0) +++ trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/DirectionTest.java 2007-11-01 00:00:59 UTC (rev 158) @@ -0,0 +1,44 @@ +package net.sf.mud4j.world; + +import junit.framework.TestCase; + +public class DirectionTest extends TestCase { + +// /** +// * Tests when an exception occurs when the placeable is not in the source +// * location. +// */ +// public void testDirectionMoveFailure() { +// Room room1 = new Room(); +// Room room2 = new Room(); +// +// Placeable placeable = new Placeable() { +// private Location currentLocation; +// +// @Override +// public Location getCurrentLocation() { +// return currentLocation; +// } +// +// @Override +// public void place(Location to) { +// this.currentLocation = to; +// } +// +// }; +// +// assertNull("Placeable should be null.", placeable.getCurrentLocation()); +// +// // Direction going north from room1 to room2 +// Direction direction = new Direction(DirectionName.NORTH, room1, room2); +// +// boolean directionMoveFailure = false; +// try { +// direction.move(placeable); +// } catch (MoveableException ex) { +// directionMoveFailure = true; +// } +// +// assertTrue("Failure must occur when placeable is not in source location.", directionMoveFailure); +// } +} Property changes on: trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/DirectionTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Added: trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/RoomTest.java =================================================================== --- trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/RoomTest.java (rev 0) +++ trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/RoomTest.java 2007-11-01 00:00:59 UTC (rev 158) @@ -0,0 +1,32 @@ +package net.sf.mud4j.world; + +import net.sf.mud4j.world.link.LocationLink; +import net.sf.mud4j.world.link.TileLocationLink; + +public class RoomTest { + public void testRoomDirection() { + // Form a square of rooms + // 1-2 + // | | + // 4-3 + Room room1 = new Room(); + Room room2 = new Room(); + Room room3 = new Room(); + Room room4 = new Room(); + + // Link locations room1 and room2 going "east" from room1 + LocationLink link1and2 = new TileLocationLink(room1, room2, Direction.EAST); + + // Link locations room2 and room3 going "south" from room2 + LocationLink link2and3 = new TileLocationLink(room2, room3, Direction.SOUTH); + + // Link locations room1 and room2 going "east" from room1 + LocationLink link3and4 = new TileLocationLink(room3, room4, Direction.WEST); + + // Link locations room2 and room3 going "south" from room2 + LocationLink link4and1 = new TileLocationLink(room4, room1, Direction.NORTH); + + // @todo Register links with rooms or manager + // @todo Test moving Placeable through rooms using link + } +} Property changes on: trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/RoomTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Added: trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/TileLocationTest.java =================================================================== --- trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/TileLocationTest.java (rev 0) +++ trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/TileLocationTest.java 2007-11-01 00:00:59 UTC (rev 158) @@ -0,0 +1,9 @@ +package net.sf.mud4j.world; + +import junit.framework.TestCase; + +public class TileLocationTest extends TestCase { + public void testMove() { + + } +} Property changes on: trunk/mud4j-core/src/test/unit/net/sf/mud4j/world/TileLocationTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |