1 /*
2 * JavaBean Tester (https://github.com/hazendaz/javabean-tester)
3 *
4 * Copyright 2012-2021 Hazendaz.
5 *
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of The Apache Software License,
8 * Version 2.0 which accompanies this distribution, and is available at
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Contributors:
12 * CodeBox (Rob Dawson).
13 * Hazendaz (Jeremy Landis).
14 */
15 package com.codebox.bean;
16
17 import com.codebox.enums.CheckClear;
18 import com.codebox.enums.CheckConstructor;
19 import com.codebox.enums.CheckEquals;
20 import com.codebox.enums.CheckSerialize;
21 import com.codebox.enums.LoadData;
22 import com.codebox.enums.SkipStrictSerialize;
23 import com.codebox.instance.ConstructorInstance;
24
25 import java.util.Arrays;
26
27 /**
28 * The Class JavaBeanTesterBuilder.
29 *
30 * @param <T>
31 * the generic type
32 * @param <E>
33 * the element type
34 */
35 public class JavaBeanTesterBuilder<T, E> {
36
37 /** The worker. */
38 private final JavaBeanTesterWorker<T, E> worker;
39
40 /**
41 * Instantiates a new java bean tester builder.
42 *
43 * @param clazz
44 * the clazz
45 */
46 JavaBeanTesterBuilder(final Class<T> clazz) {
47 this.worker = new JavaBeanTesterWorker<>(clazz);
48 }
49
50 /**
51 * Instantiates a new java bean tester builder.
52 *
53 * @param clazz
54 * the clazz
55 * @param extension
56 * the extension
57 */
58 JavaBeanTesterBuilder(final Class<T> clazz, final Class<E> extension) {
59 this.worker = new JavaBeanTesterWorker<>(clazz, extension);
60 }
61
62 /**
63 * Check Clear.
64 *
65 * @return the java bean tester builder
66 */
67 public JavaBeanTesterBuilder<T, E> checkClear() {
68 return this.checkClear(true);
69 }
70
71 /**
72 * Check Clear.
73 *
74 * @param value
75 * the value
76 *
77 * @return the java bean tester builder
78 */
79 public JavaBeanTesterBuilder<T, E> checkClear(final boolean value) {
80 this.worker.setCheckClear(value ? CheckClear.ON : CheckClear.OFF);
81 return this;
82 }
83
84 /**
85 * Check Constructor.
86 *
87 * @return the java bean tester builder
88 */
89 public JavaBeanTesterBuilder<T, E> checkConstructor() {
90 return this.checkConstructor(true);
91 }
92
93 /**
94 * Check Constructor.
95 *
96 * @param value
97 * the value
98 *
99 * @return the java bean tester builder
100 */
101 public JavaBeanTesterBuilder<T, E> checkConstructor(final boolean value) {
102 this.worker.setCheckConstructor(value ? CheckConstructor.ON : CheckConstructor.OFF);
103 return this;
104 }
105
106 /**
107 * Check equals.
108 *
109 * @return the java bean tester builder
110 */
111 public JavaBeanTesterBuilder<T, E> checkEquals() {
112 return this.checkEquals(true);
113 }
114
115 /**
116 * Check equals.
117 *
118 * @param value
119 * the value
120 *
121 * @return the java bean tester builder
122 */
123 public JavaBeanTesterBuilder<T, E> checkEquals(final boolean value) {
124 this.worker.setCheckEquals(value ? CheckEquals.ON : CheckEquals.OFF);
125 return this;
126 }
127
128 /**
129 * Check Serializable.
130 *
131 * @return the java bean tester builder
132 */
133 public JavaBeanTesterBuilder<T, E> checkSerializable() {
134 return this.checkSerializable(true);
135 }
136
137 /**
138 * Check Serializable.
139 *
140 * @param value
141 * the value
142 *
143 * @return the java bean tester builder
144 */
145 public JavaBeanTesterBuilder<T, E> checkSerializable(final boolean value) {
146 this.worker.setCheckSerializable(value ? CheckSerialize.ON : CheckSerialize.OFF);
147 return this;
148 }
149
150 /**
151 * Load data.
152 *
153 * @return the java bean tester builder
154 */
155 public JavaBeanTesterBuilder<T, E> loadData() {
156 return this.loadData(true);
157 }
158
159 /**
160 * Load data.
161 *
162 * @param value
163 * the value
164 *
165 * @return the java bean tester builder
166 */
167 public JavaBeanTesterBuilder<T, E> loadData(final boolean value) {
168 this.worker.setLoadData(value ? LoadData.ON : LoadData.OFF);
169 return this;
170 }
171
172 /**
173 * Skip Strict Serializable is intended to relax strict check on serializable objects. For complex objects, strict
174 * checking will result in issues with equals check. Testing has shown this to be generally not a normal use case of
175 * javabean tester as it is normally used with POJOs only. In such a case, caller will get an error and if there is
176 * not actually a code problem they should turn this skip on.
177 *
178 * @return the java bean tester builder
179 */
180 public JavaBeanTesterBuilder<T, E> skipStrictSerializable() {
181 this.worker.setSkipStrictSerializable(SkipStrictSerialize.ON);
182 return this;
183 }
184
185 /**
186 * Skip.
187 *
188 * @param propertyNames
189 * the property names
190 *
191 * @return the java bean tester builder
192 */
193 public JavaBeanTesterBuilder<T, E> skip(final String... propertyNames) {
194 if (propertyNames != null) {
195 this.worker.getSkipThese().addAll(Arrays.asList(propertyNames));
196 }
197 return this;
198 }
199
200 /**
201 * Test.
202 */
203 public void test() {
204 this.worker.test();
205 }
206
207 /**
208 * Private Constructor Test.
209 */
210 public void testPrivateConstructor() {
211 ConstructorInstance.inaccessible(this.worker.getClazz());
212 }
213
214 /**
215 * Tests the equals/hashCode/toString methods of the specified class.
216 */
217 public void testObjectMethods() {
218 this.worker.equalsHashCodeToStringSymmetricTest();
219 }
220
221 /**
222 * Getter Setter Tests.
223 *
224 * @param instance
225 * the instance of class under test.
226 */
227 public void testInstance(final T instance) {
228 this.worker.getterSetterTests(instance);
229 }
230
231 /**
232 * Test equals.
233 *
234 * @param instance
235 * the instance
236 * @param expected
237 * the expected
238 */
239 public void testEquals(final T instance, final T expected) {
240 this.worker.equalsTests(instance, expected);
241 }
242
243 }