有关如何在Java代码中检查非唯一ID的常规信息

Chr*_*ldi 0 java unit-testing

在一个非常庞大的项目中,有很多ExceptionClasses,其中实现了异常细节enum.

NAME_OF_EXCEPTION_DETAIL(ID, PRIORITY)
Run Code Online (Sandbox Code Playgroud)

例如:

NO_LICENSE_FOUND(100, 0)
UNSUPPORTED_LICENSE(101, 1)
WRONG_LICENSE_FOR_VERSION(101, 0)
Run Code Online (Sandbox Code Playgroud)

在某些情况下,异常详细信息ID是相同的,应该永远不会发生.很可能有更多的重复,但这个项目是巨大的手工检查...

是否有一种智能的方法来检查重复的代码,例如UnitTesting - 从未使用它.

谢谢你的帮助!

编辑(当只使用一个枚举时,Bens答案是解决此问题的好方法):为了更清楚我的具体任务,这就是情况.编写了很多类来处理ExceptionDetails,例如

TopicOneExceptionDetails.java
TopicTwoExceptionDetails.java  (and so on)
Run Code Online (Sandbox Code Playgroud)

这些类中的每一个都定义了这个主题的枚举,如下所示:

public enum TopicOneExceptionDetails implements ApplicationException.ExceptionDetails { .... }
Run Code Online (Sandbox Code Playgroud)

然后声明与TopicOne错误相关的错误,例如

SETTING_TIMEZONE_FAILED(55, ...)
Setting_DATETIME_FAILED(56, ...)
Run Code Online (Sandbox Code Playgroud)

在这个TopicOne的枚举中,每个错误ID都必须是唯一的.但是例如ID 55(此处用于SETTING_TIMEZONE_FAILED)可用于声明与TopicTwo相关的错误而没有问题.

编辑2(使用思考?)我找到了一篇关于马特·谢泼德写的关于java 的非常有用的答案.我认为这可以解决我的问题.我需要休息但我会报告回来.我目前的想法是:Reflections

Reflections reflections = new Reflections("project.de")
for(Class<? extends ApplicationException.ExceptionDetails> exceptionDetailsClasses : reflections.getSubTypesOf(ApplicationException.ExceptionDetails.class)){
for ( ... ) .. }
Run Code Online (Sandbox Code Playgroud)

我应该能够检查ID,例如Ben建议.我稍后会报告.

编辑3(问题解决了!)就像在编辑2中描述的那样,我能够轻松地解决这个问题reflections.

public class ApplicationExceptionDetailsErrorCodeValidationTest {


@Test
  public void validateErrorCodeUniqueness() throws ErrorCodeUniquenessBroken{
    Reflections reflections = new Reflections("de.xyz");
for (Class<? extends ApplicationException.ExceptionDetails> exceptionDetailsClass : reflections.getSubTypesOf(ApplicationException.ExceptionDetails.class)) {
  if (exceptionDetailsClass.getSimpleName().equals("TestExceptionDetails")) {
    continue;
  }

  List<Integer> errorCodes = new ArrayList<Integer>();

  for (ApplicationException.ExceptionDetails exceptionDetails : exceptionDetailsClass.getEnumConstants()) {
    if (errorCodes.contains(exceptionDetails.getErrorCode().getId()))  throw new ErrorCodeUniquenessBroken("ErrorCode uniqueness broken! ErrorCode: " + exceptionDetails.getMessageKey() + " has repeated ErrorCode! Change it!");
    else errorCodes.add(exceptionDetails.getErrorCode().getId());
  }
}
Run Code Online (Sandbox Code Playgroud)

}}

特别感谢Ben和Spotted的努力.两者都回答了一些非常好的例子,说明如何解决一般的主题问题.在我的情况下,它有点复杂.

Ben*_*Ben 6

检查Enum是否有重复ID的一种方法(它未经过测试,但应该可以使用).
您可以将此代码的一部分用于单元测试(或单独使用):

import java.util.HashSet;
import java.util.Set;

public class NewClass 
{
   public static enum Errors
   {
      ERROR_A(1),
      ERROR_B(2),
      ERROR_C(3),
      ERROR_D(2), //duplicate is here!
      ERROR_E(5);

      int m_id;
      Errors(int id)
      {
          m_id = id;
      }

      public int getId()
      {
          return m_id;
      }
   }

   public static void main(String[] args) 
   {
      Set<Integer> usedIds = new HashSet<>();
      for (Errors e : Errors.values()) //iterate over all Error-Enum-Items
      {
          int id = e.getId(); //fetch id from enum-item
          if (usedIds.contains(id)) //check if id was used before
              System.err.println("ID ALREADY USED: " + e.name() + ":" + id);
          else
              usedIds.add(id); //remember new used id here
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

干杯!