Archive

Archive for the ‘Programming’ Category

My Lessons Learned From Rhino.Mocks

January 25th, 2010

Recently I have begun working with Rhino.Mocks and I stumbled a little bit getting up to speed. My experience with mocks is from using JMock1 with a Java project. This is the first time I have used mocks on a .NET project so it took me a little bit getting used to the syntax.

From looking at all the tutorials and examples on the web about Rhino.Mocks I couldn’t really find anything that was a kick in the pants from my stand point. Most of the tutorials didn’t discuss the methods Rhino.Mocks framework in any detail, and would instead go into, somewhat, depth about the theory of mocking. I already know (or at least think I do) the theory of mocking so I was really wanting something to get me going quickly.

I found a video by the creator of Rhino.Mocks, Ayende, which really helped out. I wanted to put up here some of the key points. You can watch the video at http://www.ayende.com/hibernating-rhinos.aspx.

The ‘_mocks’ variable listed below is a reference to the mocks repository, this will basically contain all of your mocks:

var _mocks = new MockRepository();

The ReplayAll method, _mocks.ReplayAll(), puts the framework into record mode. You will need to call this method after creating your mocks and setting expectations. The Rhino.Mock framework will begin to track all the method calls and verify the expected counts (if any) are meet. In the video Ayende talks about this around 11:08, 28:10 & 29:50. There is not any issue with calling this method repeatedly, so having it in your TearDown method as a precaution will not cause any problems.

One other thing that sent me on a wild goose chase is the error message:

require a return value or an exception to throw

This will occur when you don’t move your tests from Record mode to Replay mode (typically you are missing a _mocks.ReplayAll() method call). What happens is you call a method such as ‘user.GetPurchases()’, and in my case I was making an assertion on the expected values. But the framework is waiting on you to specify an expectation or a return value, but since this is missing Rhino.Mocks can not handle it and so throws that exception.

As the name implies ‘_mocks.VerifyAll()’ will verify any outstanding expectations which were set by you, I have this in my TearDown method as well.

What’s the difference between Expect.Call and LastCall.Constraints?
As he discusses in the video at 36:55, this is due to a constraint the compiler enforces. If you had the following code:

class User {
  public void Save() { .... }
}

Then of course the compiler will not allow you to have syntax such as:

Expect.Call(user.Save())

You will need a way to execute the call, then set expectations on that … well, last call. Simple name huh? Shockingly he discusses this as well in the video at 36:55. Both of syntaxes are basically the same, the Expect.Call syntax is actually changed into the LastCall format. He shows how to mock out a void return type around 19:40.

Another exception/error you might stumble across is:

This action is invalid when the mock object is in verified state.

You will run across this while trying to setup a mock or expectation and in the record phase. My mistake was putting the creation of the MockRepository instance (the _mocks variable) in the TestFixtureSetup method – being this is only called once per the entire test class, my MockRepository was not being recreated. So I received the error – switching this to be what I expected, the Setup attribute, then all was well.

Something I have yet to look into is the difference between the following lines:

Expect.Call(order.PaidInFull).Return(false);

-- and --

SetupResult.For(order.PaidInFull).Return(false);

If you know of what the difference might be, I would be happy to hear.

Author: gorshing Categories: Programming Tags:

Subversion, TortoiseSVN, and Putty

October 29th, 2009

I recently changed development machines and needed to setup TortoiseSVN again using SSH, and I ran into a problem where I could only access Subversion if it was the only authorized key. For my host I’m using Site5, but I’m sure this applies more generally than just for Site5. I already have another set of keys working for ssh, and that public key is the first line of my authorized_keys2 file (the ‘2′ is somewhat optional).

My url for svn is something similar to ’svn+ssh://domainname_svn/trunk’, I verified this just to make sure from a backup I have on another machine. So I knew I was using the right url, but I still wasn’t able to connect. I have pageant running and have both (one for ssh and one specifically for svn) keys loaded.

