Wednesday, August 25, 2010

Rx sample

In this post i have implemented a simple program which writes all the information of a customer class to a file.I took this example as it can be functional in a very short period of time.The code in this blog is inspired from Rx wiki.


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Threading;
using System.Concurrency;
using System.Diagnostics;

namespace ObservablePrototype
{
public class Prototype
{
string errorfile = ConfigurationManager.AppSettings["CustomerFile"];

public void RunProto()
{
Stopwatch sw = new Stopwatch();
sw.Start();

//Making Customer as observableCollection 
var customers = new ObservableCollection();

// adding event handler 
var customerChanges = Observable.FromEvent(
(EventHandler ev)
=> new NotifyCollectionChangedEventHandler(ev),
ev => customers.CollectionChanged += ev,
ev => customers.CollectionChanged -= ev);

//ling query for notify collection changed

var line = (from c in customerChanges
where c.EventArgs.Action == NotifyCollectionChangedAction.Add
from cu in c.EventArgs.NewItems.Cast().ToObservable()
select cu).BufferWithCount(10);

//using scheduler or u can just subscribe directly
Scheduler.ThreadPool.Schedule(() =>
{
line.Subscribe(cus =>
{
WriteCustomers(cus.ToList());
}
);
});

//start adding customers  
AddCustomers(customers);
WriteTimings(sw.Elapsed.Seconds + "Observable");
}

private void WriteTimings(string p)
{
TextWriter writer = new StreamWriter(errorfile, true);
writer.WriteLine(p);
writer.Close();
}



private void AddCustomers(ObservableCollection customers)
{
for (int i = 0; i < 50; i++)
            {
                Thread.Sleep(500);
                customers.Add(new Customer() { firstName = "K", lastName = "P" + i });
            }
            
        }



        public void StraightWrite()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            List customers = new List();
for (int i = 0; i < 50; i++)
            {
                Thread.Sleep(500);
                customers.Add(new Customer() { firstName = "K", lastName = "P" + i });
            }
            WriteCustomers(customers);
            WriteTimings(sw.Elapsed.Seconds + "Normal");
        }



        private void WriteCustomers(List customers)
{
foreach (var c in customers)
{
TextWriter writer = new StreamWriter(errorfile, true);
writer.WriteLine("FirstName : " + c.firstName + "  LastName : " + c.lastName);
writer.Close();
}
}

}
}






Wednesday, July 14, 2010

OTAClient + QC reporting with C#

When writing a Data Comparison tool, one of the requirements was to log the errors into Quality Center. Thats where OTAClient.dll comes into picture.
Open test Architecture is provided to access QC's database without using its GUI.
The code block below populates the bug object,posts it in QC's DB and also sends email to the concerned person.

public void GetDistinctRowsForQC()
{
DataContext dataContext = new DataContext();

var qc = (from details in dataContext.TableName
where details.RunId == _testRunID
select details);

QCReporting.LogErrorInQualityCenter(qc.Take(2));
}


internal static void LogErrorInQualityCenter(IQueryable qc)
{
TDConnectionClass QCConnection = new TDConnectionClass();
QCConnection.InitConnectionEx("http://qualitycenter/qcbin/");
QCConnection.Login("UserID", string.Empty);
QCConnection.Connect("C", "C");

BugFactory BFactory = (BugFactory)QCConnection.BugFactory;
Customization field = (Customization)QCConnection.Customization;
CustomizationFields f = (CustomizationFields)field.Fields;

List list = f.get_Fields("Bug");
Dictionary columnnames = new Dictionary();
foreach (CustomizationField fieldd in list)
{
if(!columnnames.ContainsKey(fieldd.UserLabel))
columnnames.Add(fieldd.UserLabel,fieldd.ColumnName);
}
int i = 0;
foreach(var q in qc)
{
IBug bug = (IBug)BFactory.AddItem(i);
bug.AutoPost = false;
bug.AssignedTo = "UserID";
bug.DetectedBy = Environment.UserName;
bug.Priority = "1-Low";
bug.Status = "New";
bug.Summary = q.ErrorMessage;
bug.Project = "CCC";
bug["BG_Reproducible"] = "Y";
bug["BG_Severity"] = "2-Medium";
bug[columnnames.Where(P => P.Key == "Issue Type").Select(p => p.Value).FirstOrDefault()] = "Defect";
bug[columnnames.Where(P => P.Key == "SDLC Phase").Select(p => p.Value).FirstOrDefault()] = "2-Project Execution";
bug[columnnames.Where(P => P.Key == "Detected on Date").Select(p => p.Value).FirstOrDefault()] = DateTime.Now;
bug[columnnames.Where(P => P.Key == "Application Group").Select(p => p.Value).FirstOrDefault()] = "Cu";
bug[columnnames.Where(P => P.Key == "Application Sub Group").Select(p => p.Value).FirstOrDefault()] = "CA";
bug[columnnames.Where(P => P.Key == "Project Number").Select(p => p.Value).FirstOrDefault()] = "C";
bug[columnnames.Where(P => P.Key == "Environment").Select(p => p.Value).FirstOrDefault()] = "QA";
bug[columnnames.Where(P => P.Key == "Detector Name").Select(p => p.Value).FirstOrDefault()] = new DirectoryEntry("WinNT://p/" + Environment.UserName + "").Name;
bug[columnnames.Where(P => P.Key == "Assigned Name").Select(p => p.Value).FirstOrDefault()] = new DirectoryEntry("WinNT://p/" + Environment.UserName + "").Name;
bug[columnnames.Where(P => P.Key == "Verified By").Select(p => p.Value).FirstOrDefault()] = "UserID";
bug[columnnames.Where(P => P.Key == "Verifier Name").Select(p => p.Value).FirstOrDefault()] = new DirectoryEntry("WinNT://p/" + Environment.UserName + "").Name;
bug[columnnames.Where(P => P.Key == "ReopenCountField").Select(p => p.Value).FirstOrDefault()] = "0";
bug[columnnames.Where(P => P.Key == "Source").Select(p => p.Value).FirstOrDefault()] = "Test";
bug[columnnames.Where(P => P.Key == "Reopen Count").Select(p => p.Value).FirstOrDefault()] = "0";
bug.Post();

bug.Mail("UserID", "UserID", 4, "New Defect Notification", q.ErrorMessage);
i++;
}
QCConnection.DisconnectProject();
QCConnection.Logout();
}