开发者

Is there a JUnit library/utility to detect the presence of a library? [closed]

开发者 https://www.devze.com 2023-01-16 15:42 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or e开发者_StackOverflowxpertise, but this question will like
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or e开发者_StackOverflowxpertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago.

We're using a few libraries that include an xml parser library through maven. We think we've excluded most of those as we are using a newer version.

Is there some sort of JUnit library to check an ear/war for the presence of that jar and fail the test if its there? Or do we just need to write some code to unzip the archive and loop over the directory checking for it?


Often when this happens, there are classes that appear in both jars, so you can check your classpath looking for duplicates. Given a class, you can see how often it appears on the classpath:

public void testSomeClassInClassPathOnce() {
  String className = SomeClass.class.getName();
  String path = className.replace('.', File.separatorChar) + ".class";
  Enumeration<URL> urls = ClassLoader.getSystemResources(path);
  assertTrue(urls.hasMoreElements());
  urls.nextElement();
  assertFalse(urls.hasMoreElements());
}

If the old jar has a class that isn't in the new one, then you can use ClassLoader.getSystemResource() to see if that class is on your classpath.

Note that you need to make sure your JUnit test has all of the production jars in the classpath.

0

精彩评论

暂无评论...
验证码 换一张
取 消