Category: Eclipse, IDE, Maven Gene De Lisa @ 9:46 am — Comments (0)
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

The m2Eclipse plugin has been accepted into the Eclipse Foundation.  Perhaps this will push development along. Already there is a ton of new features in the current release - along with decent documentation!

Thanks!

The update site at codehaus is still alive but it looks like the new versions will be at

http://m2eclipse.sonatype.org/update/

and for those who wish to be bleeding edge the snapshots are at

http://m2eclipse.sonatype.org/update-dev/

Category: Spring Framework Gene De Lisa @ 10:42 am — Comments (0)
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
Loading ... Loading ...

Let’s say you wrap stored procedures with Spring’s StoredProcedure class like this:

@Repository

public class SPGetWhatever extends StoredProcedure {

   @Autowired

   public SPGetWhatever (DataSource dataSource)etc.

That @Repository annotation like @Component and @Service allows you to tell Spring to scan the
classpath and instantiate them without you having to write a bean element in your Spring XML file.
You can even inject dependencies like the DataSource with the @Autowired annotation as you see here.

You enable it with this in your XML config file:

schema stuff...

<context:annotation-config />

<context:component-scan base-package="com.rockhoppertech.someproject" />
setup the datasource etc.

It will recursively scan the package you specify for these annotations. You will get a bean in your context
with the simple classname. (You can override that if you want).

N.B. this used to be aop:component-scan but it was moved.

Then in some DAO you can do this:

@Repository

public class MyDaoImpl implements MyDao {

@Autowired(required=true)

SPGetWhatever sp;

Pretty cool. Unless you use AOP - and hey, you’re just not hip and happenin’ if you’re not right?

<aop:aspectj-autoproxy/>

Turn the logging level up and you will indeed see Spring create the SP. But then it will fail when creating your
DaoImpl saying that there is no bean that satisfied the dependency on the SP class. But two lines above that
it just said it has the bean in the context. WTF?

Here’s a clue. Do this:

<aop:aspectj-autoproxy proxy-target-class="true"/>

You’ll see that Spring’s StoredProcedure class does not have a default constructor so there is a problem creating the proxy.

What RJ says in his Intro to Spring 2.5 article about StoredProcedures is that you can have them implement an interface blah blah fishcake…
Something like this.

@Repository

public class SPGetWhatever extends StoredProcedure implements SomeBloodyInterface {

etc.

Sure enough that solves the problem. OK, interfaces are good.
A glass of wine is good too. Drinking a vat of wine will kill you.

Category: DB, Spring Framework, Testing Gene De Lisa @ 7:08 am — Comments (0)
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

I’ve been taking a legacy codebase, putting testing in place and refactoring. For DB access I retrofitted the persistence classes to be injected via Spring with a DataSource instead of looking them up via JNDI. IN my Spring context I defined a PlatformTransactionManager.

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
scope="singleton" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />

<!-- has a property named dataSource which we autowire -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
autowire="byName" />

Then I refer to it in the the test case class.

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class MyTestCases extends
   AbstractTransactionalJUnit4SpringContextTests {

etc.

Sure enough the console blathered on about starting and rolling back the transaction. The problem is, it wasn’t. It was just saying it did.

How do I know? I checked.

int nrowsInMyTable;

@BeforeTransaction
public void onSetUpInTransaction() throws Exception   {
   nrowsInMyTable = countRowsInTable("SomeTable");
}

@AfterTransaction
public void verifyFinalDatabaseState()   {
   int rows = countRowsInTable("SomeTable");
   assertEquals(nrowsInMyTable, rows);
}

So what was the problem?

I was doing this like every other J(2)EE programmer.

connection = this.dataSource.getConnection();

But with Spring you need to do this instead to associate the transaction thread with the datasource.

connection = DataSourceUtils.getConnection(this.dataSource);

and

DataSourceUtils.releaseConnection(conn, this.dataSource);

Then it worked.

Category: Selenium, Spring Framework, Testing Gene De Lisa @ 6:43 am — Comments (1)
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

In a previous post I wrote about using JUnit4 with Selenium.

So how about injecting your JUnit run listener via Spring?

My first attempt modified my runner to be a subclass of SpringJUnit4ClassRunner

Then I created the usual XML spring config file but named it after my test class name. So for

FooTests I created FooTests-context.xml. If you want another name you can pass that into
@ContextConfiguration.

Here is a bit of it:

<bean id="Selenium"
	class="com.thoughtworks.selenium.DefaultSelenium">
	<constructor-arg>
		<value>localhost</value>
	</constructor-arg>
	<constructor-arg>
		<value>4444</value>
	</constructor-arg>
	<constructor-arg>
		<value>*firefox</value>
	</constructor-arg>
	<constructor-arg>
		<value>http://localhost:9080/myapp/</value>
	</constructor-arg>
</bean>

Here is is what I have for the test cases

@RunWith(SpringRunner.class)
@ContextConfiguration

public class FooTests() {

   @Autowired
   private Selenium selenium;

  @Test
   public void whatever() {}
}

and the JUnit 4 listener:

public class SpringRunListener extends RunListener {
   @Autowired // wishful thinking
   private Selenium selenium;

etc.

So there is the problem. Spring will autowire testcases but not the runners nor the listeners.

What is the Spring way? Forget the JUnit RunListener. You write a Spring TestExecutionListener.
But that’s not autowired either so you have to grab your beans yourself. Luckily you have access to
the Spring context.

public class MyTestExecutionListener extends AbstractTestExecutionListener {
   private static final Log logger = LogFactory.getLog(MyTestExecutionListener.class);
   Selenium selenium;

   @Override
   public void beforeTestMethod(TestContext testContext) throws Exception {
      // not autowired so go grab it
      selenium = (Selenium) testContext.getApplicationContext().getBean("Selenium");
   }

   @Override
   public void afterTestMethod(TestContext testContext) throws Exception {
      Throwable t = testContext.getTestException();
      if (t != null) {
         report(t); // go ahead and do the screendump using selenium
      }
   }

and to make your listener get used you add it to the list of test execution listeners in your test classes.
I put it in the superclass of my test classes.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners(
{
MyTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class
})

public class FooTests ...

But you say, what about that new @Configurable annotation?
Can’t you use DI on an object that Spring does not instantiate?
Stay tuned.

Category: Selenium, Testing Gene De Lisa @ 10:34 am — Comments (2)
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5 out of 5)
Loading ... Loading ...

The Selenium website seems to be biased towards TestNG. That’s fine but I’m using JUnit4.

One nice feature of the selenium testcase class is to produce a screendump for failures. But it’s a JUnit 3 class; it seems they have not noticed that JUnit4 exists.

So how to get a screendump with a JUnit4 test case? Wrapping and delegation works but I wanted to do it directly.

In JUnit4 you can install a Listener that is notified at test execution checkpoints. Since I care about only failures I defined it like this:

public class MyRunListener extends RunListener {
   @Override
   public void testFailure(Failure failure) throws Exception
   { }
}

To make JUnit4 use your listener you need to install it with a Runner. Here is a simple one that gets the job done.

public class MyRunner extends JUnit4ClassRunner {
   private MyRunListener myRunListener;

   public MyRunner(Class<?> c) throws InitializationError {
      super(c);
      myRunListener = new MyRunListener();
   }

   @Override
   public void run(RunNotifier rn) {
      rn.addListener(myRunListener);
      super.run(rn);
   }
}

Then in the superclass of my tests I use the RunWith annotation and specify my runner. Since I want to do the selenium setup only once per class I use the JUnit4 BeforeClass and AfterClass annotations. In production I’d probably start the server externally though. Or if I use Selenium userextensions.

@RunWith(MyRunner.class)
public class MyBaseTest {
   protected static Selenium selenium;
   private static SeleniumServer seleniumServer;
   private static boolean startSeleniumServer = false;

@BeforeClass
public static void startUp() {
   if (startSeleniumServer) {
      try {
         SeleniumServer.setReusingBrowserSessions(true);
         seleniumServer = new SeleniumServer();
         seleniumServer.start();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   selenium = new DefaultSelenium(serverHost, serverPort,
   browserStartCommand, browserURL);
   selenium.start();
   selenium.setBrowserLogLevel(SeleniumLogLevels.INFO);
}

@AfterClass
public static void cleanUp() {
   if (startSeleniumServer)
      seleniumServer.stop();
   selenium.stop();
}

etc.

Finally in my listeners testFailure method I get the Selenium instance from the base test class.

Selenium selenium = MyBaseTest.getSelenium();

It works!

In a future post, let’s see if I can use Spring for dependency injection of that selenium instance.

Category: DB, News, Weblogic Gene De Lisa @ 1:46 pm — Comments (0)
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Sun Microsystems Announces Agreement to Acquire MySQL, Developer of the World’s Most Popular Open Source Database

And then Oracle announces that it’s buying BEA. (Will it become BEAL? Bill, Ed, Alfred and Larry?)

Category: Eclipse, JavaFX Gene De Lisa @ 9:26 am — Comments (0)
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3 out of 5)
Loading ... Loading ...

There is a plugin for Eclipse for developing JavaFX. The official installation instructions are
ok to a point. You do get the plugin installed. But then what? I hope to save you a bit of time with this post.

Create a new Java project. You don’t have to add anything to the classpath.

I have a preference set to create the src/bin folders for my java projects. If that is your preference, create your JavaFX files in src. If you are using the project as the source root, then put them there. If you create a package, put them in there. Just like regular Java files. So, how to create them?

Select your project (or project/src) in the package explorer and open the New menu. (control-n). There is a category for JavaFX and one entry under it to create a JavaFX file. Check the parent folder is correct (that’s why you selected the project in the first place) and change File1.fx to whatever you want like YoWorld.fx.

So what happened? You get a nice empty file. No boilerplate code to delete :) You also get your classpath set correctly for JavaFX. Take a look at your project’s classpath to verify.

Type in your script. Notice that command line completion works. Here’s a simple one.
(I live 9 miles from South Philly)

YoWorld.fx

import javafx.ui.*;

Frame {
   title: "JavaFx Yo World"
   visible: true
   content: Label {
      text: "Yo World!"
   }
}

Make a syntax error and you get the familiar red Xs - just like Java errors.

Now what? How do you run it?
The hard way: Create a new Java run configuration.
Run->Open Run Dialog
Choose Java Application and press the New button
Set net.java.javafx.FXShell as the main class.
Choose the arguments tab and type the name of your script e.g. YoWorld

Ok, that works. But for each script you need to change the script name in the argument tab.
A bit of a pain.

Here is an easier way:
Run->Open Run Dialog
Choose JavaFX Application and press the New button. Name it something like JavaFX :)
What you gain with this is you don’t have to remember that the main class is
net.java.javafx.FXShell
Choose the arguments tab.
Now under Program Arguments do not type the script name. Instead press the variables button right under the text area. I use resource_name. Choose that and when you press OK you should see just ${resource_name} in the text area. Press Apply and OK.

Now, in Package Explorer View choose your script. Go to the Run menu and choose your launch configuration. It should run. Choose another script. That script should now run. Choose nothing (e.g. click on the project). You should get an error dialog.

Extra convenience: edit your run configuration again and choose the Common tab. You can
have your JavaFX run config as a favorite at the top of the Run menu by choosing Display in Favorites Menu->Run.

Category: Web Gene De Lisa @ 5:07 pm — Comments (0)
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

One of the food related sites I visit has a nice layout and presentation in general. So, I took a look at their source. I had to laugh when I saw the CSS conditional. (I had to do something similar for this blog’s theme).

ie css file

Category: IDE, News Gene De Lisa @ 5:02 pm — Comments (0)
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Netbeans version 6 final has been released.
View announcement

Category: microformat Gene De Lisa @ 8:39 am — Comments (0)
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

I use microformats whenever I can.

This blog uses hCard, hAtom, and geo

If you use the excellent Firefox plugin Operator you can see them. There are several built in formats.

You can download additional scripts for Operator here:

Mike’s Musings » Operator User Scripts