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 DocumentProxyTest extends BaseDocumentProxyTest {
35  
36      /***
37       * Builds all tests for DocumentProxies.
38       * @param name the tests name
39       */
40      public DocumentProxyTest(String name) {
41          super(name);
42      }
43  
44      /***
45       * Tests the equality of two Documents.
46       */
47      public void testEquals() {
48          System.out.println("-> testEquals");
49  
50          DDocument doc = getDatabase().createDocument();
51          doc.save();
52          DDocument receivedDoc = getDatabase().getDocumentByUNID(doc.getUniversalID());
53          assertEquals("Documents should be equal but are not.", doc, receivedDoc);
54  
55          DBaseDocument anotherOne = createDBaseDocument();
56          assertTrue("Documents should NOT be equal but are.", !doc.equals(anotherOne));
57  
58          doc.remove(true);
59      }
60  
61      /***
62       * Tests saving of a document.
63       *
64       */
65      public void testSave() {
66          System.out.println("-> testSave");
67  
68          DDocument doc = getDatabase().createDocument();
69          String someValue = "someValueForTestSave";
70          doc.appendItemValue(getItemName(), someValue);
71          doc.save();
72  
73          String uniID = doc.getUniversalID();
74  
75          DDocument received = getDatabase().getDocumentByUNID(uniID);
76          String receivedValue = received.getItemValueString(getItemName());
77          assertEquals(
78              "The Document received from the database should contain an item, but does not.",
79              someValue, receivedValue);
80  
81          assertTrue("The Document received should be equal to the previously saved one.", doc.equals(received));
82  
83          doc.remove(true);
84      }
85  
86      /***
87       * Tests save(boolean).
88       */
89      public void testSaveBoolean() {
90          System.out.println("-> testSaveBoolean");
91          DDocument doc = getDatabase().createDocument();
92          String someValue = "someValueForTestSave";
93          doc.appendItemValue(getItemName(), someValue);
94          boolean saved = doc.save(true);
95          assertTrue("The save action should work properly.", saved);
96  
97          doc.appendItemValue(getItemName() + getItemName(), someValue);
98          saved = doc.save(false);
99          assertTrue("The unforced save action should work properly.", saved);
100 
101         String uniID = doc.getUniversalID();
102 
103         DDocument received = getDatabase().getDocumentByUNID(uniID);
104         assertNotNull("The received document should exist.", received);
105         String receivedValue = received.getItemValueString(getItemName());
106         assertEquals("Document should contain an item, but does not.", someValue, receivedValue);
107 
108         String receivedValue1 = received.getItemValueString(getItemName() + getItemName());
109         assertEquals(" Document should contain a second item, but does not.", someValue, receivedValue1);
110         assertTrue("Document should be equal to the previously saved one.", doc.equals(received));
111 
112         doc.remove(true);
113     }
114 
115     /***
116      * Tests save(boolean, boolean).
117      */
118     public void testSaveBooleanBoolean() {
119         System.out.println("-> testSaveBooleanBoolean");
120 
121 //      todo test save(boolean, boolean) if another user is editing the document
122 //      see also testSaveWithOneArgument in DocumentProxyTest
123 
124         DDocument doc = getDatabase().createDocument();
125         String someValue = "someValueForTestSave";
126         doc.appendItemValue(getItemName(), someValue);
127         boolean saved = doc.save(true, false);
128         assertTrue("The save action should work properly.", saved);
129 
130         doc.appendItemValue(getItemName() + getItemName(), someValue);
131         saved = doc.save(true, true);
132         assertTrue("The unforced save action should work properly.", saved);
133 
134         String uniID = doc.getUniversalID();
135 
136         DDocument received = getDatabase().getDocumentByUNID(uniID);
137         assertNotNull("The received document should exist.", received);
138         String receivedValue = received.getItemValueString(getItemName());
139         assertEquals("Document should contain an item, but does not.", someValue, receivedValue);
140 
141         String receivedValue1 = received.getItemValueString(getItemName() + getItemName());
142         assertEquals("Document should contain a second item, but does not.", someValue, receivedValue1);
143         assertTrue("Document should be equal to the previously saved one.", doc.equals(received));
144 
145         doc.remove(true);
146     }
147 
148     /***
149      * Tests relative general methods as isNewNote and getNoteId.
150      *
151      */
152     public void testGeneralInfo() {
153         System.out.println("-> testGeneralInfo");
154         DDocument doc = getDatabase().createDocument();
155         assertTrue("Document should be new (not saved)", doc.isNewNote());
156         doc.save();
157         assertTrue("Document should NOT be new (saved) (" + getClass().getName() + ")", !doc.isNewNote());
158 
159         String noteId = doc.getNoteID();
160         String received = getDatabase().getDocumentByUNID(doc.getUniversalID()).getNoteID();
161         assertTrue("The NoteID of a saved one differs from the direct accessible one.", noteId.equals(received));
162 
163         doc.remove(true);
164     }
165 
166     /***
167      * Tests the functionality of a document to be a response.
168      *
169      */
170     public void testResponses() {
171         System.out.println("-> testResponses");
172         DDocument doc = getDatabase().createDocument();
173         doc.save();
174         assertTrue("Document should be no response.", !doc.isResponse());
175 
176         Iterator it = doc.getResponses();
177         assertTrue("Document should have no responses.", !it.hasNext());
178 
179         DDocument parent = getDatabase().createDocument();
180         parent.save();
181         doc.makeResponse(parent);
182         assertTrue("Document should be a response.", doc.isResponse());
183 
184         parent.save();
185         doc.save();
186         it = parent.getResponses();
187         assertTrue("Document should have responses.", it.hasNext());
188 
189     }
190 
191     /***
192      * Tests a document to its ability to have parents.
193      *
194      */
195     public void testParents() {
196         System.out.println("-> testParents");
197 
198         DDocument doc1 = getDatabase().createDocument();
199         DDocument doc2 = getDatabase().createDocument();
200         doc1.save();
201         doc2.save();
202         assertNull("Document has a parent, but should have NONE.", doc1.getParentDocument());
203 
204         DDocument parent = getDatabase().createDocument();
205         parent.save();
206         doc1.makeResponse(parent);
207         doc2.makeResponse(parent);
208 
209         DDocument parent1 = doc1.getParentDocument();
210         assertTrue("Document has no parent, but should have one.", (parent1 != null && parent1.equals(parent)));
211 
212         DDocument parent2 = doc2.getParentDocument();
213         assertTrue("Document has no parent, but should have one.", (parent2 != null && parent2.equals(parent)));
214 
215         assertTrue("Parent has wrong UniveralID.", parent2.getUniversalID().equals(parent.getUniversalID()));
216 
217         doc1.remove(true);
218         doc2.remove(true);
219         parent.remove(true);
220     }
221 
222     /***
223      * Tests the copyToDatabase method.
224      */
225     public void testCopyToDatabase() {
226         DDocument doc = getDatabase().createDocument();
227         String itemValue = "some really unimportant text";
228         doc.appendItemValue(getItemName(), itemValue);
229         doc.save();
230 
231         DDatabase newDB = getSession().createDatabase("", System.currentTimeMillis() + "testCopyToDatabase.nsf");
232         doc.copyToDatabase(newDB);
233 
234         Iterator it = newDB.getAllDocuments();
235 
236         boolean ok = false;
237         while (it.hasNext()) {
238             DDocument doc1 = (DDocument) it.next();
239             if (itemValue.equals(doc1.getItemValueString(getItemName()))) {
240                 ok = true;
241             }
242         }
243         assertTrue("The document is NOT in the new Database", ok);
244         doc.remove(true);
245         newDB.remove();
246     }
247 
248     /***
249      * Tests send(String).
250      */
251     public void testSendString() {
252         System.out.println("-> testSendString");
253         DDocument doc = getDatabase().createDocument();
254         doc.replaceItemValue("Subject", "# Mailing test from Domingo tests - please delete this mail.");
255         doc.save();
256         String user = getSession().getUserName();
257         doc.send(user);
258         assertNotNull("Document not sent. Maybe disconnected from mail server?.", doc.getItemValueDate("PostedDate"));
259         doc.remove(true);
260     }
261 
262     /***
263      * Tests setSaveMessageOnSend(boolean).
264      */
265     public void testSetSaveMessageOnSaveTrue() {
266         System.out.println("-> testSaveMessageOnSend");
267         DDocument doc = getDatabase().createDocument();
268         doc.replaceItemValue("Subject", "# Mailing test from Domingo tests - please delete this mail.");
269         doc.setSaveMessageOnSend(true);
270         String user = getSession().getUserName();
271         doc.send(user);
272         assertNotNull("Document not sent. Maybe disconnected from mail server?.", doc.getItemValueDate("PostedDate"));
273         doc.remove(true);
274     }
275 
276     /***
277      * Tests setSaveMessageOnSend(boolean).
278      */
279     public void testSetSaveMessageOnSaveFalse() {
280         System.out.println("-> testSaveMessageOnSend");
281         DDocument doc = getDatabase().createDocument();
282         doc.replaceItemValue("Subject", "# Mailing test from Domingo tests - please delete this mail.");
283         doc.setSaveMessageOnSend(false);
284         String user = getSession().getUserName();
285         doc.send(user);
286         assertNotNull("Document not sent. Maybe disconnected from mail server?.", doc.getItemValueDate("PostedDate"));
287         doc.remove(true);
288     }
289 
290     /***
291      * Tests setEncryptOnSendTrue(boolean).
292      */
293     public void testSetEncryptOnSendTrue() {
294         System.out.println("-> testSetEncryptOnSendTrue");
295         DDocument doc = getDatabase().createDocument();
296         doc.replaceItemValue("Subject", "# Mailing test from Domingo tests - please delete this mail.");
297         doc.setEncryptOnSend(false);
298         doc.save();
299         String user = getSession().getUserName();
300         doc.send(user);
301         assertNotNull("Document not sent. Maybe disconnected from mail server?.", doc.getItemValueDate("PostedDate"));
302         doc.remove(true);
303     }
304 
305     /***
306      * Tests setEncryptOnSendTrue(boolean).
307      */
308     public void testSetEncryptOnSendFalse() {
309         System.out.println("-> testSetEncryptOnSendFalse");
310         DDocument doc = getDatabase().createDocument();
311         doc.replaceItemValue("Subject", "# Mailing test from Domingo tests - please delete this mail.");
312         doc.setEncryptOnSend(false);
313         doc.save();
314         String user = getSession().getUserName();
315         doc.send(user);
316         assertNotNull("Document not sent. Maybe disconnected from mail server?.", doc.getItemValueDate("PostedDate"));
317         doc.remove(true);
318     }
319 
320     /***
321      * Tests setSignOnSendTrue(boolean).
322      */
323     public void testSetSignOnSendTrue() {
324         System.out.println("-> testSetSignOnSendTrue");
325         DDocument doc = getDatabase().createDocument();
326         doc.replaceItemValue("Subject", "# Mailing test from Domingo tests - please delete this mail.");
327         doc.setSignOnSend(false);
328         doc.save();
329         String user = getSession().getUserName();
330         doc.send(user);
331         assertNotNull("Document not sent. Maybe disconnected from mail server?.", doc.getItemValueDate("PostedDate"));
332         doc.remove(true);
333     }
334 
335     /***
336      * Tests setEncryptOnSendTrue(boolean).
337      */
338     public void testSetSignOnSendFalse() {
339         System.out.println("-> testSetSignOnSendFalse");
340         DDocument doc = getDatabase().createDocument();
341         doc.replaceItemValue("Subject", "# Mailing test from Domingo tests - please delete this mail.");
342         doc.setSignOnSend(false);
343         doc.save();
344         String user = getSession().getUserName();
345         doc.send(user);
346         assertNotNull("Document not sent. Maybe disconnected from mail server?.", doc.getItemValueDate("PostedDate"));
347         doc.remove(true);
348     }
349 
350     /***
351      * Tests sign(boolean).
352      */
353     public void testSign() {
354         System.out.println("-> testSign");
355         DDocument doc = getDatabase().createDocument();
356         doc.replaceItemValue("Subject", "# Mailing test from Domingo - please delete this mail.");
357         doc.sign();
358         doc.save();
359         String user = getSession().getUserName();
360         doc.send(user);
361         assertNotNull("Document not sent. Maybe disconnected from mail server?.", doc.getItemValueDate("PostedDate"));
362         doc.remove(true);
363     }
364 
365     /***SetSignOnSend
366      * Tests send(List).
367      */
368     public void testSendList() {
369         System.out.println("-> testSendString");
370         DDocument doc = getDatabase().createDocument();
371         doc.replaceItemValue("Subject", "# Mailing test from Domingo tests - please delete this mail.");
372         doc.save();
373         String user = getSession().getUserName();
374         List list = new ArrayList();
375         list.add(user);
376         doc.send(list);
377         assertNotNull("Document not sent. Maybe disconnected from mail server?.", doc.getItemValueDate("PostedDate"));
378         doc.remove(true);
379     }
380 
381     /***
382      * Tests send(String).
383      */
384     public void testDateTimeValues() {
385         System.out.println("-> testDateTimeValues");
386         DDocument doc = getDatabase().createDocument();
387         try {
388             Calendar date1 = new GregorianCalendar(2004, 03, 03);
389             Calendar date2 = new GregorianCalendar(2004, 04, 04);
390             doc.appendItemValue("date1", date1);
391             doc.appendItemValue("date2", date2);
392             List value1 = doc.getItemValue("date1");
393             doc.save();
394             List value2 = doc.getItemValue("date2");
395             assertEquals("Got wrong date from document.", date1, value1.get(0));
396             assertEquals("Got wrong date from document.", date2, value2.get(0));
397         } catch (Exception e) {
398             fail("Unexpected Exception. Maybe a recycle problem?");
399             e.printStackTrace();
400         }
401         doc.remove(true);
402     }
403 
404     /***
405      * Returns a normal Document.
406      * @return DBaseDocument a normal Document
407      */
408     protected DBaseDocument createDBaseDocument() {
409         return getDatabase().createDocument();
410     }
411 
412     /***
413      * Sets up the proper test environment for every method again.
414      */
415     public void setUp() {
416         baseDocumentSetUp();
417     }
418 }