Archive for ◊ September, 2009 ◊

Author:
Wednesday, September 30th, 2009

Did you ever wanted to quickly find which sessions are blocking each other in Oracle?

In this post I will introduce an SQL script, which would quickly tell you if there is blocking between the sessions and which would also show you what SQL these sessions are using.

To see the script working, first create a dummy table and insert some test data.

SQL> create table t (a char(1));

Table created.

SQL> insert into t values (‘z’);

1 row created.

SQL> commit;

Then select some rows from the dummy table for update.

SQL> select * from t where a=’z’ for update;

A
-
z

In second session try to update the rows which you have selected above. Due to locks this will block! ACID and serialization kicks in.

SQL> update t set a=’x’ where a=’z';

It will just hung!

To see what is blocking, run this query in a third session as SYSDBA.

SELECT TO_CHAR(sysdate, ‘DD-MON-YYYY HH24:MI:SS’)
|| ‘ User ‘
||s1.username
|| ‘@’
|| s1.machine
|| ‘ ( SID= ‘
|| s1.sid
|| ‘ ) with the statement: ‘
|| sqlt2.sql_text
||’ is blocking the SQL statement on ‘
|| s2.username
|| ‘@’
|| s2.machine
|| ‘ ( SID=’
|| s2.sid
|| ‘ ) blocked SQL -> ‘
||sqlt1.sql_text AS blocking_status
FROM v$lock l1,
v$session s1 ,
v$lock l2 ,
v$session s2 ,
v$sql sqlt1 ,
v$sql sqlt2
WHERE s1.sid =l1.sid
AND s2.sid =l2.sid
AND sqlt1.sql_id= s2.sql_id
AND sqlt2.sql_id= s1.prev_sql_id
AND l1.BLOCK =1
AND l2.request > 0
AND l1.id1 = l2.id1
AND l2.id2 = l2.id2
/

BLOCKING_STATUS
——————————————————————————–
15-JAN-2009 07:41:27 User hero@world.com ( SID= 144 ) with the statemen
t: select * from t where a=’z’ for update is blocking the SQL statement on hero@world.com ( SID=147 ) blocked SQL -> update t set a=’x’ where a=’z’

Category: Databases | Tags:  | Leave a Comment
Author:
Wednesday, September 23rd, 2009

Well, well, well, what do you know? Anders copies his own ideas from the 80′s (Object Pascal, Borland Delphi, anyone) and packs them in a new bottle called C# 4.0 and VS 2010.

Optional parameters

Optional parameters have been around in Object Pascal since the 80′s. Let us take a look at what C# 4.0 tries to do with it -

A parameter is declared optional simply by providing a default value for it:

public void M(int x, int y = 5, int z = 7);

Here y and z are optional parameters and can be omitted in calls:

M(1, 2, 3); // ordinary call of M
M(1, 2); // omitting z €€œ equivalent to M(1, 2, 7)
M(1); // omitting both y and z €€œ equivalent to M(1, 5, 7)

Multi Monitor Support

Back in 1995, when no one had heard of Multi monitor, Delphi 1.0 had undockable windows (editors, designers etc.) and the good thing with this architecture was that it could be moved across multi monitors later in early 2000′s when people started using multi monitors for development work.

Well, I have been using a dual monitor setup for quite some time now and with VS 2005, 2008, I could never extend the IDE beyond my primary monitor. But with VS 2010, I can undock the “windows” from the main window of the IDE and move it across! It was about time, I suppose?

Here is how VS 2010 looks on dual monitor -

step3

 

Category: .Net  | Leave a Comment
Author:
Sunday, September 13th, 2009

REST follows

As mentioned in my previous post(RESTful implementation using .Net) i would like to share further lights on RESTful implementation using .net, this time on implementing friendly RESTful urls using HTTP Modules. Before getting into the post as such, i would like the audience to look into topics related to query string injection and prevention techniques as  RESTful urls mostly deal with query strings , HTTP GET, POST verbs, etc.

Friendly REST urls

As an example , for a url, (the below shown url is the same RESTful url, that i had implemented in my previous post(RESTful implementation using .Net) to get employee details).

http://isense.com/employeeprofile.asmx/GetInfo?empid=1

we can make it user friendly as below,

http://isense.com/employeeprofile/GetInfo/1 

i am going take this simple scenario and show how such friendly url rewriting can be implemented using HTTP Modules. Once we get the concept, we can go ahead in developing robust url rewriting engines.

 

HTTP Module

 HTTP Module is one of the extensibility feature and advantage provide in .Net Framework. This helps in intercepting and manipulating each of the http requests that comes  from the client to the server application. Now in order to provide meaningful RESTFul urls, i am going to make use of this advantage provided by the HTTP pipeline. Below diagram describes the HTTP pipeline and the components involved in a typical client server architecture involving .net.

