Syntax Highlighter, WordPress & CSS

I recently changed my Syntax Highlighter (which was cool, but old and abandon-ware) for the SyntaxHighlighter Evolved. Which I must say I love. Old versions were really bad. They were not that flexible. But version 2.0 is VERY flexible, and looks great. So I did the change in Neonlabs and Structum.

I stumbled with several problems. The first, and the one that bothered me more, was that in the Structum page, the code seemed to be broken into weird lines.

Broken Code

This was obviously a CSS problem since I didn’t have that problem in Neonlabs. So I used several tools to see which was the offending style. I mostly looked for the ‘pre’ tag on CSS but was not successful. After looking through the whole CSS style for my theme, I found the offending line of code:

#primary code {
	display:block;
	...
}

Yep, it was a simple display attribute. I just commented that line and everything just worked.

Fixed Code

After that I tried editing the code to make it look better for the Structum page and I noticed that when it loaded it had a nasty Warning:

Warning: htmlspecialchars_decode() expects parameter 1 to be string, NULL given in /…/wp-includes/compat.php on line 105

And when I looked at the code blocks in my posts, there was nothing. They were empty.

I did a little research to see what was the problem, and it seems it is a WordPress bug. Simple variable naming bug in compat.php:

	function htmlspecialchars_decode( $str, $quote_style = ENT_COMPAT )
	{
		if ( !is_scalar( $string ) ) {
			trigger_error( 'htmlspecialchars_decode() expects parameter 1 to be string, ' . gettype( $string ) . ' given', E_USER_WARNING );
			return;
		}
	...
  	}

Can you see the problem ? The problem resides in the $string variable. It doesn’t exist, so its value is null. Changing the variable parameter for is_scalar and gettype from $string to $str fixed the problem.

This is how it should look:

	function htmlspecialchars_decode( $str, $quote_style = ENT_COMPAT )
	{
		if ( !is_scalar( $str ) ) {
			trigger_error( 'htmlspecialchars_decode() expects parameter 1 to be string, ' . gettype( $str ) . ' given', E_USER_WARNING );
			return;
		}
	...
  	}

After that, everything is working seamlessly again :D. hope this helps :D.

Easy .Net Transaction Management with Transaction Scope

Transactions are a common technique to ensure consistency of the data when using database applicationsfor example with Sql Server. The System.Transactions namespace in the .Net framework simplifies Transaction Managements considerably.

This time we are going to talk about TransactionScope, which is part of the System.Transactions assembly. Using TransactionScope to manage transactions is fairly simple, yet powerful. Let’s look at an simple example using ADO:

TransactionScope tranScope = new TransactionScope();

SqlConnection connection = new SqlConnection(connectionSettigs);
SqlCommand cmd = new SqlCommand(query, connection);

connection.Open();
cmd.ExecuteNonQuery();
connection.Close();

transScope.Complete();

In the example we first initialized the TransactionScope object, then we initialized the connection (very important point, we will talk about it later), execute the command, etc. And then at the end we called the method Complete for the transaction scope object.

When the transaction scope object is initialized, the transaction is effectively created and all commands after that will be protected by the transaction. After the commands are executed, then we decide to either commit the transaction or rollback. Now, for the transaction scope, the method Complete effectively commits the transaction to the database, when Dispose executes the rollback.

For some developers, is quite confusing since most developers are used to:

// Handling the transactions explicitly.
SqlConnection connection = new SqlConnection(connectionSettigs);
SqlTransaction trans = connection.BeginTransaction();

SqlCommand cmd = new SqlCommand(query, connection);
cmd.Transaction = trans;

connection.Open();
cmd.ExecuteNonQuery();
connection.Close();

connection.Commit();

Not only is the TransactionScope idiom different but also the Commit and Rollback method names are also different. The reason why the names are also different is because 1) The TransactionScope object is not a transaction per se, it is more of a transaction handler and 2) Because TransactionScope follows the Dispose pattern (just like SqlConnection). Following the Dispose pattern to implement the TransactionScope makes it easy to do this:

// Handling the transactions implicitly.
using(TransactionScope tranScope = new TransactionScope()) {

	SqlConnection connection = new SqlConnection(connectionSettigs);
	SqlCommand cmd = new SqlCommand(query, connection);

	connection.Open();
	int id = cmd.ExecuteScalar();
	connection.Close();

	// If success commit
	if(id > 0) {
		transScope.Complete();
	}
}

