Friday, April 24, 2009

Short Rant on StringBuilder and String

I’ve been running into lots of code which seems to be using Strings to build up HTML or Error Messages.  Unfortunately there is lots of inconsistency in the way it’s been done, but for anyone interested here is my take on it.  First off let me say any hard coding HTML into a code behind is generally a bad idea (don’t get me wrong I’ve done it myself lots of time) so try to avoid it at much as possible.  If however you are unable to avoid it, please review the examples below and try to keep to using the “Good” technique.

The Good

Here is a reasonably good example of code for building HTML in a code behind if you can’t avoid it.  Using StringBuilder is faster and better suited for concatenation than the primitive String type.  Also using the AppendFormat function allows you to use the {#} place mark which ensures your value goes into the correct location, making the string far more readable and maintainable.

StringBuilder sbTemp = new StringBuilder();
sbTemp.AppendFormat("<a href=\”mailto:{0}\”>{1}</a>",
                                                          ConfigurationManager.AppSettings["IT_EMAIL"],
                                                          ConfigurationManager.AppSettings["IT_EMAIL"]);
litITEmail.Text = sbTemp.ToString();

 

The Bad

Here is a bad example of the same code using String Builder.  although it will accomplish the same goal as above it requires more code and is much more difficult to read or maintain.
sbTemp.Append("<a href=\"mailto:");
sbTemp.Append(ConfigurationManager.AppSettings["IT_EMAIL"]);
sbTemp.Append("\">");
sbTemp.Append(ConfigurationManager.AppSettings["IT_EMAIL"]);
sbTemp.Append("</a>");
litITEmail.Text = sbTemp.ToString();

 

The Down Right Ugly (and wrong on so many levels)

Here is the wrong way to do it and anyone doing this should be taken out and shot.
string s = "<a href=\"mailto:";
s += ConfigurationManager.AppSettings["IT_EMAIL"];
s += "\">";
s += ConfigurationManager.AppSettings["IT_EMAIL"];
s += "</a>";
litITEmail.Text = s;

Saturday, April 4, 2009

Crazy Open Source project!

I found out about this project the other day, ReactOS (http://www.reactos.org/en/index.html ) which is an open source version of Windows!  Apparently it will “eventually” replace MS Windows on the Users desktop without Users ever noticing the change. Oh my God!, is this a law suit waiting to happen or not?

It’s only in Alpha at the moment, but apparently it works well……

Friday, April 3, 2009

Web Testing Applications

One of the great things about the unit testing is that it breaks down tasks into small meaningful chunks; sadly testing web brings up a whole set of new issues.  With web applications the testing is usually based on a script or number of steps which could be spread over any number of different screens.  I’ve used a number of different tools to do this an all have issues and limitations and they all cost heaps of cash.  then I’ve discovered Selenium a free open source tool for doing just that.

Installing Selenium on your development machine

Selenium is a java based application so you need to install the latest Java Runtime which can be downloaded from the Sun website.  Run the installer taking all the standard defaults and this should place everything needed into your C:\Program Files\Java\ folder.

Next you need to download a copy of Selenium RC (selenium-remote-control-1.0-beta-2-dist.zip) from the application share or from the Selenium website to your drive. Open this file using Window Explorer or any Zip file reader and extract only the \selenium-remote-control-1.0-beta-2\selenium-server-1.0-beta-2 folder.

image

Extract these file to a folder d:\applications\selenium-server.

To start the service running you need to go to the command prompt and typing:

    d:
    cd d:\Applications\selenium-server\
    java –jar selenium-server.jar

image

You should start to see the output above showing that you now have a server running on port 4444.

Installing the Selenium Extension

Open the Fire Fox browser and select tools/Add-ons from the menu.

image

Select Get Add-Ons from the menu and type Selenium into the search text box and hit return. You should see Selenium IDE appear as the first item in the list. Select this and click the “Add to Firefox” button.  After installation restart Firefox.

image

There should now be a new item added to your Tools menu called Selenium IDE. 

Recording a test

While on the webpage select Selenium IDE from the Tools menu in Firefox.

image

A dialogue box should appear and you can notice that the red circle in the top right hand side indicates that you are now in recording mode.

On the page dome some work, in this case I placed the word “Fred” into the search field and clicked Search.

image

Select the “Page” text (or any text for that matter) on the page and right click.  You should see a submenu stating “Show All Available Commands” and within this “AssertTextPresent Page”.  Assert is the test in this case, you are testing that “Page” text will exist following the search.

image

Going back to your Selenium IDE should present you with 4 commands, one for each of the steps you’ve taken.  Click the red button in to top right of the screen to stop recording your steps and Save by click on File/Save from the menu.

Running the test

Once you have a saved test, you can run it at any time in the future by opening if from disk using File/Open from the menu.

image

Click the green play button on the tool bar to run your test, ensuring that the Base URL is correct.

image

If all the steps turn green you have successfully run the tests.

Adding the Selenium to Integration Server

The great thing about Selenium is that you can build each of these tests into the continuous integration server. 

image

From the options menu select Format/C# – Selenium RC.

image

This will present your test in C# code which can be cut/paste into the standard UnitTest infrastructure.

Monday, March 30, 2009

Adding UnitTests to CruiseControl

In this post I’ll describe how to add these tests to the Continuous Integration Server.  The process is very simple really, only a matter of a adding a new task in the Control Control Config Console (CCNETConfig) for that project.

Adding your Test to Continuous Integration

Assuming you have already added your UnitTest project and added it to the solution. Logon onto your build server and open CCNetConfig.  this is available from the start/all programs menu.

image

Click File/Open from the menu and browse to the servers configuration file (ccnet.config), which should be found in D:\Program Files\CruiseControl.NET\server folder on the local server and click Open.

Expand the project on which you have added the Unit tests and right click the Tasks node on the tree, in this case it’s MSS.

image

Choose Add Task and NUnitTask from the menu, you should then see the new node added to the tree.  Select this node and add the relevant information to the detail pane on the right.

image

(Assemblies) should point to the UnitTest DDL that is built during compilation.

Output File; should point to the logs folder on the build server

Path; points to the location of the NUnit Console application

timeout; Default to 100 seconds for timeout, however increase this for long running test projects.

Save your changes and you should be fine.

Viewing the results

Now every time the project builds your tests will be run against all the code.  This will highlight any places where the new code has adversely affected other areas of the solution.

image

From the CCNet control website (http://<server>/CCNet/) you can browse to your project and select “Latest Build” then select the “NUnit Details” link on the right of the screen.  Here you can see that 1 test was completed successfully.  Clicking the NUNIT Timings link will tell you how long it took to run the tests which have be very useful for performance testing.

Saturday, March 28, 2009

Blogging via Windows Live Writer

Its important to let everyone know what your doing, so here is an easy way to interact with a blog. I’d encourage everyone to post once or twice a week anything they’ve learned; be it technical or organisational, important or trivial. Once it has been entered into the blog others will benefit from that knowledge and we’ll forever have a record.

This is software is also ideal for documenting systems and procedures “as-you-go”, as all you need to do is take screen shots and paste them directly into your documentation.

To start I’ll show you how to download and use Microsoft Windows Live Writer and connect it to a SharePoint Blog.

Getting Live Writer

The software is freely available from the Microsoft Website, simply click the Download button, Click Run and Run again when prompted.

lr1

Uncheck everything except “Writer” and click Install.

lr2

You’ll be asked to close Internet Explorer, do this and click Continue.

lr3

You’ll see the Progress bar during the installation.

lr4

Deselect everything from the additional installation window as you don’t need it and click Continue.

lr5

Finally click close and you should be ready to start using the software.

Starting Live Writer and connecting to the IT Blog.

Once installed you should get a new item in your Start/Programs/Windows Live menu called Windows LiveWriter. It should open first time only asking you to connect to a default Blogging site.

image

Click Add..

image

Select SharePoint Blog and click Next.

image

In the Blog URL you need to type the following: /sites/it/blog/default.aspx, then">http://<server>/sites/it/blog/default.aspx, then click Next.

image

You should see a progress window as Live Writer searches for the Blog details.

image

If successful you will be asked for a Blog Name, I’d recommend choosing the same name as the blog, as you can add additional blogs to the application if needed.

image

You should now be able to access the Blog each time the application starts. If this is your only blog, it will open be default, however if you want to use multiple blogs be sure to check the upper right of the screen which should say “Blog”.

Publishing a post

Live writer is much like Word simply type in the work space and cut/paste images and screen shots. When you are finished click the Publish button in the upper left hand corner of the tool bar.

image

You should see the progress bar and finally a new Browser window will open and you can see your blog entry.

Wednesday, March 25, 2009

ASP.NET Mobile Browser

I’ve been investigating doing a port of my iPhone application to other platforms and discovered an interesting tool called Mobile Browser Definition File.  The file contains capability definitions for about 70% of the main mobile devices and browsers in use today.  Your ASP.NET project can use the .browser file, along with the information in the HTTP request header, to determine what type of device/browser has made the request and what the capabilities of that device are.

Check it out, it’s very useful if anyone wants to do work on mobile devices.  Did you know for example that many of the new Nokia phones don’t support the PNG graphic format!? 

Monday, March 23, 2009

Enable ASP debugging in an ASP.NET 3.5 Site

Today I got this error on an ASP running under Visual Studio 2008.

image

The error text was as follows:

The type of page you have requested is not served because it has been explicitly forbidden.  The extension '.asp' may be incorrect.   Please review the URL below and make sure that it is spelled correctly.

It turns out that ASP is not supported under Visual Studio 2008 unless you make 2 additions to the local Web.Config file. 

<compilation debug="false">
      <buildProviders>
        <add extension=".asp" type="System.Web.Compilation.PageBuildProvider" />
      </buildProviders>

</compilation>

 

<httpHandlers>
<add path="*.asp" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true" />
</httpHandlers>

This will enable ASP to work in the debug browser however if you have relative paths it won’t work as you’re running only one project under a specific port number.

i.e You will get this type of error.

image

Alternatively you can just set the server in the project properties to run under local IIS, which is just as good.

image