HTTPModule

In order to tap the incoming request using http module in our application (Web, Web service, etc) following things are to be performed.

 

Implementation

1. Creating Http Module class :

Add a normal class file to the application which implements  IHttpModule and its members,

System.Web.IHttpModule.Init(System.Web.HttpApplication)  &

System.Web.IHttpModule.Dispose().

The Init method can be made use to perform  initializations that are required and which also holds the handle to the HttpApplication object which  in turn has control over the request, response objects.

Here in the Init method, i am adding a event handler which processes the application’s begin request event.

public void Init(System.Web.HttpApplication app)
        {
            app.BeginRequest += new EventHandler(Rewrite_BeginRequest);

        }

then in my Rewrite_BeginRequest custom method,  i am adding a simple piece of code,which intercepts the incoming request url, checks for the user-friendly format ( these formats are to be predefined and shared to the consumers of the webs service as some manuals / guidelines, so that they get an idea on how to query for a particular data), if valid,  rewrites the url  to the original page with the desired & required parameters as query strings.

Pseudo code of the Rewrite_BeginRequest(object sender, System.EventArgs args) implementation,

1. Typecast the incoming sender object to the http application object in order to get the handle of request, response, context and other asp.net objects.

System.Web.HttpApplication Appl = (System.Web.HttpApplication)sender;

2. Tap the incoming request url, look for the friendly url pattern "/employeeprofile/GetInfo/€, if valid , crop , validate and store the id part which follows the pattern.(the valid employee id number like 1, 10, etc which comes as part of the ur after the pattern -  "/employeeprofile/GetInfo/1 ) .

3. Now, once the id is retrieved, rewrite the url to the original page by using,

Appl.Context.RewritePath("employeeprofile.asmx/GetInfo?empid=1");

That’s it, now the coding for the url rewriting is done. as this is a simple demonstration i have hard-code the urls, but when it comes to real-time implementation we can define the friendly url patterns and their respective redirection urls in some configuration file and create  a url redirection module / library /engine which performs the above pseudo logic.

 

2. Web Config entry:

Once the coding part is over, we need to configure the custom http module in the web config, hence forth ASP.Net engine recognizes the module and invokes it,

<httpModules>
              <add type="RESTFulService.RESTfulHandlerModule,RESTFulService"      name="RESTFulRewriter" /> </httpModules>

the type attribute, takes in classname, assemblyname as input.

the name attribute is any meaningful name.

I hope this post would be a starter point in the future implementation of a robust URL rewriter engine.

Author:
Tuesday, September 01st, 2009

Querystring manipulation attack is one of the most common methods of attacking a vulnerable web site. Basically it involves changing querystring passed to webpage. Let me show this with an example:

Lets say you have a page on your site to search orders. In search results the OrderID has as a hyperlink pointing to a page ShowOrderDetails.aspx?id=XYZ, where XYZ is the actual orderid (eg. ShowOrderDetails.aspx?id=34).

A user can visit the ShowOrderDetails.aspx page and change the ID value and see any order. This is could be a problem if your site requires a login and not all users are allowed to see all orders. Even if all users are allowed to see all orders, this could still cause problems. Someone can easily write a program to loop through all orders and scrape all the data. This could be a risk based on the sensitivity of the data.

To solve this, you can simply check if a user is “logged in” in ShowOrderDetails.aspx page and avoid unauthorized access. But it will still allow any registered/valid user to see all orders, which may not be desirable.

An easy way to fix this is to encrypt the ID that’s passed in the query string (eg. ShowOrderDetails.aspx?id=XyS3frfasYFx). You should use a strong encrytion algorithm like DES or TrippleDes. The framework library has classes for most standard encryption algorithms.

Now that you’ve encrypted the ID in the querystring, you’d assume your application to be pretty secure. But, there’s still one way to see unauthorized orders: Browser history. The querystring is stored in browser history, so technically you can see all orders someone else has seen if the brower history is available. This defeats the encrypted query string.

One way to prevent this problem is to either use/include the sessionId (or userid) as a part of the key that’s used to encrypt/decrypt orderid. This would give a different encrypted string for the each session/user. Even if a different user click a link in the history, his sessionid/userid will be different, hence decryption will fail.

Notes:

1) The session is active until expiry or browser window closure, so technically, you’re still at risk if the user doesn’t log off from your site and leaves the browser window open (even if he has closed the tab that had your site open).

2) Some people suggest using HTTP_REFERER to prevent querystring manipulation. But its very easy to break since http headers as sent in plaintext over network. Also you can simply use the WebBroswer control in a .NET WinForms application to change the href of any link and hit the link. This defeats the HTTP_REFERER check.

3) Query string manipulation attack is not restricted to GET. POST method is also vulnerable since the information is sent in plain text over the network.

