1
/* Copyright (c) 2007 Google Inc.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
16
using Google.GData.Apps;
17
using Google.GData.Apps.Migration;
18
using Google.GData.Client;
19
using Google.GData.Extensions;
20
using Google.GData.Extensions.Apps;
25
using NUnit.Framework;
27
namespace Google.GData.Apps.UnitTests
30
[Category("GoogleApps")]
31
public class MailItemEntryTest
33
private MailItemEntry entry;
38
entry = new MailItemEntry();
48
public void GetRfc822MsgTest()
50
Assert.IsNull(entry.Rfc822Msg, "Rfc822Msg should initially be null after construction");
54
public void SetRfc822MsgTest()
56
entry.Rfc822Msg = new Rfc822MsgElement("Hi");
58
Assert.IsNotNull(entry.Rfc822Msg, "Rfc822Msg should not be null after setting");
59
Assert.AreEqual("Hi", entry.Rfc822Msg.ToString(),
60
"Entry's Rfc822Msg does not have correct value after setting");
64
public void GetLabelsTest()
66
Assert.IsNotNull(entry.Labels, "Labels should initially not be null after construction");
70
public void AddLabelTest()
72
LabelElement label = new LabelElement("Friends");
73
entry.Labels.Add(label);
75
Assert.AreEqual(1, entry.Labels.Count, "Entry's label collection should have size 1 after insertion");
76
Assert.IsTrue(entry.Labels.Contains(label), "Entry's label collection does not contain label after insertion");
80
public void GetMailItemPropertiesTest()
82
Assert.IsNotNull(entry.MailItemProperties,
83
"MailItemProperties should initially not be null after construction");
87
public void AddMailItemPropertiesTest()
89
entry.MailItemProperties.Add(MailItemPropertyElement.INBOX);
91
Assert.AreEqual(1, entry.MailItemProperties.Count,
92
"Entry's mail item property collection should have size 1 after insertion");
93
Assert.IsTrue(entry.MailItemProperties.Contains(MailItemPropertyElement.INBOX),
94
"Entry's mail item property collection does not contain INBOX property after insertion");
98
public void ParseXmlTest()
100
string xmlElementText = "<entry xmlns=\"http://www.w3.org/2005/Atom\" " +
101
"xmlns:gd=\"http://schemas.google.com/g/2005\">" + Rfc822MsgTest.xmlElementAsText +
102
LabelTest.xmlElementAsText + LabelTest.xmlElementAsText.Replace("Friends", "Acquaintances") +
103
MailItemPropertyTest.xmlElementAsText +
104
MailItemPropertyTest.xmlElementAsText.Replace("SENT", "STARRED") +
105
"<gd:when startTime=\"2005-10-06\"/></entry>";
107
// Parse the XML back into a new MigrationEntry
108
XmlDocument document = new XmlDocument();
109
document.LoadXml(xmlElementText);
111
foreach (XmlNode node in document.FirstChild.ChildNodes)
113
ExtensionElementEventArgs e = new ExtensionElementEventArgs();
114
e.ExtensionElement = node;
115
entry.Parse(e, new AtomFeedParser());
118
// Verify that the labels are correct
119
Assert.AreEqual(2, entry.Labels.Count, "Parsed MailItemEntry should have exactly two labels");
120
Assert.AreEqual("Friends", entry.Labels[0].LabelName,
121
"Parsed MailItemEntry does not contain a \"Friends\" label");
122
Assert.AreEqual("Acquaintances", entry.Labels[1].LabelName,
123
"Parsed MailItemEntry does not contain an \"Acquaintances\" label");
125
// Verify that the mail item properties are correct
126
Assert.AreEqual(2, entry.MailItemProperties.Count,
127
"Parsed MailItemEntry should have exactly two mail item properties");
128
Assert.AreEqual("IS_SENT", entry.MailItemProperties[0].Value.ToString(),
129
"Parsed MailItemEntry does not contain a SENT property.");
130
Assert.AreEqual("IS_STARRED", entry.MailItemProperties[1].Value.ToString(),
131
"Parsed MailItemEntry does not contain the STARRED property.");
133
// Verify that the RFC822 message is correct
134
Assert.AreEqual("Hi", entry.Rfc822Msg.ToString(), "Parsed MailItemEntry has incorrect Rfc822Msg");
138
public void SaveXmlTest()
140
// Set up the MailItemEntry.
141
entry.Rfc822Msg = new Rfc822MsgElement("Hi");
142
entry.Labels.Add(new LabelElement("Friends"));
143
entry.Labels.Add(new LabelElement("Acquaintances"));
144
entry.MailItemProperties.Add(MailItemPropertyElement.SENT);
145
entry.MailItemProperties.Add(MailItemPropertyElement.TRASH);
147
// Save it as a string using an XmlWriter.
148
StringBuilder sb = new StringBuilder();
149
XmlWriter writer = new XmlTextWriter(new StringWriter(sb));
150
entry.SaveToXml(writer);
153
string savedXml = sb.ToString();
156
Assert.IsTrue(savedXml.IndexOf(Rfc822MsgTest.xmlElementAsText) != 0,
157
"Saved XML form of MailItemEntry does not contain correct rfc822Msg element");
158
Assert.IsTrue(savedXml.IndexOf(LabelTest.xmlElementAsText) != 0,
159
"Saved XML form of MailItemEntry does not contain \"Friends\" label element");
160
Assert.IsTrue(savedXml.IndexOf(LabelTest.xmlElementAsText.Replace("Friends", "Acquaintances"))!=0,
161
"Saved XML form of MailItemEntry does not contain \"Acquaintances\" label element");
162
Assert.IsTrue(savedXml.IndexOf(MailItemPropertyTest.xmlElementAsText) !=0,
163
"Saved XML form of MailItemEntry does not contain SENT property");
164
Assert.IsTrue(savedXml.IndexOf(MailItemPropertyTest.xmlElementAsText.Replace("SENT", "TRASH")) !=0,
165
"Saved XML form of MailItemEntry does not contain TRASH property");