View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2000 William Chesters
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     William Chesters <williamc@paneris.org>
42   *     http://paneris.org/~williamc
43   *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
44   */
45  package org.melati.poem.test;
46  
47  import org.melati.poem.Group;
48  import org.melati.poem.Table;
49  import org.melati.poem.CachedSelection;
50  import org.melati.poem.TableInfo;
51  import org.melati.poem.User;
52  
53  /**
54   * Test the behaviour of CachedSelections in a multithreaded setup.
55   * 
56   * @see org.melati.poem.CachedSelection
57   */
58  public class CachedSelectionTest extends PoemTestCase {
59  
60    /**
61     * Constructor.
62     */
63    public CachedSelectionTest() {
64      super();
65    }
66  
67    public CachedSelectionTest(String name) {
68      super(name);
69    }
70    protected void setUp()
71        throws Exception {
72      super.setUp();
73    }
74  
75    protected void tearDown()
76        throws Exception {
77      super.tearDown();
78    }
79  
80    /**
81     * @see org.melati.poem.CachedSelection#firstObject()
82     */
83    public void testFirstObject() {
84      CachedSelection<TableInfo> cachedSelection = new CachedSelection<TableInfo>(getDb().getTableInfoTable(), null, null, null);
85      if (!getDb().getDbms().canDropColumns()) {
86        return;
87      }
88      assertEquals("tableInfo/0", cachedSelection.firstObject().toString());
89    }
90  
91    /**
92     * @see org.melati.poem.CachedSelection#nth(int)
93     */
94    public void testNth() {
95      CachedSelection<TableInfo> cachedSelection = new CachedSelection<TableInfo>(getDb().getTableInfoTable(), null, null, null);
96      if (!getDb().getDbms().canDropColumns()) {
97        return;
98      }
99      assertEquals("tableInfo/0", cachedSelection.nth(0).toString());
100     assertEquals("tableInfo/7", cachedSelection.nth(6).toString());
101     assertNull(cachedSelection.nth(999));
102   }
103 
104   /**
105    * Test multi-table selection.
106    */
107   public void testMultiTableSelection() {
108     getDb().uncache();
109     Table<?>[] others = new Table[] {getDb().getGroupMembershipTable(),
110                                   getDb().getGroupTable()};
111     String query =  
112     getDb().getUserTable().troidColumn().fullQuotedName() +
113     // user.id
114     " = 1 AND " +
115     getDb().getGroupMembershipTable().getUserColumn().fullQuotedName() +
116     //groupmembership.user 
117     " = " +
118     // user.id 
119     getDb().getUserTable().troidColumn().fullQuotedName()   +
120     " AND " +
121     getDb().getGroupMembershipTable().quotedName()  + "." +
122     getDb().getGroupMembershipTable().getGroupColumn().quotedName()
123     //groupmembership.group 
124     + " = " +  
125     //group.id
126     getDb().getGroupTable().troidColumn().fullQuotedName() + 
127     " AND " + 
128     getDb().getGroupTable().troidColumn().fullQuotedName()  +
129     // group.id
130     " = 0";
131    
132     int count = getDb().getQueryCount();
133     
134     
135     CachedSelection<User> cachedSelection = new CachedSelection<User>(
136         getDb().getUserTable(), query, null, others);
137     assertEquals(count + 4, getDb().getQueryCount());    
138     assertEquals("_administrator_", cachedSelection.nth(0).toString());
139     assertEquals(count + 6, getDb().getQueryCount());    
140     assertEquals("_administrator_", cachedSelection.nth(0).toString());
141     assertEquals(count + 6, getDb().getQueryCount());
142     String currentName = getDb().guestUser().getName();
143     String lastQuery = getDb().getLastQuery(); 
144     assertEquals(count + 8, getDb().getQueryCount());
145     assertEquals(lastQuery, getDb().getLastQuery());
146     getDb().guestUser().setName(currentName);
147     lastQuery = getDb().getLastQuery();
148     assertEquals("_administrator_", cachedSelection.nth(0).toString());
149     assertEquals("_administrator_", cachedSelection.nth(0).toString());
150     Group g = getDb().getGroupTable().getGroupObject(0);
151     g.setName(g.getName());
152     assertEquals("_administrator_", cachedSelection.nth(0).toString());
153     assertEquals("_administrator_", cachedSelection.nth(0).toString());
154     assertEquals("org.melati.poem.CachedSelection " + 
155             "SELECT " + getDb().getDbms().getQuotedName("user") + "." + getDb().getDbms().getQuotedName("id") + 
156             " FROM " + getDb().getDbms().getQuotedName("user") + ", " + 
157             getDb().getDbms().getQuotedName("groupmembership") + ", " + 
158             getDb().getDbms().getQuotedName("group") + " WHERE " + 
159             "(" + getDb().getDbms().getQuotedName("user") + "." + getDb().getDbms().getQuotedName("id") + 
160             " = 1 AND " + getDb().getDbms().getQuotedName("groupmembership") + "." + 
161             getDb().getDbms().getQuotedName("user") + " = " + 
162             getDb().getDbms().getQuotedName("user") + "." + getDb().getDbms().getQuotedName("id") + " AND " + 
163             getDb().getDbms().getQuotedName("groupmembership")  + "." + getDb().getDbms().getQuotedName("group") + 
164             " = " + getDb().getDbms().getQuotedName("group") + "." + getDb().getDbms().getQuotedName("id") + 
165             " AND " + 
166             getDb().getDbms().getQuotedName("group") + "." + getDb().getDbms().getQuotedName("id") + 
167             " = 0) ORDER BY " + getDb().getDbms().getQuotedName("user") + "." + getDb().getDbms().getQuotedName("name"), 
168             cachedSelection.toString());
169     getDb().setLogSQL(false);
170   }
171   
172   /**
173    * Test toString. 
174    */
175   public void testToString() {
176     
177   }
178 }