My name is Juri Strumpflohner and this is my technical blog. I'm a software architect, .Net, Android, Web and Java dev, TDD and best practices promoter and martial arts practitioner.

Recent Posts Subscribe

Dear reader of Juri's TechBlog,
I moved my blog to a new domain and a new hosting solution as well. I'm now blogging on juristr.com.

Why factories with configuration files are better for decoupling but still a testability killer

As I already highlighted in several previous posts one of the most critical things when facing unit testing is an appropriate design. My sensation is that those rejecting unit tests...

  • do not care about code quality
  • are often poor programmers in terms of the code they produce (poor code design) and have therefore difficulties in writing automated tests for it
  • do not see the value behind them but still see them as just "more" work to do.
Anyway, without going into details on this, I'd like to highlight the poor code design problem since that's probably one of the most prevalent reasons for people failing to write appropriate tests. Let's look at a "real-world" scenario again.

Assume you really care about software architecture and design. You use interfaces for nicely decoupling the different system functionalities where the concrete object instances are being resolved at runtime by using factories. These factories access a configuration file (i.e. the web.config in ASP.net) for retrieving the type information and hosting DLL and then, through reflection, fetch the specific instance that has been configured for a given interface.
An entry in the config file could look like this:
...
<add requestedType="CompanyName.ProjectName.BusinessLogicInterface.INationBL, CompanyName.ProjectName.BusinessLogicInterface" fetcher="Singleton" paramType="CompanyName.ProjectName.BusinessLogic.NationBL, CompanyName.ProjectName.BusinessLogic"/>
...
The according code using the factory then looks as follows
...
INationBL nationBl = BLFactory.Instance.Get<INationBL>();
...
According to the above mentioned configuration, your interface INationBL will be associated with an instance of type NationBL. I don't know whether you already got the advantage here: by having a configuration based factory and by only relying on interfaces, you externalize you dependencies from the code and you defer the resolving of the correct type to the moment of code execution.
This is good practice because by avoiding dependencies, you decouple which in turn will make life easier when writing automated tests against this logic.

Why is this?
Well, what we usually want when writing unit tests is to verify the correctness of a specific logic in isolation. So one of the things we do is to mock out undesirable object dependencies (like network resource access, DB access etc..). Assume the following sample code has to be tested. We want to verify whether it correctly returns the italian Nation object.
...
public Nation GetItalianNation()
{
   Nation result = null;

   INationBL nationBl = BLFactory.Instance.Get<INationBL>();
   List allNations = nationBl.ReadAllNations();
   if(allNations != null)
   {
      foreach (Nation nation in allNations)
      {
         if(nation.CountryCode == Nation.TypeOf.CountryCodeItaly)
         {
            result = nation;
            break;
         }
      }
   }

   return result;
}   
...
To verify this, a possibility is to test the following scenarios
  • check for "null" result
    • allNations contains no objects at all
    • allNations contains multiple Nation objects, none of them however satisfying the condition of the nation.CountryCode.
  • check for a valid italian Nation object: allNations contains multiple entries, one of them being an italian Nation meaning the nation.CountryCode is equal to "300" (for example) which is represented by the constant Nation.TypeOf.CountryCodeItaly.
To realize our testing strategy we have to assure that allNations get's properly filled. As you see, the code piece above fetches an instance of a INationBL object on which the ReadAllNations() method is invoked for retrieving the list of nations (from the DB). This is where our logic has to take place. Now given that we have our nice decoupling through our config-based factories we can place our own MockNationBl class.
public class MockNationBl : INationBL
{
 //these are used to modify the nations collection
 private IList<Nation> _Nations;
 public IList<Nation> Nations
 {
  get
  {
   return _Nations;
  }
  set
  {
   _Nations = value;
  }
 }
...
 public IList<Nation> ReadAllNations()
 {
  return _Nations;
 }
...
}
Now this generic mock let's us pretty easy do the testing job. Inside our testing project (you should have a different project for your tests!!) we create the factory mappings in the config file but instead of returning an instance of NationBL we return an instance of our MockNationBl.
...
<add requestedType="CompanyName.ProjectName.BusinessLogicInterface.INationBL, CompanyName.ProjectName.BusinessLogicInterface" fetcher="Singleton" paramType="CompanyName.ProjectName.BusinessLogic.MockNationBl, CompanyName.ProjectName.UnitTests"/>
...
Something like the above. For those who still don't see the benefit of using interfaces...well this is it. You can easily switch the concrete object instances (see strategy pattern which exploits this concept). When launching the unit test and calling the GetItalianNation() method an instance of the MockNationBl will be used.

