KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > inheritance > joined > SubclassTest


1 //$Id: SubclassTest.java,v 1.4 2005/07/20 00:18:05 epbernard Exp $
2
package org.hibernate.test.annotations.inheritance.joined;
3
4 import java.util.List JavaDoc;
5
6 import org.hibernate.Session;
7 import org.hibernate.Transaction;
8 import org.hibernate.test.annotations.TestCase;
9
10 /**
11  * @author Emmanuel Bernard
12  */

13 public class SubclassTest extends TestCase {
14
15     public SubclassTest(String JavaDoc x) {
16         super(x);
17     }
18
19     public void testDefault() throws Exception JavaDoc {
20         Session s;
21         Transaction tx;
22         s = openSession();
23         tx = s.beginTransaction();
24         File doc = new Document("Enron Stuff To Shred", 1000);
25         Folder folder = new Folder("Enron");
26         s.persist(doc);
27         s.persist(folder);
28         tx.commit();
29         s.close();
30
31         s = openSession();
32         tx = s.beginTransaction();
33         List JavaDoc result = s.createCriteria(File.class).list();
34         assertNotNull(result);
35         assertEquals( 2, result.size() );
36         File f2 = (File) result.get(0);
37         checkClassType(f2, doc, folder);
38         f2 = (File) result.get(1);
39         checkClassType(f2, doc, folder);
40         s.delete( result.get(0) );
41         s.delete( result.get(1) );
42         tx.commit();
43         s.close();
44     }
45
46     public void testManyToOneOnAbstract() throws Exception JavaDoc {
47         Folder f = new Folder();
48         f.setName("data");
49         ProgramExecution remove = new ProgramExecution();
50         remove.setAction("remove");
51         remove.setAppliesOn(f);
52         Session s;
53         Transaction tx;
54         s = openSession();
55         tx = s.beginTransaction();
56         s.persist(f);
57         s.persist(remove);
58         tx.commit();
59         s.clear();
60         tx = s.beginTransaction();
61         remove = (ProgramExecution) s.get( ProgramExecution.class, remove.getId() );
62         assertNotNull(remove);
63         assertNotNull( remove.getAppliesOn().getName() );
64         s.delete( remove );
65         s.delete( remove.getAppliesOn() );
66         tx.commit();
67         s.close();
68
69     }
70
71     private void checkClassType(File fruitToTest, File f, Folder a) {
72         if ( fruitToTest.getName().equals( f.getName() ) ) {
73             assertFalse(fruitToTest instanceof Folder);
74         } else if ( fruitToTest.getName().equals( a.getName() ) ) {
75             assertTrue(fruitToTest instanceof Folder);
76         } else {
77             fail("Result does not contains the previously inserted elements");
78         }
79     }
80
81     /**
82      * @see org.hibernate.test.annotations.TestCase#getMappings()
83      */

84     protected Class JavaDoc[] getMappings() {
85         return new Class JavaDoc[] {
86             File.class,
87             Folder.class,
88             Document.class,
89             SymbolicLink.class,
90             ProgramExecution.class
91         };
92     }
93
94 }
95
Popular Tags