1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH München (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo;
24  
25  import java.util.ArrayList;
26  import java.util.Calendar;
27  import java.util.GregorianCalendar;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /***
32   * @author MarcusT
33   */
34  public final class ViewProxyTest extends BaseProxyTest {
35  
36      private DDatabase newDB;
37      private DView view;
38      private DView newView;
39  
40      private Calendar correctNow;
41      private String viewName;
42  
43      /***
44       * @param name the name of the test
45       */
46      public ViewProxyTest(String name) {
47          super(name);
48      }
49  
50      /***
51       * todo testRefresh (But how?). Just call the method and check for no exceptions.
52       */
53      public void testRefresh() {
54  
55      }
56  
57      /***
58       * Tests the method getName().
59       */
60      public void xtestGetName() {
61          System.out.println("-> ViewProxy.testGetName");
62          String result = view.getName();
63          assertTrue("View name should be '" + viewName + "' not '" + result + "'", viewName.equals(result));
64      }
65  
66      /***
67       * Tests <code>getAllDocumentsByKey</code> for Lists. Tests both methods
68       * with and without boolean parameter.
69       *
70       */
71      public void xtestGetAllDocumentsByKeyList() {
72          System.out.println("-> testGetAllDocumentsByKeyList");
73  
74          createNewDBAndView("testGetAllDocumentsByKeyList.nsf");
75  
76          List keys = new ArrayList();
77          keys.add("somePath");
78          keys.add(correctNow);
79          keys.add("ObjStoreName");
80          keys.add("DbName1");
81  
82          int docCounter = 0;
83          Iterator it = newView.getAllDocumentsByKey(keys, true);
84          while (it.hasNext()) {
85              DDocument doc = (DDocument) it.next();
86              doc.getNoteID();
87              docCounter++;
88          }
89          assertEquals("View should have 2 documents.", 2, docCounter);
90  
91          keys = new ArrayList();
92          keys.add("somePath");
93          keys.add(correctNow);
94          keys.add("ObjStoreName");
95          // partial key should be in non-categorised column:
96          keys.add("DbName");
97  
98          docCounter = 0;
99          it = newView.getAllDocumentsByKey(keys, false);
100         while (it.hasNext()) {
101             DDocument doc = (DDocument) it.next();
102             doc.getNoteID();
103             docCounter++;
104         }
105         assertTrue("Found " + docCounter + " documents, but 4 expected.", 4 == docCounter);
106 
107         newDB.remove();
108     }
109 
110     /***
111      * Tests a method to retrieve all documents that match a string in the
112      * first column.
113      */
114     public void testGetAllDocumentsByKeyString() {
115         System.out.println("-> testGetAllDocumentsByKeyString");
116 
117         createNewDBAndView("testGetAllDocumentsByKeyString.nsf");
118 
119         String key = "somePath";
120 
121         int docCounter = 0;
122         Iterator it = newView.getAllDocumentsByKey(key, true);
123         while (it.hasNext()) {
124             DDocument doc = (DDocument) it.next();
125             doc.getNoteID();
126             docCounter++;
127         }
128         assertEquals("View should have 4 documents.", 4, docCounter);
129 
130         // todo a test for a partial key is still required here
131         // but it makes no sense in a categorized view, but that are the only
132         // ones which are available in a database derived from log.ntf...
133 
134         newDB.remove();
135     }
136 
137     /***
138      * Tests the retrieval of one suitable document by specifying a key list.
139      */
140     public void testGetDocumentByKeyList() {
141         System.out.println("-> testGetDocumentByKeyList");
142 
143         createNewDBAndView("testGetDocumentByKeyList.nsf");
144 
145         List keys = new ArrayList();
146         keys.add("somePath");
147         keys.add(correctNow);
148         keys.add("ObjStoreName");
149         keys.add("DbName1");
150 
151         DDocument doc = newView.getDocumentByKey(keys, true);
152         assertNotNull("The view should have a document with this key list.", doc);
153         assertEquals("The Document should have a field 'DbName'.", "DbName1", doc.getItemValueString("DbName"));
154 
155         keys = new ArrayList();
156         keys.add("somePath");
157         keys.add(correctNow);
158         keys.add("ObjStoreName");
159         // partial key should be in non-categorised column:
160         keys.add("DbName");
161 
162         DDocument doc1 = newView.getDocumentByKey(keys, false);
163         assertNotNull("The view should have a document with this key list.", doc1);
164         String fieldDbName = doc1.getItemValueString("DbName");
165         assertTrue(
166             "The Document should have a field 'DbName'.",
167             ("DbName1".equals(fieldDbName) || "DbName2".equals(fieldDbName)));
168 
169         newDB.remove();
170     }
171 
172     /***
173      * Tests the retrieval of one suitable document by specifying a key list.
174      */
175     public void xtestGetDocumentByKeyString() {
176         System.out.println("-> testGetDocumentByKeyString");
177 
178         createNewDBAndView("testGetDocumentByKeyString.nsf");
179         String key = "somePath";
180 
181         DDocument doc = newView.getDocumentByKey(key, true);
182         assertNotNull("The view should contain a Document with that key.", doc);
183         String fieldDBName = doc.getItemValueString("DbName");
184         boolean match = "DbName1".equals(fieldDBName) || "DbName2".equals(fieldDBName);
185         assertTrue("Document should have a field 'DbName'.", match);
186 
187         // todo a test for a partial key is still required here
188         // but it makes no sense in a categorized view, but that are the only
189         // ones which are available in a database derived from log.ntf...
190 
191         newDB.remove();
192     }
193 
194     /***
195      * Tests the method getAllEntries.
196      */
197     public void xtestGetAllEntries() {
198         System.out.println("-> testGetAllEntries");
199 
200         createNewDBAndView(System.currentTimeMillis() + "testGetAllEntries.nsf");
201 
202         Iterator it = newView.getAllEntries();
203         int itCounter = 0;
204         while (it.hasNext()) {
205             DViewEntry entry = (DViewEntry) it.next();
206             assertNotNull("ViewEntry " + itCounter + " should not be null.", entry);
207             itCounter++;
208         }
209         int expected = 4;
210         assertEquals("The view should have " + expected + " entries.", expected, itCounter);
211 
212         newDB.remove();
213     }
214 
215     /***
216      * Tests the method getAllEntries.
217      */
218     public void xtestGetAllEntriesFrom() {
219         System.out.println("-> testGetAllEntriesFrom");
220 
221         createNewDBAndView(System.currentTimeMillis() + "testGetAllEntriesFrom.nsf");
222 
223         Iterator it = newView.getAllEntries();
224         int itCounter = 0;
225         if (it.hasNext()) {
226             DViewEntry entry = (DViewEntry) it.next();
227             assertNotNull("ViewEntry " + itCounter + " should not be null.", entry);
228             Iterator iter = newView.getAllEntries(entry);
229             while (iter.hasNext()) {
230                 DViewEntry entry1 = (DViewEntry) iter.next();
231                 assertNotNull("ViewEntry " + itCounter + " should not be null.", entry1);
232                 itCounter++;
233             }
234         }
235         int expected = 4;
236         assertEquals("The view should have " + expected + " entries.", expected, itCounter);
237 
238         newDB.remove();
239     }
240 
241 
242     /***
243      * Tests the method getAllEntriesByKey(String, boolean).
244      */
245     public void xtestGetAllEntriesByKeyString() {
246         System.out.println("-> getAllEntriesByKeyString");
247 
248         createNewDBAndView(System.currentTimeMillis() + "getAllEntriesByKeyString.nsf");
249 
250         String key = "somePath";
251         Iterator it = newView.getAllEntriesByKey(key, true);
252         int itCounter = 0;
253         while (it.hasNext()) {
254             DViewEntry entry = (DViewEntry) it.next();
255             assertNotNull("ViewEntry " + itCounter + " should not be null.", entry);
256             itCounter++;
257         }
258         int expected = 4;
259         assertEquals("The view should have " + expected + " entries.", expected, itCounter);
260 
261         // todo some test for a partial key would be nice here
262 
263         newDB.remove();
264     }
265 
266     /***
267      * Tests the method getAllEntriesByKey(List, boolean).
268      */
269     public void xtestGetAllCategories() {
270         System.out.println("-> testGetAllCategories");
271         DView categorisedView = getDatabase().getView("MiscEvents");
272         Iterator iterator = categorisedView.getAllCategories(1);
273         while (iterator.hasNext()) {
274             DViewEntry entry = (DViewEntry) iterator.next();
275             if (entry.isCategory()) {
276                 System.out.println("Category: " + entry.getColumnValues().toString());
277             } else {
278                 System.out.println("    not a category");
279             }
280         }
281     }
282 
283     /***
284      * Tests the method getAllEntriesByKey(List, boolean).
285      */
286     public void xtestGetAllEntriesByKeyList() {
287         System.out.println("-> getAllEntriesByKeyList");
288 
289         createNewDBAndView(System.currentTimeMillis() + "getAllEntriesByKeyList.nsf");
290 
291         List list = new ArrayList();
292         list.add("somePath");
293         list.add(correctNow);
294         list.add("ObjStoreName");
295         list.add("DbName1");
296 
297         Iterator it = newView.getAllEntriesByKey(list, true);
298         int itCounter = 0;
299         while (it.hasNext()) {
300             DViewEntry entry = (DViewEntry) it.next();
301             assertNotNull("ViewEntry " + itCounter + " should not be null.", entry);
302             itCounter++;
303         }
304         int expected = 2;
305         assertEquals("The view should have " + expected + " entries.", expected, itCounter);
306 
307         list = new ArrayList();
308         list.add("somePath");
309         list.add(correctNow);
310         list.add("ObjStoreName");
311         list.add("DbName");
312 
313         it = newView.getAllEntriesByKey(list, false);
314         itCounter = 0;
315         while (it.hasNext()) {
316             DViewEntry entry = (DViewEntry) it.next();
317             assertNotNull("ViewEntry " + itCounter + " should not be null.", entry);
318             itCounter++;
319         }
320         expected = 4;
321         assertEquals("The view should have " + expected + " entries.", expected, itCounter);
322 
323         newDB.remove();
324     }
325 
326     /***
327      * Excluded part of setUp, because not in every test, a new DB is necessary.
328      * If this method were executed before every testMethod, each such
329      * testMethod would have to call <code>newDB.remove()</code>, but this way,
330      * only callers of this method need to call it.
331      *
332      * @param name the databases localhost filePath
333      */
334     private void createNewDBAndView(String name) {
335         String dbName = System.currentTimeMillis() + name;
336 
337         correctNow = new GregorianCalendar();
338         correctNow.clear(Calendar.HOUR);
339         correctNow.clear(Calendar.MINUTE);
340         correctNow.clear(Calendar.SECOND);
341         correctNow.clear(Calendar.MILLISECOND);
342 
343         DDatabase db = null;
344         try {
345             db = getSession().getDatabase("", "log.ntf");
346         } catch (DNotesException e1) {
347             e1.printStackTrace();
348             fail("Cannot open local database log.ntf");
349         }
350         try {
351             newDB = db.createDatabaseFromTemplate(getServerName(), dbName, true);
352         } catch (DNotesException e) {
353             assertTrue("Could not create DB from template.", false);
354         }
355 
356         DDocument doc1 = newDB.createDocument();
357         doc1.replaceItemValue("Form", "ObjStoreUsageForm");
358         doc1.replaceItemValue("ObjStoreName", "ObjStoreName");
359         doc1.replaceItemValue("DbName", "DbName1");
360         doc1.replaceItemValue("DbTitle", "DbTitle");
361         doc1.replaceItemValue("DocCount", 2);
362         doc1.replaceItemValue("Server", "somePath");
363         doc1.replaceItemValue("StartTime", correctNow);
364         doc1.save();
365         DDocument doc2 = newDB.createDocument();
366         doc2.replaceItemValue("Form", "ObjStoreUsageForm");
367         doc2.replaceItemValue("ObjStoreName", "ObjStoreName");
368         doc2.replaceItemValue("DbName", "DbName1");
369         doc2.replaceItemValue("DbTitle", "DbTitle");
370         doc2.replaceItemValue("DocCount", 2);
371         doc2.replaceItemValue("Server", "somePath");
372         doc2.replaceItemValue("StartTime", correctNow);
373         doc2.save();
374         DDocument doc3 = newDB.createDocument();
375         doc3.replaceItemValue("Form", "ObjStoreUsageForm");
376         doc3.replaceItemValue("ObjStoreName", "ObjStoreName");
377         doc3.replaceItemValue("DbName", "DbName2");
378         doc3.replaceItemValue("DbTitle", "DbTitle");
379         doc3.replaceItemValue("DocCount", 2);
380         doc3.replaceItemValue("Server", "somePath");
381         doc3.replaceItemValue("StartTime", correctNow);
382         doc3.save();
383         DDocument doc4 = newDB.createDocument();
384         doc4.replaceItemValue("Form", "ObjStoreUsageForm");
385         doc4.replaceItemValue("ObjStoreName", "ObjStoreName");
386         doc4.replaceItemValue("DbName", "DbName2");
387         doc4.replaceItemValue("DbTitle", "DbTitle");
388         doc4.replaceItemValue("DocCount", 2);
389         doc4.replaceItemValue("Server", "somePath");
390         doc4.replaceItemValue("StartTime", correctNow);
391         doc4.save();
392         newView = newDB.getView("Object Store Usage");
393     }
394 
395     /***
396      * Calls setUp on super and also sets: viewName and view.
397      */
398     public void setUp() {
399         baseSetUp();
400         correctNow = new GregorianCalendar();
401         viewName = "Miscellaneous Events";
402         view = getDatabase().getView(viewName);
403     }
404 }