Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with a Mock Object

 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Mock Object that implements Connection. I am trying to compile it using different versions (jdk 1.3 and jdk 1.4).

The problem is that in version 1.4, Connection includes methods that use a "Savepoint" (a new interface only found in v1.4). If I implement those methods, I can compile using v1.4 but not v1.3 (since 1.3 doesn't have a Savepoint interface). If I don't implement those methods, I can compile using 1.3 but not 1.4 because the class must be declared abstract since all the methods of the interface haven't been implemented.

Is there any way to resolve this and allow compilation in both versions?
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately there is no way around this... you will need to keep two copies of the class (one for each version). At build time, depending on the version of Java detected, Ant could build and include the appropriate class in the distribution.
More over, have you seen the MockObjects Project? It provides many different mock objects, ranging from JDBC to JMS (they provide different distributions depending on jdk and servlet version). Give it a look, maybe you are reinventing the wheel here...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, there is a way around this - by using the dynamic proxy pattern (that is, creating an implementation to an interface at runtime through the java.lang.reflect.Proxy class).
http://www.easymock.org/ is providing a mock framework based on this pattern. It's quite powerfull, though for complex mocks it might become rather unhandy.
Hope this helps...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just remember a different solution we used successfully: For JDK 1.3, just provide your own SavePoint interface!
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Just remember a different solution we used successfully: For JDK 1.3, just provide your own SavePoint interface!

I tried that one, but then I get an error about the Connection method returning the wrong thing. (I can give you the exact error later, when I'm at home).
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to put your Savepoint interface in the java.sql package. Which of course is illegal according to the JLS but once you get your mock Connection compiled than it shouldn't matter much...
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
[QB]In fact, there is a way around this - by using the dynamic proxy pattern (that is, creating an implementation to an interface at runtime through the java.lang.reflect.Proxy class).

That was very useful, Ilja. Thank you.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic