|
| 1 | +package fi.helsinki.cs.tmc.cli.command.core; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.mockito.Matchers.any; |
| 6 | +import static org.mockito.Matchers.anyString; |
| 7 | +import static org.mockito.Mockito.doReturn; |
| 8 | +import static org.mockito.Mockito.mock; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import fi.helsinki.cs.tmc.cli.io.Io; |
| 12 | + |
| 13 | +import org.apache.commons.cli.CommandLine; |
| 14 | +import org.apache.commons.cli.Options; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.StringWriter; |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Locale; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | +import javax.annotation.processing.Filer; |
| 27 | +import javax.annotation.processing.Messager; |
| 28 | +import javax.annotation.processing.ProcessingEnvironment; |
| 29 | +import javax.annotation.processing.RoundEnvironment; |
| 30 | +import javax.lang.model.SourceVersion; |
| 31 | +import javax.lang.model.element.Element; |
| 32 | +import javax.lang.model.element.ElementKind; |
| 33 | +import javax.lang.model.element.Name; |
| 34 | +import javax.lang.model.element.TypeElement; |
| 35 | +import javax.lang.model.util.Elements; |
| 36 | +import javax.lang.model.util.Types; |
| 37 | +import javax.tools.JavaFileObject; |
| 38 | + |
| 39 | +public class CommandAnnotationProcessorTest { |
| 40 | + CommandAnnotationProcessor processor; |
| 41 | + RoundEnvironment roundEnv; |
| 42 | + StringWriter stringWriter; |
| 43 | + Elements mockedElementUtils; |
| 44 | + |
| 45 | + public CommandAnnotationProcessorTest() throws IOException { |
| 46 | + final JavaFileObject fileObject = mock(JavaFileObject.class); |
| 47 | + final Filer mockedFiler = mock(Filer.class); |
| 48 | + when(mockedFiler.createSourceFile(anyString())).thenReturn(fileObject); |
| 49 | + |
| 50 | + stringWriter = new StringWriter(); |
| 51 | + when(fileObject.openWriter()).thenReturn(stringWriter); |
| 52 | + roundEnv = mock(RoundEnvironment.class); |
| 53 | + |
| 54 | + mockedElementUtils = mock(Elements.class); |
| 55 | + |
| 56 | + ProcessingEnvironment processingEnv = new ProcessingEnvironment() { |
| 57 | + @Override |
| 58 | + public Map<String, String> getOptions() { |
| 59 | + return new HashMap<String, String>(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Messager getMessager() { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Filer getFiler() { |
| 69 | + return mockedFiler; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Elements getElementUtils() { |
| 74 | + return mockedElementUtils; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Types getTypeUtils() { |
| 79 | + throw new UnsupportedOperationException(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public SourceVersion getSourceVersion() { |
| 84 | + throw new UnsupportedOperationException(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Locale getLocale() { |
| 89 | + throw new UnsupportedOperationException(); |
| 90 | + } |
| 91 | + |
| 92 | + }; |
| 93 | + processor = new CommandAnnotationProcessor(); |
| 94 | + processor.init(processingEnv); |
| 95 | + } |
| 96 | + |
| 97 | + @Command(name = "commmand1", desc = "abc") |
| 98 | + public static class CommandAnnotationExample1 extends AbstractCommand { |
| 99 | + @Override |
| 100 | + public void getOptions(Options options) { |
| 101 | + throw new UnsupportedOperationException(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void run(CommandLine args, Io io) { |
| 106 | + throw new UnsupportedOperationException(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Command(name = "commmand2", desc = "abc") |
| 111 | + public static class CommandAnnotationExample2 extends AbstractCommand { |
| 112 | + @Override |
| 113 | + public void getOptions(Options options) { |
| 114 | + throw new UnsupportedOperationException(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void run(CommandLine args, Io io) { |
| 119 | + throw new UnsupportedOperationException(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void warnsAboutInvalidElementWithCommandAnnotation() { |
| 125 | + final Element classElement = mock(Element.class); |
| 126 | + when(classElement.getKind()).thenReturn(ElementKind.FIELD); |
| 127 | + doReturn(new HashSet<>(Arrays.asList(classElement))).when(roundEnv) |
| 128 | + .getElementsAnnotatedWith(Command.class); |
| 129 | + |
| 130 | + Set<TypeElement> annotations = new HashSet<>(); |
| 131 | + assertFalse(processor.process(annotations, roundEnv)); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void worksWithoutCommands() { |
| 136 | + doReturn(new HashSet<>(Arrays.asList())).when(roundEnv) |
| 137 | + .getElementsAnnotatedWith((Class<Annotation>)any(Class.class)); |
| 138 | + |
| 139 | + Set<TypeElement> annotations = new HashSet<>(); |
| 140 | + assertTrue(processor.process(annotations, roundEnv)); |
| 141 | + assertFalse(stringWriter.toString().contains(".class")); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void worksWithOneCommands() { |
| 146 | + final Element classElement = mock(TypeElement.class); |
| 147 | + when(classElement.getKind()).thenReturn(ElementKind.CLASS); |
| 148 | + Command annotation = CommandAnnotationExample1.class.getAnnotation(Command.class); |
| 149 | + doReturn(annotation).when(classElement).getAnnotation(Command.class); |
| 150 | + doReturn(new HashSet<>(Arrays.asList(classElement))).when(roundEnv) |
| 151 | + .getElementsAnnotatedWith(any(Class.class)); |
| 152 | + |
| 153 | + Name mockedName = mock(Name.class); |
| 154 | + when(mockedName.toString()) |
| 155 | + .thenReturn("abc.TestTest"); |
| 156 | + when(mockedElementUtils.getBinaryName((TypeElement) classElement)) |
| 157 | + .thenReturn(mockedName); |
| 158 | + |
| 159 | + Set<TypeElement> annotations = new HashSet<>(); |
| 160 | + assertTrue(processor.process(annotations, roundEnv)); |
| 161 | + assertTrue(stringWriter.toString().contains("TestTest.class")); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void generateCorrectCodeWithTwoCommands() { |
| 166 | + final Element classElement1 = mock(TypeElement.class); |
| 167 | + final Element classElement2 = mock(TypeElement.class); |
| 168 | + when(classElement1.getKind()).thenReturn(ElementKind.CLASS); |
| 169 | + when(classElement2.getKind()).thenReturn(ElementKind.CLASS); |
| 170 | + Command annotation = CommandAnnotationExample1.class.getAnnotation(Command.class); |
| 171 | + doReturn(annotation).when(classElement1).getAnnotation(Command.class); |
| 172 | + annotation = CommandAnnotationExample2.class.getAnnotation(Command.class); |
| 173 | + doReturn(annotation).when(classElement2).getAnnotation(Command.class); |
| 174 | + |
| 175 | + doReturn(new HashSet<>(Arrays.asList(classElement1, classElement2))).when(roundEnv) |
| 176 | + .getElementsAnnotatedWith(any(Class.class)); |
| 177 | + |
| 178 | + Name mockedName1 = mock(Name.class); |
| 179 | + when(mockedName1.toString()) |
| 180 | + .thenReturn("abc.TestTest1"); |
| 181 | + when(mockedElementUtils.getBinaryName((TypeElement) classElement1)) |
| 182 | + .thenReturn(mockedName1); |
| 183 | + |
| 184 | + Name mockedName2 = mock(Name.class); |
| 185 | + when(mockedName2.toString()) |
| 186 | + .thenReturn("abc.TestTest2"); |
| 187 | + when(mockedElementUtils.getBinaryName((TypeElement) classElement2)) |
| 188 | + .thenReturn(mockedName2); |
| 189 | + |
| 190 | + Set<TypeElement> annotations = new HashSet<>(); |
| 191 | + assertTrue(processor.process(annotations, roundEnv)); |
| 192 | + assertTrue(stringWriter.toString().contains("(\"commmand1\", TestTest1.class)")); |
| 193 | + assertTrue(stringWriter.toString().contains("(\"commmand2\", TestTest2.class)")); |
| 194 | + } |
| 195 | +} |
0 commit comments