What is dimple?
Dimple is a simple framework that allows you to implement interface(s) in a duck-typing way in pure Java. There are two typical senarios where one would want to use dimple:
- To stub a third-party interface such as java.sql.Connection where only 1 or 2 methods out of 100 or so need to be implemented. (An alternative to jMock or Easymock, if what you really want is a stub.)
- To intercept a few methods of a third-party interface.
What is the license?
Why wouldn't I just use the "implements" keyword?
Normally you would (and you should). But there're two cases where you may not want to "implements":
- The interface is too fat, full of stuff that are irrelevant to what you are trying to accomplish. With "implements", you are forced to at least "return null" or "throw new UnsupportedOperationException()" for each one of them. You will also suffer the source code level dependency on types used on the signatures of all the stub methods.
- Interfaces like java.sql.Connection are intended for the client to call, not implement. It is typical that new methods get added to the interface in version 1.1 while your "MyStubConnection" was coded according to version 1.0. That presents a nasty versioning problem.
How do I stub a Connection using dimple?
This way, you are stubbing the "prepareStatement()" method without worrying about any other methods.
How do I intercept method in a Connection object?
Suppose a legacy system has a proprietary way of releasing jdbc connection, dimple can be used to wrap it up nicely:
So I just need to make sure the method name is the same?
The rules for the interceptor methods are:
- Method name is the same as the intercepted (implemented) method.
- Have the same number of parameters.
- Parameter types have to be the same as the intercepted or cotravariant.
- Return types have to be the same as the intercepted or covariant.
- Implementor.willImplement() and Implementor.implementedBy() can be used to verify that all interceptor methods can be used to implement some method in the target interface(s).
- Use @Implement annotation to automatically verify the method signature against the method being implemented.