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.text.DateFormat;
26  import java.text.SimpleDateFormat;
27  import java.util.ArrayList;
28  import java.util.Calendar;
29  import java.util.GregorianCalendar;
30  import java.util.List;
31  import java.util.SimpleTimeZone;
32  import java.util.TimeZone;
33  
34  /***
35   * @author MarcusT
36   */
37  public final class ItemProxyTest extends BaseItemProxyTest {
38  
39      private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
40  
41      /***
42       * @param name the name of the test
43       */
44      public ItemProxyTest(String name) {
45          super(name);
46      }
47  
48      /***
49       * @see de.bea.domingo.BaseItemProxyTest#createDBaseItem()
50       * @return DBaseDocument a normal Document
51       */
52      protected DBaseItem createDBaseItem() {
53          DDocument doc = getDatabase().createDocument();
54          DBaseItem item = doc.appendItemValue(getItemName());
55          return item;
56      }
57  
58      /***
59       * Creates a normal Document and appends d.
60       * @param d double
61       * @return DBaseDocument a normal Document
62       */
63      protected DBaseItem createDBaseItem(double d) {
64          DDocument doc = getDatabase().createDocument();
65          DBaseItem item = doc.appendItemValue(getItemName(), d);
66          return item;
67      }
68  
69      /***
70       * Creates a normal document and adds i.
71       * @param i  int
72       * @return DBaseDocument a normal Document
73       */
74      protected DBaseItem createDBaseItem(int i) {
75          DDocument doc = getDatabase().createDocument();
76          DBaseItem item = doc.appendItemValue(getItemName(), i);
77          return item;
78      }
79  
80      /***
81       * Creates a normal Document and adds s.
82       * @param s string
83       * @return DBaseDocument a normal Document
84       */
85      protected DBaseItem createDBaseItem(String s) {
86          DDocument doc = getDatabase().createDocument();
87          DBaseItem item = doc.appendItemValue(getItemName(), s);
88          return item;
89      }
90  
91      /***
92       * Creates a normal Document.
93       * @param d a Date
94       * @return DBaseItem a normal Document
95       */
96      protected DBaseItem createDBaseItem(Calendar d) {
97          DDocument doc = getDatabase().createDocument();
98          DBaseItem item = doc.appendItemValue(getItemName(), d);
99          return item;
100     }
101 
102     /***
103      * Creates a normal Document.
104      * @param l a List
105      * @return DBaseItem a normal Document
106      */
107     protected DBaseItem createDBaseItem(List l) {
108         DDocument doc = getDatabase().createDocument();
109         DBaseItem item = doc.appendItemValue(getItemName(), l);
110         return item;
111     }
112 
113     /***
114      * Tests appending of values to a text list.
115      *
116      */
117     public void testAppendToTextList() {
118         System.out.println("-> testAppendToTextList");
119         List l = new ArrayList();
120         DItem item = (DItem) createDBaseItem(l);
121         List before = item.getValues();
122         assertNotNull("Value list is null", before);
123         assertTrue(
124             "Item should contain a value list with one element that is an empty String.",
125             before.size() == 1 && "".equals(before.get(0)));
126         String secondValue = "second entry";
127         item.appendToTextList(secondValue);
128         List after = item.getValues();
129         assertTrue(
130             "Item should contain a value list with two elements that are an empty String and '"
131                 + secondValue + "', but has: " + after,
132             after.size() == 2 && "".equals(after.get(0)) && secondValue.equals(after.get(1)));
133     }
134 
135     /***
136      * Tests getValueDateTime.
137      *
138      */
139     public void testSetGetValueDateTime() {
140         System.out.println("-> testSetGetValueDateTime");
141         Calendar correct = new GregorianCalendar();
142         correct.clear(Calendar.HOUR);
143         correct.clear(Calendar.HOUR_OF_DAY);
144         correct.clear(Calendar.MINUTE);
145         correct.clear(Calendar.SECOND);
146         correct.clear(Calendar.MILLISECOND);
147         DItem item = (DItem) createDBaseItem(correct);
148         Calendar date = item.getValueDateTime();
149         System.out.println("date1 = " + format.format(correct.getTime()));
150         System.out.println("date2 = " + format.format(date.getTime()));
151         assertNotNull("Date value was a valid date, thus should be not null.", date);
152         assertEquals("Both dates should be equal.", correct, date);
153     }
154 
155     /***
156      * Tests getValueDateTime.
157      */
158     public void testSetGetValueDateRange() {
159         System.out.println("-> testSetGetValueDateRange");
160         final Calendar calendar1 = new GregorianCalendar();
161         calendar1.clear(Calendar.HOUR);
162         calendar1.clear(Calendar.MINUTE);
163         calendar1.clear(Calendar.SECOND);
164         calendar1.clear(Calendar.MILLISECOND);
165         final Calendar calendar2 = (Calendar) calendar1.clone();
166         calendar2.add(Calendar.YEAR, 1);
167         calendar2.add(Calendar.MONTH, 1);
168         calendar2.add(Calendar.DAY_OF_MONTH, 1);
169         final DDocument doc = getDatabase().createDocument();
170         doc.replaceItemValue("range", calendar1, calendar2);
171         final DItem item = (DItem) doc.getFirstItem("range");
172         DDateRange range = item.getValueDateRange();
173         final Calendar c1 = range.getFrom();
174         final Calendar c2 = range.getTo();
175         assertEquals(calendar1, c1);
176         assertEquals(calendar2, c2);
177     }
178 
179     /***
180      * Tests getValueInteger.
181      */
182     public void testSetGetValueInteger() {
183         System.out.println("-> testSetGetValueInteger");
184         int expected = 12;
185         DItem item = (DItem) createDBaseItem(expected);
186         int value = item.getValueInteger().intValue();
187         assertEquals("Returned value should be " + expected + " and not " + value + ".", expected, value);
188     }
189 
190     /***
191      * Tests getValueDouble.
192      */
193     public void testSetGetValueDouble() {
194         System.out.println("-> testSetGetValueDouble");
195         double expected = 12.3;
196         DItem item = (DItem) createDBaseItem(expected);
197         double value = item.getValueDouble().doubleValue();
198         assertTrue("Returned value should be " + expected + " and not " + value + ".", expected == value);
199     }
200 
201     /***
202      * Tests getValueString.
203      */
204     public void testSetGetValueString() {
205         System.out.println("-> testSetGetValueString");
206         String expected = "someValueXXX";
207         DItem item = (DItem) createDBaseItem(expected);
208         String value = item.getValueString();
209         assertEquals("Returned value should be " + expected + " and not " + value + ".", expected, value);
210     }
211 
212     /***
213      * Tests getValues.
214      */
215     public void testSetGetValues() {
216         System.out.println("-> testSetGetValues");
217         {
218             String exp1 = "someValue1";
219             String exp2 = "someValue2";
220             List expList = new ArrayList();
221             expList.add(exp1);
222             expList.add(exp2);
223             DItem item = (DItem) createDBaseItem(expList);
224             List values = item.getValues();
225             assertEquals("Item should contain the list " + expList + " but contains: " + values, expList, values);
226         }
227         {
228             List dateList = new ArrayList();
229             Calendar calendar = new GregorianCalendar();
230             calendar.clear(Calendar.MILLISECOND);
231             dateList.add(calendar);
232             dateList.add(calendar);
233 
234             DItem item = (DItem) createDBaseItem(dateList);
235 
236             List values = item.getValues();
237             System.out.println("dateRef = " + format.format(calendar.getTime()) + "(" + calendar.getTime().getTime() + ")");
238             System.out.println("date[0] = " + format.format(((Calendar) values.get(0)).getTime()));
239             System.out.println("date[1] = " + format.format(((Calendar) values.get(0)).getTime()));
240             assertEquals("date 0 should be equal", calendar, (Calendar) values.get(0));
241             assertEquals("date 1 should be equal", calendar, (Calendar) values.get(1));
242         }
243     }
244 
245     /***
246      * Tests the method isSummary.
247      *
248      */
249     public void testIsSummary() {
250         DBaseItem item = createDBaseItem();
251         if (item instanceof DItem) {
252             assertTrue("The summary flag of this item should be true.", ((DItem) item).isSummary());
253         } else {
254             assertTrue("The summary flag of this item should be false.", !((DItem) item).isSummary());
255         }
256     }
257 
258     /***
259      * Tests the method setSummary.
260      */
261     public void testSetSummary() {
262         DBaseItem item = createDBaseItem();
263         if (item instanceof DItem) {
264             assertTrue("The summary flag of this item should indicate true.", ((DItem) item).isSummary());
265         } else {
266             assertTrue("The summary flag of this item should indicate false.", !((DItem) item).isSummary());
267         }
268         ((DItem) item).setSummary(false);
269         assertTrue("The summary flag of this item should be false.", !((DItem) item).isSummary());
270         ((DItem) item).setSummary(true);
271         assertTrue("The summary flag of this item should again be true.", ((DItem) item).isSummary());
272     }
273 
274     /***
275      * Tests the method setAuthors.
276      */
277     public void testSetReaders() {
278         DBaseItem item = createDBaseItem();
279         if (item instanceof DItem) {
280             assertTrue("The summary flag of this item should indicate true.", !((DItem) item).isReaders());
281             ((DItem) item).setReaders(true);
282             assertTrue("The summary flag of this item should be false.", ((DItem) item).isReaders());
283             ((DItem) item).setReaders(false);
284             assertTrue("The summary flag of this item should again be true.", !((DItem) item).isReaders());
285         }
286     }
287 
288     /***
289      * Tests the method setAuthors.
290      */
291     public void testSetAuthors() {
292         DBaseItem item = createDBaseItem();
293         if (item instanceof DItem) {
294             assertTrue("The summary flag of this item should indicate true.", !((DItem) item).isAuthors());
295             ((DItem) item).setAuthors(true);
296             assertTrue("The summary flag of this item should be false.", ((DItem) item).isAuthors());
297             ((DItem) item).setAuthors(false);
298             assertTrue("The summary flag of this item should again be true.", !((DItem) item).isAuthors());
299         }
300     }
301 
302     /***
303      * Tests the method setAuthors.
304      */
305     public void testSetTimezone() {
306         DItem item = (DItem) createDBaseItem();
307         TimeZone timezone = new SimpleTimeZone(+1, "CET");
308         // TODO check this
309         item.setValueDateTime(timezone);
310         if (item instanceof DItem) {
311             assertTrue("The summary flag of this item should indicate true.", !((DItem) item).isAuthors());
312             ((DItem) item).setAuthors(true);
313             assertTrue("The summary flag of this item should be false.", ((DItem) item).isAuthors());
314             ((DItem) item).setAuthors(false);
315             assertTrue("The summary flag of this item should again be true.", !((DItem) item).isAuthors());
316         }
317     }
318 }