|
| 1 | +package liquidjava.utils; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import spoon.Launcher; |
| 9 | +import spoon.reflect.code.CtFieldRead; |
| 10 | +import spoon.reflect.visitor.filter.TypeFilter; |
| 11 | + |
| 12 | +class StaticConstantsTest { |
| 13 | + @Test |
| 14 | + void resolvesInheritedLibraryConstant() { |
| 15 | + assertEquals(2, resolveRead( |
| 16 | + "class Repro { int mode = javax.imageio.plugins.jpeg.JPEGImageWriteParam.MODE_EXPLICIT; }")); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + void resolvesInheritedInterfaceConstant() { |
| 21 | + assertEquals(0, resolveRead("class Repro { int alignment = javax.swing.JLabel.CENTER; }")); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void resolvesDeclaredLibraryConstant() { |
| 26 | + assertEquals(2, resolveRead("class Repro { int mode = javax.imageio.ImageWriteParam.MODE_EXPLICIT; }")); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void resolvesPrivateSourceConstant() { |
| 31 | + assertEquals(3, resolveRead("class Repro { private static final int LIMIT = 3; int value = LIMIT; }")); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void rejectsMutableSourceField() { |
| 36 | + assertNull(resolveRead("class Repro { static int LIMIT = 3; int value = LIMIT; }")); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void rejectsInstanceSourceField() { |
| 41 | + assertNull(resolveRead("class Repro { final int LIMIT = 3; int value = LIMIT; }")); |
| 42 | + } |
| 43 | + |
| 44 | + private static Object resolveRead(String source) { |
| 45 | + var type = Launcher.parseClass(source); |
| 46 | + CtFieldRead<?> read = type.getElements(new TypeFilter<>(CtFieldRead.class)).get(0); |
| 47 | + return StaticConstants.resolve(read.getVariable()); |
| 48 | + } |
| 49 | +} |
0 commit comments