1   /*
2    * $Source: /usr/cvsroot/melati/poem/src/test/java/org/melati/poem/test/CachedSelectionTest.java,v $
3    * $Revision: 1.28 $
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  
51  /**
52   * Test the behaviour of CachedSelections in a multithreaded setup.
53   * 
54   * @see org.melati.poem.CachedSelection
55   */
56  public class CachedSelectionTest extends PoemTestCase {
57  
58    /**
59     * Constructor.
60     */
61    public CachedSelectionTest() {
62      super();
63    }
64  
65    /**
66     * Constructor.
67     * @param name
68     */
69    public CachedSelectionTest(String name) {
70      super(name);
71    }
72    /**
73     * {@inheritDoc}
74     * @see org.melati.poem.test.PoemTestCase#setUp()
75     */
76    protected void setUp()
77        throws Exception {
78      super.setUp();
79    }
80  
81    /**
82     * {@inheritDoc}
83     * @see org.melati.poem.test.PoemTestCase#tearDown()
84     */
85    protected void tearDown()
86        throws Exception {
87      super.tearDown();
88    }
89  
90    /**
91     * @see org.melati.poem.CachedSelection#firstObject()
92     */
93    public void testFirstObject() {
94      CachedSelection cachedSelection = new CachedSelection(getDb().getTableInfoTable(), null, null, null);
95      if (!getDb().getDbms().canDropColumns()) {
96        return;
97      }
98      assertEquals("tableinfo/0", cachedSelection.firstObject().toString());
99    }
100 
101   /**
102    * @see org.melati.poem.CachedSelection#nth()
103    */
104   public void testNth() {
105     CachedSelection cachedSelection = new CachedSelection(getDb().getTableInfoTable(), null, null, null);
106     if (!getDb().getDbms().canDropColumns()) {
107       return;
108     }
109     assertEquals("tableinfo/0", cachedSelection.nth(0).toString());
110     assertEquals("tableinfo/7", cachedSelection.nth(6).toString());
111     assertNull(cachedSelection.nth(999));
112   }
113 
114   /**
115    * Test multi-table selection.
116    */
117   public void testMultiTableSelection() {
118     getDb().uncache();
119     Table[] others = new Table[] {getDb().getGroupMembershipTable(),
120                                   getDb().getGroupTable()};
121     String query =  
122     getDb().getUserTable().troidColumn().fullQuotedName() +
123     // user.id
124     " = 1 AND " +
125     getDb().getGroupMembershipTable().getUserColumn().fullQuotedName() +
126     //groupmembership.user 
127     " = " +
128     // user.id 
129     getDb().getUserTable().troidColumn().fullQuotedName()   +
130     " AND " +
131     getDb().getGroupMembershipTable().quotedName()  + "." +
132     getDb().getGroupMembershipTable().getGroupColumn().quotedName()
133     //groupmembership.group 
134     + " = " +  
135     //group.id
136     getDb().getGroupTable().troidColumn().fullQuotedName() + 
137     " AND " + 
138     getDb().getGroupTable().troidColumn().fullQuotedName()  +
139     // group.id
140     " = 0";
141    
142    // System.err.println("IN test:" + query);
143     int count = getDb().getQueryCount();
144     // FIXME counts differ between Maven and Eclipse, as cache is persistent 
145     
146     
147     CachedSelection cachedSelection = new CachedSelection(
148         getDb().getUserTable(), query, null, others);
149     assertEquals(count + 4, getDb().getQueryCount());    
150     //getDb().setLogSQL(true);
151     assertEquals("_administrator_", cachedSelection.nth(0).toString());
152     assertEquals(count + 6, getDb().getQueryCount());    
153     assertEquals("_administrator_", cachedSelection.nth(0).toString());
154     assertEquals(count + 6, getDb().getQueryCount());
155     String currentName = getDb().guestUser().getName();
156     String lastQuery = getDb().getLastQuery(); 
157     assertEquals(count + 8, getDb().getQueryCount());
158     assertEquals(lastQuery, getDb().getLastQuery());
159     getDb().guestUser().setName(currentName);
160     lastQuery = getDb().getLastQuery();
161     assertEquals("_administrator_", cachedSelection.nth(0).toString());
162     assertEquals("_administrator_", cachedSelection.nth(0).toString());
163     Group g = getDb().getGroupTable().getGroupObject(0);
164     g.setName(g.getName());
165     assertEquals("_administrator_", cachedSelection.nth(0).toString());
166     assertEquals("_administrator_", cachedSelection.nth(0).toString());
167     assertEquals("org.melati.poem.CachedSelection " + 
168             "SELECT " + getDb().getDbms().getQuotedName("user") + "." + getDb().getDbms().getQuotedName("id") + 
169             " FROM " + getDb().getDbms().getQuotedName("user") + ", " + 
170             getDb().getDbms().getQuotedName("groupmembership") + ", " + 
171             getDb().getDbms().getQuotedName("group") + " WHERE " + 
172             "(" + getDb().getDbms().getQuotedName("user") + "." + getDb().getDbms().getQuotedName("id") + 
173             " = 1 AND " + getDb().getDbms().getQuotedName("groupmembership") + "." + 
174             getDb().getDbms().getQuotedName("user") + " = " + 
175             getDb().getDbms().getQuotedName("user") + "." + getDb().getDbms().getQuotedName("id") + " AND " + 
176             getDb().getDbms().getQuotedName("groupmembership")  + "." + getDb().getDbms().getQuotedName("group") + 
177             " = " + getDb().getDbms().getQuotedName("group") + "." + getDb().getDbms().getQuotedName("id") + 
178             " AND " + 
179             getDb().getDbms().getQuotedName("group") + "." + getDb().getDbms().getQuotedName("id") + 
180             " = 0) ORDER BY " + getDb().getDbms().getQuotedName("user") + "." + getDb().getDbms().getQuotedName("name"), 
181             cachedSelection.toString());
182     getDb().setLogSQL(false);
183   }
184   
185   /**
186    * Test toString. 
187    */
188   public void testToString() {
189     
190   }
191 }