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.Iterator;
26  import java.util.List;
27  
28  /***
29   * @author MarcusT
30   */
31  public final class DatabaseProxyTest extends BaseProxyTest {
32  
33      /***
34       * @param name the name of the test
35       */
36      public DatabaseProxyTest(String name) {
37          super(name);
38      }
39  
40      /***
41       * Setup test case.
42       */
43      public void setUp() {
44          baseSetUp();
45      }
46  
47      /***
48       * test replicate.
49       */
50      public void testReplicate() {
51          // todo test replicate
52      }
53  
54      /***
55       * test createReplica.
56       */
57      public void testCreateReplica() {
58          System.out.println("-> testCreateReplica");
59          DDatabase replica = null;
60          try {
61              replica = getDatabase().createReplica(getServerName(), "temp/log.nsf");
62          } catch (DNotesException e1) {
63              e1.printStackTrace();
64              fail("Cannot create replica: temp/log.nsf");
65          }
66          replica.remove();
67      }
68  
69      /***
70       * Tests retrieval of Documents in a newly created database that is removed
71       * after tests.
72       */
73      public void testGetAllDocuments() {
74          System.out.println("-> testGetAllDocuments");
75  
76          String dbName = System.currentTimeMillis() + "DBTest_testGetAllDocuments.nsf";
77          DDatabase newDB = getSession().createDatabase(getServerName(), dbName);
78  
79          DDocument doc1 = newDB.createDocument();
80          doc1.replaceItemValue("item1", "value1");
81          doc1.replaceItemValue("item2", "value2");
82          doc1.save();
83          DDocument doc2 = newDB.createDocument();
84          doc2.replaceItemValue("item3", "value3");
85          doc2.replaceItemValue("item4", "value4");
86          doc2.save();
87  
88          int docCounter = 0;
89          Iterator it = getDatabase().getAllDocuments();
90          for (int i = 0; i < 2; i++) {
91              DDocument doc = (DDocument) it.next();
92              assertNotNull("At least two documents should be in database, but no " + (2 - docCounter) + " is missing.", doc);
93              if (doc != null) {
94                  docCounter++;
95              }
96          }
97  
98          assertTrue("Database should hold at least 2 documents.", 2 == docCounter);
99  
100         newDB.remove();
101     }
102 
103     /***
104      * Tests creation of a database from the Notes log database template.
105      */
106     public void testCreateDatabaseFromTemplate() {
107         System.out.println("-> testCreateDatabaseFromTemplate");
108 
109         DDatabase template = null;
110         try {
111             template = getSession().getDatabase(getServerName(), "log.ntf");
112         } catch (DNotesException e) {
113             e.printStackTrace();
114             fail("Cannot open local database log.ntf");
115         }
116         String newName = System.currentTimeMillis() + "testCreateDatabaseFromTemplate.nsf";
117         DDatabase newDB = null;
118         try {
119             System.out.println("   template.createDatabaseFromTemplate");
120             newDB = template.createDatabaseFromTemplate(getServerName(), newName, true);
121         } catch (DNotesException e) {
122             assertTrue("Database could not be created: " + e, false);
123         }
124         assertNotNull("Database not created.", newDB);
125         System.out.println("   db created");
126 
127         assertEquals("Databases name does not match.", newName, newDB.getFilePath());
128 
129         String viewName = "Database//Sizes";
130         DView view = newDB.getView(viewName);
131         assertNotNull("The received view should not be NULL.", view);
132         assertEquals("The views name does not match.", viewName, view.getName());
133 
134         DDatabase newDB2 = null;
135         try {
136             newDB2 = template.createDatabaseFromTemplate(getServerName(), newName, true);
137         } catch (DNotesException e) {
138             e.printStackTrace();
139         }
140         assertNull("Database should NOT have been created: ", newDB2);
141 
142         newDB.remove();
143     }
144 
145     /***
146      * Tests removing of a database.
147      */
148     public void testRemove() {
149         System.out.println("-> DatabaseProxy.testRemove");
150 
151         String dbName = System.currentTimeMillis() + "DatabaseProxyTest.nsf";
152         DDatabase lDatabase = getSession().createDatabase(getServerName(), dbName);
153 
154         lDatabase.remove();
155 
156         //        DDocument doc = lDatabase.createDocument();
157         //        assertNull("Document should not have been created.", doc);
158     }
159 
160     /***
161      * Tests the creation of documents.
162      */
163     public void testCreateDocument() {
164         System.out.println("-> testCreateDocument");
165         DDocument doc = getDatabase().createDocument();
166         assertTrue("Database should have created a document, but returned: " + doc, (doc instanceof DDocument));
167         assertTrue("Document should be new.", doc.isNewNote());
168     }
169 
170     /***
171      * Tests the retrieval of documents by their UnId.
172      */
173     public void testGetDocumentByUNID() {
174         System.out.println("-> testGetDocumentByUNID");
175         DDocument doc = getDatabase().createDocument();
176         doc.save();
177         DDocument newDoc = getDatabase().getDocumentByUNID(doc.getUniversalID());
178         assertEquals("Document and requested Document (by UnID) should be equal.", doc, newDoc);
179 
180         doc.remove(true);
181     }
182 
183     /***
184      * Tests the retrieval of documents by their NoteId.
185      */
186     public void testGetDocumentByID() {
187         System.out.println("-> testGetDocumentByID");
188         DDocument doc = getDatabase().createDocument();
189         doc.save();
190         DDocument newDoc = getDatabase().getDocumentByID(doc.getNoteID());
191         assertEquals("Document and requested Document (by ID) should be equal.", doc, newDoc);
192 
193         doc.remove(true);
194     }
195 
196     /***
197      * Tests the retrieval of the databases filename.
198      */
199     public void testGetFileName() {
200         System.out.println("-> testGetFileName");
201         String fileName = getDatabase().getFileName();
202         String path = getDatabase().getFilePath();
203         assertTrue("The fileName should be equals to the end of the file path", path.endsWith(fileName));
204     }
205 
206     /***
207      * Tests the retrieval of the databases file path.
208      */
209     public void testGetFilePath() {
210         System.out.println("-> testGetFilePath");
211         String path = getDatabase().getFilePath();
212         assertTrue("At least the database file should exist.", path != null && path.length() > 0);
213     }
214 
215     /***
216      * Tests the retrieval of the databases filename.
217      */
218     public void testGetCurrentAccessLevel() {
219         System.out.println("-> testGetCurrentAccessLevel");
220         int level = getDatabase().getCurrentAccessLevel();
221         assertTrue("Illegal access level", DACL.LEVEL_NOACCESS <= level && level <= DACL.LEVEL_MANAGER);
222     }
223 
224     /***
225      * Tests creation or retrieval of profile documents.
226      */
227     public void testGetProfileDocument() {
228         System.out.println("-> testGetProfileDocument");
229         String profile = "some_profile_name";
230         String profileKey = "some_Profile_Key";
231         DProfileDocument pDoc = getDatabase().getProfileDocument(profile, profileKey);
232         assertNotNull("Document should note be null.", pDoc);
233     }
234 
235     /***
236      * Tests accessing views.
237      */
238     public void testGetView() {
239         System.out.println("-> testGetView");
240         String viewName = "Database//Sizes";
241         DView view = getDatabase().getView(viewName);
242         assertTrue(
243             "Each Notes log DB (log.nsf) should have a view '" + viewName + ", but this DB has not: " + view,
244             view != null && viewName.equals(view.getName()));
245     }
246 
247     /***
248      * Tests accessing agents.
249      */
250     public void testGetAgent() {
251         System.out.println("-> testGetAgent");
252         String agentName = "OutOfOffice";
253         DDatabase nab = null;
254         try {
255             nab = getSession().getDatabase(getServerName(), "names.nsf");
256         } catch (DNotesException e) {
257             e.printStackTrace();
258             fail("Cannot open local names.nsf");
259         }
260         DAgent agent = nab.getAgent(agentName);
261         assertTrue(
262             "Each Notes NAB (names.nsf) should have an agent '" + agentName + ", but this DB has not.",
263             agent != null && agentName.equals(agent.getName()));
264     }
265 
266     /***
267      * Tests getting calendar values from a document that is taken via view.getAllDocuments().
268      */
269     public void testAllDocs() {
270         DDocument doc = null;
271         try {
272             DDatabase nab = getSession().getDatabase("", "names.nsf");
273             DView users = nab.getView("($Users)");
274             Iterator allDocs = users.getAllDocuments();
275             doc = (DDocument) allDocs.next();
276         } catch (DNotesException e) {
277             e.printStackTrace();
278             fail("Cannot open local names.nsf");
279         }
280         try {
281             List itemValue = doc.getItemValue("$Revisions");
282             System.out.println(itemValue);
283         } catch (DNotesRuntimeException e) {
284             e.printStackTrace();
285             fail("Cannot get date/time.");
286         }
287     }
288 }