KickJava   Java API By Example, From Geeks To Geeks.

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


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

14 public class SubclassTest extends TestCase {
15
16     public SubclassTest(String JavaDoc x) {
17         super(x);
18     }
19
20     public void testDefault() throws Exception JavaDoc {
21         Session s;
22         Transaction tx;
23         s = openSession();
24         tx = s.beginTransaction();
25         File doc = new Document("Enron Stuff To Shred", 1000);
26         Folder folder = new Folder("Enron");
27         s.persist(doc);
28         s.persist(folder);
29         try {
30             tx.commit();
31         } catch (SQLGrammarException e) {
32             System.err.println( e.getSQLException().getNextException() );
33         }
34         s.close();
35
36         s = openSession();
37         tx = s.beginTransaction();
38         List JavaDoc result = s.createCriteria(File.class).list();
39         assertNotNull(result);
40         assertEquals( 2, result.size() );
41         File f2 = (File) result.get(0);
42         checkClassType(f2, doc, folder);
43         f2 = (File) result.get(1);
44         checkClassType(f2, doc, folder);
45         s.delete( result.get(0) );
46         s.delete( result.get(1) );
47         tx.commit();
48         s.close();
49     }
50
51     private void checkClassType(File fruitToTest, File f, Folder a) {
52         if ( fruitToTest.getName().equals( f.getName() ) ) {
53             assertFalse(fruitToTest instanceof Folder);
54         } else if ( fruitToTest.getName().equals( a.getName() ) ) {
55             assertTrue(fruitToTest instanceof Folder);
56         } else {
57             fail("Result does not contains the previously inserted elements");
58         }
59     }
60
61     /**
62      * @see org.hibernate.test.annotations.TestCase#getMappings()
63      */

64     protected Class JavaDoc[] getMappings() {
65         return new Class JavaDoc[] {
66             File.class,
67             Folder.class,
68             Document.class,
69             SymbolicLink.class
70         };
71     }
72
73 }
74
Popular Tags