Misc. about environment and coding

torsdag 8 december 2011

How to batch convert files using ffmpeg and the command line tool

I needed a quick and simple way to batch convert a set of video files using ffmpeg but couldnt find anything really simple when searching the web for an answer. Finally I managed to find a simple way using the command line tool and a BATCH for loop, heres an example:

:> for %A IN (*.MP4) DO (ffmpeg.exe -i %A converted_files/%A.avi)

In this case, your files will end up in a subfolder and be named file1.MP4.avi, file2.MP4.avi, ...). I found everything I needed to know about BATCH for loops here.

Blackbox testing with nunit using custom test case source

Hi! For my current project, I needed to perform some black box/system testing on a service layer and I choose NUnit for the task. I wanted to use the testcase functionality in NUnit to be able to provide a set of different test data for each unit tests.

However, the default NUnit attribute (TestCaseSourceAttribute) only allows you to point out asingle method that will provide a list of your test case data, but this method takes no arguments, which means that you have no way of selecting what test cases should be returned for what NUnit test. This could be useful if you have a large set of tests and eg want to be able to handle all test case data in a single configuration file (which was my case). Here's my solution:

First, lets look at some NUnit test samples using the default test case source attribute and the extended one:

using NUnit.Framework;

namespace Custom.BlackBox.Tests
    public class SampleTests 

        // This is the standard way, no way of selecting tests case data
        // in the referenced class/method
        [TestCaseSource(typeof(SampleTests), "GetTestData")]
        public void ShouldDoBlah(string firstName, int shoeSize) {

        // No input parameters possible here
        // (eg which unit test requested the test data)
        public IEnumerable GetTestData() {

        // This is the way I solved the problem, read on to see
        // how the custom attribute works
        public void ShouldDoBlah2(string firstName, int shoeSize) {


I extended the default NUnit attribute TestCaseSource:

using System.Collections.Generic;
using NUnit.Framework;

namespace Custom.BlackBox.Tests.Addin
    public class CustomTestCaseSourceAttribute : TestCaseSourceAttribute 
        //DummyTestCaseSourceFetcher is to fool the original testcase source fetcher which cannot be easily removed
        public CustomTestCaseSourceAttribute() : base(typeof(DummyTestCaseSourceFetcher), "GetData") { }

    public class DummyTestCaseSourceFetcher {

        public IEnumerable<object> GetData()
            return new object[] { };



My attribute does not take any parameters, instead I wanted to use reflection to decide which test cases should be selected for each unit test. This will become more clear in the next step. Note that I needed to add a "dummy" test case data fetcher because I could not find any way to remove the default testcase provider. Without this dummy fetcher, the default test case provider will kick in an complain cause no tests cases could be found for the current NUnit test, if you have any idea how to get around this problem, please comment.

Next, I created an NUnit addin used install my custom test case source provider. Simply place this adding in your project and NUnit will pick it up and execute it automatically (this fact was not easily found in the NUnit documentation).

using NUnit.Core.Extensibility;

namespace Custom.BlackBox.Tests.Addin
    [NUnitAddin(Description = "Custom test case provider addin", Name = "Custom test case provider addin", Type = ExtensionType.Core)]
    public class TestCaseProviderAddin : IAddin

        public bool Install(IExtensionHost host)
            host.GetExtensionPoint("TestCaseProviders").Install(new CustomTestCaseProvider());
            return true;

Next step, create the test case provider. The most interesting methods are the two first ones (from the NUnit interface ITestCaseProvider), here you could of course implement any test case lookup method you want. In my case I've put all my test cases in a configuration file and select the tests matching the signature of the NUnit test method using reflection.

If you want the implementation of the configuration related stuff, please comment. In order to use this stuff, you need to use NUnit 2.5 at least, see the NUnit documentation for more info.

using System.Configuration;
using System.Linq;
using System.Reflection;
using Custom.BlackBox.Tests.Config;
using NUnit.Core.Extensibility;
using NUnit.Framework;

namespace Custom.BlackBox.Tests.Addin
    public class CustomTestCaseProvider : ITestCaseProvider

        public System.Collections.IEnumerable GetTestCasesFor(MethodInfo method)
            var testSettings = ((TestSettings)ConfigurationManager.GetSection("TestSettings"));
            var testCaseDatas = testSettings
                .Where(testSetting =>
                    testSetting.Type == method.DeclaringType.FullName &&
                    testSetting.Method == method.Name)

            if (testCaseDatas == null) throw new ConfigurationErrorsException("Could not find the testdata for '" + 
                method.GetType().FullName + "." + method.Name + 
                "', please make sure a corresponding testdata-node for this unit test exists in app.config.");

            return testCaseDatas;

        public bool HasTestCasesFor(MethodInfo method)
            return (method.GetCustomAttributes(typeof(CustomTestCaseSourceAttribute), false).Count() > 0);

        private TestCaseData CreateTestCaseDataFromTestSetting(TestSetting testSetting)
            var nUnitTestCaseData = new TestCaseData(testSetting.Arguments);
            if (!string.IsNullOrEmpty(testSetting.Result))
            return nUnitTestCaseData;


tisdag 25 oktober 2011

Status code 550 when using Msdeploy

I ran into problems using msdeploy after changing some settings in IIS. Struggled a whole day with this issue until I finally realized what was causing the error. I just thought I'd share what was wrong in case someone else has the same problem. Here's the error message:
Microsoft.Web.Deployment.DeploymentException: An unsupported response was received.
The response header 'MSDeploy.Response' was '' but 'v1' was expected.
System.Net.WebException: The remote server returned an error: (550).
The problem was an errorneous iisApp-path, you need to specify the name of the iis site (the name displayed in iis manager) and not the associated host, eg in my case my site name in iis was 'test.mysite.com', but it was listening to the host name 'mysite.com'. If you specify -dest:iisApp=mysite.com, you'll get the error above (not a very good error message) This guy had the same problem, and the resolution was the same: http://forums.iis.net/p/1174284/1965776.aspx

onsdag 2 december 2009


Having read the chapter "consuming pleasures" in Bill Brysons book "Notes from a big country" I really started to feel bad about the way most people in rich countries (including myself to some extent) engage in meaningless consumption. I googled for anti-consumerism and found this page. Unable to submit my comments, I wrote this blog entry.

I believe that the whole environmental debate is way too much focused on CO2 emissions, it seems like people believe that if we simply could stop producing greenhouse gases, everything would be fine!? Of course, this is not true, we' would still be "refining" our natural resources into hazardous products that cant be recycled back to nature. Think about how many different kinds of materials an iPod consists of, metals, minerals, plastics. How are you suppose to recycle a product like that back to nature? You cant! The natural resources spent producing that product is gone forever! To quote Annnie Leonard (from the excellent movie www.storyofstuff.com): It's a linear system from production to disposal run on a planet with finite resources. Running a system like that is like spending the money from your savings account without getting any or only a small income. Slowly, you're going bankrupt. Think about what bankrupt really means in this sense, no more eco-services, no more clean air or water.

Some people seem to think we can live "without the environment". "I live in the city where there are no trees, nature is a luxury we'll have to do without in the future". Of course, the whole society and our survival is dependent on the survival of nature whether we fail to realize it or not.

Here are some tips on how to consume less and be happier:

  1. Consume experiences rather than products, it's better for the environment and you won't get the hassle of owning another product that needs to be worried about/taken care of.
  2. When feeling like buying a product, get into the habit of waiting a couple of days before actually buying it. In the meantime, ask yourself: "What REAL (if any) value will this product bring to my life?" Most of the times, you will find that you don't really need it at all.
  3. A friend of mine told me that when he felt the urge to buy something, simply going on the internets reading forums, looking at pictures and accessories would alleviate the need to actually buy the product.
  4. If you've decided that you do indeed need something, go for quality (this is surprisingly a lot harder than it sounds, simply buying expensive stuff is unfortunately no guarantee for a long lasting, good quality product). Go on the internet, read reviews about the particular product, pay special attention to the negative comments. Don't become a "fanboy" simply looking for positive comments that will simply reinforce what you already think you know or love about the product.
  5. Read a book on something with eastern philosophy, eg. "the power of now" by Eckhart Tolle (does it qualify?). It might help you realize a few things about yourself, what makes you happy etc.
  6. Spread the message!

onsdag 18 februari 2009

Generic FindControl extension method

The other day I needed a way to find controls of a certain type in the control hierarchy, preferably in a recursive fashion. The regular Control.FindControl(string controlID) didn't quite cut it. I created two simple extension methods using Jeff Atwood's recursive version as a starting point. Here's an example of why you might need it and how you can use it.

Lets say eg. that you are dynamically adding custom user controls in a repeater and that you want to retrieve all of them without knowing their exact position in the control tree.

   1:  <asp:Repeater runat="server" DataSource="<%# MyDataSource %>" OnLoad="DataBind">
   2:     <ItemTemplate>
   3:        <My:Customer runat="server" Customer="<%# Container.DataItem as My.Customer %>" />
   4:     </ItemTemplate>
   5:  </asp:Repeater>

I came up with a simple extension method that recursively finds controls of a certain type:

   1:  List<CustomerControl> list = myControl.FindControls<CustomerControl>();

By adding an argument (in the form of a predicate) to the extension method you could easily find controls that has a certain feature enabled, eg. where a checkbox is checked.

This could be useful if you are making some kind of web based adminstration tool that dynamically generates edit controls. When the user hits the save button you could easily find all controls that has a certain feature

   1:  List<CustomerControl> list = myControl.FindControl<CustomerControl>(c =>
   2:     c.CustomerIsEnabled);

Finally, here are the extension methods:

   1:          /// <summary>
   2:          /// Recursively finds controls of a certain type anywhere in the 
   3:          /// controls "sub tree".
   4:          /// </summary>
   5:          public static List<T> FindControls<T>(this Control control) where T : class
   6:          {
   7:              return FindControls<T>(control, null);
   8:          }
   9:          /// <summary>
  10:          /// Recursively finds controls of a certain type that matches the specified predicate
  11:          /// anywhere in the controls "sub tree".
  12:          /// </summary>
  13:          public static List<T> FindControls<T>(this Control control, Predicate<T> predicate) where T : class
  14:          {
  15:              List<T> result = new List<T>();
  16:              foreach (Control childControl in control.Controls)
  17:              {
  18:                  if (childControl is T)
  19:                  {
  20:                      T tmp = childControl as T;
  21:                      if(predicate==null || predicate.Invoke(tmp))
  22:                          result.Add(tmp);
  23:                  }
  24:                  result.AddRange(FindControls<T>(childControl, predicate));
  25:              }
  26:              return result;
  27:          }

Hope you find this useful.

torsdag 4 december 2008

C# 3.0 Serialize anonymous types to JSON

Since C# 3.0 it's possible to create anonymous types. This can be pretty useful when serializing objects to JSON.

Let's say you have a Class with quite a few members, eg. Person, now you want to generate a json-array out of a generic list of these objects, however you are really only interested in a couple of properties (Name and ShoeSize) from the Person class.

You could of course create a SimplifiedPerson Class, and convert your List<Person> to a List<SimplifiedPerson>, eg:

   1:  List<Person> persons = GetPersons();
   2:  List<Simplifiedperson> simplifiedPersons =
   3:     persons.ConvertAll(person =>
   4:        new SimplifiedPerson(person.Name, person.ShoeSize));

However, if you are only going to use SimplifiedPerson in one context, you may want to consider using an anonymous type:

   1:  var simplifiedPersons = persons.ConvertAll(person => new {
   2:     Name = person.Name,
   3:     ShoeSize = person.ShoeSize
   4:  });

Finally, serialize your list to Json using your serializer of choice. I like the simplicity of the JavaScriptSerializer, and it works with anonymous types. However JavaScriptSerializer is obsolete but I don't think the suggested DataContractJsonSerializer can serialize anonymous types. see Rick Strahl's blog post about this topic.

   1:  JavaScriptSerializer serializer = new JavaScriptSerializer();
   2:  string json = serializer.Serialize(simplifiedPersons);

This is probably not news for many of you, but anyway, hope you'll find this useful :).