The example displayed above shows how to handle implicit transactions using the ‘using‘ keyword. When applying the ‘using‘ idiom on an object which implements the Dispose pattern, the ‘Dispose‘ method of such object will be called when the using block ends.

What the transaction scope is doing is registering the transaction in the connection contained in the transaction scope block. Making it easy to handle transactions in .Net code.

In the case of the example, if the transaction scope is not committed before the block ends, it is effectively rolled back automatically, since the using idiom calls the Dispose method of the selected object. This makes the use of Transactions extremely easy to read and maintain.

Now what happens if there is an exception inside of the TransactionScope block ? Well, the object’s Dispose method is called, which automatically rolls back the transaction.

Now it is very important, while using TransactionScope, that the connection is opened inside of the transaction scope block, otherwise the transaction won’t be registered in the connection.

SqlConnection connection = new SqlConnection(connectionSettigs);

// This will not work. The transaction will not be registered in the connection,
// and it will deadlock your application.
connection.Open();

// Handling the transactions implicitly.
using(TransactionScope tranScope = new TransactionScope()) {

	SqlCommand cmd = new SqlCommand(query, connection);

	int id = cmd.ExecuteScalar();

	// If success commit
	if(id > 0) {
		transScope.Complete();
	}
}

connection.Close();

The above example will not work. What happens is that when the Connection is opened inside the TransactionScope block, the transaction is registered in the connection which means that any command for that connection will be in the transaction until the connection is closed.

Be very careful to initialize the connection inside the transaction scope block, otherwise the transaction won’t register itself in the connection, and your data won’t be protected by the transaction. In some cases, any command inside of the transaction scope block, for which the connection is opened before the transaction block will result in a dead lock. The code above serves as an example of this case.

More information:
MSDN:Transaction Scope Class.

MSDN:Implementing an Implicit Transaction using Transaction Scope

Unit Testing Data-Driven WCF Services

Any data driven module, class or method needs to be tested. At this moment anything that is data-drive (which would actually cover almost anything) needs to be verified according to specs to ensure correctness. In this post we will talk about creating tests for Data-Driven WCF Services. For simplicity and ease of use, we are going to use the Enterprise Library Data Application Block to talk to the database.

Before we get deeper into the topic at hand, let’s talk about the unit test environment that I use and recommend for testing data-driven applications.

Testing Context
The basic objective of a unit test is to verify correctness of a an application. Testing every unit in a layer ensures the correctness of the behavior of such units, specially when being used by other units in the application. This way all units can be reused with confidence.

Now in order to maintain confidence in the Unit being tested, we need to ensure that the tests are reliable and that providence confidence in the correctness of the unit under test. When a test sometimes pass and sometimes fails is called an Erratic Test, and in order to maintain confidence we need to avoid such behavior.

Unit Tests, as explained before, focus on testing the a single unit in a system, unlike integration testing. So it is a best practice to have one database instance per developer or tester. Having two developers running the test fixtures at the same time on the same database create an erratic test behavior, which is not acceptable.

Data-Driven Tests
The pattern we are going to follow in this post is a simple form of the Data-Driven Test Pattern, which is described as:

“We store all the information needed for each test in a data file and write an interpreter that reads the file and executes the tests”.

This pattern is mostly used to avoid code duplication, especially when several test cases just change on different data conditions. This way one test can be executed with several test scripts, instead of writing one test for every data condition.

We’ll use a simple form of the pattern, using Sql syntax for scripts instead of Xml data files. I find it easier to just create an Sql script, execute it and continue with the testing.

Each test must have a context setup. There are several patterns that specify that one test method sets up the data context for the others; for example, one test method tests the insertion of data, while the next test method tests the retrieval of the inserted data. I don’t recommend this setting, this leads to Erratic Tests. Every unit test must be independent of each other. This means that every test method must setup its own environment and it must also clean it up to the initial state.

Unit Testing WCF Services
Unit Testing WCF Services is extremely easy. There are two ways to do this:

1) Include the WCF Library as a reference.

2) Load up the WCF Service host and call the service from the test fixture.

Since we are mostly doing unit testing and not integration testing, loading the WCF Library as a reference works fine. For integration purposes I would write one test fixture for each endpoint in the configuration, but this is out of the scope of this post.

Test Code

