English 中文(简体)
比较固定等级的固定值
原标题:Compare the String values of a class of constants

我的任务是根据我们的<代码>Constants类别进行测试,以确保该类别中的每一数值都是独特的。 我不谈固定名称(如果是(d)的,民主选举学会将告诉开发商,而是固定名称的数值。

我对贾瓦说了新鲜话,并不知道如何做到这一点。

很显然,我的<代码>类别:

public static final String STATUS_RECEIVED = "RE";
public static final String STATUS_CANCELLED = "CA";
public static final String STATUS_REVIEWED = "RE";

在上述例子中,我想试一下一下一下,有价值重复(因为STATUS_RECEIVED=STATUS_REVIEWED)。 我如何在方案上这样做?

最佳回答
Field[] fields = Constants.class.getDeclaredFields();
List<Field> staticFields = new ArrayList<>();
for(Field f:fields) {
  if (Modifier.isStatic(f)) {
    staticFields.add(f);
  }
}
for (int i = 0; i < staticFields.size(); i++) {
  for (int j = i + 1; j < staticFields.size(); j++) {
    if (staticFields.get(i).get(null).equals(staticFields.get(j).get(null))) {
      throw new AssertionFailedError(staticFields.get(i) + " equals " 
        + staticFields.get(j));
    } 
  }
}
问题回答

它通过所有固定的价值观,如果它们尚未达到既定标准,就将予以保留。 如果这些试验已经安装,那么测试就会失败。

否则,你就可以将重复放在另一套不同的轨道上,并对这套重复材料进行抽空测试,这样,每当你进行试验时,就会给你一份重复清单(而不是在你去除所有重复物之前必须重新进行测试)。

你们只能发现田地,然后检查独一无二之处(例如,将田地列入树图,然后算出)。

public void test() throws Exception {
    for (Field f : ConstantsClass.class.getDeclaredFields()) {
        System.out.println(f.getName() + ": " + f.get(null));
    }
}

摘自 准入 通过思考实现 Java最终定值

如果你要用大刀子的话,你的生活就会变得非常容易。 那么,在使成员姓名与所需数值相匹配之间,你将有一个选择。

enum Status {
RE,CA,RE;
}

在此情况下,汇编者保证其独一无二,或储存作为财产的价值

enum Status {
  averbosenamedescribingwhatremeans("RE") 
  etc, etc

  private final String statusCode;

  Status(String code) {
  this.statusCode = code;
  }  
}

你们很容易在使用权势规定的价值观方法进行单位测试时检查编码的独特性。

加上你,就会有安全守则。

With your current approach you would need to query the class via reflection.

考虑制定<条码> 编码> 编码<> 编码> > 。 类似:

public enum Constants {

    STATUS_RECEIVED("RE"),
    STATUS_CANCELLED("CA"),
    STATUS_REVIEWED("RE")

    private String value;

    public Constants(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

Then your test could iterate over the value field of each Enum constant and fail if that value already exists For example:

public void testConstantValueUniqueness() {
    Set<String> values = new HashSet<String>();
    for(Constants constant : Constants.values()) {
        if(values.contains(constant.getValue())) {
            // fail
        }
        values.add(constant.getValue());
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签