Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hibernate/test/readonly/ReadOnlyTest.java


1   //$Id: ReadOnlyTest.java 9058 2006-01-13 20:11:31Z steveebersole $
2   package org.hibernate.test.readonly;
3   
4   import java.math.BigDecimal;
5   import java.util.List;
6   
7   import junit.framework.Test;
8   import junit.framework.TestSuite;
9   
10  import org.hibernate.CacheMode;
11  import org.hibernate.ScrollMode;
12  import org.hibernate.ScrollableResults;
13  import org.hibernate.Session;
14  import org.hibernate.Transaction;
15  import org.hibernate.Hibernate;
16  import org.hibernate.cfg.Configuration;
17  import org.hibernate.cfg.Environment;
18  import org.hibernate.test.TestCase;
19  
20  /**
21   * This is how to do batch processing in Hibernate.
22   * Remember to enable JDBC batch updates, or this 
23   * test will take a Very Long Time!
24   * 
25   * @author Gavin King
26   */
27  public class ReadOnlyTest extends TestCase {
28    
29    public ReadOnlyTest(String str) {
30      super(str);
31    }
32  
33    public void testReadOnlyOnProxiesFailureExpected() {
34      Session s = openSession();
35      s.setCacheMode( CacheMode.IGNORE );
36      s.beginTransaction();
37      DataPoint dp = new DataPoint();
38      dp.setX( new BigDecimal( 0.1d ).setScale(19, BigDecimal.ROUND_DOWN) );
39      dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) );
40      dp.setDescription( "original" );
41      s.save( dp );
42      long dpId = dp.getId();
43      s.getTransaction().commit();
44      s.close();
45  
46      s = openSession();
47      s.setCacheMode(CacheMode.IGNORE);
48      dp = ( DataPoint ) s.load( DataPoint.class, new Long( dpId ) );
49      assertFalse( "was initialized", Hibernate.isInitialized( dp ) );
50      s.setReadOnly( dp, true );
51      assertFalse( "was initialized during setReadOnly", Hibernate.isInitialized( dp ) );
52      dp.setDescription( "changed" );
53      assertTrue( "was not initialized during mod", Hibernate.isInitialized( dp ) );
54      assertEquals( "desc not changed in memory", "changed", dp.getDescription() );
55      s.flush();
56      s.getTransaction().commit();
57      s.close();
58  
59      s = openSession();
60      s.beginTransaction();
61      List list = s.createQuery( "from DataPoint where description = 'changed'" ).list();
62      assertEquals( "change written to database", 0, list.size() );
63      s.createQuery("delete from DataPoint").executeUpdate();
64      s.getTransaction().commit();
65      s.close();
66    }
67  
68    public void testReadOnlyMode() {
69      
70      Session s = openSession();
71      s.setCacheMode(CacheMode.IGNORE);
72      Transaction t = s.beginTransaction();    
73      for ( int i=0; i<100; i++ ) {
74        DataPoint dp = new DataPoint();
75        dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) );
76        dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) );
77        s.save(dp);
78      }
79      t.commit();
80      s.close();
81      
82      s = openSession();
83      s.setCacheMode(CacheMode.IGNORE);
84      t = s.beginTransaction();
85      int i = 0;
86      ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc")
87          .setReadOnly(true)
88          .scroll(ScrollMode.FORWARD_ONLY);
89      while ( sr.next() ) {
90        DataPoint dp = (DataPoint) sr.get(0);
91        if (++i==50) {
92          s.setReadOnly(dp, false);
93        }
94        dp.setDescription("done!");
95      }
96      t.commit();
97      s.clear();
98      t = s.beginTransaction();
99      List single = s.createQuery("from DataPoint where description='done!'").list();
100     assertEquals( single.size(), 1 );
101     s.createQuery("delete from DataPoint").executeUpdate();
102     t.commit();
103     s.close();
104     
105   }
106   
107   protected void configure(Configuration cfg) {
108     cfg.setProperty(Environment.STATEMENT_BATCH_SIZE, "20");
109   }
110 
111   protected String[] getMappings() {
112     return new String[] { "readonly/DataPoint.hbm.xml" };
113   }
114 
115   public static Test suite() {
116     return new TestSuite(ReadOnlyTest.class);
117   }
118 
119   public String getCacheConcurrencyStrategy() {
120     return null;
121   }
122 
123 }
124