Just A Bit More REST

Just a few more examples, kind of a follow up to my Simian Satasin Asynchronous Calls Entry.  Keep in mind, I'm providing these as examples, I do NOT suggest writing tests with Thread.Wait calls in them.  Very very bad practice.

    [TestClass]
    public class GetRestServices
    {
        private XDocument doc;
 
        [TestMethod]
        public void TestSynchronousCallWithHttpWebRequest()
        {
            var request = (HttpWebRequest) WebRequest.Create("https://twitter.com/statuses/public_timeline.xml");
            var response = request.GetResponse();
            var responseStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            var doc = new XmlDocument();
            doc.Load(responseStream);
            Assert.IsNotNull(doc);
        }
 
        [TestMethod]
        public void TestAsynchronousCall()
        {
            const string baseUri = "http://twitter.com/statuses/public_timeline.xml";
            var svc = new WebClient();
            svc.DownloadStringCompleted += svc_DownloadStringCompleted;
            svc.DownloadStringAsync(new Uri(baseUri));
 
            Thread.Sleep(1000);
            Assert.IsNotNull(doc);
            var docTest = new XDocument();
            Assert.IsInstanceOfType(doc, docTest.GetType());
        }
 
        private void svc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            doc = XDocument.Parse(e.Result);
        }
 
        [TestMethod]
        public void TestAsynchronousCallWithSecurity()
        {
            const string baseUri = "http://twitter.com/statuses/friends_timeline.xml";
            var svc = new WebClient();
            svc.Credentials = new NetworkCredential("yourtuweetusername", "yourtuweetpassword");
            svc.DownloadStringCompleted += svc_DownloadStringCompleted;
            svc.DownloadStringAsync(new Uri(baseUri));
 
            Thread.Sleep(3000);
            Assert.IsNotNull(doc);
            var docTest = new XDocument();
            Assert.IsInstanceOfType(doc, docTest.GetType());
        }
    }

Hope those are helpful for those searching.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by: adron
Posted on: 2/26/2009 at 7:31 PM
Tags: , , ,
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Simian Satasin Asynchronous Calls

All I wanted was an asynchronous WebClient call.  What do I get?  Tons of dead links in MSDN, tons of pure crap of non working code all over the net.  Then I realized I needed to search for silverlight AND asynchronous instead of just asynchronous.  Anyway I finally found some legit code snippets and put together this simple test.  The story goes like this...

Here's a little test for getting asynchronous calls up and running, and tested.  Keep in mind long tests that take place over the network or that will introduce extensive latency should NOT be part of a CI build.  If anything set these into a separate build that can run over a period of time to assure integrity and that the services are up, but seriously, don't add them to a CI build.  I've seen enough of these, and it is VERY destructive to productive development.  No really, did I point this out yet, do NOT add these to a standard CI build.  K - thx.

With that warning, here's a way to test some services over REST.

   1:      [TestClass]
   2:      public class AsyncTest
   3:      {
   4:          private XDocument doc;
   5:   
   6:          [TestMethod]
   7:          public void TestMethod1()
   8:          {
   9:              string baseUri = "http://twitter.com/statuses/public_timeline.xml";
  10:              var svc = new WebClient();
  11:              svc.DownloadStringCompleted += svc_DownloadStringCompleted;
  12:              svc.DownloadStringAsync(new Uri(baseUri));
  13:   
  14:              Thread.Sleep(1000);
  15:              Assert.IsNotNull(doc);
  16:              XDocument docTest = new XDocument();
  17:              Assert.IsInstanceOfType(doc, docTest.GetType());
  18:          }
  19:   
  20:          private void svc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  21:          {
  22:              doc = XDocument.Parse(e.Result);
  23:          }
  24:      }

Create a variable that the handler will assign a value to, it doesn't really matter what it assigns as long as it proves the handler executed.  Of course, if you're looking for a particular bit of code or a property you would want to test for that, but otherwise the primary concern is to test that it fired appropriately and didn't cause an explosion of some type.

Next create the method that initiates the call and wire up the event handler to the event.  Now when you execute the method, which I've left as the default lame name of TestMethod1, you should get a positive test.  The big question for this test is, "Will the REST service return in less than a second?"  If it takes longer you need to add to the Thread.Sleep(X) method.

There is of course all types of issue with this particular test.  Isolation is broken, it breaches boundaries, yadda yadda yadda yadda.  The point is, you get a test that can verify that a service is up, and that it responds with something.  As an extra kicker you can even verify that the service is responding within a certain threshold of time since a Thread.Sleep method is used.  If it fails because the time isn't enough, you would know you need to do something about the performance.

Either way, good luck on testing these, if you have any other ideas on legit ways to test services like these, please pop a comment on this entry.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by: adron
Posted on: 2/20/2009 at 7:07 AM
Tags: , ,
Categories: How-To, Samples, and Such | Unit Testing
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

PEX, Ben Hall, and Some Other Notes

