Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Resolve namespace conflict

Tags:

java

We have a jar that we lost the source code to. I decompiled the jar and created new source from it. I want to then verify that the source code and the old jar have the same behavior. I am writing unit tests to do the verification the problem is that they both have same namespace / class name so I do not know how to disambiguate the old jar and the new source code. What can I do or is it impossible?

like image 888
Bishnu Avatar asked Mar 08 '11 20:03

Bishnu


3 Answers

You need to only have one version on the class path at once to guarantee that you are running that version of the code. Develop your unit test separate from the code so you can drop in either version.

like image 100
krock Avatar answered Oct 21 '22 17:10

krock


Give the new source a temporary namespace for testing purposes. Then instead of import, you can refer your new classes as:

com.yourfirm.test.packagename.TheClassName

the old ones can be simply imported and refered to as TheClassName. This way you can tell by looking at your test cases which is which.

Or simply run the tests with -cp oldpackage.jar and then -cp newpackage.jar.

like image 23
vbence Avatar answered Oct 21 '22 15:10

vbence


It's possible, but you have to mess around with class loading. Instead of putting either of the jars on the classpath, you'll need to load them at runtime. Check out JCL for a library to allow you to do this. (Disclaimer: I have never used JCL.)

Basically, each test would have to load the class from the old JAR, grab the results of the method you're testing, then unload that JAR, load up the new one, run the same method against the new version, and compare the results.

like image 1
sworisbreathing Avatar answered Oct 21 '22 17:10

sworisbreathing