So what about the title. These factories should be a good thing, shouldn't they? Why "testability killer"?
Well "testability killer" is quite provocant actually :) . What I don't like about the above mentioned strategy is the overhead these factories create during testing. Config files are good for "outsourcing" configuration to a central place. This is what they are good for, but still, people tend to forget them or how to correctly set them. Moreover they limit your possibilities: what about if another developer needs his INationBL to be instantiated with another mock, not yours?? He just cannot.

So what I'd like to have is an environment where concrete object instances are being retrieved from configuration files if you run your system, but still give you enough flexibility to inject your own ones during testing. May sound complex but it actually isn't. This is how DI frameworks basically work. To adapt the code above we don't have to change much, but just delegate the fetching of the concrete type to a class property as follows:
public class SomeClass
{

 private INationBL _NationBl;
 public INationBL NationBl
 {
  get
  {
   if(_NationBl == null)
    _NationBl = BLFactory.Instance.Get<INationBL>();
   return _NationBl;
  }
  set
  {
   _NationBl = value;
  }
 }

 public Nation GetItalianNation()
 {
  Nation result = null;
  List allNations = _NationBl.ReadAllNations();
  if(allNations != null)
  {
   foreach (Nation nation in allNations)
   {
    ...
         }
  }

  return result;
 }
}
With this little modification the code gets much cleaner, the instance of an INationBL (if used multiple times) gets fetched/reused from a single place within the class, by default using the factory. In a possible test setup we can however easily create our mock implementation and "inject" it:
...
[TestInitialize()]
public void SetUp()
{
   INationBL mockNationBL = new MockNationBL();
   someClass.NationBL = mockNationBl;
   ...
}
...
In this way you don't even need a configuration file in your test project. DI frameworks work similar, what I like about them is that they work according to the "hollywood principle". The impact on your written code is very small.

Boost your productivity using shortcuts in Visual Studio

A major aim of everyone is (or should be) to increase his personal productivity. Now productivity is a vast term actually. It can be related to individual programming productivity focusing on code quality problems but it can also be related to management issues and so on.
This post targets the individual human-computer-interaction topics, more specifically on how you interact with your Visual Studio IDE during development. Personally what I don't like is when I have to use the mouse. Yes, I'm a keyboard fanatic ;) , just use your mouse if you have no other possibilities. Mouse interactions cost time! Take for instance the following situation:
You're currently going over some code in your editor, you see a line which looks suspicious and really shouldn't be there. Your approach: comment the line and launch the app to check it. I assume your hands are on the keyboard (as they should be ;) )
Mouse interaction:

  1. Move your hand (right/left depending on your orientation) from your keyboard to the mouse.
  2. Move the mouse pointer up to point to the right symbol on the toolbar for commenting out the current line (ensure you have the focus on that line, otherwise you have to position it first)
  3. Click the toolbar button for commenting that line of code (hopefully you hit it correctly, otherwise the steps will increase for the according undo operation)
  4. Move your mouse pointer to the save button (let's save explicitly)
  5. Click the save button (again, hopefully you hit it correctly)
  6. Move your mouse pointer to the toolbar button for launching the app
  7. Click the toolbar button for launching the app.
These are 7 steps. Now let's take a look at the keyboard interaction. Again, I assume the hands are on the keyboard, and the cursor blinks at the line which we want to comment out:
  1. Press Ctrl + K,C (on VS2008).
  2. Press Ctrl + S (to save explicitly)
  3. Press F5 to launch in debug mode or Ctrl + F5 to launch without debugging.
That's it. Pretty simple, huh? Now imagine how much faster I am by using these simple keyboard shortcuts. While you are on the way to the save button with your mouse, I'm probably already waiting for my app to launch. Of course the example above is a really simple one, but adding these things up, you save quite a lot of time. Or another example, I always again get goosebumps (do you call them like this?) when I see devs formatting their code by manually pressing the Tab key instead of just writing it down followed by a quick Ctrl + K,D (for auto-formatting) and a Ctrl + S (for saving).

Below there is a small list of the top shortcuts I'm using in my everyday working with Visual Studio. I tried to just pick the most frequently used and unfortunately they're taken to my working experience related to VS2008. Most of them should work with VS2010 too, however. I'll update the list  below as I find more useful ones.

Simple keyboard text navigation shortcuts (work in most text editors)
Ctrl + (Arrow left/right)
Quickly jump from word to word. Combine this with Shift (see below).

Shift + (above mentioned shortcuts)
Select the text.

Pos 1 and End
These are quite useful for quickly jumping to the beginning/end of line. Try combining them with Shift for selecting whole rows, like Shift (right) + End.

Ctrl + (Arrow up/down)
Scroll the code editor without moving the cursor.

Predefined VS2008 Shortcuts
F5 or Ctrl + F5
First to launch in debug, second to launch the app without debugging.

Alt + Shift + F10
Open the suggestion context menu which appears beneath code statements, i.e. for implementing an interface, creating a method which doesn't exist but is referenced and so on.

Ctrl + - / Ctrl + Shift + -
This allows you to navigate back and forth in your cursor position history. This is veeery useful. Try it out.

Ctrl + Tab / Ctrl + Shift + Tab
Easy, for navigating among the open code editor windows.

Ctrl + Shift + B
Start a build.

Ctrl + K, D
Auto-formats your code.

Ctrl + R,R
Refactor > Rename.

Ctrl + K, C
Comment out code.

Ctrl + K, U
Un-comment a commented code part.

Custom defined VS2008 Shortcuts
Ctrl + W
Close the active code editor window. This is a must to quickly navigate.

Ctrl + R, S (combined shortcut)
Rebuild the entire solution. Scrolling up the Solution Explorer and right-clicking on that solution icon takes just too much time :)

