ActorsProcessor extends AbstractProcessor { @VisibleForTesting void generateFromModel(@NotNull ActorParsedModel model, @NotNull ActorGeneratedModel generated) { if (model.collectInterfaces) { for (ActorInterfaceDescription _interface : model.interfaces) { generated.interfaces.add(mModelFactory.createInterfaceGenerator(_interface)); } for (ActorInterfaceGenerator _interface : generated.interfaces) { generated.files.add(_interface.generate(new NamesProviderImpl())); } } if (!model.collectInterfaces && generated.builder == null) { assert model.packages.size() == 1; generated.builder = mModelFactory.createBuilderGenerator(model.packages.get(0)); generated.files.add(generated.builder.generate(generated.interfaces)); } } @SuppressWarnings("unused")  ActorsProcessor(); @VisibleForTesting  ActorsProcessor(@NotNull ModelFactory modelFactory); @Override synchronized void init(ProcessingEnvironment processingEnv); @Override SourceVersion getSupportedSourceVersion(); @Override boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv);  }
@Test public void generateFromModel_filesList_notEmptyInterfaces() { ActorParsedModel model = new ActorParsedModel(); ActorGeneratedModel generated = new ActorGeneratedModel(); model.collectInterfaces = true; ActorInterfaceDescription _interface = Mockito.mock(ActorInterfaceDescription.class); model.interfaces.add(_interface); ActorInterfaceGenerator generator = Mockito.mock(ActorInterfaceGenerator.class); JavaFile file = JavaFile.builder("com.truecaller.androidactors", TypeSpec.classBuilder("SimpleActor").build()).build(); Mockito.doReturn(generator).when(mFactory).createInterfaceGenerator(_interface); Mockito.doReturn(file).when(generator).generate(Mockito.<NamesProvider>any()); ActorsProcessor processor = new ActorsProcessor(mFactory); processor.generateFromModel(model, generated); Mockito.verify(mFactory).createInterfaceGenerator(_interface); Mockito.verify(generator).generate(Mockito.<NamesProvider>any()); Assert.assertSame(file, generated.files.get(0)); Mockito.verifyNoMoreInteractions(mFactory); }
@Test public void generateFromModel_filesList_builder() { ActorParsedModel model = new ActorParsedModel(); ActorGeneratedModel generated = new ActorGeneratedModel(); ActorInterfaceGenerator generator = Mockito.mock(ActorInterfaceGenerator.class); generated.interfaces.add(generator); ActorsPackageDescription packageDescription = Mockito.mock(ActorsPackageDescription.class); model.packages.add(packageDescription); ActorsPackageGenerated packageGenerated = Mockito.mock(ActorsPackageGenerated.class); Mockito.doReturn(packageGenerated).when(mFactory).createBuilderGenerator(packageDescription); JavaFile file = JavaFile.builder("com.truecaller.androidactors", TypeSpec.classBuilder("SimpleActor").build()).build(); Mockito.doReturn(file).when(packageGenerated).generate(Mockito.anyListOf(ActorInterfaceGenerator.class)); ActorsProcessor processor = new ActorsProcessor(mFactory); processor.generateFromModel(model, generated); Assert.assertEquals(1, model.packages.size()); Assert.assertSame(file, generated.files.get(0)); }
@Test public void generateFromModel_doNothing_builder() { ActorParsedModel model = new ActorParsedModel(); ActorGeneratedModel generated = new ActorGeneratedModel(); ActorInterfaceGenerator generator = Mockito.mock(ActorInterfaceGenerator.class); generated.interfaces.add(generator); generated.builder = Mockito.mock(ActorsPackageGenerated.class); ActorsPackageDescription packageDescription = Mockito.mock(ActorsPackageDescription.class); model.packages.add(packageDescription); ActorsPackageGenerated packageGenerated = Mockito.mock(ActorsPackageGenerated.class); Mockito.doReturn(packageGenerated).when(mFactory).createBuilderGenerator(packageDescription); ActorsProcessor processor = new ActorsProcessor(mFactory); processor.generateFromModel(model, generated); Mockito.verifyZeroInteractions(mFactory); Assert.assertEquals(0, generated.files.size()); }