While checking out more ideas for my data generation tool/app/libraries I stumbled on this video of a presentation by Ben Hall.  The presentation “DDD7 Session Video - Microsoft Pex - The Future of unit testing?” is a great introduction to the PEX framework/toolset.  If you write unit tests, or are trying to get started writing tests, this is a great video to watch.

I watched the video and have a new level of admiration for this guy, and for Red Gate.  He’s using ReSharper (+ points), steps through code smoothly and logically (+ points), and above all he knows his information.  Excellent traits.

PEX Worries?

At 40:14 in the video Ben brings up a major point of contention that I too have.  I don’t really want to use PEX with the Microsoft Test Framework.  I?d much rather use NUnit, mbUnit, or SOMETHING besides Microsoft Test.  Even though, I know, the last dozen code entries I’ve posted uses the Microsoft Test Framework I intend to change that soon.  Ben goes on to point out that almost all of the major test frameworks are now supported.  This is awesome news!  I now have full motivation and have downloaded the PEX libs.

With That Written?

I’m now off to download NUnit and get rid of my Microsoft Tests.  I will prospectively go get XUnit too and start taking a solid look at that tool.

Data Generation Tools

While viewing this video Ben also mentioned a data generation project on CodePlex.  I couldn’t help but get a bit giddy about this fact.  So I checked it out.  Red Gate Generators – Red Gate also has a professional tool that they sell.  The libraries look pretty cool and solid, but I’ll have to review the library license before I know if I can use anything there or not.  I might have to just “not see” anything there.

Fini.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by: Adron
Posted on: 2/17/2009 at 7:56 AM
Tags: , , ,
Categories: IDEs, Software Tools, and Applications | Keeping Up | Website and Application Write-Ups
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Data Generation, TDD Like

On the tail of my recent additions to the data generator app I?ve been building I fell right into a need for the middle name to be generated along with standard names.  Here?s how I went about fixing this situation up with a sling at TDD & such.

The first thing I did was a quick refactor, to change the previous FullName class I had been using in my previous Part 1, Part 2, and Part 3 examples to FirstLastAmericanName.  Next I added the first test for the new class I?ll create right afterward.  The test looked like this.

   1:  [TestMethod]
   2:  public void VerifyFirstMiddleLastName()
   3:  {
   4:      var name = new FirstMiddleLastAmericanName();
   5:      Assert.IsTrue(name.FirstName.Length == 0);
   6:      Assert.IsTrue(name.LastName.Length == 0);
   7:      Assert.IsTrue(name.MiddleName.Length == 0);
   8:  }

The test, of course doesn?t build, let alone run.  So I jumped into this new class and created the following.

   1:  namespace Generator.Core.Model
   2:  {
   3:      public class FirstMiddleLastAmericanName : IFullName
   4:      {
   5:          public FirstMiddleLastAmericanName()
   6:          {
   7:              FirstName = string.Empty;
   8:              LastName = string.Empty;
   9:              MiddleName = string.Empty;
  10:          }
  11:   
  12:          public string MiddleName;
  13:   
  14:          #region IFullName Members
  15:   
  16:          public string FirstName { get; set; }
  17:   
  18:          public string LastName { get; set; }
  19:   
  20:          #endregion
  21:      }
  22:  }

So now I get a green light and I head right into the next test.

   1:  [TestMethod]
   2:  public void VerifyFirstMiddleLastNameObjectReturnsFromFactory()
   3:  {
   4:      var factory = new NameFactory();
   5:      var name = factory.Build(NameFactory.NameCulture.AmericanFirstMiddleLast) as FirstMiddleLastAmericanName;
   6:      Assert.IsTrue(name.FirstName.Length > 0);
   7:      Assert.IsTrue(name.LastName.Length > 0);
   8:      Assert.IsTrue(name.MiddleName.Length > 0);
   9:  }

I needed to then add the NameCulture enumeration.  This I added directly inside of the NameFactory.  I reviewed the other classes and name spaces I had setup.  At this time it seemed like the best place to put the enumeration.

   1:  namespace Generator.Core.Factories
   2:  {
   3:      public class NameFactory
   4:      {
   5:          public enum NameCulture
   6:          {
   7:              American,
   8:              AmericanFirstMiddleLast,
   9:              British,
  10:              French,
  11:              Japanese
  12:          }

Make note that is a very partial code segment. Also make note, I?m not adding anything except functionality right now for that second value AmericanFirstMiddleLast. I left off the rest of the class as it will be changing a lot and I?ll copy in each part as I change it. In this entry I?ll be refactoring so that the enumeration is a legitimately used part of the code base and how it reverberates through. The next thing was to get at least a basic method to get a green light for the test listed. What I ended up with to get to that point is as follows.

   1:  public IFullName Build(NameCulture nameCulture)
   2:  {
   3:      var name =
   4:          new FirstMiddleLastAmericanName
   5:              {
   6:                  FirstName = "test",
   7:                  MiddleName = "test",
   8:                  LastName = "test"
   9:              };
  10:   
  11:      return name;
  12:  }

Now it?s time to get some real randomness in the names.  I first wrote out a test.

   1:  [TestMethod]
   2:  public void VerifyFirstMiddleLastNameIsRandom()
   3:  {
   4:      var factory = new NameFactory();
   5:      var nameOne = factory.Build(NameFactory.NameCulture.AmericanFirstMiddleLast) as FirstMiddleLastAmericanName;
   6:      var nameTwo = factory.Build(NameFactory.NameCulture.AmericanFirstMiddleLast) as FirstMiddleLastAmericanName;
   7:   
   8:      Assert.AreNotEqual(nameOne.FirstName, nameTwo.FirstName);
   9:      Assert.AreNotEqual(nameOne.MiddleName, nameTwo.MiddleName);
  10:      Assert.AreNotEqual(nameOne.LastName, nameTwo.LastName);
  11:  }

This got me to a stage where I actually have a good test that will make sure I?m getting solid random names back.  After this test, with the red light received, I set out and applied these changes the Build method I created above.

   1:  public IFullName Build(NameCulture nameCulture)
   2:  {
   3:      var rand = new Random(DateTime.Now.Millisecond);
   4:      var entities = new GeneratorEntities();
   5:   
   6:      List<Names> firstNames = (from name in entities.Names
   7:                                where name.Type == "Female First Names" || name.Type == "Male First Names"
   8:                                select name).ToList();
   9:      List<Names> lastNames = (from name in entities.Names
  10:                               where name.Type == "Last Names"
  11:                               select name).ToList();
  12:      List<Names> middleNames = new List<Names>();
  13:   
  14:      middleNames.AddRange(firstNames);
  15:      middleNames.AddRange(lastNames);
  16:   
  17:      var firstMiddleLastAmericanName =
  18:          new FirstMiddleLastAmericanName
  19:              {
  20:                  FirstName = firstNames[rand.Next(0, firstNames.Count)].Name,
  21:                  MiddleName = middleNames[rand.Next(0, middleNames.Count)].Name,
  22:                  LastName = lastNames[rand.Next(0, lastNames.Count)].Name
  23:              };
  24:   
  25:      return firstMiddleLastAmericanName;
  26:  }

That should give me a full range of middle names.  Next step, that?s right, write a test.  After this test I noticed something though?

   1:  [TestMethod]
   2:  public void VerifyMultitudesFirstMiddleLastNameIsRandom()
   3:  {
   4:      var factory = new NameFactory();
   5:      var list =
   6:          factory.Build(NameFactory.NameCulture.AmericanFirstMiddleLast, 1000) as
   7:          List<FirstMiddleLastAmericanName>;
   8:   
   9:      for (int i = 0; i < list.Count; i++)
  10:      {
  11:          int compareUp = i + 1;
  12:          if (compareUp == list.Count)
  13:          {
  14:              compareUp = 0;
  15:          }
  16:          Assert.AreNotEqual(
  17:              list[compareUp].FirstName + list[compareUp].MiddleName + list[compareUp].LastName,
  18:              list[i].FirstName + list[compareUp].MiddleName + list[i].LastName);
  19:      }
  20:  }

Yeah, I needed to do some serious refactoring.  With this test, not even building, I jumped into resolving the Build methods.  During each of these stages of the refactor I made a point to run all of my tests, except the one above.  This way I assured I didn?t break anything else.  My final NameFactory code looked like what is below.  Take a good look at the following code, and below I?ll have listed a few of the major refactor changes I made.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using Generator.Core.Model;
   5:   
   6:  namespace Generator.Core.Factories
   7:  {
   8:      public class NameFactory
   9:      {
  10:          public NameFactory()
  11:          {
  12:              entities = new GeneratorEntities();
  13:              random = new Random(DateTime.Now.Millisecond);
  14:              firstNames = new List<Names>();
  15:              lastNames = new List<Names>();
  16:              middleNames = new List<Names>();
  17:              namesList = (from name in entities.Names select name).ToList();
  18:          }
  19:   
  20:          private readonly GeneratorEntities entities;
  21:          private readonly Random random;
  22:          private List<Names> firstNames;
  23:          private List<Names> lastNames;
  24:          private readonly List<Names> middleNames;
  25:          private readonly List<Names> namesList;
  26:   
  27:          public enum NameCulture
  28:          {
  29:              American,
  30:              AmericanFirstMiddleLast,
  31:              British,
  32:              French,
  33:              Japanese
  34:          }
  35:   
  36:          public IFullName Build(NameCulture nameCulture)
  37:          {
  38:              return CreateNames(nameCulture, 1)[0];
  39:          }
  40:   
  41:          public IFullName Build()
  42:          {
  43:              return CreateNames(NameCulture.American, 1)[0];
  44:          }
  45:   
  46:          public List<IFullName> Build(NameCulture nameCulture, int numberOfNames)
  47:          {
  48:              return CreateNames(nameCulture, numberOfNames);
  49:          }
  50:   
  51:          private List<IFullName> CreateNames(NameCulture nameCulture, int numberOfNames)
  52:          {
  53:              firstNames = GetFirstNames();
  54:              lastNames = GetLastNames();
  55:              
  56:              switch (nameCulture)
  57:              {
  58:                  case NameCulture.American:
  59:                      var firstLastAmericanName = new List<IFullName>();
  60:   
  61:                      for (int i = 0; i < numberOfNames; i++)
  62:                      {
  63:                          firstLastAmericanName.Add(MapName(new FirstLastAmericanName()));
  64:                      }
  65:   
  66:                      return firstLastAmericanName;
  67:   
  68:                  case NameCulture.AmericanFirstMiddleLast:
  69:                      middleNames.AddRange(firstNames);
  70:                      middleNames.AddRange(lastNames);
  71:   
  72:                      var americanFirstMiddleLastNames = new List<IFullName>();
  73:   
  74:                      for (int i = 0; i < numberOfNames; i++)
  75:                      {
  76:                          var fullName = new FirstMiddleLastAmericanName();
  77:                          fullName = MapName(fullName) as FirstMiddleLastAmericanName;
  78:                          fullName.MiddleName = middleNames[random.Next(0, middleNames.Count)].Name;
  79:                          americanFirstMiddleLastNames.Add(fullName);
  80:                      }
  81:   
  82:                      return americanFirstMiddleLastNames;
  83:   
  84:                  default:
  85:                      throw new ArgumentOutOfRangeException("nameCulture");
  86:              }
  87:          }
  88:   
  89:          private IFullName MapName(IFullName name)
  90:          {
  91:              name.FirstName = firstNames[random.Next(0, firstNames.Count)].Name;
  92:              name.LastName = lastNames[random.Next(0, lastNames.Count)].Name;
  93:              return name;
  94:          }
  95:   
  96:          private List<Names> GetFirstNames()
  97:          {
  98:              return (from name in namesList
  99:                      where name.Type == "Female First Names" || name.Type == "Male First Names"
 100:                      select name).ToList();
 101:          }
 102:   
 103:          private List<Names> GetLastNames()
 104:          {
 105:              return (from name in namesList
 106:                      where name.Type == "Last Names"
 107:                      select name).ToList();
 108:          }
 109:      }
 110:  }

The first thing I did was create the GetFirstNames & GetLastNames methods to clean up some of the junk that was in the Build methods.  Immediately I realized that I could make it even more performing by removing the actual queries against the database by simple returning the whole table into memory ? take a look at the last line of the constructor for that.  I completed that clean up, with appropriate lists being created.  Another thing I added was the mapper method.  I?m not 100% sure I like that method, but it sort of cleaned up things a little, but in turn it just doesn?t seem as elegant as it should be.

Overall I got it down to a single hit against the database & more in memory movements of the lists.  Also setup several private properties.  After a minute I thought, ?I?ll toss those setup some readonly members and save a bit of memory??  So I tried that and it worked out well.  I went through a number of other refactor steps and ended up with what I have listed above.  Next task was to see if my test would work.  So I uncommented my test and tried to run it.  It blew up with exceptions, so I dug in.

I immediately realized I had to do a little last minute refactor of my test also.  My final test ended up list this, which it appears to be, a bit more readable.  I might follow up though, with an entry cleaning this up even more.

   1:  [TestMethod]
   2:  public void VerifyMultitudesFirstMiddleLastNameIsRandom()
   3:  {
   4:      var factory = new NameFactory();
   5:      List<IFullName> list = factory.Build(NameFactory.NameCulture.AmericanFirstMiddleLast, 100);
   6:   
   7:      for (int i = 0; i < list.Count; i++)
   8:      {
   9:          int compareUp = i + 1;
  10:          if (compareUp == list.Count)
  11:          {
  12:              compareUp = 0;
  13:          }
  14:   
  15:          var fullName = list[compareUp] as FirstMiddleLastAmericanName;
  16:          var fullNameBase = list[i] as FirstMiddleLastAmericanName;
  17:   
  18:          Assert.AreNotEqual(fullName.FirstName + fullName.MiddleName + fullName.LastName,
  19:              fullNameBase.FirstName + fullNameBase.MiddleName + fullNameBase.LastName);
  20:      }
  21:  }

Hope that was useful and happy Presidents day at ya.  Fini.

Shout it kick it on DotNetKicks.com
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by: adron
Posted on: 2/16/2009 at 1:50 AM
Tags: , , ,
Categories: How-To, Samples, and Such
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (2) | Post RSSRSS comment feed

Getting Up To Date, Staying Up To Date?

I jumped in head first to get things updated.  Went after the ASP.NET MVC Engine, grabbed the toolkit for Silverlight, made sure the Silverlight SDK was updated, got the Deep Zoom Composer, Blend installed, made sure SP1 was installed, grabbed the WPF toolkit, and?   ok, at this point I lost track.  I think my home machine is fully updated.  Now the question is, are the other three machines updated?

Aaaaaaaaaaaaaaaaggggggggggggghhhhhhhhh!

Oh well, time to hit the ground running and get up to speed.  With some of the work I?ve been completing, I?ve seriously been contemplating a move to a new domain for my tech work (I already own it, so don?t go try to guess what it is an nab it!).  This way I can add it as a DBA (Doing Business As name) and get to work on expanding my reach in the community.

For now though, I just finished up my three part series I just posted and have started on some more entries in that vein.  I?ll also have some interesting and functional data generation utilities coming available soon.  Stay tuned!  ;)

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by: adron
Posted on: 2/15/2009 at 9:09 PM
Categories: Keeping Up | Just Stuff
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Test Driven Development Built Name Generator Part 3

Now it is time finally for the random name generation.  I’ve gotten everything into place, so it’s just a matter of grabbing the names and randomizing among them.  Just as a check, after Part 2 was finished I had a total of 98,572 rows of data, or simply 98,572 names.

Many times I would not write actual unit tests against the database as it breaks isolation.  This time though, I'm making an exception.  First I added a reference to Generator.Core, System.Data, and System.Data.Entity.  I then created a new unit test class called DatabaseTests and added the following test to get things rolling.  Keep in mind, since these tests break isolation, I don't write them in the red light green light idea of TDD.  My assumption is that they only prove the database is setup, running, the connection string is correct, and other minor configuration based and server based components.  In addition, yes, these are somewhat silly tests, such as the RetrieveFirstName() test has a bit of redundancy to it, but the point is to have an automated test to assure that the data has been moved into the database and is returning appropriately.

   1:  [TestClass]
   2:  public class DatabaseTests
   3:  {
   4:      [TestMethod]
   5:      public void RetrieveFirstFemaleName()
   6:      {
   7:          var entities = new GeneratorEntities();
   8:          var returnName = (from name in entities.Names
   9:                            where name.Type == "Female First Names"
  10:                            select name).FirstOrDefault();
  11:          Assert.IsNotNull(returnName);
  12:          Assert.IsTrue(returnName.Name != string.Empty);
  13:      }
  14:   
  15:      [TestMethod]
  16:      public void RetrieveLastName()
  17:      {
  18:          var entities = new GeneratorEntities();
  19:          var returnName = (from name in entities.Names
  20:                            where name.Type == "Last Names"
  21:                            select name).FirstOrDefault();
  22:          Assert.IsNotNull(returnName);
  23:          Assert.IsTrue(returnName.Name != string.Empty);
  24:      }
  25:   
  26:      [TestMethod]
  27:      public void RetrieveFirstMaleName()
  28:      {
  29:          var entities = new GeneratorEntities();
  30:          var returnName = (from name in entities.Names
  31:                            where name.Type == "Male First Names"
  32:                            select name).FirstOrDefault();
  33:          Assert.IsNotNull(returnName);
  34:          Assert.IsTrue(returnName.Name != string.Empty);
  35:      }
  36:   
  37:      [TestMethod]
  38:      public void RetrieveFirstName()
  39:      {
  40:          var entities = new GeneratorEntities();
  41:          var returnName = (from name in entities.Names
  42:                            where name.Type == "Female First Names" || name.Type == "Male First Names"
  43:                            select name).FirstOrDefault();
  44:          Assert.IsNotNull(returnName);
  45:          Assert.IsTrue(returnName.Name != string.Empty && returnName.Type == "Female First Names" ||
  46:                        returnName.Type == "Male First Names");
  47:      }
  48:  }

The next step now was to refactor the existing tests to prove the assembly of the name data transfer objects that would hold the first name and last name derived from the returned entity objects.

The first thing I did was break out the actual name objects into their respective files.  I did this using ReSharper, because it's super insanely simple with ReSharper.  After that I put the class files in the project that has the model.  Next I fixed the namespaces to Generator.Core.Model.  The class files after these changes looked like the code below.

IFullName.cs

   1:  namespace Generator.Core.Model
   2:  {
   3:      public interface IFullName
   4:      {
   5:          string FirstName { get; set; }
   6:          string LastName { get; set; }
   7:      }
   8:  }

FullName.cs

   1:  namespace Generator.Core.Model
   2:  {
   3:      public class FullName : IFullName
   4:      {
   5:          public FullName()
   6:          {
   7:              FirstName = string.Empty;
   8:              LastName = string.Empty;
   9:          }
  10:   
  11:          #region IFullName Members
  12:   
  13:          public string FirstName { get; set; }
  14:          public string LastName { get; set; }
  15:   
  16:          #endregion
  17:      }
  18:  }

NameFactory.cs

   1:  namespace Generator.Core.Model
   2:  {
   3:      public class NameFactory
   4:      {
   5:          public static IFullName Build()
   6:          {
   7:              return new FullName {FirstName = "TestFirst", LastName = "TestLast"};
   8:          }
   9:      }
  10:  }

Now that we have these files moved out to the project where all the munging will happen, I'm going to start the actual refactor of the tests.  What I've come up with for the test refactor is as follows.

   1:  using Generator.Core.Model;
   2:  using Microsoft.VisualStudio.TestTools.UnitTesting;
   3:   
   4:  namespace Factory.Tests
   5:  {
   6:      [TestClass]
   7:      public class FactoryNameBuilder
   8:      {
   9:          [TestMethod]
  10:          public void VerifyFullNameObjectInstantiatesWithNames()
  11:          {
  12:              var name = new FullName();
  13:              Assert.IsTrue(name.FirstName.Length == 0);
  14:              Assert.IsTrue(name.LastName.Length == 0);
  15:          }
  16:   
  17:          [TestMethod]
  18:          public void VerifyFullNameObjectReturnsFromFactory()
  19:          {
  20:              var factory = new NameFactory();
  21:              IFullName name = factory.Build();
  22:              Assert.IsTrue(name.FirstName.Length > 0);
  23:              Assert.IsTrue(name.LastName.Length > 0);
  24:          }
  25:   
  26:          [TestMethod]
  27:          public void VerifyFullNameIsRandom()
  28:          {
  29:              var factory = new NameFactory();
  30:              IFullName nameOne = factory.Build();
  31:              IFullName nameTwo = factory.Build();
  32:   
  33:              Assert.AreNotEqual(nameOne.FirstName, nameTwo.FirstName);
  34:              Assert.AreNotEqual(nameOne.LastName, nameTwo.LastName);
  35:          }
  36:      }
  37:  }

In the factory I removed the fake return and fleshed out the appropriate build method.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:   
   5:  namespace Generator.Core.Model
   6:  {
   7:      public class NameFactory
   8:      {
   9:          public IFullName Build()
  10:          {
  11:              Random rand = new Random(DateTime.Now.Millisecond);
  12:   
  13:              GeneratorEntities entities = new GeneratorEntities();
  14:   
  15:              List<Names> FirstNames = (from name in entities.Names
  16:                                        where name.Type == "Female First Names" || name.Type == "Male First Names"
  17:                                        select name).ToList();
  18:              List<Names> LastNames = (from name in entities.Names
  19:                                       where name.Type == "Last Names"
  20:                                       select name).ToList();
  21:              
  22:              FullName fullName = new FullName();
  23:              fullName.FirstName = FirstNames[rand.Next(0, FirstNames.Count)].Name;
  24:              fullName.LastName = LastNames[rand.Next(0, LastNames.Count)].Name;
  25:              
  26:              return fullName;
  27:          }
  28:      }
  29:  }

Run the tests now and you have a random name gold mine.  However, I wanted a bit more than that.  With another test added...

   1:  [TestMethod]
   2:  public void VerifyMultipleNameReturn()
   3:  {
   4:      var factory = new NameFactory();
   5:      List<IFullName> list = factory.Build(1000);
   6:   
   7:      for (int i = 0; i < list.Count; i++)
   8:      {
   9:          int compareUp = i + 1;
  10:          if (compareUp == list.Count)
  11:          {
  12:              compareUp = 0;
  13:          }
  14:          Assert.AreNotEqual(list[compareUp].FirstName + list[compareUp].LastName, list[i].FirstName + list[i].LastName);
  15:      }
  16:  }

...then the extra Build method.

   1:  public List<IFullName> Build(int numberOfNames)
   2:  {
   3:      var rand = new Random(DateTime.Now.Millisecond);
   4:      var entities = new GeneratorEntities();
   5:   
   6:      List<Names> FirstNames = (from name in entities.Names
   7:                                where name.Type == "Female First Names" || name.Type == "Male First Names"
   8:                                select name).ToList();
   9:      List<Names> LastNames = (from name in entities.Names
  10:                               where name.Type == "Last Names"
  11:                               select name).ToList();
  12:   
  13:      var names = new List<IFullName>();
  14:   
  15:      for (int i = 0; i < numberOfNames; i++)
  16:      {
  17:          var fullName = new FullName();
  18:          fullName.FirstName = FirstNames[rand.Next(0, FirstNames.Count)].Name;
  19:          fullName.LastName = LastNames[rand.Next(0, LastNames.Count)].Name;
  20:          names.Add(fullName);
  21:      }
  22:   
  23:      return names;
  24:  }

and now I have a strong verification that I get solid randoms for 1000 name pairs.  I bumped it up to 2000 too just for fun, and didn't hit duplicates until I put it up to 200,000.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by: adron
Posted on: 2/13/2009 at 7:47 AM
Tags: , , ,
Categories: How-To, Samples, and Such
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (2) | Post RSSRSS comment feed

File Extension ".svc" Annoyance!

REST Services are intended to have clean URIs.  So when Microsoft started pushing their REST over WCF Services I thought, "SWEET", lemme get some of that.  While working with these services though a problem arose, "http://www.soemthing.com/someservice.svc" is NOT a very clean REST URI.  I just don't dig nonsense like this so I went digging about for a fix.

Sure one can go into the whole process of doing some URI rewriting, but I wanted something fast, simple, and deployed in about 10-15 minutes.  HTTP Modules to the rescue!

First I created a regular class project and created a class file called RestUriCleanup.cs.  I then added the following code into the class file.

   1:  using System.Web; 
   2:   
   3:  namespace StandardServicesApplication
   4:  {
   5:      public class RestUriCleanup : IHttpModule
   6:      {
   7:          #region IHttpModule Members 
   8:   
   9:          public void Init(HttpApplication context)
  10:          {
  11:              context.BeginRequest +=
  12:                  delegate
  13:                      {
  14:                          var ctx = HttpContext.Current;
  15:                          string path = ctx.Request.AppRelativeCurrentExecutionFilePath; 
  16:   
  17:                          int i = path.IndexOf('/', 2);
  18:                          if (i > 0)
  19:                          {
  20:                              string svc = path.Substring(0, i) + ".svc";
  21:                              string rest = path.Substring(i, path.Length - i);
  22:                              string qs = ctx.Request.QueryString.ToString();
  23:                              ctx.RewritePath(svc, rest, qs, false);
  24:                          }
  25:                      };
  26:          } 
  27:   
  28:          public void Dispose()
  29:          {
  30:          } 
  31:   
  32:          #endregion
  33:      }
  34:  }

In the web.config file I added the following configuration settings in the httpModules Section.  Notice that the ScriptModule is already included as a handler as of .NET 3.5.

   1:      <httpModules>
   2:        <add name="RestUriCleanup" type="StandardServicesApplication.RestUriCleanup, StandardServicesApplication" />
   3:        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
   4:      </httpModules>

Now at this point things can get tricky.  Depending on the version of IIS you'll possibly need to make some changes at this point.  IIS 6.0 is setup as follows.  Open up the management console and right click the app or virtual directory and click on properties.  Click on the Home Directory tab and click on the Configuration button.

Once you have that dialog open then Insert a new wildcard application map.  Find and select the aspnet_isapi.dll file.  It should be located here:  "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll"

Make SURE when you are adding the file, do NOT leave or check the Verify that file exists box.  I repeat, do NOT check it.

That's it.  You now have your *.svc extension removed from your services file and should have nice clean REST based URIs.  This process is slightly different on IIS 7 w/ Vista or Windows Server 2008.  First off you do NOT need to create a wildcard on IIS 7 but you will need to go in and create a setting in IIS for the HTTP Module.  If interested let me know and I'll add another blog entry for that, but for now...  this is how one gets rid of the *.svc extension.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by: adron
Posted on: 2/11/2009 at 10:21 AM
Tags: , , , , ,
Categories: How-To, Samples, and Such
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Difficult Even Among Ourselves, Developers

Ugh.  I was interrupted after a solid 30+ minutes of concentration while working through a recursion problem.  It all screeched to a stop.  I lost track of what was going on.  Everything I had been concentrating on was immediately dumped as a request for my attention came to assist in doing something else, something completely unrelated.  That 30 minutes of work was completely erased, only the skeletal remains left in actual code, that at least built, but was not working appropriately.  Everything I held in memory, the multiple memories I was using in my gray matter, each was catastrophically purged.

I then completed this alternate, non-related development task.  It was fun, I enjoyed it.

Now knew I?d have to step back into that 30 minutes of memory, and spend another 10 minutes just to get back to the point where I could continue with what I was working on.  I now faced the challenge of putting all of those multiple memories back into place, of remembering each and every piece of data that I held in my mind, and assure that each connection was once again related appropriately in my gray matter.

What was originally about 20-30 more minutes of work before the interruption, now grew into the 20-30 minutes plus the 10 or so minutes taken to reacquire the appropriate working state.  This happens to every developer, sometimes we can maintain our state, but mostly we?re completely ruined and must start by rebuilding our working state.

How many project managers, significant others, and generally the planet just disregards this fact?  Maybe people just don?t understand, even though it is inherent to the human process of thought.  Simply, anything requiring attention and thought cannot be interrupted without devastating consequences to the intended end result.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by: Adron
Posted on: 2/8/2009 at 2:13 PM
Tags: , ,
Categories: Discussion Points or Ideas | Rants
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

hack some code proposal

I'm not sure what the name should be, not sure how or where to go about this yet, but would love to hold some lunch time sessions and put some code together.  The idea I have revolves around small team meet ups during lunch for about 30-45 minutes.  A lead will be chosen to cover a topic and some functional code will be put together per that leads direction.

Of course this would mean whoever would come would need a laptop or other device of sorts to do development on.  They would also need appropriate tools.  If we?re working with C# something like VS.NET, if we?re working with Java then Eclipse, or with Erlang we?d need your favorite text editor.  Technically, I guess any of these could be built in a text editor, and the point isn?t what it is built in but to cover the language or specific skill topic.

Anyone else have any ideas?  I?ll be looking and talking to establishments around town that could or would host something like this.  Next I?ll just need to figure out who and what topics, which anyone could chime in on.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by: adron
Posted on: 2/7/2009 at 9:38 AM
Tags: , , ,
Categories: Keeping Up | Discussion Points or Ideas
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Test Driven Development Built Name Generator Part 2

Alright, time to tear into getting that name data imported into the database.  To review the previous entry check out part 1.

Next I set the Windows App Project I added in part 1 to be the startup project.  After that I created a screen that looks like the image below (click to see larger image).

Also add an open file dialog control.  Next double click on the Process button and add the following code.  Make sure not to miss the instantiation of the names list property in the initialization.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.IO;
   4:  using System.Text.RegularExpressions;
   5:  using System.Windows.Forms;
   6:   
   7:  namespace TextFileImporter
   8:  {
   9:      public partial class Form1 : Form
  10:      {
  11:          public Form1()
  12:          {
  13:              InitializeComponent();
  14:              Names = new List<string>();
  15:          }
  16:   
  17:          private List<string> Names { get; set; }
  18:   
  19:          private void buttonProcessFile_Click(object sender, EventArgs e)
  20:          {
  21:              if (openFile.ShowDialog() == DialogResult.OK)
  22:              {
  23:                  if (File.Exists(openFile.FileName))
  24:                  {
  25:                      TextReader tr = new StreamReader(openFile.FileName);
  26:                      textProcessedFile.Text = tr.ReadToEnd();
  27:                      tr.Close();
  28:                      // Cleanup text.
  29:                      textProcessedFile.Text = ProcessName(textProcessedFile.Text);
  30:                  }
  31:                  else
  32:                  {
  33:                      MessageBox.Show("Select a file that exists.", "File doesn't exist.", MessageBoxButtons.OK,
  34:                                      MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
  35:                  }
  36:              }
  37:          }
  38:   
  39:          private string ProcessName(string textBody)
  40:          {
  41:              var regex = new Regex("[^A-Z']+");
  42:              textBody = regex.Replace(textBody, " ");
  43:   
  44:              foreach (var s in textBody.Split(Convert.ToChar(" ")))
  45:              {
  46:                  Names.Add(s);
  47:              }
  48:   
  49:              return textBody;
  50:          }
  51:      }
  52:  }

With the beauty of those regular expressions the files will parse appropriately in about 1-5 seconds.  Next we need a way to write this data into the database.  I did this by adding a Class Project named Generator.Core and added a folder called Model.

In the Model Directory I added a new ADO.NET Entity Framework Model.  The images below show my steps through the wizard.

At the end I also deleted the Class1.cs file from the project.

I then added a reference to the Generator.Core Project in the TextFileImporter Application Project.  Next add an application configuration file to the TextFileImporter Project with the following configuration section.

   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <configuration>
   3:    <connectionStrings>
   4:      <add name="GeneratorEntities" connectionString="metadata=res://*/Model.Data.csdl|res://*/Model.Data.ssdl|res://*/Model.Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\carriage;Initial Catalog=Generator;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
   5:    </connectionStrings>
   6:  </configuration>

At the last minute, and to make sure anyone following along with these instructions is really paying attention, I made a minor change to the database table.  I changed the column Type to a data type of nvarchar(50).  I then updated the Data.edmx file in the Generator.Core Project.  To do this open up the file and right click anywhere in the white space around the model.  Then click on Update Model from Database....  Now, as things go, the stupid model didn't update for me.  This is something that seems to occur very frequetly with the ADO.NET Entity Framework Model Files.  Since it didn't update I had to manually right click on the Type Property and then set the Type of the Type (yes I know, redundant naming) to String.

So if anyone from Microsoft is reading this, the entity model files are STILL broken for some reason.  It's been about 6 months since the entity framework and such where in beta and they still behave in many ways like they're in beta, please fix this.  k thx bye.

Next I double clicked the Save (buttonSaveToDb) button and entered the following code to write the file data into the database.

   1:  private void buttonSaveToDb_Click(object sender, EventArgs e)
   2:  {
   3:      if (comboNameType.Text == string.Empty)
   4:      {
   5:          MessageBox.Show("Select a type of name.", "Select Name", MessageBoxButtons.OK,
   6:                          MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
   7:      }
   8:      else
   9:      {
  10:          // TODO Write to DB.
  11:          var generatorEntities = new GeneratorEntities();
  12:          foreach (var name in Names)
  13:          {
  14:              var newName =
  15:                  new Names
  16:                      {
  17:                          NameId = Guid.NewGuid(),
  18:                          Name = name,
  19:                          Type = comboNameType.Text
  20:                      };
  21:              generatorEntities.AddToNames(newName);
  22:          }
  23:          generatorEntities.SaveChanges(true);
  24:      }
  25:  }

Then a quick reference and that finishes up the TextFileImporter Project.

   1:  using Generator.Core.Model;

Now mind you, I broke TDD principles because this app was merely a utility, would be used a few times, and above all breaks isolation and crosses boundaries.  A good rule is to not write tests for a project like this.  Unless you feel it absolutely necessary then I wouldn't even think of it.

Now run the application and open and import each of the three files into the database.  This will give us the data we need for the next step of our random name generator.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted by: adron
Posted on: 2/5/2009 at 7:18 AM
Tags: , ,
Categories: How-To, Samples, and Such | My Projects
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (3) | Post RSSRSS comment feed