Category: .Net | Tags: ,  | 3 Comments
Author:
Tuesday, September 01st, 2009

Introduction : Multi-threaded programming is the real challenge for developers .With many threads running in the system to accomplish different tasks ,it’s very much required to keep track of the status & manage individual threads.There are different ways to achieve the thread management e.g. Thread pooling,Thread concurrency API ,Thread groups etc.

I will give a simple example to manage & keep track of the thread status.

In a particular multithreading scenario ,say the requirements are -

1.Multiple threads should run to do different tasks.
2.Each thread should start/stop individually.
3.Sleep Time of the thread should be configurable.

Steps to implemement-

1.The parameters start/stop,sleep time we can set in the database.
2.When the threads are executing we can keep track of the status of the thread by creating a user defined APIs, e.g. We created a singleton Thread Manager class ,which has 3 API’s to monitor status of the thread -

register()-It will register the thread into a java collection, when the thread has started executing.
unregister()-It will remove the thread from the collection ,when the thread is stopped.
isActive()-It will check every time whether the thread is active(return value is boolean).

Code Snippet:

public class ThreadManager {

private static HashMap hmThreadState = null;

private ThreadManager() {
}

public static synchronized boolean register(String processor, long id) {

boolean isSuccess = false;
if( hmThreadState == null)
{
hmThreadState = new HashMap();
}

if (!hmThreadState.containsKey(processor)) {
hmThreadState.put(processor, id);
isSuccess = true;

} else {
System.out.println(“unable to register ” + processor + ” with thread id ” + id + ” another thread is active”);
}

return isSuccess;
}

public static synchronized boolean unregister(String processor, long id) {
boolean isSuccess = false;
if (hmThreadState != null && hmThreadState.containsKey(processor)) {
hmThreadState.remove(processor);
isSuccess = true;
} else {
System.out.println(“unable to unregister ” + processor + ” with thread id ” + id);
}
return isSuccess;

}

public static synchronized boolean isActive(String processor) {

boolean isActive = false;
if (hmThreadState != null && hmThreadState.containsKey(processor)) {

System.out.println(“processor ->” + processor + ” thread id ” + hmThreadState.get(processor));
isActive = true;
}

return isActive;
}
}

We register and unregister the threads when the run() gets invoked and when the thread exits (i.e stopped)

Code snippet for run()-

public abstract class ThreadProcessor extends Thread {

public void run() {
try {
String thrdName = Thread.currentThread().getName();

if (ThreadManager.register(this.getClass().getName(), Thread.currentThread().getId())) //Registering the thread into
collection
{
while (true) {

int stop = Configuration.getSleeptime(this.getClass().getName()+ “_Stop”); //getting stop parameter for the thread
from database,if stop==0 ,thread will
start ,else it will stop

if (stop == 0) {
int sleeptime = Configuration.getSleeptime(this.getClass().getName() + “_SleepTime”); //getting the sleeptime from
database
Thread.sleep(sleeptime * 60 * 1000);
} else {
if ( ThreadManager.unregister(this.getClass().getName(), Thread.currentThread().getId() )) // unregistered the
thread from collection
{
break;
}
}
}

}

} catch (InterruptedException e) {
e.printstacktrace();
}
}

}

Code snippet to intiate and check the thread status :

We start the thread and check the status of the thread in the following API.The Processor class has extended the ThreadProcessor class.If the stop parameter is ’0′ or the isActive API is returning false ,the thread will start otherwise not.

public void start() {

int processorstop = Configuration.getSleeptime(“processor_Stop”);

if (processorstop== 0 && ThreadManager.isActive(“processorstop”) == false) {
pd = new Processor();
pd.start();
}

}


Category: Java | Tags: ,  | One Comment
Author:
Tuesday, September 01st, 2009

Data Profiling and Automated Cleansing Using Oracle Warehouse Builder

Data profiling is the process of examining the data available in an existing data source (e.g. a database or a file) and collecting statistics and information about that data. The purpose of these statistics may be to:

  1. Find out whether existing data can easily be used for other purposes
  2. Improve the ability to search the data by tagging it with keywords, descriptions, or assigning it to a category
  3. Give metrics on data quality, including whether the data conforms to particular standards or patterns
  4. Assess the risk involved in integrating data for new applications, including the challenges of joins
  5. Assess whether metadata accurately describes the actual values in the source database
  6. Understanding data challenges early in any data intensive project, so that late project surprises are avoided. Finding data problems late in the project can lead to delays and cost overruns.
  7. Have an enterprise view of all data, for uses such as Master Data Management where key data is needed, or Data governance for improving data quality.

Most organizations build a data warehouse to provide an integrated, reliable, and consistent €œsingle version of the truth.€ Data is usually sourced from a number of systems and has to be extracted, cleansed, and integrated before being made available for users to query.

The quality of the data loaded into the data warehouse is often variable, however, and for that reason, historically the process of profiling your source data has been a time-consuming, manual process that has required either lots of experience with SQL*Plus or the purchase of an expensive third-party tool.

Oracle Warehouse Builder has the built in ability to profile data and no knowledge of SQL*Plus is required. Furthermore, the data profiles through Oracle Warehouse Builder can be used to generate automatic corrections to the data.

Data Profiling and Correcting Within Oracle Warehouse Builder

Data within your data warehouse can only be turned into actionable information when you are confident of its reliability. When you bring data into your data warehouse, you need to first understand the structure and the meaning of your data, and then assess the quality and the extent to which you may need to cleanse and transform it. Once you know what actions you need to take, you then need to make the required corrections to the data, and put in place a means to detect and correct any more errors that might occur in future loads. To do this, Oracle Warehouse Builder includes three new features that make this process simple and straightforward:

  1. Graphical Data Profiler€€Enables you to understand the structure, semantics, content, anomalies, and outliers present in your data, and derive data rules that will later be used within your data warehouse
  2. Correction Wizard€€Takes your data rules and applies them to your data, automatically generating correction mappings to cleanse and transform your data
  3. Data Auditor€€Takes your data rules and monitors the quality of subsequent data loads

Apart from removing the need for complex SQL*Plus scripts or third-party tools, doing your data profiling and corrections within Oracle Warehouse Builder has several advantages. The metadata that you generate about your data quality is stored alongside the other metadata in your design repository. Also, the mappings used to correct your data are regular Oracle Warehouse Builder mappings and can be monitored and managed with all of your other ETL (extract, transform, and load) processes. Doing your data cleansing and profiling within Oracle Warehouse Builder means that you only have to learn a single tool, and in addition, by integrating this process with your other ETL work, you ensure that data quality and data cleansing becomes an integral part of your data warehouse build process, and not just an afterthought.

Category: Data Warehouse | Tags: ,  | 2 Comments
Author:
Tuesday, September 01st, 2009

There are number of problems that may be encountered in trying to automate testing. Having some idea of the type of problems that encounter should help in implementing an effective automation regime.

Few common problems are described below.

1. Unrealistic expectations. Generally there is a tendency to be optimistic/have high expectation about what can be achieved by a new test tool. It is human nature to hope that this new test solution will at last solve all of the problems we are currently experiencing. Vendors usually emphasize the benefits and successes, and may play down the amount of effort needed to achieve the desired benefits. If management expectations are unrealistic, then no matter how well the tool is implemented from a technical point of view, it will not meet expectations.

2. Expectation that automated tests will find a lot of new defects. A test might more likely find a defect the first time it is run. If a test has already run and passed, running the same test again is much less likely to find a new defect (unless the test is exercising code that has been changed or could be affected by a change made in a different part of the software, or is being run in a different environment).

Test execution tools are ‘record – replay’ tools, i.e. regression testing tools. Their use is in repeating tests that have already run. This is a very useful thing to do, but it is not likely to find a large number of new defects, particularly when run in the same hardware and software environment as before. Knowing that a set of tests has passed again gives confidence that the software is still working as well as it was before, and that changes elsewhere have not had unforeseen effects.

3. Poor testing practice. If testing practice is poor, with poorly organized/designed tests, little or inconsistent documentation and tests that are not very effective at finding defects, automating those tests is not a good idea.

4. Maintenance of automated tests. When software is changed it is often necessary to update some, or even entire test suite, so they can be re-run successfully. This is particularly true for automated tests. Test maintenance effort is the biggest challenge and often reason to truncate many test automation initiatives. When it takes more effort to update the tests than it would take to re-run those tests manually, test automation will be stopped.

5. False sense of security. Just because a test suite runs without finding any defects, it does not mean that there are no defects in the software. The tests may be incomplete, or may contain defects themselves. If the expected outcomes are incorrect, automated tests will simply preserve those defective results.

6. Technical problems of tools. Commercial test execution tools are software products, sold by vendor companies, they are not immune from defects or problems of support. Interoperability of the tool with other software, either your own applications or third-party products, can be a serious problem. Many tools look ideal on paper, but simply fail to work in some environments.

In addition to technical problems with the tools themselves, we may experience technical problems with the software we are trying to test. If software is not designed and built with testability in mind, it can be very difficult to test, either manually or automatically. Trying to use tools to test such software will add complication which will only make test automation even more difficult.

7. Organizational issues. Automating testing is not a trivial exercise, and it needs to be well supported by management and implemented into the culture of the organization. Time must be allocated for choosing tools, for training, for experimenting and learning what works best, and for promoting tool use within the organization.