When I tried to check out a working copy with Tortoise, it would give me a error. When I would change my authorized_keys file on the server to only have the svn key then it worked just fine. Once again I knew the server was setup correctly as I had no problems previously.

What my problem was I had not specified a key under putty. I use putty’s saved sessions to specify a couple of options only for my svn connection. I had not specified anything under Connection -> SSH -> Auth -> Private Key.

Once I filled that in with the correct key then it didn’t matter what keys Pageant had loaded.

Author: gorshing Categories: Programming Tags:

Maven Default Profiles

January 2nd, 2009

I have been using Maven for a while now and as such I have ran into my fair share of using profiles with Maven. As there are many other articles/posts about Maven profiles I will not be providing an overview here. I would like to delve into a particular area of interest when dealing with profiles and the activation aspect.

One of the many ways a profile can be activated is by ‘activeByDefault’, I currently use this both on our CI box and Windows (typically developers) box. These basically provide system properties for a database connection information and paths to property files. There has not been a time where the activeByDefault profiles has caused us any problems at all. So I was a little apprehensive when I saw James Lorenzen’s blog entry on the problems with using these types of profiles.

James professes (by way of providing a link to documentation) when a profile is activated by any other way then the so-called default profile is turned off. So I created a simple example pom in an attempt to replicate this and in doing so I ran across the same problem as he did. For the following, I use this pom as an example.

gorshing@touch ~
$ mvn clean
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Unnamed - com.example:ProfileProject:pom:0.1-SNAPSHOT
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val 54
     [echo] yep 42
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Wed Dec 31 12:29:32 CST 2008
[INFO] Final Memory: 3M/7M
[INFO] ----------------------------------------------------------------------
 
gorshing@touch ~
$ mvn clean -Pqual.val
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Core000Build
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val ${default.profile.val}
     [echo] yep 69
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Wed Dec 31 12:03:01 CST 2008
[INFO] Final Memory: 3M/7M
[INFO] ----------------------------------------------------------------------

As you can easily tell, this is exactly as the documentation specifies. The key difference between my experience and his is solely based on where the profiles are located. Given the setup I am using at work, the ‘activeByDefault’ profile isn’t in the pom itself, but is in ${M2_HOME}/conf/settings.xml.

I have added the following to my ${M2_HOME}/conf/settings.xml configuration, and using this pom (same as above only without the activeByDefault profile entry).

    <profile>
      <id>default.val</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <default.profile.val>54</default.profile.val>
      </properties>
    </profile>

As you can tell in the following listing, the activeByDefault truly does what one might expect.

gorshing@touch ~
$ mvn clean
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Unnamed - com.example:ProfileProject:pom:0.1-SNAPSHOT
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val 54
     [echo] some val 42
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Fri Jan 02 18:42:53 CST 2009
[INFO] Final Memory: 3M/7M
[INFO] ----------------------------------------------------------------------
 
gorshing@touch ~
$ mvn clean -Pqual.val
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Unnamed - com.example:ProfileProject:pom:0.1-SNAPSHOT
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val 54
     [echo] some val 69
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Fri Jan 02 18:43:11 CST 2009
[INFO] Final Memory: 3M/6M
[INFO] ----------------------------------------------------------------------

I am curious as to if this is expected results, or if this is a bug which might be resolved …. if that happens then it will most defiantly break some of my builds.

Something to look into and chew on.

Update: I asked on #maven @ irc.codehaus.org and received the following from wsmoak (these are of course her opinion):

activeByDefault was originally intended to be active only if no other profiles were explicitly activated … a cheap way to activate exactly one of a set of profiles … however I think it’s been “improved” since then. not sure exactly when/how … I generally avoid using it and don’t recommend it

Author: gorshing Categories: Programming Tags:

Convert Local Maven Repository Into Managed

December 24th, 2008

There are developers using Maven which decide, for some reason or another, not to use a repository manager. As time progresses on they realize they need something more robust in order to manage all there releases, or possibly the team begins to grow in size. No matter the reason, there are times when a local Maven repo needs to be converted to a managed repo.

Stuart McCulloch ran into this as well and wrote a blog post on how to accomplish this. One caveat is the scripts expect the machine to be a Linux flavor, I guess if you had Cygwin installed then this would still work on Windows (albeit I haven’t tried).

Now with the latest release (1.2.0.2 at the time of writing) Sonatype’s Nexus Repository Manager comes prepackaged with a set of tools. One of which is a repository converter tool. Refer to the user story for more information.

Author: gorshing Categories: Programming Tags:

xmllib2 and python

November 15th, 2008

I have been working with Python and libxml2 for a bit now and have found out there is little documentation out there on how to get everything working together. Also as I have very little permissions on some of the machine I work on, I need to be able to have my own installation of Python without affecting the entire system. I have put together the following steps to follow for installing your own installation of Python using least privileges (without root access) for both Windows and Linux.

One quick note about Linux, a lot of GNU Linux distributions out there might already have Python2.5, but I have not had the luck of dealing with any of these. You should check to make sure you need to install a custom installation.

Typically I would like the latest and greatest of a package, however; the libxml2 Windows package does not seem to acknowledge Python2.6 or above, so to keep everything on the same page I use Python2.5 on Linux as to match up with a Windows version also. It goes without saying, but if platform independence doesn’t matter to you then choose whichever way you would like, for myself I like to have my scripts be able to run on Linux and Windows.

Download and extract Python-2.5.2.tar.bz2 from http://www.python.org/ftp/python/2.5.2/. I typically just work out of my home directory, but to each their own.

wget http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tar.bz2

Now just a simple extraction:

tar xjf Python-2.5.2.tar.bz2
 
cd Python-2.5.2

Now we need to configure the python setup. The key part is the –prefix portion, as many people out there are not too familiar with building a source distribution by hand. This basically is the location where all the binaries, libraries, and man pages will be stored. The layout under this directory will be something similar to (I kept this brief, you will have more than this):

$HOME/utils
  +- include/
       |- python2.5
  +- lib/
       |- python2.5
  +- share/
       |- man
  +- bin/
       |- python
       |- pydoc
       |- python2.5
export LDFLAGS="-R$HOME/utils/lib"
 
./configure --prefix=$HOME/utils
 
make

I use the LDFLAGS variable to keep from dealing with LD_LIBRARY_PATH, please these links for more information.

Note: On some hosts, such as Site5, you will need to ask IT Support to be put into a compiler group in order to obtain access to gcc.

Now we will install all the built binaries and libraries into the prefix directory we specified earlier.

make install

I will not get into it here, but it is possible to install multiple versions of Python side-by-side. If this is something you would like, then look into using make altinstall instead. The README file details this in the Installing section (around 75% into the file).

If all goes well, then $PREFIX/bin/python should work. I typically adjust my PATH environment variable to accommodate this:

export PATH=$HOME/utils/bin:$PATH

Installing libxml2 on Linux

As these steps follow extremely close to the preceding steps, I will make this a little more brief.

Download libxml2-2.7.2.tar.gz from ftp://xmlsoft.org/libxml2/ (a newer version might be available).

Extract the package in to a working directory.

tar xzf libxml2-2.7.2.tar.gz
 
cd libxml2-2.7.2

Then execute the following:

./configure --prefix=$HOME/utils --with-python=$HOME/utils/bin/python
 
make
 
make install

Installing libxml2 on Windows

To get libxml2 working on Windows, it is as trivial as downloading a package from http://users.skynet.be/sbi/libxml-python/, it automatically detects an installation of python and installs itself.

In some future posts, I’ll go over using libxml2 with python.

Author: gorshing Categories: Linux, Programming Tags:

Comparing Maven Repository Managers

August 29th, 2008

This seems to do a pretty good job at comparing, as far as a feature matrix, three of the top repository managers for Maven.

I know for sure developers from Nexus and Archiva have made sure this is up-to-date.

Author: gorshing Categories: Programming Tags:

Meet the Design Team

July 26th, 2008

The following link is a video I genuinely enjoyed watching and I felt I would share it as well. It is pertaining to the C# 4.0 Design Team, and I try to watch most of Anders Hejlsberg videos if I am able.

They do not get into an in depth/technical discussion concerning C# 4.0, they touch more on architectural items. They mention a few languages but happen to leave Java out of it. Another thing I thought was interesting is how they have met many times to discuss a feature. This might drag on for multiple meetings (they mention 10 hours), but they end up not implementing it. I felt as thought this was really neat how they put forth a lot of time and thinking into aspects of the language and are not afraid to say no.

Author: gorshing Categories: Programming Tags:

What Are You Afraid Of?

June 28th, 2008

Jeff Atwood has a really good post titled Don’t Go Dark. I myself sometimes get caught up in things like this. With the exception of code reviews (which I really do enjoy), I have a tendency to clam up when posting my code out for public viewing for things such as open source projects.

Just last night I happened to post a python class I had been working on, it was (and is) nowhere close to being complete. But I did feel as though it had value in it and could be used by other people. I had hopes others would pick it up and improve upon it as well. I reflected on why I believe people would behave this way, and my only insight was to how I felt. My biggest hesitation was not being able to clarify any misunderstandings or to justify why I had written it the way I did and my name would be attached to the code as it stood.

I thought back to a blog post I read over a year ago, I can’t for the life of me remember who posted it. But one of the reason why was to give you a chance to defend yourself on how lousy the code might have been. It might be because of a bug or some user requirement, but since the code will always be there, it is best to document it with explanations on why you did things a certain way (or do not do things).

Performing code reviews, I feel, are extremely important in a developers life. If developers are using the agile principle of pair programming then code reviews are taken place on a daily basis. But all too often developers do not implement pair programming. I have witnessed teams, which even though they profess to use agile principles, are quick to cut out pair programming from their software development.

Maybe this should be a code kata, in that a developer should write up a snippet of code and send out for review amongst their peers.

As I was about to ‘publish’ this post, another similar fear hit me. As I put out my thoughts and opinions on this blog, I am sure that I will get something wrong. But I will always have a way to correct it and publish it out again … hey, that sounds awfully familiar to writing code doesn’t it?

Author: gorshing Categories: Programming Tags:

CVS Relocate

April 16th, 2008

Many times I have needed to relocate a Subversion repository. This is relatively easy in Subversion, there is a ’svn relocate’ command. A stipulation of this command is the new locations repository must match identical to the old repository.

I recently needed to change (relocate) the server name of a CVS repository at a clients site. I found a small example which used a Perl script. I do not really have a problem with this … if I had Perl. The build box is a Linux environment, but my development environment is Windows XP.

I had installed cygwin previously, but choose not to install Perl (shockingly none of the items I installed had Perl has a dependency either). So I used the following in cygwin:

find -path '*/CVS/Root' -exec sed -i 's/oldcvs.example.com/newcvs.example.com/' '{}' \;

I’m sure somebody has a better/faster way, and I would love to see it. But this worked for me quite well.

Author: gorshing Categories: Programming Tags:

OpenStreamOnFile Does Not Support Unicode?

February 28th, 2008

I have been working on converting .msg file to .eml files and I just couldn’t get anything to work correctly. I was wanting to have my small converter project work with Unicode. All the functions returned as though everything worked correctly, the only problem was that nothing was written out to the eml file.

I googled around and found the following post from Google Groups.

I will be watching the documentation for this function as I left snide remark for MSFT feedback.

I will be posting the converter project which I am using. I have written a C++ DLL to access the MAPI items, and then wrote a small wrapper C# DLL to make it easy for managed developers to use in there projects.

Author: gorshing Categories: Programming Tags: