Cory Foy

Tuesday, May 31, 2005

Using JUnit with Eclipse IDE

One of my new projects at work is to ramp up a way to get our J2ME stuff under tests with JUnit. I've used NUnit quite a bit now, but hadn't had a chance to get up to speed with JUnit. I found a nice article on getting JUnit running with Eclipse. The article is a tad old, but pretty straightforward. If you want a quick rundown, here it is:


  • Install Eclipse from eclipse.org.

  • File->New->Project...->Java Project

  • Name it whatever you want, and choose Next

  • Click on the "Libraries" tab

  • Click "Add External JARs..."

  • Choose $ECLIPSE_HOME/plugins/org.junit_3.x.x (or whatever the junit version is)/junit.jar

  • Click Finish



Now your project has the JUnit jar on the classpath. Creating a test is very easy:


  • File->New->JUnit Test Case

  • Name it whatever you want - say TestThatWeGetHelloWorldPrompt to borrow from the article. Choose the method stub for main and "Add TestRunner Statement for text ui"

  • Create a basic testWorks - public void testWorks(){assertTrue(true);}

  • Save the file (autocompile)

  • Choose Run->Run As->JUnit Test

  • Watch the Green Bar pass

Thursday, May 26, 2005

Tracking bluetooth devices

A very interesting article came out today on the SecurityFocus web site talking about Bluetooth security. More interesting than the security portions was the discussion of privacy concerns with the devices.

For example, Bluetooth signals, like GPS and other devices, can be triangulated using some fairly basic equipment. Buy using some 2.4GHz amplifiers and a standard Bluetooth Dongle you can achieve connection ranges of between 500m and 1.5km. And since most Bluetooth devices allow you to see their unique IDs (even if they don't let you access the services on the device), you can fairly easily track the devices.

And all of *that* is pretty whoop-de-doo until you realize that more and more stuff is coming out with Bluetooth. Like cars. So if your car is broadcasting its unique ID, and a city wanted to, they could track you around.

Perhaps the most visible project is the LOCA (Location Oriented Critical Arts) project. They have taken a city and hooked up a series of Bluetooth receivers which watch the city and send SMS messages to Bluetooth phones like:

We are currently experiencing difficulties monitoring your position: please wave you network device in the air.


or

We've seen you pass this spot 6 times in the past 3 days


Which, while fun, is spooky at the same time.

Anyway, all in all a very interesting read.

Sunday, May 22, 2005

How to defeat those annoying crane game machines

How many times have you been in the arcade, late at night, with a fistful of quarters, a steady hand, and a hankering for a stuffed toy? Yet, with all your skill and might (and quarters), do you still walk away empty handed?

Well, a handy solution has been found in Indiana. In a Wal-mart no less. Here's what you need:


  1. One crane game machine, with something you want

  2. A slightly not-so-busy Wal-mart

  3. A juice box

  4. One slightly cranky 3-year old



Got all that? Good. Now, straight from CNN are the next steps:


After she denied him money to play the vending-machine game, he threw a juice box and climbed into the machine while his mother picked it up.

"Within two seconds he had climbed through the hole, into the chute and pushed the door shut so we couldn't get him out," she said.


In case you were wondering, here is the outcome:



(Picture from the CNN story)

Wednesday, May 18, 2005

Binding ASP.NET Controls to QueryString parameters

I was flipping through Code Project today, and came across an article by Piers7 called Declarative QueryString Parameter Binding in ASP.NET. While I liked his approach, it required you to wrap all of your controls you wanted to bind with CustomAttributes.

I had to do something similar (probably we all have), but I don't have the overhead of the custom attributes. Granted, I am only binding TextBoxes, but if that's all you need, this works very well.

First, in my Page_Load, I check for PostBack listen for PreRender:


private void Page_Load(object sender, EventArgs e)
{
 if(!Page.IsPostBack)
 {
  this.PreRender +=
   new EventHandler(CommonMobilePage_PreRender);
 }
}


Now that we are in PreRender, we can call the method to check the controls. This will be a recursive call, since controls can hold other controls.


private void CommonMobilePage_PreRender(object sender, EventArgs e)
{
  BindControlsToURLRecursive(this.Controls);
}


Finally, we sprinkle a bit of reflection in to do the binding.


protected void BindControlsToURLRecursive(ControlCollection collection)
{
 foreach(Control control in collection)
 {
  if(control.GetType().Equals(typeof(System.Web.UI.MobileControls.TextBox)))
  {
   System.Web.UI.MobileControls.TextBox tb =
    (System.Web.UI.MobileControls.TextBox)control;

   //See if the field value is in the querystring
   if(Request.QueryString[control.ID] != null)
   {
    tb.Text = Request.QueryString[control.ID];
   }

  }

  if(control.HasControls())
  {
   BindControlsToURLRecursive(control.Controls);
  }
 }
}


Note that this is the code I use for my MobileControls, but the same concept would apply to WebControls.

Tuesday, May 17, 2005

You got it?

A funny little quote from one of the Vulnerability Development lists I'm on. The poster was talking about doing a remote exploitation of a web server:


So, I am basically thinking, i overflow EIP with an address that JMP's -260 to the beginning of the Authorization header. The Authorization header then contains my Stage1 shellcode that starts searching down the stack for my Stage2 shellcode which it will find about 2k down the stack in the GET request.....

I hope somebody understands what the hell i am talking about....


Yep, I feel that way sometimes too. :)

Monday, May 16, 2005

CharJUG Meeting - Hibernate with Gavin King

This week's Charlotte Java User's Group meeting is on Hibernate with Gavin King of JBoss. Gavin founded the Hibernate project and is the author of /Hibernate in Action/.

As always, the meeting starts at 6:30pm, and is being held at my employer's offices. More information and directions can be found on the CharJUG web site.

Sunday, May 15, 2005

OT: Not responding to 911 Calls

Before my wife and I moved to Charlotte, I spent nearly 7 years with the Fire Department in Lutz, FL. Our particular station, averaged 4-5 calls a day, though some days we were *much* busier. We had our share of people who called us, sometimes frequently, with made-up problems. People who were just lonely and had no one else to turn to, so they called 911. Or people who just wanted rides to the hospital. We knew some of the addresses, and cases, by heart. And when the call would come in, we would go, rain or shine, because that was our duty.

Recently in Pinellas County, which is across the bay from Tampa, two paramedics decided that they *didn't* need to go on a call. A "frequent flyer" called 911 to report she had been sexually assaulted. These two medics, both veterans, decided on their own that they did not need to go to the call, and told their dispatch that maybe they should send the cops over.

Nobody caught the fact that they didn't go until a recent review of run sheets. The reviewer noticed the medics didn't put an arrival time down on their form, and found out that was because they didn't go. After finding out the cause, the two were fired on the spot.

A recent editorial in the St. Pete Times hits on pretty much how I feel. When you call 911, and you report that you are injured, you expect someone to come to your house. Granted, if the dispatcher determines your call to not be a priority call ("I have a bug in my ear!"), it may take a while (up to an hour by the dispatching standards we worked under) - but that wasn't the case here. Their dispatchers determined that the lady who called required Paramedic level medical assistance, and they turned it down.

We've all done stupid things in our careers. Maybe we skipped the oxygen bottle check during morning routine and so once on scene we had to get the spare out of the truck. Or maybe we read the map wrong on the way to the person's house. Or maybe we thought to ourselves, "Here we go again, I hate going to so-and-sos house" before we headed off. But to not go, to not even show up to see what is going on is so amazingly stupid that no one deserves to be fired and stripped of their licenses more than these two.

Thursday, May 12, 2005

If Microsoft bought Red Hat, and IBM bought Novell...

Just finished reading a blog entry over on ZDNet about some speculation over the warming trend between Red Hat and Microsoft.

While it certainly seems beyond ludicrous to me, it did seem funny to think about if Microsoft bought Red-Hat, and IBM in kind bought Novell (acquiring rights to Mono along the way) what kind of a battle that would bring about.

I personally don't think it would make sense for Microsoft to buy out Red Hat. I mean, what would they gain? The author says it would happen because Red Hat is possible an IP hot potato, but it seems silly to me. The thought that Microsoft would make some sort of a preemptive strike - I don't know.

Tuesday, May 10, 2005

It's lonely being in the UK

As I was flipping through The Register this morning I came across an article about a slight problem with Google Maps. Look at the following screen grab:



Yes, that is the U.K., zoomed out and panned over just far enough to see the U.S. Maybe there's something someone isn't telling us?

Sunday, May 08, 2005

Getting Tomcat and Apache running on Gentoo - Day 1

(Edit 3/6/2006: This is wrong, in that it still uses mod_jk2. This is not a how-to for mod_jk. Rjae Easton has a good post on getting mod_jk working that you should head over to if that's what you need. - Cory)

I don't really anticipate this taking that long, but figured I would document it out since I can't
find much documentation on this.

So the basic premise is that I have a Gentoo Linux box that I am hosting some sites on. I want to get Tomcat up and running so I can do some dynamic content with it. I already have Apache running, which I installed by doing the very difficult:

emerge apache2

I also had to do some config changes because I am running virtual hosts. I made sure that all this was fat and happy (which it was), so Apache is good.

Next I installed Tomcat. This also was just:

emerge tomcat

which hummed along quite nicely and plunked it on there. Doing a netstat -a showed me that it was running and listening, and browsing to localhost:8080 showed me the nice Tomcat install page.

Now, the final step is to get Apache to send my jsp's to Tomcat for processing. Much of the documentation I found revolved around mod_jk2 which has been deprecated for mod_jk. Of course, nobody really talks about mod_jk (which is why you are reading this!).

I started with a document called HOWTO: Apache2 and Tomcat5 from the Gentoo Wiki. The big difference was in mod_jk, which I built from source using the directions from this article on something called SnipSnap. My workers.conf was nearly identical to the workers.properties listed in the HOWTO article. For the apache2.conf, I moved it over to my vhosts/vhosts.conf file, which looks basically like:


################# Named VirtualHosts
NameVirtualHost *:80

LoadModule jk2_module extramodules/mod_jk2.so
JkSet config.file /etc/apache2/conf/workers2.conf

<VirtualHost *:80>
ServerName www.mysite1.com
ServerAlias mysite1.com *.mysite1.com
DocumentRoot /var/www/localhost/htdocs
<Location /*.jsp>
JkUriSet worker ajp13:localhost:8009
</Location>
</VirtualHost>

<VirtualHost *:80>
ServerName www.mysite2.com
ServerAlias mysite2.com *.mysite2.com
DocumentRoot /var/www/mysite2/htdocs
<Directory /var/www/mysite2/htdocs>
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>


At that's where I'm at. Both of my sites are still up, and sending a request to mysite1.com/test.jsp (a simple jsp) appears to do something, but it isn't quite working yet. Right now the page just sits there and spins, but I don't see anything funky in the server logs to help track it down. I'll try some more tomorrow.





UPDATE: So it turns out that the only thing I had wrong was my workers2.conf file. I had:


[channel.socket:localhost:8009]
port=8009
host=mysite1.com
type=ajp13


which was resolving the site name to the public IP address, which, of course, is firewalled and had port 8009 blocked. Changing it to:


[channel.socket:localhost:8009]
port=8009
host=localhost
type=ajp13


fixed it, and all is well with the site! Yea, no day 2 necessary!

Monday, May 02, 2005

Code Camp Follow up

Quick follow up from the code camp. We got an email with the aggregate of comments from the attendees. Overall some good comments for the code camp that will hopefully let them bring it back next year. As for me it was an interesting mix - one that said I was all over the place and had no depth, one that said it was pure presentation, and one said that they preferred sessions where the presenter didn't go through code step by step.

It's hard because it was such a tough topic to cover. Most of the people in the room had not heard of NUnit before, nor attended the NUnit overview session just before mine. So because of that I had to spend a lot of time introducing the NUnit and Unit Testing concepts. I learned that next time I have to push through the stuff I list as prerequisites so I can get to the meat faster, and spend more time going in depth with it.

Also a funny aside. The night before the speakers had dinner together, and someone mentioned to me that the crowd liked animations in the powerpoint. I kind of blew it off thinking that of course people find them interesting. But then, during Brady's presentation on SOA, he pops up this graphic with arrows signifying a repeating process, and when the arrows start to spin in a circle the entire room breaks out into applause. Funny stuff.