|
< class="pagetitle">Archive for the “Software development” Category>
Reading up on theory about mashups I found an article on Wikipedia that states the following about “oldfashioned” portals’ aggregation style: “Salad bar” style - Aggregated content is presented ’side-by-side’ without overlaps”. I think this is a nice analogy, and I would like to take it a step further, saying that:
- Oldfashioned portals (”portlet” technology) are salad bars, while
- mashups are salads
Portlets:

(Image: Wikipedia commons)
Mashups:

(Image: Wikipedia Commons)
No Comments »
No measurement beats WTFs/min!
No Comments »
When trying to introduce unit tests into an existing project, code dependencies is often a problem. During a teeth-grinding session grappling with this problem I came to think about pure functions.
Pure functions are functions that (according to Wikipedia’s definition):
- always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change as program execution proceeds, nor can it depend on any external input from I/O devices, and
- evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.
Seems to me that pure functions would be a wet dream for unit testing. Imagine, you could pass in whatever the function needed to execute, and then check its return value. No mocking about with mock objects with side effect expectations needed!
But alas, we live in a world with non-pure functions. Even in functional programming, which does explicitly keep any state, you find them. In an object oriented language a primary idea is that objects should carry state. An object has a state. Period. However, if we can be inspired by the notion of pure functions, and make methods that have less dependencies, we will be much better off when it comes to unit testing.
I my experience, the ASP.NET programming model with ASPX code behind files is a model that promotes writing methods that are problematic to create unit tests for.
Take, for example, the following (theoretical, I admit) code behind snippet example:
protected void Page_Load(object sender, EventArgs e)
{
ShowOrHideEditButton(Page.User.Identity.Name);
}
void ShowOrHideEditButton(string currentUser)
{
if (Request.QueryString["user"].Equals(currentUser))
{
EditButton.Enabled = true;
}
else
{
EditButton.Enabled = false;
}
}
So, what is the problem here? Actually, there are two problems.
- The ShowOrHideMethod is not only dependant on its input parameters, but also on another member variable. When looking at the method signature, you do not see this, and might be misleaded about what the outcome of the methods is dependent on.
- The method does not return any value, but has a side-effect on its environment (setting the edit button visibility).
These two issues, makes it troublesome to test the method in a unit test. You have spend time to set up the methods context, and you have to measure the method’s side effects in order to make assesments about its result.
A much better approach would be something like this:
protected void Page_Load(object sender, EventArgs e)
{
EditButton.Enabled = ShowOrHideEditButton(Page.User.Identity.Name, Request.QueryString["user"]);
}
bool ShowOrHideEditButton(string currentUserName, string profilePerson)
{
return (currentUserName.Equals(profilePerson));
}
This way, we can easily test the ShowOrHideEditButton method in a unit test without having to use a great deal test code to set up the test context.
For increased testability, I would suggest the following for better method purity:
- A method should evaluate the same result, given the same argument value(s) and object state.
- A method execution result should be represented as, in order of preference: 1) a return value, 2) changed object state (mutation), or 3) a side-effect on an I/O device
4 Comments »
One of the things that seem very simple on a Powerpoint presentation, but are not that simple in practice, is having a web user’s identity forwarded from a calling web application to another web application when using Kerberos.
The case is as follows: I have an intranet application A which uses Integrated Windows Authentication to authenticate the user. During processing of a request from a web users, application A then makes an HTTP request to intranet application B. Application B requires the web user to be authenticated to process the request. The often most attractive solution for solving this is what Microsoft refers to as identity delegation. Simple in a Powerpoint presentation, but alas, not so simple in practice.
First of all, there are a number of preconditions in the computing environment configuration that need to be fulfilled. I found a very good summary of gotchas in this respect here. In my case, the points 2 and 6 was missing (I knew about the other once beforehand). So, when all configuration stuff set up, then the only thing left is the code and configuration in the application A.
Basically, you need to make the application impersonate the web user (meaning that it will run with the credentials of the web user). There are two ways to do this. If you wish the entire request to run as the web user, you can insert an <identity impersonate=”true” /> element under <system.web> in the application’s web.config. Or, if you wish only the request to application B to run as the web user, you can do this programmatically:
using System.Security.Principal;
...
WindowsIdentity identity = (WindowsIdentity)HttpContext.Current.User.Identity();
using (identity.Impersonate())
{
// ... code to call application B goes here ...
}
Then, the next task is to call application B itself. You can do this by creating a web request:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.somethingcompletelydifferent.com");
request.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
request.UseDefaultCredentials = true;
...
HttpWebResponse response = request.GetResponse();
...
The important things to notice here is that we set the ImpersonationLevel property to “Delegation” and that we set the UseDefaultCredentials property to “true”. So, it together, we get:
using System.Security.Principal;
...
WindowsIdentity identity = (WindowsIdentity)HttpContext.Current.User.Identity();
using (identity.Impersonate())
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.somethingcompletelydifferent.com");
request.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
request.UseDefaultCredentials = true;
...
HttpWebResponse response = request.GetResponse();
...
}
You can then test that it works in application B by checking the name in HttpContext.Current.User.Identity.Name.
No Comments »
My experience is that Spring.NET configuration files tend to grow very large. As far as I can figure, there are two principal problems that arise from this:
- The configuration files get difficult to read and maintain
- It gets easier to introduce errors in the configuration because of its size
In general, I am in favour of keeping configuration files as small as possible. I often work with web applications that can (quite) easily be redeployed to the production environment, hence I always ask the question “will this value ever change between environments or deployments” when considering introducing a new configuration part.
Now, the Spring XML configuration usually serves two main purposes; to wire together the application, and to provide values that should be possible to change between deployments of the application or for different environments. The first purpose, I would argue does not necessarily need to be in the XML configuration. Rather, if this is done in code, we get the benefit that the compiler will tell us right away if there are typos or missing references. If this wiring is in the XML configuration file, such errors will not surface until the application starts.
So, the question that I had, was how Spring context wiring could be combined in code and in XML. I found one way of doing it, but it is only applicable to singleton objects.
Say, for instance that we have an object “something” that we wish to have configured in XML:
<object id="something" type="SpringTest.Something, SpringTest" singleton="false"/>
Then, we have a class that we want to initialize in code:
class Foo
{
public Foo() { }
private Something _s;
Something S
{
set { _s = value; }
get { return _s; }
}
}
Now, we see that Foo has a dependency on Something; it needs an instance of Something to be injected. We can use the Spring context to do this after we have created the instance of Foo:
IApplicationContext context = ContextRegistry.GetContext();
Foo f = new Foo();
context.ConfigureObject(f, "fooPrototype");
But Spring does not yet know that the Foo instance needs to be injected Something. Hence, we need to tell Spring that by creating what I would call a “prototype” or “template” object configuration:
<object id="fooPrototype" type="ContextTestProject.Foo, ContextTestProject">
<property name="S" ref="something"></property>
</object>
The final step is then to register our newly created object in the Spring context:
XmlApplicationContext xmlContext = context as XmlApplicationContext;
xmlContext.ObjectFactory.RegisterSingleton("foo", f);
After this, the Foo instance is available for the application in the Spring context.
No Comments »
Posted by: vidarkongsli in Software development, Software testing, tags: .net, automation, c#, http, internet explorer, internetexplorer, statuscode, watin, web test
One question have troubled me for some time when automating Internet Explorer (actually, I am doing web testing with Watin): how to test for HTTP status codes. Finally, I figured out how to do this. The lies in an event that the InternetExplorer object raises when navigation is unsuccessful.
I ended up with writing a C# helper class:
using System.Net;
using WatiN.Core;
using SHDocVw;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
namespace Test
{
public class NavigationObserver
{
private HttpStatusCode _statusCode;
public NavigationObserver(IE ie)
{
InternetExplorer internetExplorer = (InternetExplorer)ie.InternetExplorer;
internetExplorer.NavigateError += new DWebBrowserEvents2_NavigateErrorEventHandler(IeNavigateError);
}
public void ShouldHave(HttpStatusCode expectedStatusCode)
{
if (!_statusCode.Equals(expectedStatusCode))
{
Assert.Fail(string.Format(CultureInfo.InvariantCulture, "Wrong status code. Expected {0}, but was {1}",
expectedStatusCode, _statusCode));
}
}
private void IeNavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
{
_statusCode = (HttpStatusCode)StatusCode;
}
}
}
Note that I use Visual Studio test runner to run my web tests. Then, I can use this in my test:
using (IE ie = new IE())
{
NavigationObserver observer = new NavigationObserver(ie);
ie.GoTo("http://some.where.com");
observer.ShouldHave(HttpStatusCode.NotFound);
}
1 Comment »
When doing web testing using Watin, it is not trivial to be able to do a POST request to the server. However, with the help this article on microsoft.com, I was able to figure out how. I ended up with writing this class:
public class Navigator
{
private IE _ie;
public Navigator(IE ie) { _ie = ie; }
public void Post(Uri baseUri, params KeyValuePair<string, object>[] postData)
{
object flags = null;
object targetFrame = null;
object headers = “Content-Type: application/x-www-form-urlencoded” + Convert.ToChar(10) + Convert.ToChar(13);
object postDataBytes = MakeByteStreamOf(postData);
object resourceLocator = baseUri.ToString();
IWebBrowser2 browser = (IWebBrowser2)_ie.InternetExplorer;
browser.Navigate2(ref resourceLocator, ref flags, ref targetFrame, ref postDataBytes, ref headers);
_ie.WaitForComplete();
}
private static byte[] MakeByteStreamOf(KeyValuePair<string, object>[] postData)
{
StringBuilder sb = new StringBuilder();
if (postData.Length > 0)
{
foreach (KeyValuePair<string, object> postDataEntry in postData)
{
sb.Append(postDataEntry.Key).Append(’=').Append(postDataEntry.Value).Append(’&’);
}
sb.Remove(sb.Length - 1, 1);
}
return ASCIIEncoding.ASCII.GetBytes(sb.ToString());
}
}
For example, I can use it like so:
using (IE ie = new IE())
{
Navigator navigator = new Navigator(ie);
navigator.Post(new Uri("http://www.foo.com/"), new KeyValuePair<string, object>("p", 1));
Assert.AreEqual("OK", ie.Text);
}
No Comments »
Encapsulation is one of the most important features of object orientation, but often easy to break in practice. One common mistake to make in this respect happens when creating a class that holds some sort of collection or array.
For example, let’s assume that we want to make an immutable object (meaning that its state should never change troughout its lifecycle):
public class Request
{
private readonly string[] _acceptedTypes;
public Request(params string [] acceptedTypes) {…}
public string[] AcceptedTypes { get {…} }
public bool Accepts(string type) {…}
}
We have a class that take a list of strings as parameter to the constructor. The intent is that the list of strings should never change. Typical use of the class would be something like this:
Request request = new Request("application/json", "application/x-json");
if (request.Accepts(somestring))
{
...
}
Here’s a naîve implementation of the class:
public class Request
{
private readonly string[] _acceptedTypes;
public Request(params string[] acceptedTypes)
{
_acceptedTypes = acceptedTypes;
}
public string[] AcceptedTypes { get { return _acceptedTypes; } }
public bool Accepts(string type)
{
foreach (string acceptableType in _acceptedTypes)
{
if (acceptableType.Equals(type))
{
return true;
}
}
return false;
}
}
Our intent of creating an immutable object is here manifested in the ‘readonly’ keyword used when defining the member variable _acceptedTypes, and the fact that there is only a set accessor for the AcceptedTypes property. But alas, there are several issues that break our intent.
First, let’s have a look at the AcceptedTypes accessor. It allows us to write code such as this:
request = new Request("application/json", "application/x-json" );
bool accepts1 = request2.Accepts("application/javascript");
request.AcceptedTypes[1] = “application/javascript”;
bool accepts2 = request.Accepts(”application/javascript”);
After running this code, we find that accepts1 != accepts2. We have been able to manipulate the state of the object (in other words, it is mutable). The problem is that the AcceptedTypes exposes a reference to the array of types. Alternatively, it could only expose an IEnumerable that can be used to iterate over the types:
public IEnumerable AcceptedTypes
{
get
{
foreach (string acceptableType in _acceptedTypes)
{
yield return acceptableType;
}
}
}
Then, it will not be possible to manipulate the state of the object by calling request.AcceptedTypes[i].
Still, there is one problem with the current implementation. We still can write code such as this:
string[] types = new string[] { “application/json”, “application/x-json” };
Request request = new Request(types);
bool accepts1 = request.Accepts(”application/javascript”);
types[1] = “application/javascript”;
bool accepts2 = request.Accepts(”application/javascript”);
After running this code, accepts1 != accepts2. The problem is that we pass a referene to the constructor, and the object instance stores it in its local variable. We are free to change the object that our reference points to, indirectly changing the state of the object. In order to fix this, we change the constructor code for Request:
public Request(params string[] acceptedTypes)
{
_acceptedTypes = new string[acceptedTypes.Length];
Array.Copy(acceptedTypes, _acceptedTypes, acceptedTypes.Length);
}
2 Comments »
When developing an ASP.NET solution for a customer, I run the application locally on my laptop (IIS 5.1, Win XP). For a realistic test scenario I had set up a separate host name for my local computer in my hosts file. I then enabled my application for Integrated Windows Authentication, and was a bit puzzled by why it did not work.
Internet Explorer did not automatically authenticate me when wisiting the web application. The problem turned out to be that there was no Service Principal Name registered in Active Directory that associated the hostname with my computer. Hence, the client could not request a Kerberos service ticket for my web application.
The solution was to run the setspn.exe tool (available in the Windows Server 2003 Support Tools package) to create the desired SPN entry in Active Directory. For example:
setspn -A HOST/my.fake.hostname.com MYCOMPUTER
(where MYCOMPUTER is the network computer name for my workstation). You have to be a domain administrator to run this command.
The effect of running this command is that ‘HOST/my.fake.hostname.com’ will be added to the multi value field ’servicePrincipalName’ on my computer’s entry in Active Directory.
No Comments »
Jakub, one of the project members on my current project pointed me to this blog entry explaining a threading issue with Directory<K,V> in .NET: Hashtable and Dictionary thread safety considerations. I know that I earlier have written code exactly like the one in the blog entry, so this was a bit of an eye-opener.
No Comments »
|