aboutsummaryrefslogtreecommitdiffstats
path: root/src/scratchpad/testcases/org/apache/poi/hmef/attribute/TestMAPIAttributes.java
blob: c3707a8118386fbb225894703ed4db125db8f9be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.hmef.attribute;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.DateFormat;
import java.util.Locale;
import java.util.TimeZone;

import junit.framework.TestCase;

import org.apache.poi.POIDataSamples;
import org.apache.poi.hmef.HMEFMessage;
import org.apache.poi.hsmf.datatypes.MAPIProperty;
import org.apache.poi.util.LittleEndian;

public final class TestMAPIAttributes extends TestCase {
   private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
   private HMEFMessage quick;
   private InputStream stream;
   
   @Override
   protected void setUp() throws Exception {
      super.setUp();
      
      stream = _samples.openResourceAsStream("quick-winmail.dat");
      quick = new HMEFMessage(stream);
   }


   protected void tearDown() throws Exception {
       stream.close();

       super.tearDown();
   }


/** 
    * Test counts
    */
   public void testCounts() throws Exception {
      // Message should have 54
      assertEquals(54, quick.getMessageMAPIAttributes().size());
      
      // First attachment should have 22
      assertEquals(22, quick.getAttachments().get(0).getMAPIAttributes().size());
   }
	
   /**
    * Test various general ones
    */
   public void testBasics() throws Exception {
      // Try constructing two attributes
      byte[] data = new byte[] {
            // Level one, id 36867, type 6
            0x01,    0x03, (byte)0x90,   0x06, 0x00,
            // Length 24
            0x24, 0x00, 0x00, 0x00,
            
            // Three attributes
            0x03, 0x00, 0x00, 0x00,
            // AlternateRecipientAllowed = 01 00
            0x0B, 0x00, 0x02, 0x00,
            0x01, 0x00, 0x00, 0x00,
            // Priority = 00 00 00 00
            0x03, 0x00, 0x26, 0x00,
            0x00, 0x00, 0x00, 0x00,
            // ConversationTopic = Test
            0x1E, 0x00, 0x70, 0x00,
            0x01, 0x00, 0x00, 0x00,
            0x04, 0x00, 0x00, 0x00,
            (byte)'T', (byte)'e',
            (byte)'s', (byte)'t',
            // Checksum (may be wrong...)
            0x01, 0x00
      };
      ByteArrayInputStream bais = new ByteArrayInputStream(data);
      
      // Create it
      int level = bais.read();
      assertEquals(1, level);
      TNEFAttribute attr = TNEFAttribute.create(bais);
      
      // Check it
      assertNotNull(attr);
      assertEquals(TNEFMAPIAttribute.class, attr.getClass());
      
      TNEFMAPIAttribute mapi = (TNEFMAPIAttribute)attr;
      assertEquals(3, mapi.getMAPIAttributes().size());
      
      assertEquals(
            MAPIProperty.ALTERNATE_RECIPIENT_ALLOWED, 
            mapi.getMAPIAttributes().get(0).getProperty()
      );
      assertEquals(1, LittleEndian.getUShort(
               mapi.getMAPIAttributes().get(0).getData()
      ));
      
      assertEquals(
            MAPIProperty.PRIORITY, 
            mapi.getMAPIAttributes().get(1).getProperty()
      );
      assertEquals(0, LittleEndian.getUShort(
               mapi.getMAPIAttributes().get(1).getData()
      ));
      
      assertEquals(
            MAPIProperty.CONVERSATION_TOPIC, 
            mapi.getMAPIAttributes().get(2).getProperty()
      );
      assertEquals(
            "Test",
            ((MAPIStringAttribute)mapi.getMAPIAttributes().get(2)).getDataString()
      );
   }

   /**
    * Test String, Date and RTF ones
    */
   public void testTyped() throws Exception {
      MAPIAttribute attr;
      
      // String
      //  ConversationTopic -> This is a test message
      attr = quick.getMessageMAPIAttribute(MAPIProperty.CONVERSATION_TOPIC);
      assertNotNull(attr);
      assertEquals(MAPIStringAttribute.class, attr.getClass());
      
      MAPIStringAttribute str = (MAPIStringAttribute)attr;
      assertEquals("This is a test message", str.getDataString());
      
      // Date
      //  (Unknown/Custom) 32955 -> Wed Dec 15 2010 @ 14:46:31 UTC
      attr = null;
      for(MAPIAttribute a : quick.getMessageMAPIAttributes()) {
         if(a.getProperty().id == 32955) {
            attr = a;
            break;
         }
      }
      assertNotNull(attr);
      assertEquals(MAPIDateAttribute.class, attr.getClass());
      
      MAPIDateAttribute date = (MAPIDateAttribute)attr;
      DateFormat fmt = DateFormat.getDateTimeInstance(
            DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.UK
      );
      fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
      assertEquals("15-Dec-2010 14:46:31", fmt.format(date.getDate()));
      
      // RTF
      //   RtfCompressed -> {\rtf1...
      attr = quick.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
      assertNotNull(attr);
      assertEquals(MAPIRtfAttribute.class, attr.getClass());
      
      MAPIRtfAttribute rtf = (MAPIRtfAttribute)attr;
      assertEquals("{\\rtf1", rtf.getDataString().substring(0, 6));
   }

   /**
    * Check common ones via helper accessors
    */
   public void testCommon() throws Exception {
      assertEquals("This is a test message", quick.getSubject());
      
      assertEquals("quick.doc", quick.getAttachments().get(0).getLongFilename());
      assertEquals(".doc", quick.getAttachments().get(0).getExtension());
   }
}