Android 2.2 - Release Froyo

Take a look at the official video. Some really great enhancements, on the part of the user but also on the developer's part.
Personal highlights:

  • Separate developer account page with submitted user bug reports
  • Cloud to phone communication. (how cool  is that!)
  • Storing apps on the SD card (1-button-click action)
  • Portable Hotspot
  • Speed Speed Speed
  • ...
A detailled list can be found here. Enjoy!

Google seems to care about privacy

I just received the following mail:

Hi,
To protect your privacy we would like you to know that Google Latitude is running on your Android-powered device and reporting your location.
If you didn't enable this or want to stop reporting your location please open the Maps app on your device. Go to 'Menu' > 'Latitude' > 'Privacy' and change your privacy settings.

Thanks,
Google Latitude Team
(c) 2009 Google Inc., 1600 Amphitheatre Parkway, Mountain View, CA 94043, USA. Terms of Service |Privacy Policy


It seems as if Google puts more effort in giving the user best control over its privacy. I really like this kind of approach and it's really important these days, considering such statements and taking into account how it is handled.

Can you trust your tests?

One important thing about unit testing but also about testing in general is that you can trust your tests. If that nice little bar shines green, then your logic should be fine. If you cannot trust in your tests then they're pretty useless. But what does trust mean? When can I trust my tests?

The following things come to my mind when trying to answer this question:

Do test-first development
Now thats one of the most difficult parts, unit testing "beginners" will have to face. You will always again catch yourself, first writing the implementation part. That happens to me too, but in such a case, I immediately stop writing that logic, comment it out and see the test fail.
Seeing that test fail is so important to be able to trust it, because if you see your test succeed, although you commented out that code part you'd like to test, then you're in trouble. Basically the test isn't good for anything.


Analyze!
Analyzing is important to strengthen your trust. Having tests in place, covering 20% of your code doesn't really mean you didn't break anything if you see all of your tests succeed. Sounds reasonable, doesn't it?
It is therefore important to also have some statistics about your test-code-coverage in place. Now since I'm right now writing on my thesis project I'm bringing an example of Java and Eclipse, but Peter assured me, VS2010 has a similar thing already integrated (I have to check it out these days).
In Eclipse you have a nice plugin called EclEmma which does this job for you. Let's look at an example.

