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  
28  /***
29   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
30   */
31  public final class EmbeddedObjectProxyTest extends BaseProxyTest {
32  
33      private String itemName;
34  
35      private DDocument doc;
36      private File attachFile;
37      private DRichTextItem rTitem;
38  
39      /***
40       * @param name the tests name
41       */
42      public EmbeddedObjectProxyTest(String name) {
43          super(name);
44      }
45  
46      /***
47       * Tests extractFile.
48       */
49      public void testExtractFile() {
50          System.out.println("-> EmbeddedObjectProxy.testExtractFile");
51          String path = attachFile.getAbsolutePath();
52          rTitem.embedAttachment(path);
53          doc.save();
54          DEmbeddedObject received = rTitem.getEmbeddedAttachment(attachFile.getName());
55          assertNotNull("Embedded object to detach not found", received);
56  
57          String extractPath = "c://temp//" + System.currentTimeMillis() + "extractFile.txt";
58  
59          received.extractFile(extractPath);
60  
61          File extracted = new File(extractPath);
62          assertTrue("Extracted File should exist.", extracted.exists());
63  
64          extracted.delete();
65          attachFile.delete();
66      }
67  
68      /***
69       * Embeds a file, retrieves the Embedded object and equals its name with
70       * the placed name.
71       */
72      public void testGetName() {
73          System.out.println("-> EmbeddedObjectProxy.testGetName");
74          String path = attachFile.getAbsolutePath();
75          DEmbeddedObject embedded = rTitem.embedAttachment(path);
76          doc.save();
77          DEmbeddedObject received = rTitem.getEmbeddedAttachment(attachFile.getName());
78          assertNotNull("Embedded object not found", received);
79          assertEquals(
80              "The content (name) of the embedded object should be equal to the given name.",
81              attachFile.getName(),
82              embedded.getName());
83          assertEquals(
84              "The content (name) of both embedded objects should be equal.",
85              embedded.getName(),
86              received.getName());
87  
88          attachFile.delete();
89      }
90  
91      /***
92       * Creates and removes an attached file.
93       */
94      public void testRemove() {
95          System.out.println("-> EmbeddedObjectProxy.testRemove");
96  
97          String path = attachFile.getAbsolutePath();
98          rTitem.embedAttachment(path);
99          doc.save();
100         DEmbeddedObject rec1 = rTitem.getEmbeddedAttachment(attachFile.getName());
101         assertNotNull("Embedded object not found", rec1);
102         assertEquals("This operation should result in the embedded's name.", attachFile.getName(), rec1.getName());
103         rec1.remove();
104 
105         attachFile.delete();
106     }
107 
108     /***
109      * Sets up instance variables that are used in most of the tests.
110      */
111     public void setUp() {
112         baseSetUp();
113         itemName = "some_item_that_does_surely_not_exist";
114 
115         String suf = System.currentTimeMillis() + "testEmbedAndGetter";
116         String pre = ".txt";
117         attachFile = null;
118         try {
119             attachFile = File.createTempFile(suf, pre);
120         } catch (IOException e) {
121             assertTrue("Could not create test attachment file.", false);
122         }
123 
124         doc = getDatabase().createDocument();
125         rTitem = doc.createRichTextItem(itemName);
126     }
127 }