1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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 }