SharePoint API測試系列——對Recorded Item作OM操做(委託的妙用)

 

對Recorded Item動態調用OM Methods進行測試,界面以下:web

輸入Site的URL,File在SharePoint中的相對URL,並選擇OM操做類型,點擊Test進行測試。代碼以下:ide

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.SharePoint;

namespace RecordsOMTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            OMMethods_comboBox.Items.Add("Rename");
            OMMethods_comboBox.Items.Add("File item delete");
            OMMethods_comboBox.Items.Add("File delete");
            OMMethods_comboBox.Items.Add("File item recycle");
            OMMethods_comboBox.Items.Add("File recycle");
            OMMethods_comboBox.Items.Add("File undo check out");
            OMMethods_comboBox.Items.Add("File check in");
            OMMethods_comboBox.Items.Add("File check out");
        }

        public delegate void CallOMMethod(SPFile file);

        private void test_button_Click(object sender, EventArgs e)
        {
            /*
                Block delete test.
            */
            //Rename test.
            CallOMMethod Rename = file => { file.Item["Name"] = "kkk.txt"; file.Item.Update(); };
            //Delete test 1.
            CallOMMethod delete1 = file => { file.Item.Delete(); };
            //Delete test 2.
            CallOMMethod delete2 = file => { file.Delete(); };
            //Delete test 3.
            CallOMMethod delete3 = file => { file.Item.Recycle(); };
            //Delete test 4.
            CallOMMethod delete4 = file => { file.Recycle(); };

            /*
                Block edit and delete test.
            */
            //UndoCheckOut test.
            CallOMMethod UndoCKOut = file => { file.UndoCheckOut(); };
            //CheckIn test.
            CallOMMethod CkIn = file => { file.CheckIn("Check in."); };
            //CheckOut test.
            CallOMMethod CkOut = file => { file.CheckOut(); };

            switch (OMMethods_comboBox.Text)
            {
                case "Rename":
                    //Rename test.
                    DoOMTest(Rename);
                    break;
                case "File item delete":
                    DoOMTest(delete1);
                    break;
                case "File delete":
                    DoOMTest(delete2);
                    break;
                case "File item recycle":
                    DoOMTest(delete3);
                    break;
                case "File recycle":
                    DoOMTest(delete4);
                    break;
                case "File undo check out":
                    DoOMTest(UndoCKOut);
                    break;
                case "File check in":
                    DoOMTest(CkIn);
                    break;
                case "File check out":
                    DoOMTest(CkOut);
                    break;
            }
        }
        private void DoOMTest(CallOMMethod cm)
        {
            using (SPSite site = new SPSite(siteUrl_textBox.Text))
            {
                SPWeb web = site.RootWeb;
                SPFile file = web.GetFile(filePath_textBox.Text);
                try
                {
                    cm.Invoke(file);
                    //Reach this step means test failed.
                    result_richTextBox.Text = "Test failed.\r\n";
                    result_richTextBox.SelectionColor = Color.Red;
                }
                catch (Exception ex)
                {
                    result_richTextBox.Text = "Test passed.\r\n";
                    result_richTextBox.Select(0, result_richTextBox.Text.Length);
                    result_richTextBox.SelectionColor = Color.Green;
                    result_richTextBox.Text += string.Concat(ex);
                } 
            }
        }
    }
}

提取每一個OM Methods單元測試中的公共部分寫成DoOMTest(CallOMMethod cm)方法,對於每一個測試中不一樣的部分經過調用一個委託方法(橙色字體)來代替。而後咱們對委託方法的類型進行聲明(委託方法須要傳入一個SPFile類型的參數):單元測試

public delegate void CallOMMethod(SPFile file);

再根據每一個測試中的不一樣部份內容對委託進行實例化:測試

//Rename test.
CallOMMethod Rename = file => { file.Item["Name"] = "kkk.txt"; file.Item.Update(); };
//Delete test 1.
CallOMMethod delete1 = file => { file.Item.Delete(); };
//Delete test 2.
CallOMMethod delete2 = file => { file.Delete(); };
//Delete test 3.
CallOMMethod delete3 = file => { file.Item.Recycle(); };
//Delete test 4.
CallOMMethod delete4 = file => { file.Recycle(); };
//UndoCheckOut test.
CallOMMethod UndoCKOut = file => { file.UndoCheckOut(); };
//CheckIn test.
CallOMMethod CkIn = file => { file.CheckIn("Check in."); };
//CheckOut test.
CallOMMethod CkOut = file => { file.CheckOut(); };

這就是委託在測試中的妙用,極大的減小了重複代碼量。字體

相關文章
相關標籤/搜索