aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/utils/FieldUtils2Test.java
blob: d3c27f504794633b64c615962102be9c80b57b7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.api.utils;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import org.junit.Test;

import javax.annotation.Nullable;

import java.lang.reflect.Field;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;

public class FieldUtils2Test {

  @Test
  public void shouldGetFieldsOfSingleClass() {
    List<String> fields = fieldsName(FieldUtils2.getFields(FieldsWithDifferentModifiers.class, true));
    assertThat(fields).contains("publicField");
    assertThat(fields).contains("protectedField");
    assertThat(fields).contains("packageField");
    assertThat(fields).contains("privateField");
    assertThat(fields).contains("publicStaticField");
    assertThat(fields).contains("protectedStaticField");
    assertThat(fields).contains("packageStaticField");
    assertThat(fields).contains("privateStaticField");
  }

  @Test
  public void shouldGetFieldsOfClassHierarchy() {
    List<String> fields = fieldsName(FieldUtils2.getFields(Child.class, true));
    assertThat(fields).contains("publicField");
    assertThat(fields).contains("protectedField");
    assertThat(fields).contains("packageField");
    assertThat(fields).contains("privateField");
    assertThat(fields).contains("publicStaticField");
    assertThat(fields).contains("protectedStaticField");
    assertThat(fields).contains("packageStaticField");
    assertThat(fields).contains("privateStaticField");
    assertThat(fields).contains("childPrivateField");
  }

  @Test
  public void shouldGetOnlyAccessibleFields() {
    List<String> fields = fieldsName(FieldUtils2.getFields(Child.class, false));

    assertThat(fields).contains("publicField");
    assertThat(fields).contains("publicStaticField");
  }

  @Test
  public void shouldGetFieldsOfInterface() {
    List<String> fields = fieldsName(FieldUtils2.getFields(InterfaceWithFields.class, true));

    assertThat(fields).contains("INTERFACE_FIELD");
  }

  @Test
  public void shouldGetFieldsOfInterfaceImplementation() {
    List<String> fields = fieldsName(FieldUtils2.getFields(InterfaceImplementation.class, true));

    assertThat(fields).contains("INTERFACE_FIELD");
  }

  private static List<String> fieldsName(List<Field> fields){
    return newArrayList(Iterables.transform(fields, new Function<Field, String>() {
      @Override
      public String apply(@Nullable Field input) {
        return input != null ? input.getName() : null;
      }
    }));
  }

  static interface InterfaceWithFields {
    String INTERFACE_FIELD = "foo";
  }

  static class InterfaceImplementation implements InterfaceWithFields {
  }

  static class FieldsWithDifferentModifiers {
    public String publicField;
    protected String protectedField;
    String packageField;
    private String privateField;

    public static String publicStaticField;
    protected static String protectedStaticField;
    static String packageStaticField;
    private static String privateStaticField;
  }

  static class Child extends FieldsWithDifferentModifiers {
    private String childPrivateField;
  }

}