So we have two service methods: 1) ProjectService.Insert and 2) ProjectService.GetIdByName. This is the interface of such methods:

namespace WCFTesting
{
    using System.ServiceModel;

    /// <summary>
    /// Project Service Contract.
    /// </summary>
    [ServiceContract]
    public interface IProjectService
    {
        /// <summary>
        /// Inserts a new Project.
        /// </summary>
        /// <param name="id">Project Identifier.</param>
        /// <param name="name">Project Name</param>
        /// <param name="description">Project Description</param>
        /// <returns>True if successful, false otherwise.</returns>
        [OperationContract]
        bool Insert(int id, string name, string description);

        /// <summary>
        /// Returns the id of the project that goes by the selected name.
        /// </summary>
        /// <param name="name">Project Name</param>
        /// <returns><c>int</c></returns>
        [OperationContract]
        int GetIdByName(string name);
    }
}

The service method implementation is shown below:

/// <summary>
/// Inserts a new Project in database.
/// </summary>
/// <param name="id">Project Identifier.</param>
/// <param name="name">Project Name</param>
/// <param name="description">Project Description</param>
/// <returns>True if successful, false otherwise.</returns>
public bool Insert(int id, string name, string description)
{
	Database db = DatabaseFactory.CreateDatabase();

	DbCommand command = db.GetStoredProcCommand("[dbo].[InsertProject]");
	db.AddInParameter(command, "id", DbType.Int32, id);
	db.AddInParameter(command, "name", DbType.String, name);
	db.AddInParameter(command, "description", DbType.String, description);
	db.AddOutParameter(command, "success", DbType.Boolean, 1);

	db.ExecuteNonQuery(command);

	bool success = (bool) db.GetParameterValue(command, "success");

	return success;
}

/// <summary>
/// Returns the id of the project that goes by the selected name.
/// </summary>
/// <param name="name">Project Name</param>
/// <returns><c>int</c></returns>
public int GetIdByName(string name)
{
	Database db = DatabaseFactory.CreateDatabase();

	DbCommand command = db.GetStoredProcCommand("[dbo].[GetProjectIdByProjectName]");
	db.AddInParameter(command, "Name", DbType.String, name);

	int id = -1;
	using(IDataReader reader = db.ExecuteReader(command)) {
		if(reader.Read()) {
			id = reader.GetInt32(0);
		}
	}

	return id;
}

Note: All code can be found in the attachment at the end of the post.

As you can see the methods are really simple. We are using a simple call to a stored procedure, getting the results and sending them back to the client.

Now let’s look at the actual unit test fixture:

/// <summary>
/// Test the Service method Insert
/// </summary>
[TestMethod]
public void Insert()
{
	using(TransactionScope tranScope = new TransactionScope()) {
		int id = 1;
		string name = "Project 1";
		string description = "Description of Project 1";
		Assert.IsTrue(Service.Insert(id, name, description));
		Assert.IsTrue(ProjectExists(name, description));

		id = 2;
		name = "Project 2";
		description = "Description of Project 2";
		Assert.IsTrue(Service.Insert(id, name, description));
		Assert.IsTrue(ProjectExists(name, description));
	}
}

/// <summary>
/// Test the Service method GetIdByName
/// </summary>
[TestMethod]
public void GetIdByName()
{
	using(TransactionScope tranScope = new TransactionScope()) {
		// Insert the Test Data.
		SqlHelper.ExecuteSqlScript(Db, @"......UnitTestsScriptsInsertTestProjects.sql");

		// Test Service Method.
		int id = -1;
		string name = "Kook";
		id = Service.GetIdByName(name);
		Assert.AreEqual<int>(1, id);

		name = "Samii";
		id = Service.GetIdByName(name);
		Assert.AreEqual<int>(2, id);

		name = "CPW";
		id = Service.GetIdByName(name);
		Assert.AreEqual<int>(3, id);
	}
}

First we have the insert method, which doesn’t require any existing data, so no script is executed before the service method. We do however wrap everything into a TransactionScope, which ensures that whatever the test does, after the TransactionScope block is out of the scope it will rollback all the changes. I will leave the discussion on Transactions for a later post.

Now, the second method is more interesting. The GetIdByName test method actually needs a setup. We could have created a dependency on the Insert method so that the first test method prepares the data that the GetIdByName method will require to test. But we want to avoid Erratic Test behavior, so we created them completely independent.

The GetIdByName is also wrapped in the TransactionScope so that we can ensure that after the test is done, the data is rolled back to its initial state.

The script (InsertTestProjects.sql) contains three sample projects which the service method will retrieve and we will verify the returned data.

As you can see testing Data-Driven Services is very simple. If you pay close attention to the project you will notice that this way of doing this doesn’t only apply to WCF Services, any .Net assembly can be tested this way.

Hope this helps you on your quest for simplicity and correctness!

Download Test Solution

Test Solution Requirements

  • Visual Studio 2008
  • Enterprise Library 4.x
  • .Net 3.5
  • SQL Server 2005

The Database folder contains the table and stored procedures needed by the code. Just modify the ExecuteScripts.bat to set the database authentication info. Also modify the App.config in the UnitTests project in order to run accordingly.

(Disclaimer: This project has been stripped down so that it is easy enough to understand. This second method, iin order to follow Data-Driven Test, should get the expected data from an Xml file, insert it into the database, and compare accordingly. In this sample we have the data hard coded for simplicity. Building an interpreter is our of the scope of this post.)

* References:
- Meszaros, Gerard. “xUnit Test Patterns: Refactoring Test Code”
2007, Pearson Education.
Amazon

Let the Clouds Make your life easier…

I just stumbled upon this great link that explains the power of the clouds for software solutions and how it reduces complexity: Here

Remove files recursively..

I had a problem a couple of weeks ago, when my main Subversion repository completely died out of data corruption. I was trying to check-in my latest changes, but I just couldn’t. Nothing was lost since I do regular backups of my repositories. So I was in good shape. I thought to myself this was the best chance to migrate to TFS, which I have been wanting to do for a while.

Note: I did not leave subversion in favor of TFS because of this corruption. This data corruption was expected to happen because the machine usually gets killed in the middle of backup or by the occasional  “Windows Update”; so the repository was destined to die eventually. I could have fixed the repository in no time with the tools that Subversion provides or simply by restoring backups. But I moved to TFS mainly because of the project integration in all aspects and not only source control, and because most of my clients use TFS, so integration with their systems is also easier.

Anyways I needed a clean copy of my newest changes so that I could import it into TFS. I felt lazy at the moment and I chose the complex solution, clean up my subversion repository copy. My project has a LOT of folders, so deleting the .svn folders became a HUGE effort. I decided to use a simple shell command to do that, like I just to do in linux:

rm -rf `find . -type d -name .svn`

So I started looking on google to see what was the way to it in Powershell and I found really complex solutions like:

$fso = New-Object -com "Scripting.FileSystemObject";
$folder = $fso.GetFolder("C:\\Test\\");

foreach ($subfolder in $folder.SubFolders) {
    if ($subfolder.Name -like "*.svn"){
        remove-item $subfolder.Path -Verbose
    }
}

Source: Here

It really scared me! I thought that, if that was the easiest way to delete recursively, then powershell is doomed. Thanks to google, I did find a simpler way to do this:

dir . -recurse .svn -fo | remove-item -force

Which is quite elegant and fast! Sometimes I just don’t understand why it is so difficult to come up with the simple answer. It happens to me quite often. It is extremely easy to come up with a complex solution…but to actually have a simple solution is always difficult. This is only one example.

Anyways this did the trick and it works perfectly.

[Update]: I forgot to add the ‘-fo’ option which shows the hidden files (in this case the .svn folders). Otherwise this would not work.

Sticky Sorter

Microsoft just announced a very cool Research project called ‘Sticky Sorter‘ which is a tool that helps us organize information using sticker notes or post-its.

From the website:

StickySorter is an Office Labs sponsored spare-time project by two Microsoft employees, Julie and Sumit. The idea sprung from the need for project teams all over the world to gather and organize data using a collaborative process known as affinity diagramming.  Since its inception, StickySorter has evolved into a desktop application so anyone can use it to collaborate and organize ideas electronically, using a familiar sticky note interface.

What I see here is something that I have been looking for some time now and which is specially useful for Scrum. This can be used to recreate the Scrum sprint backlog in a very friendly way.

I have created a sample of what it would look like using the tool:

I need to play with it a bit more in order to see it’s full capabilities, and try to apply it to scrum. You can download the tool here.

Alchemy….

I have not really been a follower of Macromedia (now part of Adobe) or its products; specially Flash, but I just found out about one research project that does capture my attention: Alchemy.

From the press release:

Alchemy is a research project that allows users to compile C and C++ code that is targeted to run on the open source ActionScript Virtual Machine (AVM2). The purpose of this preview is to assess the level of community interest in reusing existing C and C++ libraries in Web applications that run on Adobe® Flash® Player and Adobe AIR®.

This project promises that C/C++ libraries that have no OS dependencies can be run on the Adobe AIR platform. This is HUGE. Just to think that at least 90% of my libraries can be used in Web Applications is awesome.

A not so direct competitor, Microsoft’s Silverlight, does provide a way to reuse C++ libraries via C++/CLI, and it works like a charm; I happend to have a lot of C++ libraries  which have valuable code. Lately I have been reusing those libraries in Web Applications via C++/CLI and WebServices. It works, and it works fine.

I have not worked with Silverlight 2.0 yet; I have played around with it, but I have not done anything that is worth mentioning. In my tests, I see Ican also reuse those C++ libraries with C++/CLI and Webservices. The problem is that this approach will only work on Windows, since C++/CLI is not supported by Mono.

Another approach would be to make the WebServices in native C++, and call it from anywhere: Silverlight, Javascript and .Net. This would work anywhere.

This doesn’t take away the credit for Alchemy, which makes the C++ libraries natively accessible to Adobe AIR applications, in a way this is very close to what C++/CLI does.

The purpose of the Alchemy project is :

Alchemy is primarily intended to be used with C/C++ libraries that have few operating system dependencies. Ideally suited for computation-intensive use cases, such as audio/video transcoding, data manipulation, XML parsing, cryptographic functions or physics simulation, performance can be considerably faster than ActionScript 3.0 and anywhere from 2-10x slower than native C/C++ code. Alchemy is not intended for general development of SWF applications using C/C++.

Comparing it with C++/CLI, I do see an advatage from using C++/CLI to reuse C++ libraries in RIA applications over the Alchemy project: I can reuse .Net libraries with C++ native code, which gives me the best of both worlds.

I did some research on Alchemy, and it seems it doesn’t allow Flex code to be embedded with C++ native code. My research was not very comprehensive to say the least, so I could be wrong. It could also be too soon to tell since this is still in early stages of research and development.

In conclusion, this project seems very interesting, and I’m going to keep an eye out for it.

Matrix on Windows

I just found this great video (sorry for all the fun stuff and no interesting stuff, but this is so funny):

Video

Mac Upgrade

I stumbled upon this pretty little comic, and I laughed so much when I read it… it is pretty funny:

Source.

Fowler on Oslo

I just found this document written by Martin Fowler on his thoughts on Oslo.

Some comments made by Folwer:

  • Talking about Oslo in general:

“A couple of weeks ago I got an early peek behind the curtain as I, and my language-geek colleague Rebecca Parsons, went through a preview of the PDC coming-out talks with Don Box, Gio Della-Libera and Vijaye Raji. It was a very interesting presentation, enough to convince me that Oslo is a technology to watch.”

  • Talking about M:

“I think I like this. When I first came across it, I rather liked the MPS notion of: “it looks like text, but really it’s a structured editor”. But recently I’ve begun to think that we lose a lot that way, so the Oslo way of working is appealing.”

  • Talking about comparison of Oslo with other tools:

“One particularly interesting point in this comparison is comparing Oslo with Microsoft’s DSL tools. They are different tools with a lot of overlap, which makes you wonder if there’s a place for both them. I’ve heard vague “they fit together” phrases, but am yet to be convinced. It could be one of those situations (common in big companies) where multiple semi-competing projects are developed. Eventually this could lead to one being shelved. But it’s hard to speculate about this as much depends on corporate politics and it’s thus almost impossible to get a straight answer out of anyone (and even if you do, it’s even harder to tell if it is a straight answer).”

“It was only a couple of hours, so I can’t make any far-reaching judgements about Oslo. I can, however, say it looks like some very interesting technology. What I like about it is that it seems to provide a good pathway to using language workbenches. Having Microsoft behind it would be a big deal although we do need to remember that all sorts of things were promised about Longhorn that never came to pass. But all in all I think this is an interesting addition to the Language Workbench scene and a tool that could make DSLs much more prevalent.”

The article is very good. I liked a lot, I think everyone should read it in full.