public void TestArgumentExceptions() { var contentType = new ContentType("application", "octet-stream"); var attachments = new AttachmentCollection(); var items = new MimeEntity[10]; var data = new byte[1024]; using (var stream = new MemoryStream()) { Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty)); Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null)); Assert.Throws <ArgumentNullException> (() => attachments.Add((MimeEntity)null)); Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty, data)); Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null, data)); Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty, stream)); Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null, stream)); Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty, contentType)); Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null, contentType)); Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty, data, contentType)); Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null, data, contentType)); Assert.Throws <ArgumentException> (() => attachments.Add(string.Empty, stream, contentType)); Assert.Throws <ArgumentNullException> (() => attachments.Add((string)null, stream, contentType)); Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", (byte[])null)); Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", (Stream)null)); Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", (byte[])null, contentType)); Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", (Stream)null, contentType)); Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", (ContentType)null)); Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", data, null)); Assert.Throws <ArgumentNullException> (() => attachments.Add("file.dat", stream, null)); Assert.Throws <ArgumentNullException> (() => attachments.Contains(null)); Assert.Throws <ArgumentNullException> (() => attachments.CopyTo(null, 0)); Assert.Throws <ArgumentOutOfRangeException> (() => attachments.CopyTo(items, -1)); Assert.Throws <ArgumentNullException> (() => attachments.IndexOf(null)); Assert.Throws <ArgumentNullException> (() => attachments.Remove(null)); Assert.Throws <ArgumentOutOfRangeException> (() => attachments.RemoveAt(0)); attachments.Add(new TextPart("plain")); Assert.Throws <ArgumentOutOfRangeException> (() => { var x = attachments[10]; }); Assert.Throws <ArgumentOutOfRangeException> (() => attachments[10] = new TextPart("plain")); Assert.Throws <ArgumentNullException> (() => attachments[0] = null); Assert.Throws <ArgumentOutOfRangeException> (() => attachments.Insert(-1, new TextPart("plain"))); Assert.Throws <ArgumentNullException> (() => attachments.Insert(0, null)); } }
/// <summary> /// Handles the remove attachment button's Click event. /// </summary> /// <param name="sender">The button object.</param> /// <param name="e">The event arguments.</param> private void btnRemoveAttachment_Click(object sender, EventArgs e) { bool b = _changed; // Removes an attachment from the collection. _attachments.RemoveAt(cbxAttachment.SelectedIndex); // and the combo box. if (_attachments.Count > 0) { // and the combo box. cbxAttachment.Items.RemoveAt(cbxAttachment.SelectedIndex); } else { cbxAttachment.SelectedItem = null; cbxAttachment.Items.Clear(); } if (cbxAttachment.Items.Count == 0) { // Disables the "Remove" button. btnRemoveAttachment.Enabled = false; _attachments = null; saveAttachmentsButton.Enabled = false; } else if (cbxAttachment.SelectedIndex == -1) { cbxAttachment.SelectedIndex = cbxAttachment.Items.Count - 1; } _changed = true; saveButton.Enabled = true; if (b == false) { SetTitle(); } }