Index: polymorphism/Computer.java =================================================================== RCS file: polymorphism/Computer.java diff -N polymorphism/Computer.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ polymorphism/Computer.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,24 @@ +package polymorphism; + +/** + * @author werner.guttmann + * + * This is a type. + */ +public class Computer extends Product { + + private String cpu; + + /** + * @return Returns the cpu. + */ + public String getCpu() { + return this.cpu; + } + /** + * @param cpu The cpu to set. + */ + public void setCpu(String cpu) { + this.cpu = cpu; + } +} Index: polymorphism/Laptop.java =================================================================== RCS file: polymorphism/Laptop.java diff -N polymorphism/Laptop.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ polymorphism/Laptop.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,36 @@ +package polymorphism; + + +/** + * @author werner.guttmann + */ +public class Laptop extends Computer { + + private int weight; + private String resolution; + + /** + * @return Returns the resolution. + */ + public String getResolution() { + return this.resolution; + } + /** + * @param resolution The resolution to set. + */ + public void setResolution(String resolution) { + this.resolution = resolution; + } + /** + * @return Returns the weight. + */ + public int getWeight() { + return this.weight; + } + /** + * @param weight The weight to set. + */ + public void setWeight(int weight) { + this.weight = weight; + } +} Index: polymorphism/PolymorphismTest.java =================================================================== RCS file: polymorphism/PolymorphismTest.java diff -N polymorphism/PolymorphismTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ polymorphism/PolymorphismTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,179 @@ +package polymorphism; + +import org.exolab.castor.jdo.Database; +import org.exolab.castor.jdo.JDO2; +import org.exolab.castor.jdo.OQLQuery; +import org.exolab.castor.jdo.QueryResults; + +import junit.framework.TestCase; + +/** + * @author werner.guttmann + * + * This is a type. + */ +public class PolymorphismTest extends TestCase { + + /** + * Castor JDO manager + */ + private JDO2 jdo = null; + + /* + * @see TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + + JDO2.loadConfiguration (getClass().getResource("jdo-conf.xml").toString()); + jdo = JDO2.createInstance("poly"); + + } + + public void testLoadLaptop() throws Exception { + + Database database = null; + + database = jdo.getDatabase(); + + database.begin(); + Laptop laptop = (Laptop) database.load (Laptop.class, new Integer (1)); + database.commit(); + + assertNotNull (laptop); + assertEquals (1, laptop.getId()); + + database.close(); + + } + + public void testQueryLaptops () throws Exception { + + Database database; + + database = jdo.getDatabase(); + + database.begin(); + OQLQuery query = database.getOQLQuery("select l from polymorphism.Laptop as l"); + QueryResults results = query.execute(); + + if (results.hasMore()) { + int counter = 1; + Laptop laptop = null; + while (results.hasMore()) { + laptop = (Laptop) results.next(); + assertNotNull (laptop); + assertEquals (counter, laptop.getId()); + + counter += 1; + } + } else { + fail ("Query does not return any Laptop instances."); + } + database.commit(); + + database.close(); + } + + public void testLoadServer() throws Exception { + + Database database = null; + + database = jdo.getDatabase(); + + database.begin(); + Server server = (Server) database.load (Server.class, new Integer (3)); + database.commit(); + + assertNotNull (server); + assertEquals (3, server.getId()); + + database.close(); + + } + + public void testQueryServers () throws Exception { + + Database database; + + database = jdo.getDatabase(); + + database.begin(); + OQLQuery query = database.getOQLQuery("select l from polymorphism.Server as l"); + QueryResults results = query.execute(); + + if (results.hasMore()) { + int counter = 3; + while (results.hasMore()) { + Server server = (Server) results.next(); + assertNotNull (server); + assertEquals (counter, server.getId()); + + counter += 1; + } + } else { + fail ("Query does not return any Laptop instances."); + } + database.commit(); + + database.close(); + } + + public void testLoadComputer() throws Exception { + + Database database = null; + + database = jdo.getDatabase(); + + database.begin(); + Laptop laptop = (Laptop) database.load (Computer.class, new Integer (1)); + database.commit(); + + assertNotNull (laptop); + assertEquals (1, laptop.getId()); + assertEquals ("laptop 1", laptop.getName()); + + assertEquals (2800, laptop.getWeight()); + assertEquals ("1280", laptop.getResolution()); + + database.close(); + + } + + public void testQueryComputers () throws Exception { + + Database database; + + String[] classNames = + { + "polymorphism.Laptop", + "polymorphism.Laptop", + "polymorphism.Server", + "polymorphism.Server" + }; + + database = jdo.getDatabase(); + + database.begin(); + OQLQuery query = database.getOQLQuery("select l from polymorphism.Computer as l"); + QueryResults results = query.execute(); + + if (results.hasMore()) { + int counter = 1; + while (results.hasMore()) { + Computer computer = (Computer) results.next(); + assertNotNull (computer); + assertEquals (counter, computer.getId()); + assertEquals (classNames[counter - 1], computer.getClass().getName()); + + counter += 1; + } + } else { + fail ("Query does not return any Computer instances."); + } + database.commit(); + + database.close(); + } + +} Index: polymorphism/Product.java =================================================================== RCS file: polymorphism/Product.java diff -N polymorphism/Product.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ polymorphism/Product.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,35 @@ +package polymorphism; + +/** + * @author werner.guttmann + */ +public class Product { + + private int id; + private String name; + + /** + * @return Returns the id. + */ + public int getId() { + return this.id; + } + /** + * @param id The id to set. + */ + public void setId(int id) { + this.id = id; + } + /** + * @return Returns the name. + */ + public String getName() { + return this.name; + } + /** + * @param name The name to set. + */ + public void setName(String name) { + this.name = name; + } +} Index: polymorphism/Server.java =================================================================== RCS file: polymorphism/Server.java diff -N polymorphism/Server.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ polymorphism/Server.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,35 @@ +package polymorphism; + +/** + * @author werner.guttmann + */ +public class Server extends Computer { + + private int numberOfCPUs; + private int supportInYears; + + /** + * @return Returns the numberOfCPUs. + */ + public int getNumberOfCPUs() { + return this.numberOfCPUs; + } + /** + * @param numberOfCPUs The numberOfCPUs to set. + */ + public void setNumberOfCPUs(int numberOfCPUs) { + this.numberOfCPUs = numberOfCPUs; + } + /** + * @return Returns the supportInYears. + */ + public int getSupportInYears() { + return this.supportInYears; + } + /** + * @param supportInYears The supportInYears to set. + */ + public void setSupportInYears(int supportInYears) { + this.supportInYears = supportInYears; + } +} Index: polymorphism/ddl.sql =================================================================== RCS file: polymorphism/ddl.sql diff -N polymorphism/ddl.sql --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ polymorphism/ddl.sql 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,41 @@ +drop table if exists prod; +create table prod ( + id int not null, + name varchar(200) not null +); + +drop table if exists computer; +create table computer ( + id int not null, + cpu varchar(200) not null +); + +drop table if exists laptop; +create table laptop ( + id int not null, + weight int not null, + resolution varchar(19) not null +); + +drop table if exists server; +create table server ( + id int not null, + numberOfCPUs int not null, + support int not null +); + +insert into prod (id, name) values (1, 'laptop 1'); +insert into computer (id, cpu) values (1, 'centrino'); +insert into laptop (id, weight, resolution) values (1, 2800, '1280'); + +insert into prod (id, name) values (2, 'laptop 2'); +insert into computer (id, cpu) values (2, 'centrino'); +insert into laptop (id, weight, resolution) values (2, 2700, '1024'); + +insert into prod (id, name) values (3, 'serevr 3'); +insert into computer (id, cpu) values (3, 'pentium 4'); +insert into server (id, numberOfCPUs, support) values (3, 4, 3); + +insert into prod (id, name) values (4, 'serevr 4'); +insert into computer (id, cpu) values (4, 'pentium 4'); +insert into server (id, numberOfCPUs, support) values (3, 16,5); Index: polymorphism/jdo-conf.xml =================================================================== RCS file: polymorphism/jdo-conf.xml diff -N polymorphism/jdo-conf.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ polymorphism/jdo-conf.xml 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + Index: polymorphism/mapping.xml =================================================================== RCS file: polymorphism/mapping.xml diff -N polymorphism/mapping.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ polymorphism/mapping.xml 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,68 @@ + + + + + + Product definition + + + + + + + + + + + + + + Computer definition, extends generic product + + + + + + + + + + + + + Laptop definition, extends generic computer + + + + + + + + + + + + + + + + + Server definition, extends generic computer + + + + + + + + + + + + + + + + +