Revision: 16663
http://datanucleus.svn.sourceforge.net/datanucleus/?rev=16663&view=rev
Author: andy_jefferson
Date: 2013-02-27 10:29:40 +0000 (Wed, 27 Feb 2013)
Log Message:
-----------
test for persistence/retrieval of interface field
Modified Paths:
--------------
test/accessplatform/trunk/test.jpa.general/src/java/META-INF/persistence.xml
test/accessplatform/trunk/test.jpa.general/src/test/org/datanucleus/tests/EntityManagerTest.java
Added Paths:
-----------
test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/
test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Circle.java
test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Rectangle.java
test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Shape.java
test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/ShapeHolder.java
test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Square.java
test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Triangle.java
Modified: test/accessplatform/trunk/test.jpa.general/src/java/META-INF/persistence.xml
===================================================================
--- test/accessplatform/trunk/test.jpa.general/src/java/META-INF/persistence.xml 2013-02-27 10:08:47 UTC (rev 16662)
+++ test/accessplatform/trunk/test.jpa.general/src/java/META-INF/persistence.xml 2013-02-27 10:29:40 UTC (rev 16663)
@@ -103,6 +103,11 @@
<class>org.jpox.samples.validation.ValidatedPerson</class>
<class>org.jpox.samples.validation.ValidatedPerson2</class>
+
+ <class>org.datanucleus.samples.types.interfaces.Rectangle</class>
+ <class>org.datanucleus.samples.types.interfaces.Circle</class>
+ <class>org.datanucleus.samples.types.interfaces.Square</class>
+ <class>org.datanucleus.samples.types.interfaces.Triangle</class>
</persistence-unit>
</persistence>
\ No newline at end of file
Added: test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Circle.java
===================================================================
--- test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Circle.java (rev 0)
+++ test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Circle.java 2013-02-27 10:29:40 UTC (rev 16663)
@@ -0,0 +1,102 @@
+/**********************************************************************
+Copyright (c) 2013 Andy Jefferson and others. All rights reserved.
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+Contributors:
+ ...
+**********************************************************************/
+package org.datanucleus.samples.types.interfaces;
+
+import java.io.Serializable;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * Circle class.
+ */
+@Entity
+public class Circle implements Shape, Cloneable, Serializable
+{
+ @Id
+ private int id;
+ protected double radius=0.0;
+
+ public Circle(int id, double radius)
+ {
+ this.id = id;
+ this.radius = radius;
+ }
+
+ public String getName()
+ {
+ return "Circle";
+ }
+
+ public void setRadius(double radius)
+ {
+ this.radius = radius;
+ }
+
+ public double getRadius()
+ {
+ return radius;
+ }
+
+ public double getArea()
+ {
+ return Math.PI*radius*radius;
+ }
+
+ public double getCircumference()
+ {
+ return Math.PI*radius*2;
+ }
+
+ public int getId()
+ {
+ return id;
+ }
+
+ public void setId(int id)
+ {
+ this.id = id;
+ }
+
+ public Object clone()
+ {
+ try
+ {
+ return super.clone();
+ }
+ catch (CloneNotSupportedException cnse)
+ {
+ return null;
+ }
+ }
+
+ public boolean equals(Object o)
+ {
+ if (o == null || !o.getClass().equals(this.getClass()))
+ {
+ return false;
+ }
+ Circle rhs = (Circle)o;
+ return radius == rhs.radius;
+ }
+
+ public String toString()
+ {
+ return "Circle [radius=" + radius + "]";
+ }
+}
\ No newline at end of file
Added: test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Rectangle.java
===================================================================
--- test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Rectangle.java (rev 0)
+++ test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Rectangle.java 2013-02-27 10:29:40 UTC (rev 16663)
@@ -0,0 +1,109 @@
+/**********************************************************************
+Copyright (c) 2013 Andy Jefferson and others. All rights reserved.
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+Contributors:
+ ...
+**********************************************************************/
+package org.datanucleus.samples.types.interfaces;
+
+import java.io.Serializable;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * Rectangle class.
+ */
+@Entity
+public class Rectangle implements Shape, Cloneable, Serializable
+{
+ @Id
+ private int id;
+ protected double width=0.0;
+ protected double length=0.0;
+
+ public Rectangle(int id, double width,double length)
+ {
+ this.id = id;
+ this.length = length;
+ this.width = width;
+ }
+
+ public String getName()
+ {
+ return "Rectangle";
+ }
+
+ public void setLength(double length)
+ {
+ this.length = length;
+ }
+
+ public void setWidth(double width)
+ {
+ this.width = width;
+ }
+
+ public double getArea()
+ {
+ return length*width;
+ }
+
+ public int getId()
+ {
+ return id;
+ }
+
+ public void setId(int id)
+ {
+ this.id = id;
+ }
+
+ public double getLength()
+ {
+ return length;
+ }
+
+ public double getWidth()
+ {
+ return width;
+ }
+
+ public Object clone()
+ {
+ try
+ {
+ return super.clone();
+ }
+ catch (CloneNotSupportedException cnse)
+ {
+ return null;
+ }
+ }
+
+ public boolean equals(Object o)
+ {
+ if (o == null || !o.getClass().equals(this.getClass()))
+ {
+ return false;
+ }
+ Rectangle rhs = (Rectangle)o;
+ return width == rhs.width && length == rhs.length;
+ }
+
+ public String toString()
+ {
+ return "Rectangle [width=" + width + ", length=" + length + "]";
+ }
+}
\ No newline at end of file
Added: test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Shape.java
===================================================================
--- test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Shape.java (rev 0)
+++ test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Shape.java 2013-02-27 10:29:40 UTC (rev 16663)
@@ -0,0 +1,28 @@
+/**********************************************************************
+Copyright (c) Andy Jefferson and others. All rights reserved.
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+Contributors:
+ ...
+**********************************************************************/
+package org.datanucleus.samples.types.interfaces;
+
+/**
+ * Representation of a shape.
+ */
+public interface Shape
+{
+ double getArea();
+
+ String getName();
+}
\ No newline at end of file
Added: test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/ShapeHolder.java
===================================================================
--- test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/ShapeHolder.java (rev 0)
+++ test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/ShapeHolder.java 2013-02-27 10:29:40 UTC (rev 16663)
@@ -0,0 +1,79 @@
+/**********************************************************************
+Copyright (c) 2013 Andy Jefferson and others. All rights reserved.
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+Contributors:
+ ...
+**********************************************************************/
+package org.datanucleus.samples.types.interfaces;
+
+import java.util.HashSet;
+import java.util.Random;
+import java.util.Set;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.OneToOne;
+
+/**
+ * Container of Shapes. Has the following :-
+ * <ul>
+ * <li>a field containing a Shape object (1-1 relation)</li>
+ * <li>a field containing a Set of Shapes (1-N relation)</li>
+ * </ul>
+ */
+@Entity
+public class ShapeHolder
+{
+ @Id
+ private int id;
+
+ @OneToOne
+ protected Shape shape1 = null;
+
+ @OneToMany
+ protected Set<Shape> shapeSet1 = new HashSet();
+
+ public ShapeHolder(int id)
+ {
+ this.id = id;
+ Random r = new Random();
+ shape1 = new Circle(r.nextInt(), 5.0);
+ }
+
+ public int getId()
+ {
+ return id;
+ }
+
+ public void setId(int id)
+ {
+ this.id = id;
+ }
+
+ public void setShape1(Shape sh)
+ {
+ shape1 = sh;
+ }
+
+ public Shape getShape1()
+ {
+ return shape1;
+ }
+
+ public Set getShapeSet1()
+ {
+ return shapeSet1;
+ }
+}
\ No newline at end of file
Added: test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Square.java
===================================================================
--- test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Square.java (rev 0)
+++ test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Square.java 2013-02-27 10:29:40 UTC (rev 16663)
@@ -0,0 +1,99 @@
+/**********************************************************************
+Copyright (c) 2013 Andy Jefferson and others. All rights reserved.
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+Contributors:
+ ...
+**********************************************************************/
+package org.datanucleus.samples.types.interfaces;
+
+import java.io.Serializable;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * Square class.
+ */
+@Entity
+public class Square implements Shape, Cloneable, Serializable
+{
+ @Id
+ private int id;
+ protected double width=0.0;
+ protected double length=0.0;
+
+ public Square(int id,double width,double length)
+ {
+ this.id = id;
+ this.length = length;
+ this.width = width;
+ }
+
+ public String getName()
+ {
+ return "Square";
+ }
+
+ public void setLength(double length)
+ {
+ this.length = length;
+ }
+
+ public void setWidth(double width)
+ {
+ this.width = width;
+ }
+
+ public double getArea()
+ {
+ return length*width;
+ }
+
+ public int getId()
+ {
+ return id;
+ }
+
+ public void setId(int i)
+ {
+ id = i;
+ }
+
+ public Object clone()
+ {
+ try
+ {
+ return super.clone();
+ }
+ catch (CloneNotSupportedException cnse)
+ {
+ return null;
+ }
+ }
+
+ public boolean equals(Object o)
+ {
+ if (o == null || !o.getClass().equals(this.getClass()))
+ {
+ return false;
+ }
+ Square sqs = (Square)o;
+ return width == sqs.width && length == sqs.length;
+ }
+
+ public String toString()
+ {
+ return "Square [width=" + width + ", length=" + length + "]";
+ }
+}
\ No newline at end of file
Added: test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Triangle.java
===================================================================
--- test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Triangle.java (rev 0)
+++ test/accessplatform/trunk/test.jpa.general/src/java/org/datanucleus/samples/types/interfaces/Triangle.java 2013-02-27 10:29:40 UTC (rev 16663)
@@ -0,0 +1,99 @@
+/**********************************************************************
+Copyright (c) 2013 Andy Jefferson and others. All rights reserved.
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+Contributors:
+ ...
+**********************************************************************/
+package org.datanucleus.samples.types.interfaces;
+
+import java.io.Serializable;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * Triangle class.
+ */
+@Entity
+public class Triangle implements Shape, Cloneable, Serializable
+{
+ @Id
+ private int id;
+ protected double width=0.0;
+ protected double length=0.0;
+
+ public Triangle(int id, double width, double length)
+ {
+ this.id = id;
+ this.length = length;
+ this.width = width;
+ }
+
+ public String getName()
+ {
+ return "Triangle";
+ }
+
+ public void setLength(double length)
+ {
+ this.length = length;
+ }
+
+ public void setWidth(double width)
+ {
+ this.width = width;
+ }
+
+ public double getArea()
+ {
+ return (length*width)/2;
+ }
+
+ public int getId()
+ {
+ return id;
+ }
+
+ public void setId(int i)
+ {
+ id = i;
+ }
+
+ public Object clone()
+ {
+ try
+ {
+ return super.clone();
+ }
+ catch (CloneNotSupportedException cnse)
+ {
+ return null;
+ }
+ }
+
+ public boolean equals(Object o)
+ {
+ if (o == null || !o.getClass().equals(this.getClass()))
+ {
+ return false;
+ }
+ Triangle trs = (Triangle)o;
+ return id == trs.id && width == trs.width && length == trs.length;
+ }
+
+ public String toString()
+ {
+ return "Triangle [width=" + width + ", length=" + length + "]";
+ }
+}
\ No newline at end of file
Modified: test/accessplatform/trunk/test.jpa.general/src/test/org/datanucleus/tests/EntityManagerTest.java
===================================================================
--- test/accessplatform/trunk/test.jpa.general/src/test/org/datanucleus/tests/EntityManagerTest.java 2013-02-27 10:08:47 UTC (rev 16662)
+++ test/accessplatform/trunk/test.jpa.general/src/test/org/datanucleus/tests/EntityManagerTest.java 2013-02-27 10:29:40 UTC (rev 16663)
@@ -31,6 +31,9 @@
import javax.persistence.PersistenceException;
import javax.persistence.Query;
+import org.datanucleus.samples.types.interfaces.Rectangle;
+import org.datanucleus.samples.types.interfaces.Shape;
+import org.datanucleus.samples.types.interfaces.ShapeHolder;
import org.datanucleus.tests.JPAPersistenceTestCase;
import org.jpox.samples.annotations.models.company.Account;
import org.jpox.samples.annotations.models.company.Person;
@@ -665,4 +668,63 @@
clean(Person.class);
}
}
+
+ /**
+ * Test of detaching and access of an undetached field.
+ */
+ public void testPersistEntityWithInterfaceField()
+ {
+ try
+ {
+ EntityManager em = getEM();
+ EntityTransaction tx = em.getTransaction();
+ try
+ {
+ tx.begin();
+ ShapeHolder holder = new ShapeHolder(100);
+ holder.setShape1(new Rectangle(200, 45, 55));
+ em.persist(holder);
+ tx.commit();
+ }
+ finally
+ {
+ if (tx.isActive())
+ {
+ tx.rollback();
+ }
+ em.close();
+ }
+
+ // Retrieve it
+ em = getEM();
+ tx = em.getTransaction();
+ try
+ {
+ tx.begin();
+ ShapeHolder holder = em.find(ShapeHolder.class, 100);
+ assertNotNull(holder);
+ Shape shape = holder.getShape1();
+ assertNotNull(shape);
+ assertTrue(shape instanceof Rectangle);
+ Rectangle rect = (Rectangle)shape;
+ assertEquals(45.0, rect.getWidth());
+ assertEquals(55.0, rect.getLength());
+
+ tx.commit();
+ }
+ finally
+ {
+ if (tx.isActive())
+ {
+ tx.rollback();
+ }
+ em.close();
+ }
+ }
+ finally
+ {
+ clean(ShapeHolder.class);
+ clean(Rectangle.class);
+ }
+ }
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|