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.io.File;
26  import java.io.IOException;
27  import java.util.Iterator;
28  import java.util.ResourceBundle;
29  
30  /***
31   * Test for RichText items.
32   *
33   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
34   */
35  public final class RichTextItemProxyTest extends BaseItemProxyTest {
36  
37      /***
38       * @param name the tests name
39       */
40      public RichTextItemProxyTest(String name) {
41          super(name);
42      }
43  
44      /***
45       * Tests embedding an object.
46       */
47      public void testEmbedAndGetAttachment() {
48          System.out.println("-> testEmbedAndGetAttachment");
49  
50          String suf = System.currentTimeMillis() + "testEmbedAndGetAttachment";
51          String pre = ".txt";
52          File file = null;
53          try {
54              file = File.createTempFile(suf, pre);
55          } catch (IOException e) {
56              assertTrue("Could not create test attachment file.", false);
57          }
58  
59          DDocument doc = getDatabase().createDocument();
60          DRichTextItem rTitem = doc.createRichTextItem(getItemName());
61          DEmbeddedObject embedded = rTitem.embedAttachment(file.getAbsolutePath());
62          doc.save();
63          DEmbeddedObject received = rTitem.getEmbeddedAttachment(file.getName());
64          assertNotNull("No attachment found", received);
65          assertEquals(
66              "The content (name) of the embedded object should be equal to the given name.",
67              file.getName(),
68              embedded.getName());
69          assertEquals(
70              "The content (name) of both embedded objects should be equal.",
71              embedded.getName(),
72              received.getName());
73  
74          file.delete();
75          doc.remove(true);
76      }
77  
78      /***
79       * Tests RichTextItem.getEmbeddedObjects().
80       */
81      public void testGetEmbeddedObjects() {
82          System.out.println("-> testGetEmbeddedObjects");
83  
84          DBaseDocument doc = getDatabase().createDocument();
85          DRichTextItem rtItem1 = doc.createRichTextItem(getItemName());
86  
87          String pre1 = System.currentTimeMillis() + "testGetEmbeddedObjects1";
88          String pre2 = System.currentTimeMillis() + "testGetEmbeddedObjects2";
89          String suf = ".txt";
90          File file1 = null;
91          File file2 = null;
92          try {
93              file1 = File.createTempFile(pre1, suf);
94              file2 = File.createTempFile(pre2, suf);
95          } catch (IOException e) {
96              assertTrue("Could not create test attachment file.", false);
97          }
98  
99          String key1 = file1.getAbsolutePath();
100         String key2 = file2.getAbsolutePath();
101 
102         rtItem1.embedAttachment(key1);
103         rtItem1.embedAttachment(key2);
104 
105         doc.save();
106 
107         Iterator it = rtItem1.getEmbeddedObjects();
108 
109         int eOCounter = 0;
110         while (it.hasNext()) {
111             DEmbeddedObject eo = (DEmbeddedObject) it.next();
112             eOCounter++;
113             assertNotNull("The embedded object should not be null.", eo);
114             boolean ok1 = file1.getName().equals(eo.getName());
115             boolean ok2 = file2.getName().equals(eo.getName());
116             assertTrue("The name of the embedded object should be the same as the name at embedding.", ok1 || ok2);
117         }
118         assertEquals("There should be two embedded objects.", 2, eOCounter);
119 
120         file1.delete();
121         file2.delete();
122         doc.remove(true);
123 
124     }
125 
126     /***
127      * Tests RichTextItem.getUnformattedText().
128      */
129     public void testGetUnformattedText() {
130         System.out.println("-> testGetUnformattedText");
131 
132         DBaseDocument doc = getDatabase().createDocument();
133         DRichTextItem rtItem = doc.createRichTextItem(getItemName());
134         String val = "This text is stored in a RichTextItem to test the method RichTextItem.getUnformattedText()";
135         rtItem.appendText(val);
136         doc.save();
137 
138         DRichTextItem rtItem2 = (DRichTextItem) doc.getFirstItem(getItemName());
139         String comp = rtItem2.getUnformattedText();
140 
141         assertEquals(val, comp);
142         doc.removeItem(getItemName());
143         rtItem = doc.createRichTextItem(getItemName());
144         rtItem.appendText(getBigText());
145 
146         rtItem2 = (DRichTextItem) doc.getFirstItem(getItemName());
147         comp = rtItem2.getUnformattedText();
148         String compare = comp.replaceAll("\n", "").replaceAll("\r", "");
149         assertEquals(getBigText(), compare);
150         doc.remove(true);
151     }
152 
153     /***
154      * Tests formatting methods of RichTextItem.
155      */
156     public void testFormats() {
157         System.out.println("-> testFormats");
158 
159         DBaseDocument doc = getDatabase().createDocument();
160         DRichTextItem rtItem1 = doc.createRichTextItem(getItemName());
161 
162         rtItem1.appendText("some text");
163         rtItem1.addNewLine();
164         rtItem1.appendText("some text");
165         rtItem1.addNewLine(2);
166         rtItem1.appendText("some text");
167         rtItem1.addNewLine(2, true);
168         rtItem1.appendText("some text");
169         rtItem1.addNewLine(2, false);
170         rtItem1.appendText("some text");
171         rtItem1.addTab();
172         rtItem1.appendText("some text");
173         rtItem1.addTab(2);
174         rtItem1.appendText("some text");
175         doc.save();
176         doc.remove(true);
177     }
178 
179     /***
180      * @see de.bea.domingo.BaseItemProxyTest#createDBaseItem()
181      * {@inheritDoc}
182      */
183     protected DBaseItem createDBaseItem() {
184         DDocument doc = getDatabase().createDocument();
185         DRichTextItem item = doc.createRichTextItem(getItemName());
186         return item;
187     }
188 
189     /***
190      * Returns a very long string.
191      * @return a very long string
192      */
193     private String getBigText() {
194         ResourceBundle bundle = ResourceBundle.getBundle("de.bea.domingo.test-values");
195         return bundle.getString("value.string.long");
196     }
197 }