I have a repository object, basically an extremely simplified version of Fowler's repository pattern (maybe I shouldn't even mention it, it's too simplistic, but anyway, the idea is the same). The repository is basically the connector between my logic and DB persistence related stuff. It keeps an in-memory cache and otherwise queries the DB accordingly. I wrote according tests to make sure everything works as expected. Running the tests with EclEmma results in a success of my test and furthermore my code gets highlighted depending on whether that logic is covered by some test case or not:


Oops,  note the red part, it means no test covers this logic. Apparently I forgot to test my repository with an attached DAO object. By creating a mock implementation of the according IBluetoothDeviceDao class, an according test case and by running the tests using EclEmma again, I get the following:

You immediately see the difference. I increased my code coverage. The remaining red part is because I didn't yet explicitly test the exception handling logic.
This visualization is extremely nice because you can verify the coverage immediately after each test-run.

References
http://www.eclemma.org/

"\n" will break your JSON jQuery - WCF service call

I recently coded a WCF REST webservice that needed to be accessed from JavaScript. Basically it was a system to provide admins of websites with a real-time system for managing info messages, intended to be displayed beneath input fields. jQuery came in quite handy in this context (as you may imagine ;) ). I used the standard $.ajax(...) function for sending/retrieving JSON encoded data to my webservice and everything wrapped up and displayed nicely within a small model popup window. That worked out quite well until some more intensive tests I made during which I discovered problems in case of multi-line text messages, i.e. within a text area. During transmission the following exception got fired:

{"ExceptionDetail":
 {"HelpLink":null,
  "InnerException":
  {"HelpLink":null,
   "InnerException":
   {"HelpLink":null,
    "InnerException":null,
    "Message":"Encountered invalid character '\u000a'.",
    "StackTrace":"   in System.Runtime.Serialization.Json.XmlJsonReader.ComputeQuotedTextLengthUntilEndQuote(Byte[] buffer, Int32 offset, Int32 offsetMax, Boolean& escaped)
         in System.Runtime.Serialization.Json.XmlJsonReader.ReadQuotedText(Boolean moveToText)
         in System.Runtime.Serialization.Json.XmlJsonReader.Read()
         in System.Xml.XmlBaseReader.ReadElementContentAsString()
         in System.Runtime.Serialization.XmlReaderDelegator.ReadElementContentAsString()
         in System.Runtime.Serialization.Json.JsonStringDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
         in System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
         in System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
         in System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName)",
     "Type":"System.FormatException"},
 ...

The most interesting part is the one I highlighted. The problem was clear, the newline character broke my JSON string which could then no more be deserialized on the server-side. As you can see from the transmitted data
{"keycode": "ContractFilter_txtCNCN_Desc", "messageDe": "ddd

d", "messageIt": "d"}
it got split up into multiple lines, breaking the JSON format basically.

The solution is at hand: these newline chars have to be encoded. One approach could be to make use of the JavaScript escape(..) function, but this has the drawback of polluting your transmitted string with these strange encoding chars and you would have somehow the need to again unescape it on the server side for having your data stored cleanly. Therefore a better, and probably more simple solution is to have a function like this:
/*
*  Escape newline chars for being transmitted with JSON over the wire
*/
function escapeNewLineChars(valueToEscape) {
    if (valueToEscape != null && valueToEscape != "") {
        return valueToEscape.replace(/\n/g, "\\n");
    } else {
        return valueToEscape;
    }
}
The comment pretty much explains what it does. It basically replaces all occurrences of "\n" (newline chars) with "\\n", basically escaping them. The transmitted JSON string
{"keycode": "ContractFilter_txtCNCN_PROT_NR", "messageDe": "ddd\n\nd", "messageIt": "d"}
looks clean and moreover it can be stored on the server-side without any further interventions. Other functions which you might find quite handy...
/*
*  Converts \n newline chars into <br> chars s.t. they are visible
*  inside HTML
*/
function convertToHTMLVisibleNewline(value) {
    if (value != null && value != "") {
        return value.replace(/\n/g, "<br/>");
    } else {
        return value;
    }
}

/*
*  Converts <br> chars into \n newline chars s.t. they are visible
*  inside text edit boxes
*/
function convertToTextVisibleNewLine(value) {
    if (value != null && value != "") {
        return value.replace(/\<br\>/g, "\n");
    } else {
        return value;
    }
}