~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-annotations-3.4.0.GA/test/org/hibernate/test/annotations/ConfigurationTest.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//$Id: ConfigurationTest.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
2
 
package org.hibernate.test.annotations;
3
 
 
4
 
import org.hibernate.HibernateException;
5
 
import org.hibernate.MappingException;
6
 
import org.hibernate.Query;
7
 
import org.hibernate.Session;
8
 
import org.hibernate.SessionFactory;
9
 
import org.hibernate.Transaction;
10
 
import org.hibernate.cfg.AnnotationConfiguration;
11
 
import org.hibernate.cfg.Configuration;
12
 
import org.hibernate.cfg.Environment;
13
 
 
14
 
/**
15
 
 * @author Emmanuel Bernard
16
 
 */
17
 
public class ConfigurationTest extends junit.framework.TestCase {
18
 
        public void testDeclarativeMix() throws Exception {
19
 
                AnnotationConfiguration cfg = new AnnotationConfiguration();
20
 
                cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
21
 
                cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
22
 
                SessionFactory sf = cfg.buildSessionFactory();
23
 
                assertNotNull( sf );
24
 
                Session s = sf.openSession();
25
 
                Transaction tx = s.beginTransaction();
26
 
                Query q = s.createQuery( "from Boat" );
27
 
                assertEquals( 0, q.list().size() );
28
 
                q = s.createQuery( "from Plane" );
29
 
                assertEquals( 0, q.list().size() );
30
 
                tx.commit();
31
 
                s.close();
32
 
                sf.close();
33
 
        }
34
 
 
35
 
        public void testIgnoringHbm() throws Exception {
36
 
                AnnotationConfiguration cfg = new AnnotationConfiguration();
37
 
                cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
38
 
                cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
39
 
                cfg.setProperty( AnnotationConfiguration.ARTEFACT, "class, whatever" );
40
 
                SessionFactory sf = cfg.buildSessionFactory();
41
 
                assertNotNull( sf );
42
 
                Session s = sf.openSession();
43
 
                Transaction tx = s.beginTransaction();
44
 
                Query q;
45
 
                try {
46
 
                        s.createQuery( "from Boat" ).list();
47
 
                        fail( "Boat should not be mapped" );
48
 
                }
49
 
                catch (HibernateException e) {
50
 
                        //all good
51
 
                }
52
 
                q = s.createQuery( "from Plane" );
53
 
                assertEquals( 0, q.list().size() );
54
 
                tx.commit();
55
 
                s.close();
56
 
                sf.close();
57
 
        }
58
 
 
59
 
        public void testPrecedenceHbm() throws Exception {
60
 
                AnnotationConfiguration cfg = new AnnotationConfiguration();
61
 
                cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
62
 
                cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
63
 
                cfg.addAnnotatedClass( Boat.class );
64
 
                SessionFactory sf = cfg.buildSessionFactory();
65
 
                assertNotNull( sf );
66
 
                Session s = sf.openSession();
67
 
                s.getTransaction().begin();
68
 
                Boat boat = new Boat();
69
 
                boat.setSize( 12 );
70
 
                boat.setWeight( 34 );
71
 
                s.persist( boat );
72
 
                s.getTransaction().commit();
73
 
                s.clear();
74
 
                Transaction tx = s.beginTransaction();
75
 
                boat = (Boat) s.get( Boat.class, boat.getId() );
76
 
                assertTrue( "Annotation has precedence", 34 != boat.getWeight() );
77
 
                s.delete( boat );
78
 
                //s.getTransaction().commit();
79
 
                tx.commit();
80
 
                s.close();
81
 
                sf.close();
82
 
        }
83
 
 
84
 
        public void testPrecedenceAnnotation() throws Exception {
85
 
                AnnotationConfiguration cfg = new AnnotationConfiguration();
86
 
                cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
87
 
                cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
88
 
                cfg.setProperty( AnnotationConfiguration.ARTEFACT, "class, hbm" );
89
 
                cfg.addAnnotatedClass( Boat.class );
90
 
                SessionFactory sf = cfg.buildSessionFactory();
91
 
                assertNotNull( sf );
92
 
                Session s = sf.openSession();
93
 
                s.getTransaction().begin();
94
 
                Boat boat = new Boat();
95
 
                boat.setSize( 12 );
96
 
                boat.setWeight( 34 );
97
 
                s.persist( boat );
98
 
                s.getTransaction().commit();
99
 
                s.clear();
100
 
                Transaction tx = s.beginTransaction();
101
 
                boat = (Boat) s.get( Boat.class, boat.getId() );
102
 
                assertTrue( "Annotation has precedence", 34 == boat.getWeight() );
103
 
                s.delete( boat );
104
 
                tx.commit();
105
 
                s.close();
106
 
                sf.close();
107
 
        }
108
 
 
109
 
        public void testDeclarativeAnnWoAnnConfig() throws Exception {
110
 
                Configuration cfg = new Configuration();
111
 
                try {
112
 
                        cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
113
 
                        fail( "Configuration object should fail when finding annotated elements declarations" );
114
 
                }
115
 
                catch (MappingException e) {
116
 
                        //success
117
 
                }
118
 
        }
119
 
 
120
 
        public void testHbmWithSubclassExtends() throws Exception {
121
 
                AnnotationConfiguration cfg = new AnnotationConfiguration();
122
 
                cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
123
 
                cfg.addClass( Ferry.class );
124
 
                cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
125
 
                SessionFactory sf = cfg.buildSessionFactory();
126
 
                assertNotNull( sf );
127
 
                Session s = sf.openSession();
128
 
                Transaction tx = s.beginTransaction();
129
 
                Query q = s.createQuery( "from Ferry" );
130
 
                assertEquals( 0, q.list().size() );
131
 
                q = s.createQuery( "from Plane" );
132
 
                assertEquals( 0, q.list().size() );
133
 
                tx.commit();
134
 
                s.close();
135
 
                sf.close();
136
 
        }
137
 
 
138
 
        public void testAnnReferencesHbm() throws Exception {
139
 
                AnnotationConfiguration cfg = new AnnotationConfiguration();
140
 
                cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
141
 
                cfg.addAnnotatedClass( Port.class );
142
 
                cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
143
 
                SessionFactory sf = cfg.buildSessionFactory();
144
 
                assertNotNull( sf );
145
 
                Session s = sf.openSession();
146
 
                Transaction tx = s.beginTransaction();
147
 
                Query q = s.createQuery( "from Boat" );
148
 
                assertEquals( 0, q.list().size() );
149
 
                q = s.createQuery( "from Port" );
150
 
                assertEquals( 0, q.list().size() );
151
 
                tx.commit();
152
 
                s.close();
153
 
                sf.close();
154
 
        }
155
 
}