3 import static org.junit.Assume.assumeNoException;
4 import static org.junit.jupiter.api.Assertions.assertAll;
5 import static org.junit.jupiter.api.Assertions.assertEquals;
6 import static org.junit.jupiter.api.Assertions.assertFalse;
7 import static org.junit.jupiter.api.Assertions.assertTrue;
8 import static org.junit.jupiter.api.Assertions.fail;
9 import static org.junit.jupiter.api.Assumptions.assumeFalse;
10 import static org.junit.jupiter.api.Assumptions.assumeTrue;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.ObjectInputStream;
16 import java.io.ObjectOutputStream;
17 import java.io.Serializable;
18 import java.nio.file.Path;
19 import java.util.Arrays;
21 import org.apache.logging.log4j.LogManager;
22 import org.apache.logging.log4j.Logger;
23 import org.junit.jupiter.api.BeforeAll;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.DisplayName;
26 import org.junit.jupiter.api.TestInfo;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.junit.jupiter.api.io.TempDir;
29 import org.junit.jupiter.params.ParameterizedTest;
30 import org.junit.jupiter.params.provider.CsvSource;
31 import org.junit.jupiter.params.provider.ValueSource;
40 @ExtendWith({TimingExtension.class})
51 fileName =
"SerializedObjectTest.tmp";
56 eventMethodInvoked =
false;
57 listenerMethodInvoked =
false;
66 @DisplayName(
"Test Event - Unregister event")
68 @ValueSource(ints = { 10, 0, -4 })
75 public void onValueChanged(Integer oldValue, Integer value) {
76 listenerMethodInvoked =
true;
83 assumeFalse(listenerMethodInvoked, () ->
"Listener method already invoked");
86 observedValue.
set(value);
88 assertFalse(listenerMethodInvoked, () ->
"Listener method invoked");
90 }
catch (Exception e) {
91 fail(
"Unexpected exception thrown in " + testInfo.getTestMethod().get().getName() +
"\n\tCase: " + testInfo.getDisplayName(), e);
102 @DisplayName(
"Test Event - Unregister all events")
104 @ValueSource(ints = { 10, 0, -4 })
111 public void onValueChanged(Integer oldValue, Integer value) {
112 listenerMethodInvoked =
true;
121 assumeFalse(eventMethodInvoked, () ->
"Event method already invoked.");
122 assumeFalse(listenerMethodInvoked, () ->
"Listener method already invoked.");
125 observedValue.
set(value);
127 assertAll(
"EventValues",
128 () -> assertFalse(eventMethodInvoked, () ->
"Event method invoked."),
129 () -> assertFalse(listenerMethodInvoked, () ->
"Listener method invoked.")
132 }
catch (Exception e) {
133 fail(
"Unexpected exception thrown in " + testInfo.getTestMethod().get().getName() +
"\n\tCase: " + testInfo.getDisplayName(), e);
145 @DisplayName(
"Test Event - Inline method invocation")
147 @CsvSource({
"30, 10",
"-2, 10",
"11, 10"})
153 eventMethodInvoked =
true;
154 assertAll(
"EventValues",
155 () -> assertEquals(oldVAlue, value, () ->
"New value missmatch in event invocation."),
156 () -> assertEquals(newValue, expected, () ->
"Old value missmatch in event invocation.")
160 observedValue.
set(expected);
162 assertTrue(eventMethodInvoked, () ->
"Event method not invoked.");
163 }
catch (Exception e) {
164 fail(
"Unexpected exception thrown in " + testInfo.getTestMethod().get().getName() +
"\n\tCase: " + testInfo.getDisplayName(), e);
175 @DisplayName(
"Test Event - Referenced method invocation")
177 @CsvSource({
"Paco, NewPaco, 44",
"Lola, NewLola, 41"})
184 }
catch (Exception e) {
185 assumeNoException(e);
188 assumeFalse(eventMethodInvoked, () ->
"Event method already invoked");
190 assertTrue(eventMethodInvoked, () ->
"Event method not invoked");
201 @DisplayName(
"Test Event - Referenced method invocation")
203 @CsvSource({
"Paco, NewPaco, 44",
"Lola, NewLola, 41"})
210 }
catch (Exception e) {
211 assumeNoException(e);
214 assumeFalse(eventMethodInvoked, () ->
"Event method already invoked");
216 assumeTrue(eventMethodInvoked, () ->
"Event method not invoked");
218 assertEquals(newName, personTest.
getName(), () ->
"Obtained value is the new value");
228 @DisplayName(
"Test Event - Event not fired")
230 @ValueSource(ints = { 10, 0, -4 })
237 assumeFalse(eventMethodInvoked, () ->
"Event method already invoked");
238 observedValue.
set(value);
239 assertFalse(eventMethodInvoked, () ->
"Event method invoked");
240 }
catch (Exception e) {
241 fail(
"Unexpected exception thrown in " + testInfo.getTestMethod().get().getName() +
"\n\tCase: " + testInfo.getDisplayName(), e);
252 @DisplayName(
"Test Event - Enable/Disable value change notification")
254 @CsvSource({
"true",
"false"})
261 assumeFalse(eventMethodInvoked, () ->
"Event method already invoked");
262 observedValue.
set(10, notifyChange);
263 assertEquals(notifyChange, eventMethodInvoked, () ->
"Event method invoked");
264 }
catch (Exception e) {
265 fail(
"Unexpected exception thrown in " + testInfo.getTestMethod().get().getName() +
"\n\tCase: " + testInfo.getDisplayName(), e);
276 @DisplayName(
"Test Serialization - Boolean")
278 @CsvSource({
"true",
"false"})
282 serialiceToTempFile(observedValue, fileName, tempDir);
284 @SuppressWarnings(
"unchecked")
287 assertEquals(expected, value.
get());
297 @DisplayName(
"Test Serialization - Byte")
303 serialiceToTempFile(observedValue, fileName, tempDir);
305 @SuppressWarnings(
"unchecked")
308 assertEquals(expected, value.
get());
317 @DisplayName(
"Test Serialization - Char")
319 @CsvSource({
"A",
"T"})
323 serialiceToTempFile(observedValue, fileName, tempDir);
325 @SuppressWarnings(
"unchecked")
328 assertEquals(expected, value.
get());
338 @DisplayName(
"Test Serialization - Short")
344 serialiceToTempFile(observedValue, fileName, tempDir);
346 @SuppressWarnings(
"unchecked")
349 assertEquals(expected, value.
get());
359 @DisplayName(
"Test Serialization - Int")
365 serialiceToTempFile(observedValue, fileName, tempDir);
367 @SuppressWarnings(
"unchecked")
370 assertEquals(expected, value.
get());
380 @DisplayName(
"Test Serialization - Long")
386 serialiceToTempFile(observedValue, fileName, tempDir);
388 @SuppressWarnings(
"unchecked")
391 assertEquals(expected, value.
get());
401 @DisplayName(
"Test Serialization - Float")
407 serialiceToTempFile(observedValue, fileName, tempDir);
409 @SuppressWarnings(
"unchecked")
412 assertEquals(expected, value.
get());
422 @DisplayName(
"Test Serialization - Double")
424 @CsvSource({
"65.55"})
428 serialiceToTempFile(observedValue, fileName, tempDir);
430 @SuppressWarnings(
"unchecked")
433 assertEquals(expected, value.
get());
443 @DisplayName(
"Test Serialization - String")
445 @CsvSource({
"Test of string",
"Another test"})
449 serialiceToTempFile(observedValue, fileName, tempDir);
451 @SuppressWarnings(
"unchecked")
454 assertEquals(expected, value.
get());
465 @DisplayName(
"Test Serialization - Object")
467 @CsvSource({
"Paco, 44",
"Lola, 41"})
472 serialiceToTempFile(observedValue, fileName, tempDir);
474 @SuppressWarnings(
"unchecked")
477 assertEquals(expectedTestClass, value.
get());
487 @DisplayName(
"Test Serialization - Array of int")
491 @ArraySource(array = {21, 34, 68}),
492 @ArraySource(array = {72, 84, 78})
497 serialiceToTempFile(observedValue, fileName, tempDir);
499 @SuppressWarnings(
"unchecked")
502 assertTrue(Arrays.equals(expected, value.
get()));
513 eventMethodInvoked =
true;
514 LOGGER.debug(
"Value changed event metod called. [oldValue: " + oldValue +
", newValue: " + newValue +
"]");
526 assumeTrue(tempDir !=
null);
527 assumeTrue(fileName !=
null && !fileName.trim().isEmpty());
529 File file = tempDir.resolve(fileName).toFile();
531 FileOutputStream fileOutputStream =
new FileOutputStream(file);
532 try(ObjectOutputStream objectOutputStream =
new ObjectOutputStream(fileOutputStream)) {
533 objectOutputStream.writeObject(value);
534 objectOutputStream.flush();
536 }
catch (Exception e) {
537 assumeNoException(e);
552 assumeTrue(tempDir !=
null);
553 assumeTrue(fileName !=
null && !fileName.trim().isEmpty());
555 File file = tempDir.resolve(fileName).toFile();
558 FileInputStream fileInputStream =
new FileInputStream(file);
559 try(ObjectInputStream objectInputStream =
new ObjectInputStream(fileInputStream)) {
560 value = convertInstanceOfObject(objectInputStream.readObject(), cls);
562 }
catch (Exception e) {
563 assumeNoException(e);
578 return cls.cast(obj);
579 }
catch(ClassCastException e) {
void setName(String name)
Class that wraps a value and gives the utility of registering listeners that will be called after any...
private< T > T deserialiceFromTempFile(Class< T > cls, String fileName, Path tempDir)
Method for deserialization from a temp file.
void testEventMethodValueChange(String name, String newName, int age)
Test for notify a change in the name of the input person object.
void testSerialization(String expected, @TempDir Path tempDir)
Test of the serialization of a string.
private< T extends Serializable > void serialiceToTempFile(T value, String fileName, Path tempDir)
Method for serialization to temp file.
void testSerialization(double expected, @TempDir Path tempDir)
Test of the serialization of a double.
static boolean listenerMethodInvoked
void testSerialization(long expected, @TempDir Path tempDir)
Test of the serialization of a long.
void testEventMethodInvocation(String name, String newName, int age)
Test for notify a change in the name of the input person object.
void testSerialization(boolean expected, @TempDir Path tempDir)
Test of the serialization of a boolean.
void testSerialization(int[] expected, @TempDir Path tempDir)
Test of the serialization of a array of integers.
void unregisterAllListeners()
Removes all the listeners from the list of the registered listeners.
static< T > void valueChangedTest(T oldValue, T newValue)
Method for values changes.
void onNameChanged(BiConsumer< String, String > consumer)
void testUnregisterEvent(int value, TestInfo testInfo)
Test for a unregister event.
private< T > T convertInstanceOfObject(Object obj, Class< T > cls)
Method for convert a instance of a object.
void testUnregisterAllEvents(int value, TestInfo testInfo)
Test for a unregister all events.
void testEventNotification(boolean notifyChange, TestInfo testInfo)
Test of enable/disable of value change notification.
void testSerialization(byte expected, @TempDir Path tempDir)
Test of the serialization of a byte.
The TimingExtension class implements the BeforeTestExecutionCallback and the AfterTestExecutionCallba...
void testSerialization(short expected, @TempDir Path tempDir)
Test of the serialization of a short.
T registerListener(T listener)
Adds the specified listener to the list of registered listeners.
void testSerialization(float expected, @TempDir Path tempDir)
Test of the serialization of a float.
static boolean eventMethodInvoked
void unregisterListener(T listener)
Removes the first occurrence of the specified listener from the list of the registered listeners.
void testEventMethodInvocation(int expected, int value, TestInfo testInfo)
Test for notify a change in the input integer value.
The listener interface for receiving value change events.
void testEventMethodNotInvoqued(int value, TestInfo testInfo)
Test for a event not fired.
void testSerialization(String name, int age, @TempDir Path tempDir)
Test of the serialization of a object.
void testSerialization(int expected, @TempDir Path tempDir)
Test of the serialization of a int.
void testSerialization(char expected, @TempDir Path tempDir)
Test of the serialization of a char.
void set(T value)
Changes the value with the new given one, and notifies all the registered listeners of the change.
T get()
Returns the value that is being observed.