Test automation is an infrastructure issue, not just a project issue. In large organizations, test automation can rarely be justified on the basis of a single project, since the project will bear all of the start-up costs and teething problems and may reap little of the benefits. If the scope of test automation is only for one project, people will then be assigned to new projects, and the automation initiative will be lost.

Author:
Tuesday, September 01st, 2009

We usually think of rounding as applying only to numeric values: If the digit to the right of where you want to round is 0-4, round down; if it’s 5-9, round up. In Oracle, the ROUND function is used for this: The expression ROUND(12.34, 1) evaluates to 12.3, and the expression ROUND(12.37, 1) evaluates to 12.4. Likewise, the TRUNC function truncates instead of rounding. Both TRUNC(12.34, 1) and TRUNC(12.37, 1) evaluate to 12.3.

But you can also apply ROUND and TRUNC to date expressions, giving you the ability to round and truncate to a specified date boundary. What is returned is another date value, but it is adjusted to the requested boundary. To specify the boundary, use the same format codes used with the TO_CHAR and TO_DATE functions: €˜dd’ for day, €˜mon’ for month, €˜q’ for quarter, and €˜y’ for year. You can also round time the same way: use €˜hh’ or €˜hh24€² for hours and €˜mi’ for minutes.

Listing A is a sample report from the EMPLOYEES table in Oracle’s sample HR schema. For each employee, it shows the employee name and exact hire date, the first day of the month in which the employee was hired (hire_month), and it calculates the start of the following month (benefits_date).

col employee_name format a30
col hire_date format a15
col hire_month format a15   

col benefits_date format a15
SELECT   

    last_name || ', ' || first_name employee_name   

    ,hire_date   

    ,TRUNC(hire_date, 'mon') hire_month   

    ,ADD_MONTHS(TRUNC(hire_date, 'mon'), 1) benefits_date   

 FROM   

    employees   

 WHERE   

    last_name LIKE 'B%'   

 ORDER BY   

    employee_name   

 /

The ADD_MONTHS function is used here to round up to the following month. First, I use TRUNC to get the beginning of the current month; then I use ADD_MONTHS to add one month. The resulting expression is:

ADD_MONTHS(TRUNC(hire_date,'mon'), 1)

When using DBMS_JOB to schedule batch jobs (vs. DBMS_SCHEDULER in Oracle 10g), TRUNC is a handy way to force a job to start at a particular time. Because the SYSDATE function returns both a date and a time, a job scheduled as €˜SYSDATE+1€² is scheduled for tomorrow but at the current time. TRUNC strips away the decimal portion, which represents the time and resets the date to midnight. So TRUNC(SYSDATE) is midnight of the current day, and TRUNC(SYSDATE)+1 is midnight of the following day. Adding a fractional offset to this sets the proper time. The expression TRUNC(SYSDATE) + 1 + 6/24 + 30/1440 starts the job at 6:30 A.M. (There are 24 hours in a day and 1440 minutes.)

Category: Databases | Tags: ,  | Leave a Comment
Author:
Tuesday, September 01st, 2009

If the term Unit Testing is totally new for you then here are a few definitions that will get you started about the concept€¦

What is a Unit in code?

A unit is typically a code block with a defined simplified functionality. Such a code block can be a function or a class.

What is Functionality?

Functionality is an operation that will either process input and return output or perform action that modifies the state of the object/system.

What is Unit Testing?

Each unit is tested programmatically in isolation to verify its independent behavior.

  • Establishes an artificial environment.
  • Invokes the operation.
  • Checks the results returned against some known value, thus verifying that the unit performs the defined functionality as expected.

Or precisely, a code that executes and verifies a piece of code which is part of the main system.

Now that you know what Unit Testing is, quite possibly you have also developed those initial doubts, first and foremost€¦

Why do we need Unit Test?

For two simple reasons:-

  • Unit tests check their own results and provide immediate feedback
    (can also be configured to run automatically).
  • Unit tests increase the stability of software.

Consequently enables you to develop more confidence in the code.

You will be more comfortable refactoring code and adding new features to the code that is unit tested. Whereas if the code is not unit tested then you will be paranoid about refactoring or adding new features because you don€˜t know what might break as a result of change.

For those who already know what Unit Testing is but still do not practice it, for sure they have there own valid reasons, lets just analyze some of the standard reasons:-

  • Don’t have sufficient time to unit test.
    -The time spent on unit testing is actually the time saved from debugging.
  • The client wants us to develop code, not write unit test.
    -The client surely does not want you to create bugs, unit test helps prevent bugs.
  • I am supporting a legacy application without unit tests.
    -With legacy application you can reform each unit by introducing unit test for each unit that is modified.
  • QA and User Acceptance Testing are far more effective in finding bugs.
    -True, but that does not give reason to developer for passing stupid mistakes to QA or User. Further defects can be found and fixed before they get formed. Hence less time spent on debugging.
  • Effort of unit testing is more than the effort of writing code and testing it.
    -There are many good frameworks that simplify your effort of writing unit tests. The point is being aware of such frameworks and there advantages.
  • Unit testing is for novice programmers not for me.
    - Unit test is intended to standardize the quality of code produced by a team with varying expertise of members. A good unit test demonstrates what all points where taken in consideration while writing the good code. If you think you never make mistake in your code then share your expertise with fellow programmers by writing good unit tests.
  • I don’t know how to unit test or I don’t know how to write good unit tests.
    - Read on to know about writing good unit tests.

If you need some more convincing then refer to Eric’s and & Brian’s – Top 12 Reasons to Write Unit Tests:

http://onjava.com/pub/a/onjava/2003/04/02/javaxpckbk.html

And Ian Nelson’s – 12 Reasons why he love Unit Tests:

http://ianfnelson.com/blog/why-unit-test/

The reason why I write Unit Test is because it demonstrates the ingenuity of every piece of code that shapes my perfect system. And with every successful unit test I feel the excitement of achievement.

How to write Unit Tests?

No matter which framework you chose fundamentally things remain same:-

You need one or more classes where you will write methods with code that will test other classes and functions of your main code. These classes are called TestClass in VSTS (and TestFixture in NUnit). Such classes are marked with TestClass attribute (or TestFixture in NUnit).

The methods of the Test Class that execute a unit of the main code and verify the result are called TestMethod (or Test in NUnit). These methods are marked with TestMethod attribute (or Test in NUnit).

At times there might be a need to perform certain initialization operations before running each of the Test methods such initialization operation can be written inside a TestInitialize method (or Setup in NUnit). Correspondingly the cleanup operations that should be performed after each Test can be written in TestCleanup method (or TearDown in NUnit).

VSTS Test example:-

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class SampleUnitTest
{
[TestInitialize()]
public void Init() { }

[TestMethod]
public void TestOperation() { }

[TestCleanup()]
public void Destroy() { }
}

NUnit Test example:-

using NUnit.Framework;

[TestFixture]
public class SampleUnitTest
{
[SetUp]
public void Init() { }

[Test]
public void TestOperation() { }

[TearDown]
public void Destroy() { }
}

Apart from these commonly used methods there can be additional methods provided based on the selected framework, like €€œ static ClassInitialize (or TestFixtureSetup in NUnit) and static ClassCleanup (or TestFixtureTearDown in NUnit). ClassInitialize executes only once before any test method in the test class is executed and ClassCleanup executes only once after all test methods in the test class has been executed.

Further, apart from the above attribute classes of the framework another very important class is the Assert class that contains static methods for comparison and verification of results of the test against some expected values.

Having the above fundamental knowledge of Unit Testing, we are now ready to write some example.

I do not prefer replicating the classic LoginInfoTest example by Mark Michaelis and extended by authors at codeproject.com and other technical blogs. Rather I have something very simple (and my favorite) a variation of €œHello World€, the €œHello User€ example.

Step 1: Create our main application

Lets create and assembly HelloUser with a class Greeting with a method Greet that will return greeting message string for the given user.

namespace HelloUser
{
/// <summary>
/// Greets User
/// </summary>
public class Greeting
{
/// <summary>
/// returns greetings based on given user
/// </summary>
/// <param>name of the user</param>
/// <returns>greeting for the user</returns>
public string Greet(string userName)
{
if (string.IsNullOrEmpty(userName))
return “Hello User”;

switch (userName)
{
case “Kamal”:
return “Hi Kamal”;
case “Shameer”:
return “Hey Shameer”;
case “Roy”:
return “Hoi Roy”;
default:
return “Hello ” + userName;
}
}
}
}

Note: In a Test Driven Development approach we would not have implemented the method before writing the following test, but for simplicities sake we will not get into TDD.

Note: Although I have marked the class and method as public for simplicities sake, the auto code generation of VSTS is also capable of generating test code stubs for non public members, however the generated code stub will be quite complex to understand.

Step 2: Creating Unit Tests

  • Perform a mouse right click on the class Greeting, that will give you an option to Create Unit Tests.
  • Selecting the option will open a dialog asking for a project to which the Unit Test Class should be added.
  • The default and recommended option will be create a new test project. And that will ask you to name your new test project. Lets name it has TestHelloUser.
  • The TestHelloUser project gets created with a class GreetingTest with a TestClass attribute and a lot of code and comment (compared to the size of our application code). For now lets just ignore (or remove) the rest and focus on the Test Methods.
  • For our example two test methods stubs would have been auto created, namely GreetTest and GreetingConstructorTest. Since we are relying on the default constructor of our Greeting class (or in lay terms do not have a constructor for Greeting class). Let us just delete the GreetingConstructorTest method completely.
  • Now the only Test Method we have and are concerned with is the GreetTest method that is supposed to perform Unit Test on our Greet method of Greeting class in the main assembly. As you would notice most of the code you need is already create, that is creating instance of Greeting class, invoking the Greet method and Asserting expected and actual value returned. All that we need to do in this method are:
    • Specify the input value of userName, (say €œBart€)
    • Specify expected value, (say €œHello Bart€)
    • Remove the Assert Inconclusive operation (VSTS (or more precisely, MSTest) adds this default assert to warn about test stubs (test methods) that have not been modified manually)
    • With the above changes our Test Method should look like:

[TestMethod()]
public void GreetTest()
{
Greeting target = new Greeting();
string userName = “Bart”;
string expected = “Hello Bart”;
string actual;
actual = target.Greet(userName);
Assert.AreEqual(expected, actual);
}

  • The last step left is running our Unit Test, select menu option
    Test >> Run >> Tests in Current Context
  • That should run the test and produce a Test Results dialog, reporting whether the test passed or failed. Double clicking the result should open result detail also mentioning the time it took for the test to run.

That’s it?

Well, yes that completes our example and tutorial about Unit Testing with VSTS, unless you are inquisitive about some automatically added files to your solution under Solution Items folder.

I will not get into the details as this is already beyond the topic (and honestly I don’t know much) but just one feature as a bonus gift for readers who have reached so far and also because I find this feature very useful and that is Code Coverage.

In the Solution Items folder of our above solution you will find a file named LocalTestRun.testrunconfig file. Double click this file to open Test configuration Dialog.

Select the Code Coverage option to specify assemblies to be instrumented or (observed) for Code Coverage. Select the HelloUser assembly and Apply and Close.

Run the Test Again the Test Result dialog will show the result of Unit Test (GreetTest), double clicking which you can see the same Result Details.

Now, rather than performing a mouse double click on the Result for result detail perform a mouse right click on the result that will show you a set of options, select the option Code Coverage Results . That will open the Code Coverage Results window with a tree view for the HelloUser assembly. Expand the tree nodes to review the code coverage percentage and reach till the Greet method. Double click the Greet method and you will find the Greet method color coded, showing the lines executed during the test highlighted with a light blue color and the line not executed during the test highlighted with a light pinkish color (I am sorry I don’t know the exact name of the color). And this will help you analyze and decide the input values for your further Unit Tests that will perform more code coverage.

Apart from the ability to create Unit Tests and review Code Coverage, VSTS Unit Testing Framework provides features for pulling test data from external database and also provides an integral mock framework for creating mock objects for unit tests.

How to write good Code and good Unit Tests?

A basic rule that I follow is a code that cannot be easily unit tested is the code that should be refactored. So, simple Unit Tests also prove that your code is well structured.

Further the following best practices describe the principle for writing good Code and Unit Tests:

  • Write code that is easy to Unit Test.
  • Code a little, test a little, and code a little, test a little…
  • Build in testability from the very beginning.
  • Unit test each piece before integrating them.
  • Each test should be independent of other tests and can be run autonomously.
  • Run your tests as often as possible.
  • Avoid creating unit test dependency on machine or system state. Like initial database value or directory path.
  • Use Initialize and Cleanup to set and clear system state for Unit Tests.
Category: .Net | Tags: ,  | Leave a Comment
Tuesday, September 01st, 2009

In this we are going to discuss some point about project management features and discuss what new for that in TFS 2010.

Team Foundation Server provides project management features such as centralized work item management, process management, security and permissions management, project metrics, and reporting to improve your ability to manage development projects in Visual Studio.

The software-development lifecycle has been made an integral part of the tooling to support software project development efforts. TFS provides the MSF Agile and MSF CMMI process templates, which support two very different development methodologies. We can modify the supplied process templates or create one from scratch in order to meet your team’s development process needs.

MSF for Agile Software Development Process Template

The work item types provided by this process template include:

  • Scenario €€œ Used to represent a user interaction with the application system. It records the specific steps necessary to reach a goal. When writing scenarios, be sure to be specific as there may be many possible paths.
  • Task €€œ Used to represent a unit of work that needs to be performed. Each role has its own requirements for a task. For example, a developer uses development tasks to assign work.
  • Quality of Service Requirement €€œ Used to document the system characteristics such as performance, load, availability, stress, accessibility, and serviceability
  • Bug €€œ Used to communicate a potential problem in the system.
  • Risk €€œ Used to identify and manage the inherent risks of a project.

Work Item Type: Bug

1

Work Item Type: Scenario

2

Work Item Type: Quality of Service Requirement

3

Work Item Type: Risk

4

Work Item Type: Task

5

MSF for CMMI® Process Improvement Process Template

The work item types provided by this process template include:

  • Requirement €€œ Used to capture the requirements defined during the requirements gathering phase.
  • Change Request €€œ Used to capture any change requests subsequent to the gathering of requirements.
  • Issue €€œ Used to capture issues to be tracked in the projects.
  • Task €€œ Used to represent a unit of work that needs to be performed. Each role has its own requirements for a task. For example, a developer uses development tasks to assign work.
  • Review €€œ Used to represent the review work units with in the projects, like code review, design review etc.
  • Bug €€œ Used to communicate a potential problem in the system.
  • Risk €€œ Used to identify and manage the inherent risks of a project.

Work Item Type: Bug

6

Work Item Type: Requirement

7

Work Item Type: Change Request

8

Work Item Type: Issue

9

Work Item Type: Review

10

Work Item Type: Risk

11

Work Item Type: Task

12

New in TFS-2010

Updated MSF Agile Template

Terminology €€œ In general, It had adopted common Agile community terminology (Backlog, User Story, Story Points, etc) and moved away from Microsoftish terminology.

Simplification €€œ It had simplified the work item forms, focusing more on the stuff that is immediately relevant. It had eliminated fields people didn’t care much about.

Scenario €€œ> User Story €€œ It had now moved to the Agile User Story model, including tracking User Story size as €œStory Points€.

Hierarchy €€œ Added hierarchical relationships so that User Stories can be decomposed into tasks and tasks can be decomposed into subtasks.

Improved reports €€œ Reports are much nicer.

Testing support €€œNew Team System testing tools added as first class support. The process template contains a Test Case work item type and other features to enable great integration.

Guidance rewrite

User Story:

13

14

15

Task:

16

Bug:

17

18

Updated MSF CMMI Template

New CMMI information model and the supported relationship types.

19

In addition to that:

CMMI 1.2 compliance

Two new requirement types €€œ Added Business Objective and Feature to the existing set of requirement types.

Improved reports €€œ The reports much nicer.

Testing support €€œ Added the new Team System testing tools as first class support. The process template contains a Test Case work item type and other features to enable great integration.

New Reports

:

Much more attractive and powerful €€œ Taking a dependency on SQL 2008 allowed to leverage the new reporting capabilities there. The result is reports that are much more visually attractive and can represent much more complex data relationships.

Self explanatory €€œ Lot more content put into the reports to help us understand what the report is intended to tell you, what data you are looking at and generally give much better context for interpreting the report.

New Excel reports €€œ For the first time, some of reports are authored as Excel workbooks. If we use MOSS for our portal, we can host them there, otherwise we can open them in Excel. The primary advantage of this is that although they are a bit less powerful than Reporting Services, they are much easier to customize.

Just to give you a view of all the reports its included, here are some screenshots of Team Explorer:

2021

Project Management Features in Team Foundation Server

  • Process management. Team Foundation Server process management includes Microsoft Solution Framework (MSF) process guidance as well as process templates that set up new team projects with work item types, reports, a project SharePoint portal, and source control settings.
  • Security and permissions. New projects contain default groups and permissions that map to common development team roles.
  • Centralized work item management. Work items including bugs, risks, tasks, scenarios and quality of service (QoS) requirements are centrally recorded, managed, and maintained in the TFS work item database. Centralizing their storage makes it easy for all team members to view and access them.
  • Microsoft Office Excel® and Microsoft Office Project integration. By using the Office Excel and Office Project integration features, project managers can continue to access the work item repository and schedule information by using tools they already know.
  • Metrics and reporting. TFS provides a reporting service which transforms operational data such as work items, build results, and test results into metrics stored within TFS data warehouse. Predefined reports allow you to query a variety of project health and quality metrics.
  • Project portals. For every team project, TFS creates an associated project portal that uses Microsoft Windows SharePoint® Services. You use the portal to manage project-related documentation, and to quickly view key reports and assess project’s current status.

Benefits

The project management features of TFS provide the following benefits:

  • Centralized management
  • High traceability
  • Integrated project planning and scheduling
  • Improved process control
  • Improved team communication and cohesiveness
  • Accurate progress reporting

Strategies for Team Projects

Team Project per Application

This is the most common strategy for creating team projects. This approach is useful for both large and small applications, as well as multiple releases of applications being developed in parallel. With this approach, you create one project for each application under development.

Team Project per Release

This approach is useful for large teams who are working on long-running projects. After every major release, you create a new project and have a clean start. With this approach we don’t have to worry about carrying the previous release’s baggage forward, including work items. Also, this approach provides you with the opportunity either to improve the process templates or use new ones based on your newly acquired experience and learning.

Team Project per Team

This approach is useful for large projects that span multiple teams, where central control and activity monitoring is important. With this approach, we create a project for each team. This approach closely aligns a team with the workflows defined in TFS work item types and provides a unit of reporting that spans the entire team.

Category: .Net | Tags: ,  | 4 Comments