1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/tests/test/org/dbforms/event/TestInterceptor.java,v 1.4 2005/11/29 19:18:38 hkollmann Exp $
3    * $Revision: 1.4 $
4    * $Date: 2005/11/29 19:18:38 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.event;
25  
26  import org.dbforms.config.Interceptor;
27  import org.dbforms.config.Table;
28  import org.dbforms.interfaces.DbEventInterceptorData;
29  import org.dbforms.interfaces.IDbEventInterceptor;
30  
31  import org.dbforms.util.AbstractTestCase;
32  
33  
34  
35  /***
36   * DOCUMENT ME!
37   *
38   * @author $author$
39   * @version $Revision: 1.4 $
40   */
41  public class TestInterceptor extends AbstractTestCase {
42     /***
43      * DOCUMENT ME!
44      *
45      * @throws Exception DOCUMENT ME!
46      */
47     public void testInterceptor() throws Exception {
48        Interceptor interceptor = new Interceptor();
49        interceptor.setClassName("interceptors.InterceptorTest");
50        assertTrue("Class name should be interceptors.InterceptorTest",
51                   interceptor.getClassName().equals("interceptors.InterceptorTest"));
52  
53        Table table = new Table();
54        table.addInterceptor(interceptor);
55  
56        int i = table.getInterceptors()
57                     .size();
58        assertEquals("count interceptors", i, 1);
59  
60        DbEventInterceptorData data = new DbEventInterceptorData(null, null,
61                                                                 null, table);
62  
63        table.processInterceptors(IDbEventInterceptor.PRE_INSERT, data);
64        assertTrue("preInsert", interceptors.InterceptorTest.preInsertCalled);
65  
66        table.processInterceptors(IDbEventInterceptor.POST_INSERT, data);
67        assertTrue("postInsert", interceptors.InterceptorTest.postInsertCalled);
68  
69        table.processInterceptors(IDbEventInterceptor.PRE_UPDATE, data);
70        assertTrue("preUpdate", interceptors.InterceptorTest.preUpdateCalled);
71  
72        table.processInterceptors(IDbEventInterceptor.POST_UPDATE, data);
73        assertTrue("postUpdate", interceptors.InterceptorTest.postUpdateCalled);
74  
75        table.processInterceptors(IDbEventInterceptor.PRE_DELETE, data);
76        assertTrue("preUpdate", interceptors.InterceptorTest.preDeleteCalled);
77  
78        table.processInterceptors(IDbEventInterceptor.POST_DELETE, data);
79        assertTrue("postUpdate", interceptors.InterceptorTest.postDeleteCalled);
80  
81        table.processInterceptors(IDbEventInterceptor.PRE_SELECT, data);
82        assertTrue("preSelectUpdate", interceptors.InterceptorTest.preSelectCalled);
83  
84        table.processInterceptors(IDbEventInterceptor.POST_SELECT, data);
85        assertTrue("postSelectUpdate",
86                   interceptors.InterceptorTest.postSelectCalled);
87  
88        table.processInterceptors(IDbEventInterceptor.PRE_ADDROW, data);
89        assertTrue("preAddRow", interceptors.InterceptorTest.preAddRowCalled);
90  
91        table.processInterceptors(IDbEventInterceptor.POST_ADDROW, data);
92        assertTrue("postAddRow", interceptors.InterceptorTest.postAddRowCalled);
93     }
94  
95  
96     /***
97      * DOCUMENT ME!
98      *
99      * @throws Exception DOCUMENT ME!
100     */
101    public void testInterceptorClassic() throws Exception {
102       Interceptor interceptor = new Interceptor();
103       interceptor.setClassName("interceptors.InterceptorTestClassic");
104       assertTrue("Class name should be interceptors.InterceptorTestClassic",
105                  interceptor.getClassName().equals("interceptors.InterceptorTestClassic"));
106 
107       Table table = new Table();
108       table.addInterceptor(interceptor);
109 
110       int i = table.getInterceptors()
111                    .size();
112       assertEquals("count interceptors", i, 1);
113 
114       DbEventInterceptorData data = new DbEventInterceptorData(null, null,
115                                                                null, table);
116 
117       table.processInterceptors(IDbEventInterceptor.PRE_INSERT, data);
118       assertTrue("preInsert",
119                  interceptors.InterceptorTestClassic.preInsertCalled);
120 
121       table.processInterceptors(IDbEventInterceptor.POST_INSERT, data);
122       assertTrue("postInsert",
123                  interceptors.InterceptorTestClassic.postInsertCalled);
124 
125       table.processInterceptors(IDbEventInterceptor.PRE_UPDATE, data);
126       assertTrue("preUpdate",
127                  interceptors.InterceptorTestClassic.preUpdateCalled);
128 
129       table.processInterceptors(IDbEventInterceptor.POST_UPDATE, data);
130       assertTrue("postUpdate",
131                  interceptors.InterceptorTestClassic.postUpdateCalled);
132 
133       table.processInterceptors(IDbEventInterceptor.PRE_DELETE, data);
134       assertTrue("preUpdate",
135                  interceptors.InterceptorTestClassic.preDeleteCalled);
136 
137       table.processInterceptors(IDbEventInterceptor.POST_DELETE, data);
138       assertTrue("postUpdate",
139                  interceptors.InterceptorTestClassic.postDeleteCalled);
140 
141       table.processInterceptors(IDbEventInterceptor.PRE_SELECT, data);
142       assertTrue("preSelectUpdate",
143                  interceptors.InterceptorTestClassic.preSelectCalled);
144 
145       table.processInterceptors(IDbEventInterceptor.POST_SELECT, data);
146       assertTrue("postSelectUpdate",
147                  interceptors.InterceptorTestClassic.postSelectCalled);
148    }
149 }