KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > demo > SVODemoClient


1 package org.bsf.smartValueObject.demo;
2
3 import junit.framework.TestCase;
4 import org.bsf.remoting.EJBDefinition;
5 import org.bsf.remoting.http.HttpServiceFactory;
6 import org.bsf.smartValueObject.mediator.ConcurrencyException;
7 import org.bsf.smartValueObject.Versionable;
8 import org.bsf.smartValueObject.SmartAccess;
9
10 import java.rmi.RemoteException JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * Handles the client-server exchanges of the demo.
15  */

16 public class SVODemoClient extends TestCase {
17
18     private static final EJBDefinition SVO_SERVICE =
19             new EJBDefinition( "ejb/SVODemoService",
20                                "org.bsf.smartValueObject.demo.SVODemoServiceHome",
21                                "org.bsf.smartValueObject.demo.SVODemoService" );
22
23     private SVODemoService svoService = null;
24     private SVODemoUser svoUser = null;
25
26     public SVODemoClient() {
27         HttpServiceFactory factory = new HttpServiceFactory();
28         factory.setHost( "localhost" );
29         factory.setPort( 8080 );
30         factory.setServerContext( "SvoDemo" );
31         svoService = (SVODemoService) factory.getService( SVO_SERVICE );
32         try {
33             svoUser = svoService.createUserService();
34         } catch( java.rmi.RemoteException JavaDoc e ) {
35             e.printStackTrace();
36         }
37     }
38
39     /**
40      * Retrieve the user company graph
41      * @return
42      */

43     public CompanyVO getCompanyGraph() throws RemoteException JavaDoc {
44         CompanyVO vo = new CompanyVO();
45
46         //The id is normally retrieved from a list of object (JTable).
47
vo.setId( new Long JavaDoc( 0 ) );
48
49         return (CompanyVO) svoUser.getCompanyVo( vo );
50     }
51
52     public String JavaDoc storeCompanyGraph( Object JavaDoc graph ) throws RemoteException JavaDoc,
53             ConcurrencyException {
54         return svoUser.updateCompanyVo( graph );
55     }
56
57     /**
58      * Used to simulate a concurrent modification on datas.
59      */

60     public void modifyConcurrently() throws RemoteException JavaDoc {
61         svoUser.modifyConcurrently();
62     }
63
64     ///////////////////////////////////////////////////////////////////////
65
// TESTS
66
//////////////////////////////////////////////////////////////////////
67

68     /**
69      * Test method
70      * @throws Exception
71      */

72     public void testSvoUser() throws Exception JavaDoc {
73         CompanyVO vo = this.getCompanyGraph();
74         assertNotNull( vo );
75         assertEquals( new Long JavaDoc( 0 ), vo.getId() );
76
77         SubsidiaryVO[] subAr = (SubsidiaryVO[])
78                 vo.getSubsidiaries().toArray(
79                         new SubsidiaryVO[ vo.getSubsidiaries().size() ] );
80         SubsidiaryVO parisSub = subAr[ 0 ];
81         parisSub.setName( "Joli Paris" );
82
83         System.out.println( this.storeCompanyGraph( vo ) );
84         vo = this.getCompanyGraph();
85
86         this.modifyConcurrently();
87
88         vo.setName( vo.getName().toUpperCase() );
89
90         try {
91             this.storeCompanyGraph( vo );
92         } catch( ConcurrencyException e ) {
93             return;
94         }
95         //Should have thrown a concurrency exception
96
fail( "Expected concurrency exception" );
97     }
98
99     public void testBug() throws Exception JavaDoc {
100         CompanyVO vo = this.getCompanyGraph();
101         assertNotNull( vo );
102         assertEquals( new Long JavaDoc( 0 ), vo.getId() );
103
104         for (Iterator JavaDoc it = vo.subsidiaries(); it.hasNext(); ) {
105             vo.removeSubsidiary((SubsidiaryVO) it.next());
106         }
107         assertTrue(!((Versionable) vo).isDirty());
108         System.out.println(this.storeCompanyGraph(vo));
109     }
110
111     public void testEverythingCleanOnBegin() throws Exception JavaDoc {
112         CompanyVO vo = this.getCompanyGraph();
113         assertNotNull( vo );
114         assertEquals( new Long JavaDoc( 0 ), vo.getId() );
115         assertTrue(!SmartAccess.isGraphDirty(vo));
116     }
117 }
118
Popular Tags