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

com.codebox.bean.JavaBeanTesterBuilder Maven / Gradle / Ivy

Go to download

Javabean Tester Simplifies POJO Testing and greatly simplifies Lombok Bean Testing!

The newest version!
/*
 * JavaBean Tester (https://github.com/hazendaz/javabean-tester)
 *
 * Copyright 2012-2021 Hazendaz.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of The Apache Software License,
 * Version 2.0 which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Contributors:
 *     CodeBox (Rob Dawson).
 *     Hazendaz (Jeremy Landis).
 */
package com.codebox.bean;

import com.codebox.enums.CheckClear;
import com.codebox.enums.CheckConstructor;
import com.codebox.enums.CheckEquals;
import com.codebox.enums.CheckSerialize;
import com.codebox.enums.LoadData;
import com.codebox.enums.SkipStrictSerialize;
import com.codebox.instance.ConstructorInstance;

import java.util.Arrays;

/**
 * The Class JavaBeanTesterBuilder.
 *
 * @param 
 *            the generic type
 * @param 
 *            the element type
 */
public class JavaBeanTesterBuilder {

    /** The worker. */
    private final JavaBeanTesterWorker worker;

    /**
     * Instantiates a new java bean tester builder.
     *
     * @param clazz
     *            the clazz
     */
    JavaBeanTesterBuilder(final Class clazz) {
        this.worker = new JavaBeanTesterWorker<>(clazz);
    }

    /**
     * Instantiates a new java bean tester builder.
     *
     * @param clazz
     *            the clazz
     * @param extension
     *            the extension
     */
    JavaBeanTesterBuilder(final Class clazz, final Class extension) {
        this.worker = new JavaBeanTesterWorker<>(clazz, extension);
    }

    /**
     * Check Clear.
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder checkClear() {
        return this.checkClear(true);
    }

    /**
     * Check Clear.
     *
     * @param value
     *            the value
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder checkClear(final boolean value) {
        this.worker.setCheckClear(value ? CheckClear.ON : CheckClear.OFF);
        return this;
    }

    /**
     * Check Constructor.
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder checkConstructor() {
        return this.checkConstructor(true);
    }

    /**
     * Check Constructor.
     *
     * @param value
     *            the value
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder checkConstructor(final boolean value) {
        this.worker.setCheckConstructor(value ? CheckConstructor.ON : CheckConstructor.OFF);
        return this;
    }

    /**
     * Check equals.
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder checkEquals() {
        return this.checkEquals(true);
    }

    /**
     * Check equals.
     *
     * @param value
     *            the value
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder checkEquals(final boolean value) {
        this.worker.setCheckEquals(value ? CheckEquals.ON : CheckEquals.OFF);
        return this;
    }

    /**
     * Check Serializable.
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder checkSerializable() {
        return this.checkSerializable(true);
    }

    /**
     * Check Serializable.
     *
     * @param value
     *            the value
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder checkSerializable(final boolean value) {
        this.worker.setCheckSerializable(value ? CheckSerialize.ON : CheckSerialize.OFF);
        return this;
    }

    /**
     * Load data.
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder loadData() {
        return this.loadData(true);
    }

    /**
     * Load data.
     *
     * @param value
     *            the value
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder loadData(final boolean value) {
        this.worker.setLoadData(value ? LoadData.ON : LoadData.OFF);
        return this;
    }

    /**
     * Skip Strict Serializable is intended to relax strict check on serializable objects. For complex objects, strict
     * checking will result in issues with equals check. Testing has shown this to be generally not a normal use case of
     * javabean tester as it is normally used with POJOs only. In such a case, caller will get an error and if there is
     * not actually a code problem they should turn this skip on.
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder skipStrictSerializable() {
        this.worker.setSkipStrictSerializable(SkipStrictSerialize.ON);
        return this;
    }

    /**
     * Skip.
     *
     * @param propertyNames
     *            the property names
     *
     * @return the java bean tester builder
     */
    public JavaBeanTesterBuilder skip(final String... propertyNames) {
        if (propertyNames != null) {
            this.worker.getSkipThese().addAll(Arrays.asList(propertyNames));
        }
        return this;
    }

    /**
     * Test.
     */
    public void test() {
        this.worker.test();
    }

    /**
     * Private Constructor Test.
     */
    public void testPrivateConstructor() {
        ConstructorInstance.inaccessible(this.worker.getClazz());
    }

    /**
     * Tests the equals/hashCode/toString methods of the specified class.
     */
    public void testObjectMethods() {
        this.worker.equalsHashCodeToStringSymmetricTest();
    }

    /**
     * Getter Setter Tests.
     *
     * @param instance
     *            the instance of class under test.
     */
    public void testInstance(final T instance) {
        this.worker.getterSetterTests(instance);
    }

    /**
     * Test equals.
     *
     * @param instance
     *            the instance
     * @param expected
     *            the expected
     */
    public void testEquals(final T instance, final T expected) {
        this.worker.equalsTests(instance, expected);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy