Uncategorized

Rhino Mocks Callbacks

Rhino Mocks Callbacks are what I have been trying to find for 2 days!

My problem is that I needed to test the internals of a method by checking to see that a value had been evaluated properly.  The result of this operation gets passed to my DAL to be inserted into my DB.  My test was to test my Business Layer so I needed to mock the DAL to prevent the DB hit from occurring.  The problem was, how could I grab the value from inside my BL method?

With Rhino Mocks, I mocked my DAL and recoreded an Expect on the method that does the DB insert.  Now all I needed to do was to replace the default implementation of my DAL method with one used for testing.  The internals of the testing method would be able to read the params being passed to the DAL from the BL, thus allowing me to verify that the BL evaluated what it was supposed to properly before passing the data on to be inserted.

Rhino Mocks Callbacks to the rescue!  I love the power of delegates, unfortunately it’s still the largest concept that repeatedly trips me up.  After locating this post on using Rhino Mocks Callbacks, I was finally able to accomplish my goal.

The application in the example below is a WCF service which accepts TFS Notification messages when a WorkItem changes.  When I recieve the notification from TFS, I am logging the message in a log table.  One of the params that is sent from TFS contains the publisher’s url in an XML string.  My BL needs to select the value of the attribute containing this url and insert it into a column in my table.  Thus, what I wanted to test was that my xpath is correct (and if TFS changes something in subsequent versions I’ll know immediately that they changed an expected format).

WCF Method to receive the TFS notification:

public void Notify(string eventXml, string tfsIdentityXml, SubscriptionInfo subscriptionInfo)
        {
            XmlDocument eventXmlDoc = new XmlDocument();
            XmlDocument tfsIdentityXmlDoc = new XmlDocument();
            try
            {
                eventXmlDoc.LoadXml(eventXml);
                tfsIdentityXmlDoc.LoadXml(tfsIdentityXml);
                TfsNotificationDto tfsNotificationDto = new TfsNotificationDto
                    {
                        Message = eventXml,
                        Date = DateTime.Now,
                        Publisher = tfsIdentityXmlDoc.SelectSingleNode("TeamFoundationServer").Attributes["url"].Value,
                        Subscriber = "",
                        Success = 'F'
                    };
                this._repo.LogTfsNotification(tfsNotificationDto);               
            }
            catch (Exception)
            {
                throw;
            }            
        }

Test method utilizing Rhino Mocks to mock the DAL:

[Test]
        public void TestLogTfsNotification()
        {
            string pub = string.Empty;
            With.Mocks(delegate
            {
                IRepository repo = Mocker.Current.StrictMock<IRepository>();
                TfsNotificationDto tfsNotificationDto = RepositoryTests.MockTfsNotificationDto;
                Expect.Call(() => repo.LogTfsNotification(tfsNotificationDto)).Callback(new Delegates.Function<bool, TfsNotificationDto>(notification => { pub = notification.Publisher; return true;}));
                Mocker.Current.ReplayAll();
                ITfsEventService service = new TfsEventService(repo);
                service.Notify(eventXml, tfsIdentityXml, subscriptionInfo);
            });           
            Assert.AreEqual(this.publisher, pub);
        }

What I can now do is in the LogTfsNotification method of the mocked repo, I can now read the parameter passed to it from the service.Notify method.  Service.Notify is extracting the subscriber url and packages it up into a TfsNoficationDto that is passed to LogTfsNotification.  Thus I can do notification.Publisher to see what value was extracted from the xml in the service object of my BL.

Tags: , , , , , , , , , ,

Scrum Late Fees Rule!

image

Mmmmm

Tags: ,

Friday, February 5th, 2010 Linux, Uncategorized No Comments

Linux Help

Commands:

Command Desciption Examples
find finds anything on the system find / -name password (find all files that have the name “password”)
apropos lookup all the commands that apply to a given keyword apropos dir
apropos image
apropos burn
ls directory listing ls -lh (easy to read file size)
wget downloads an internet resource wget -M http://www.shotola.com/ (creates a mirror of a site)

Paths:

Friday, December 4th, 2009 Uncategorized No Comments

Want to win a bet?

Layer a couple post-it pads together like this and bet a co-worker to pull them apart. The more precise you are with your layering the better the chance you have of winning.

Thursday, December 3rd, 2009 Uncategorized No Comments

Ye ask and ye shall receive

Dual whiteboards ready to be installed in my office.

Monday, October 19th, 2009 Uncategorized No Comments

Official Scrum Clock

Be on time, or buy donuts.

Wednesday, October 14th, 2009 Uncategorized No Comments

Is there any way in Hibernate to join on another table using a composite key?

Question:

Is there any way in Hibernate to join on another table using a composite key?  Trying to add fields to a class that come from another table.  I’d like to join with the JCDATESUM table using the same branchNumber + jobNumber composite key, but the <key /> element seems to only support a single column???

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.model">
<class name="BillingSummary" table="JSUM" mutable="false" lazy="true">
<!-- <cache usage="read-only"/> -->
<composite-id name="id" class="SummaryID" mapped="false">
<key-property name="branchNumber" type="com.dao.hibernate.usertype.DB2CharType" column="JP" length="3" />
<key-property name="jobNumber" type="com.dao.hibernate.usertype.DB2CharType" column="JN" length="6" />
</composite-id>
...
...
...
<join table="JSUM">
<key />
<property name="customerName" type="com.dao.hibernate.usertype.DB2CharType" column="JS" />
</join>
</class>
</hibernate-mapping>

