I already posted a class to enable the comparison of Java method implementations. However, this was a draft method rather than a true solution.
Today, I come back with a(n almost) valid solution. You can get it on my Github repository: labs-java, implementation-comparer branch.
Basically, it will be able to compare several implementations of any class within its context, rather than the previous solution which forced you to rewrite the methods within the comparer.
Let us imagine you have a Foo
class with a bar(String arg)
, but you suspect this method can be written in a better and more efficient way, but you want to ensure both methods return the same results and make sure which methods is the most efficient.
You will have to write the variants for your method, and suffix them with an incremental index (bar1(String arg)
, bar2(String arg)
, …). Then, you will call the implementation comparer with some arguments you wish to test:
|
Foo foo = new Foo(); ImplementationComparer comp = new ImplementationComparer(); comp.compare(foo, "bar", new Class< ?>[] { String.class }, new Object[] { "The value I use to test" }); |
This will output something like this:
|
2013-10-13 19:02:51,060 INFO === Comparing implementations for method Foo.bar 2013-10-13 19:02:51,062 INFO === Equality test: 2013-10-13 19:02:51,131 DEBUG +-------------------------+-------------------------+-------------------------+ 2013-10-13 19:02:51,131 DEBUG | Original | Variant 1 | Variant 2 | 2013-10-13 19:02:51,131 DEBUG +-------------------------+-------------------------+-------------------------+ 2013-10-13 19:02:51,131 DEBUG | The value I use to test | The value I use to test | The value I use to test | 2013-10-13 19:02:51,131 DEBUG +-------------------------+-------------------------+-------------------------+ 2013-10-13 19:02:51,131 INFO OK 2013-10-13 19:02:51,131 INFO === Time checks: 2013-10-13 19:02:51,509 INFO +-------------+-------------+-------------+ 2013-10-13 19:02:51,509 INFO | Original | Variant 1 | Variant 2 | 2013-10-13 19:02:51,510 INFO +-------------+-------------+-------------+ 2013-10-13 19:02:51,510 INFO | 0:00:00.149 | 0:00:00.067 | 0:00:00.006 | 2013-10-13 19:02:51,510 INFO | 0:00:00.059 | 0:00:00.005 | 0:00:00.002 | 2013-10-13 19:02:51,510 INFO | 0:00:00.049 | 0:00:00.003 | 0:00:00.002 | 2013-10-13 19:02:51,510 INFO +-------------+-------------+-------------+ |
The time checks are performed only if the info log level is above INFO, and the result comparison displays the details only on debug level, but the compare
method returns true
if all methods return the same result, and false
otherwise.
This comparer can be really useful when you are refactoring or even included in unit testing if you find a use for it.
For more details, you can have a look at the class itself. It is really not that complicated and I made it an obligation to have documented code nonetheless.
As a reminder, you can also have a look at my my previous draft version, which came with more explanations. The main principle remains the same, only with more freedom.
And if you need help, you can always ask below, as I will not go further into details right now.
For the logging of tables, I used Alcibiade’s asciiart library.
All this just in preparation of my next post… And yet, I am quite satisfied with myself and feels this will come in handy next time I refactor…