Friday, September 14, 2012

Running .Net 3.5 MSTest unit tests with Gallio.

Like all projects of a certain size there comes a time when there is a desire to have a dash board to quickly see the current state of affairs. 

For the dash board there is a growing open source solution called Sonar (http://www.sonarsource.org/) that is able to run all manner of plugins.  Perfect!

One of the supported plugins for .Net projects is another open source project called Gallio (http://www.gallio.org/).  Gallio has the ability to run all manner of unit test frameworks (MSTest, NUnit ,etc.) under a common API.  Nice!

One small gotcha:  Out of the box Gallio won't run .Net 3.5 unit test projects :(

The errors that were encountered were:

  • Mixed mode assembly is built against version 'v2.0.50727'
  • This method explicitly uses CAS policy, which has been obsoleted by ... please use the NetFx40_LegacySecurityPolicy configuration switch.
  • The plugin enable condition was not satisfied. Please note that this is the intended behavior for plugins that must be hosted inside third party applications in order to work. Enable condition: '${process:DEVENV.EXE} or ${process:VSTESTHOST.EXE} or ${process:QTAGENT.EXE} or ${process:QTAGENT32.EXE} or ${process:QTDCAGENT.EXE} or ${process:QTDCAGENT32.EXE} or {process:MSTEST.EXE}'
After much researching I found the solution.

Enable the 2008 Test Runner Plugin

By default the “Visual Studio 2008 Integration Shell” and “Visual Studio 2008 Test Runner plugin” are disable due to a failing prerequisite check.
To enable the plugins remove the “enableCondition” attribute and its value from the following files:

C:\Program Files\Gallio\bin\VisualStudio\v9.0\
  • Gallio.VisualStudio.Tip90.plugin
  • Gallio.VisualStudio.Shell90.plugin 

Enable Testing of unit tests authored in .Net 3.5

The command line utility “Gallio.Echo.exe” is a .Net 4.0 application that Sonar uses to run the unit tests.  This application needs to be configured to allow .Net 3.5 policies.

Update Gallio.Echo.exe.config to enable the testing of .Net 3.5 unit tests.

1).  Update the "runtime" configuration section to include the element: <NetFx40_LegacySecurityPolicy enabled="true"/>.
Example:
<runtime>
    <!-- Don't kill application on first uncaught exception.
         We don't want the test runner to terminate itself unexpectedly
         without reporting the test failure associated with that exception. -->
    <legacyUnhandledExceptionPolicy enabled="1" />

    <!-- Enable loading assemblies over the network in .Net 4.0 -->
    <loadFromRemoteSources enabled="true" />

    <NetFx40_LegacySecurityPolicy enabled="true"/>
  </runtime>

2).  Update the "startup" configuration section to include the attribute useLegacyV2RuntimeActivationPolicy="true"
Example:
<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0.30319" />
    <supportedRuntime version="v2.0.50727" />
  </startup>

   
Now Sonar is running all of the unit test projects, providing test and code coverage reports.  Life is good!

Monday, December 19, 2011

Using Firebird ISQL to create embedded database files

ISQL is a command line tool that enables the execution of SQL statements either interactively or by input script file.

I was struggling to get ISQL to work with my Firebird Embedded database files until I stumbled upon a little nugget of information in one of my many google searches.

The problem was that ISQL wasn't using the embedded client! Doh!

The fix was simple! To get ISQL working the way I needed it to all I had to do was:
  • Copy isql.exe into the Firebird embedded directory.
  • Rename fbembed.dll to fbclient.dll
Now I can create and connect to embedded database files using the ISQL command line tool.

isql.exe" -q -i myDatabase.sql

where the file myDatabase.sql contains:
CREATE DATABASE 'TEST.FDB'
USER 'SYSDBA' PASSWORD 'masterkey'
PAGE_SIZE 8192
DEFAULT CHARACTER SET NONE;

CREATE ROLE RDB$ADMIN;

CREATE TABLE MYTABLE(
ID INTEGER NOT NULL,
NAME VARCHAR(500) NOT NULL
);

ALTER TABLE MYTABLE
ADD CONSTRAINT PK_MYTABLE_1
PRIMARY KEY (ID);

COMMIT;

Why did I want to do this?
My want was driven by a desire to have my build server create/modify database files for our product install. Treating the database creation scripts like code negates the need for a "Golden" database file that no one, other than myself, knows how to create.

Thursday, October 13, 2011

Firebird embedded with .Net

Here's a simple getting started with Firebird embedded

Step one: Download FireBird embedded and the .Net provider

There are numerous options available for download but I opted for the zip packages as only the assemblies and supporting files are needed.
http://www.firebirdsql.org/en/server-packages/
http://www.firebirdsql.org/en/net-provider/
http://www.firebirdsql.org/manual/ufb-cs-embedded.html#ufb-cs-embedded-windows

Step two: Create your database file

There are many administrative tools available for FireBird but I opted to use FlameRobin as its free. http://www.flamerobin.org/

In order to get flame robin to communicate with an embedded FireBird database there are a couple of steps required.
Copy the following FireBird embedded client files to the FlameRobin installation folder (Default C:\Program Files\FlameRobin)
fbembed.dll (Rename to fbclient.dll)
firebird.msg
icudt30.dll

icuin30.dll
icuuc30.dll


Register Server


Display name
leave hostname and port empty

Create a new database







Step 3: Create your .Net project.
Add a reference to the .Net Provider.
FirebirdSql.Data.FirebirdClient.dll
The files icudt30.dll and icuuc30.dll need to be in the build directory. Add them to the project (I added as a link as all my third party components live in a single place) and set to copy to output build directory.

Step 4: Write the code.

using System;
using FirebirdSql.Data.FirebirdClient;

namespace FirebirdClient
{
class Program
{
static void Main(stri
ng[] args)
{
var builder = new FbConnectionStringBuilder
{
UserID = "SYSDBA",
Password = "masterkey",
ServerType = FbServerType.Embedded,
Dialect = 3,
Database = @"C:\MyDatabases\MyDB1.fb"
};

using (var conn = new FbConnection(builder.ConnectionString))
{
conn.Open();

using (var cmd = new FbCommand("select * from MyTable", conn))
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(string.Format("{0} {1}", reader["MyCol1"], reader["MyCol2"]));
}
}
}

Console.ReadLine();
}
}
}

Tuesday, September 28, 2010

Control catalog name when using AttachDBFilename

I was using the AttachDBFilename and discovered that the database name ended up being the full path of the file being attached.

Setting InitialCatalog as well enable me to control the catalog name of the attached file.

var cb = new SqlConnectionStringBuilder {
IntegratedSecurity = true,
DataSource = ".\MyInstance",
AttachDBFilename = @"c:\mypath\mydb.mdf",
InitialCatalog = "mydb catalog name"
};

Thursday, September 23, 2010

Visual studio designer hangs and fails to load ToolStripControlHost derived class

I found a problem using a ToolStripControlHost derived class when the class is being built in the same solution as the form that uses the component.

When performing a rebuild the designer would freeze and wouldn't redraw the toolbar. The fix I discovered solves the problem where the designer disposes the object and then attempts to access it after the rebuild.

public class CustomButtonToolStripHost : ToolStripControlHost
{
public CustomButtonToolStripHost ()
: base(CustomToolStripButton())
{
}
/// Used to overcome a designer error when rebuilding the project.
private class CustomToolStripButton : Button // Replace with your control
{
/// Override CreateHandle to avoid exception if called when object is disposed.
/// This occurs when the designer updates after a rebuild.

protected override void CreateHandle()
{
if (!IsDisposed)
{
base.CreateHandle();
}
}
}
}

.Net button that doesn't take/steal focus

This is a simple solution that I've came up with after some thinking:

public class MyButton : Button
{
public MyButton()
{
if (!DesignMode)
{
SetStyle(ControlStyles.Selectable, false);
}
}
}

Much easier than trying to handle mouse activation messages etc...

Tuesday, March 23, 2010

Isolation from database

So I'm working on a legacy project and quite intent to re-factor the heck out of it. One of my goals was to make the code testable and to do that I had to rip out the dependency on the database/DataSet implementation.

To do this I created a data provider class and then implemented an iterator block and used the yield statement.

public override IEnumerable MyDataObjects
{
get
{
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (var command = new SqlCommand{CommandText = "...", Connection = connection })
{
using (var reader = command.ExecuteReader())
{
while (reader != null && reader.Read())
{
yield return new MyDataObject
{
//extract the column values
};
}
}
}
}
}
}


Now the consumer of the data doesn't know where the data is coming from and that's just fine!

foreach(var myData in provider.MyDataObjects)
{
//do stuff with the object
}