Answer:

Look at: http://docs.jboss.org/hibernate/stable/core/reference/en/html/example-mappings.html
They have some complicated mappings toward the bottom that involves composite keys.
yeah…as usual – simple solution

<key>
<column name="JP" />
<column name="JN" />
</key>

Tags: , , ,

08/20/2009

Frozen M&M

Thursday, August 20th, 2009 Uncategorized No Comments

Creating a SharePoint Solution

The following documents how I put my first SharePoint solution together.

What is a SharePoint solution?

A SharePoint solution is a package for installing SharePoint features to a SharePoint instance.  A solution contains one or many features to install.  A solution is built into a .wsp file, which is the same format as a .cab.

Why use a SharePoint solution?

SharePoint’s stsadm.exe has built-in functionality to handle the unpacking and installation of WSP files.  So from your perspective, it makes your life easy since you can just tell stsadm to install the package.  Using a solution for a package containing only one feature may be trivial, but it could be a time-saver if you have more than one feature in your solution.

How to create a solution.

My co-worker got me going with WSPBuilder.  This is a nice Visual Studio add-in that provides you with project templates and build tools to automatically package your feature(s) into a .wsp solution.

Create the Visual Studio Project.

WSPBuilder would tell you to create a new project in VS by going to File > New > Project and selecting Visual C# > WSPBuilder > WSPBuilder Project.

Selecting the default WSPBuilder Project

Selecting the default WSPBuilder Project

The problem I have with that is that the project that is created is not a web project.  Thus, you can’t debug your code simply by pushing F5.  Sure there are ways to debug your code, but I’m lazy.  So what I would rather do is create a new ASP.NET web site and turn that into a WSPBuilder project.  When WSPBuilder builds a wsp, it basically just looks for files in a certain location within the project it is building.  So you don’t have to use their project, you just have to put things in the right place.  So I create my web project, and then create the 12 hive structure that WSPBuilder depends on and create a solutionid.txt file.

The 12 hive structure must be created so that it identically matches the 12 hive structure in SharePoint.  When the WSP is installed, it is just going to have it’s files dropped on the 12 hive, so it’s up to you to be sure they get dropped in the proper location.  The structure you see in the screenshot below follows the standard SharePoint structure for features (TEMPLATE\FEATURES\<feature_directory>\).

WSPBuilder Project as Web Project

WSPBuilder Project as Web Project

One other item you should notice is the 80/bin directories.  This is created so that WSPBuilder will install the assemblies for this feature to the bin of the SharePoint web application and not to the GAC.  I have not been a fan of the GAC for a while, so I like to keep my assemblies out of there.  Creating this structure allows me to do that while still using WSPBuilder.

Create content for your feature.

You can see from the previous screenshot that I have also created an aspx page in the 12 hive.  This is the actual content page that we will attempt to see when we install & activate this feature.  This page is a standard aspx page, inheriting from System.Web.UI.Page.  If you wish for your pages to have different functionality once installed within the bowels of SharePoint you can inherit from SharePoint pages.  One suggestion might be to inherit from Microsoft.SharePoint.WebPartPages.WebPartPage.

Tell the feature what’s included.

The feature needs to have a manifest created so that it knows what to activate when the time comes.  You can see from the previous screenshot that in the feature directory are two xml files, feature.xml and elements.xml.  Feature.xml is the only one that really needs to be there.  This is the file that contains the actual manifest.  Elements.xml in this case just contains the list of files in the manifest.  This list can go directly into feature.xml, but most examples show the two separated into separate files, so I’m just doing the same.  Nevertheless, if you look at feature.xml, you will see that it contains an ElementManifest node which points to elements.xml.  Also, in feature.xml be concerned with the Id attribute.  This attribute must be a GUID that is unique to all features in the SharePoint instance.  That shouldn’t be a problem until you start copying feature.xml files to new features to use as a template, and forget to update the Id.  Whoops!  If you’re concerned about what the other configuration settings are in this file, you’re going to have to check with MSDN.

As for elements.xml, this is basically a list of all the content files to be included when the feature is deployed.  If you want to see the options for this file, again, check with MSDN.

Build it.

So now that we have created our 12 hive, created our bin, content, and manifest, we can build our wsp.  To build the wsp, first do a rebuild on the entire solution, just to make sure everything compiles.  Built?  Good.  now right click on your feature project choose WSPBuilder > Build WSP.  Once that completes you can now refresh the solution in Visual Studio.  Make sure you have Show all Files turned on, and now you should see a .wsp file in the root of your feature project.

If you want, you can now look inside the WSP.  Rename the WSP to .cab and open it.  You will now see a browser that will show you the contents of this package.  You should see all assemblies included in your solution, all content files, feature.xml, elements.xml, and manifest.xml.  Manifest.xml?  Where did this come from?  WSPBuilder actually took care of building this for you, and thank it you will.  Manifest.xml is what’s used to install the solution to SharePoint by stsadm.exe.  This is what is going to tell stsadm what to put where.  It would probably be a good idea to get familiar with what’s in this file, so poke around in there a bit.

What’s next?

Well, we’re all built.  Check out my next post on deploying a SharePoint solution into a SharePoint instance.

Monday, August 10th, 2009 Uncategorized No Comments