All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.testable.TestableContribution Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.testable;

import javax.lang.model.element.Modifier;

import br.com.objectos.pojo.plugin.Contribution;
import br.com.objectos.pojo.plugin.Naming;
import br.com.objectos.pojo.plugin.PojoInfo;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;

/**
 * @author [email protected] (Marcio Endo)
 */
class TestableContribution {

  private static final ClassName TESTER = ClassName.get("br.com.objectos.testable", "Tester");

  private static final String FIELD_NAME = "___TESTER___";

  final PojoInfo pojoInfo;
  final TypeName testerTypeName;

  private TestableContribution(PojoInfo pojoInfo, TypeName testerTypeName) {
    this.pojoInfo = pojoInfo;
    this.testerTypeName = testerTypeName;
  }

  public static TestableContribution of(PojoInfo pojoInfo) {
    Naming naming = pojoInfo.naming();
    TypeName superClassTypeName = naming.superClassTypeName();
    ParameterizedTypeName testerTypeName = ParameterizedTypeName.get(TESTER, naming.superClass());
    return superClassTypeName instanceof ParameterizedTypeName
        ? new Generic(pojoInfo, testerTypeName)
        : new TestableContribution(pojoInfo, testerTypeName);
  }

  public Contribution execute() {
    return Contribution.builder()
        .addField(field())
        .addMethod(isEqualTo())
        .build();
  }

  void acceptField(FieldSpec.Builder field) {
  }

  private FieldSpec field() {
    FieldSpec.Builder field = FieldSpec.builder(testerTypeName, FIELD_NAME)
        .addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC)
        .initializer(fieldInitializer());
    acceptField(field);
    return field.build();
  }

  private CodeBlock fieldInitializer() {
    Naming naming = pojoInfo.naming();
    CodeBlock.Builder init = CodeBlock.builder()
        .add("$T.of($T.class)\n", TESTER, naming.superClass());

    pojoInfo.propertyStream()
        .filter(property -> !property.hasAnnotation(NotTestable.class))
        .forEach(property -> init.add("    .add($S, o -> o.$L())\n", property.name(), property.accessorName()));

    return init
        .add("    .build()")
        .build();
  }

  private MethodSpec isEqualTo() {
    return MethodSpec.methodBuilder("isEqualTo")
        .addModifiers(Modifier.PUBLIC)
        .addAnnotation(Override.class)
        .returns(Equality.class)
        .addParameter(Object.class, "that")
        .addStatement("return $L.test(this, that)", FIELD_NAME)
        .build();
  }

  private static class Generic extends TestableContribution {

    private static final AnnotationSpec SUPPRESS = AnnotationSpec.builder(SuppressWarnings.class)
        .addMember("value", "$S", "rawtypes")
        .build();

    public Generic(PojoInfo pojoInfo, TypeName superClassTypeNameRaw) {
      super(pojoInfo, superClassTypeNameRaw);
    }

    @Override
    void acceptField(FieldSpec.Builder field) {
      field.addAnnotation(SUPPRESS);
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy