KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > lob > LobTest


1 //$Id: LobTest.java,v 1.3 2005/07/11 16:23:29 epbernard Exp $
2
package org.hibernate.test.annotations.lob;
3
4 import org.hibernate.Session;
5 import org.hibernate.Transaction;
6 import org.hibernate.test.annotations.TestCase;
7
8 /**
9  * @author Emmanuel Bernard
10  */

11 public class LobTest extends TestCase {
12     public void testSerializableToBlob() throws Exception JavaDoc {
13         Book book = new Book();
14         Editor editor = new Editor();
15         editor.setName("O'Reilly");
16         book.setEditor(editor);
17         book.setCode2( new char[] { 'r' } );
18         Session s;
19         Transaction tx;
20         s = openSession();
21         tx = s.beginTransaction();
22         s.persist(book);
23         tx.commit();
24         s.close();
25         s = openSession();
26         tx = s.beginTransaction();
27         Book loadedBook = (Book) s.get( Book.class, book.getId() );
28         assertNotNull( loadedBook.getEditor() );
29         assertEquals( book.getEditor().getName(), loadedBook.getEditor().getName() );
30         loadedBook.setEditor(null);
31         tx.commit();
32         s.close();
33         s = openSession();
34         tx = s.beginTransaction();
35         loadedBook = (Book) s.get( Book.class, book.getId() );
36         assertNull( loadedBook.getEditor() );
37         tx.commit();
38         s.close();
39
40     }
41     public void testClob() throws Exception JavaDoc {
42         Session s;
43         Transaction tx;
44         s = openSession();
45         tx = s.beginTransaction();
46         Book b = new Book();
47         b.setShortDescription("Hibernate Bible");
48         b.setFullText("Hibernate in Action aims to...");
49         b.setCode( new Character JavaDoc[] { 'a', 'b', 'c' } );
50         b.setCode2( new char[] { 'a', 'b', 'c' } );
51         s.persist(b);
52         tx.commit();
53         s.close();
54
55         s = openSession();
56         tx = s.beginTransaction();
57         Book b2 = (Book) s.get( Book.class, b.getId() );
58         assertNotNull(b2);
59         assertEquals( b2.getFullText(), b.getFullText() );
60         assertEquals( b2.getCode()[1].charValue(), b.getCode()[1].charValue() );
61         assertEquals( b2.getCode2()[2], b.getCode2()[2] );
62         tx.commit();
63         s.close();
64     }
65
66     public void testBlob() throws Exception JavaDoc {
67         Session s;
68         Transaction tx;
69         s = openSession();
70         tx = s.beginTransaction();
71         CompiledCode cc = new CompiledCode();
72         Byte JavaDoc[] header = new Byte JavaDoc[2];
73         header[0] = new Byte JavaDoc( (byte) 3);
74         header[1] = new Byte JavaDoc( (byte) 0);
75         cc.setHeader(header);
76         int codeSize = 5;
77         byte[] full = new byte[codeSize];
78         for (int i = 0 ; i < codeSize ; i++) {
79             full[i] = (byte) (1+i);
80         }
81         cc.setFullCode(full);
82         s.persist(cc);
83         tx.commit();
84         s.close();
85         s = openSession();
86         tx = s.beginTransaction();
87         CompiledCode recompiled = (CompiledCode) s.get( CompiledCode.class, cc.getId() );
88         assertEquals( recompiled.getHeader()[1], cc.getHeader()[1] );
89         assertEquals( recompiled.getFullCode()[codeSize - 1], cc.getFullCode()[codeSize - 1] );
90         tx.commit();
91         s.close();
92     }
93
94     public void testBinary() throws Exception JavaDoc {
95         Session s;
96         Transaction tx;
97         s = openSession();
98         tx = s.beginTransaction();
99         CompiledCode cc = new CompiledCode();
100         byte[] metadata = new byte[2];
101         metadata[0] = (byte) 3;
102         metadata[1] = (byte) 0;
103         cc.setMetadata( metadata );
104         s.persist(cc);
105         tx.commit();
106         s.close();
107         s = openSession();
108         tx = s.beginTransaction();
109         CompiledCode recompiled = (CompiledCode) s.get( CompiledCode.class, cc.getId() );
110         assertEquals( recompiled.getMetadata()[1], cc.getMetadata()[1] );
111         tx.commit();
112         s.close();
113     }
114
115     public LobTest(String JavaDoc x) {
116         super(x);
117     }
118
119     protected Class JavaDoc[] getMappings() {
120         return new Class JavaDoc[] {
121             Book.class,
122             CompiledCode.class
123         };
124     }
125 }
126
Popular Tags