1+ package org .burningwave .common ;
2+
3+
4+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
5+ import static org .junit .jupiter .api .Assertions .assertNull ;
6+ import static org .junit .jupiter .api .Assertions .assertTrue ;
7+
8+ import java .util .Collection ;
9+ import java .util .Iterator ;
10+ import java .util .function .Function ;
11+ import java .util .function .Supplier ;
12+
13+ import org .burningwave .Strings ;
14+ import org .burningwave .Throwables ;
15+ import org .junit .jupiter .api .function .Executable ;
16+ import org .junit .jupiter .api .function .ThrowingSupplier ;
17+
18+ @ SuppressWarnings ("unused" )
19+ //@TestMethodOrder(Random.class)
20+ //@TestMethodOrder(MethodName.class)
21+ public class BaseTest {
22+
23+ public void testNotNull (ThrowingSupplier <?> supplier ) {
24+ long initialTime = System .currentTimeMillis ();
25+ Object object = null ;
26+ try {
27+ logInfo (getClass ()::getName , getCallerMethod () + " - start execution" );
28+ object = supplier .get ();
29+ logInfo (getClass ()::getName , getCallerMethod () + " - end execution" );
30+ } catch (Throwable exc ) {
31+ logError (getClass ()::getName , getCallerMethod () + " - Exception occurred" , exc );
32+ }
33+ logInfo (
34+ getClass ()::getName ,
35+ getCallerMethod () + " - Elapsed time: " + getFormattedDifferenceOfMillis (System .currentTimeMillis (),initialTime )
36+ );
37+ assertNotNull (object );
38+ }
39+
40+ protected void testNotEmpty (ThrowingSupplier <Collection <?>> supplier ) {
41+ testNotEmpty (supplier , false );
42+ }
43+
44+ protected void testNotEmpty (ThrowingSupplier <Collection <?>> supplier , boolean printAllElements ) {
45+ long initialTime = System .currentTimeMillis ();
46+ Collection <?> coll = null ;
47+ boolean isNotEmpty = false ;
48+ try {
49+ coll = supplier .get ();
50+ logInfo (getClass ()::getName , getCallerMethod () + " - Found " + coll .size () + " items in " + getFormattedDifferenceOfMillis (System .currentTimeMillis (), initialTime ));
51+ isNotEmpty = !coll .isEmpty ();
52+ if (isNotEmpty && printAllElements ) {
53+ for (Object obj : coll ) {
54+ if (obj != null ) {
55+ logDebug (getClass ()::getName , "{}" , obj );
56+ }
57+ }
58+ }
59+ } catch (Throwable exc ) {
60+ logError (getClass ()::getName , getCallerMethod () + " - Exception occurred" , exc );
61+ }
62+ assertTrue (coll != null && !coll .isEmpty ());
63+ }
64+
65+ private String getCallerMethod () {
66+ StackTraceElement [] stackTraceElements = Thread .currentThread ().getStackTrace ();
67+ for (StackTraceElement stackTraceElement : stackTraceElements ) {
68+ String className = stackTraceElement .getClassName ();
69+ if (className .contains ("Test" ) && !className .contains ("BaseTest" )) {
70+ return stackTraceElement .getMethodName ();
71+ }
72+ }
73+ return null ;
74+ }
75+
76+ public <T extends AutoCloseable > void testNotEmpty (Supplier <T > autoCloaseableSupplier , Function <T , Collection <?>> collSupplier ) {
77+ testNotEmpty (autoCloaseableSupplier , collSupplier , false );
78+ }
79+
80+ public <T extends AutoCloseable > void testNotEmpty (Supplier <T > autoCloaseableSupplier , Function <T , Collection <?>> collSupplier , boolean printAllElements ) {
81+ long initialTime = System .currentTimeMillis ();
82+ Collection <?> coll = null ;
83+ boolean isNotEmpty = false ;
84+ try (T collectionSupplier = autoCloaseableSupplier .get ()){
85+ coll = collSupplier .apply (collectionSupplier );
86+ logInfo (getClass ()::getName , getCallerMethod () + " - Found " + coll .size () + " items in " + getFormattedDifferenceOfMillis (System .currentTimeMillis (), initialTime ));
87+ isNotEmpty = !coll .isEmpty ();
88+ if (isNotEmpty && printAllElements ) {
89+ int line = 0 ;
90+ Iterator <?> collIterator = coll .iterator ();
91+ while (collIterator .hasNext ()) {
92+ Object obj = collIterator .next ();
93+ logDebug (
94+ getClass ()::getName ,
95+ ++line + " " + (obj != null ? obj .toString () : "null" )
96+ );
97+ }
98+ }
99+ } catch (Throwable exc ) {
100+ logError (getClass ()::getName , getCallerMethod () + " - Exception occurred" , exc );
101+ }
102+ logInfo (
103+ getClass ()::getName ,
104+ getCallerMethod () + " - Elapsed time: " + getFormattedDifferenceOfMillis (System .currentTimeMillis (),initialTime )
105+ );
106+ assertTrue (isNotEmpty );
107+ }
108+
109+
110+ public <T extends AutoCloseable > void testNotNull (
111+ ThrowingSupplier <T > autoCloseableSupplier ,
112+ Function <T , ?> objectSupplier
113+ ) {
114+ long initialTime = System .currentTimeMillis ();
115+ try (T autoCloseable = autoCloseableSupplier .get ()) {
116+ assertNotNull (objectSupplier .apply (autoCloseable ));
117+ logInfo (getClass ()::getName , getCallerMethod () + " - Elapsed time: " + getFormattedDifferenceOfMillis (System .currentTimeMillis (), initialTime ));
118+ } catch (Throwable exc ) {
119+ logError (getClass ()::getName , getCallerMethod () + " - Exception occurred" , exc );
120+ }
121+ }
122+
123+
124+ public void testDoesNotThrow (Executable executable ) {
125+ Throwable throwable = null ;
126+ long initialTime = System .currentTimeMillis ();
127+ try {
128+ logDebug (getClass ()::getName , getCallerMethod () + " - Initializing logger" );
129+ executable .execute ();
130+ logInfo (getClass ()::getName , getCallerMethod () + " - Elapsed time: " + getFormattedDifferenceOfMillis (System .currentTimeMillis (), initialTime ));
131+ } catch (Throwable exc ) {
132+ logError (getClass ()::getName , getCallerMethod () + " - Exception occurred" , exc );
133+ throwable = exc ;
134+ }
135+ assertNull (throwable );
136+ }
137+
138+ public void testThrow (Executable executable ) {
139+ Throwable throwable = null ;
140+ long initialTime = System .currentTimeMillis ();
141+ try {
142+ logDebug (getClass ()::getName , getCallerMethod () + " - Initializing logger" );
143+ executable .execute ();
144+ logInfo (getClass ()::getName , getCallerMethod () + " - Elapsed time: " + getFormattedDifferenceOfMillis (System .currentTimeMillis (), initialTime ));
145+ } catch (Throwable exc ) {
146+ logError (getClass ()::getName , getCallerMethod () + " - Exception occurred" , exc );
147+ throwable = exc ;
148+ }
149+ assertNotNull (throwable );
150+ }
151+
152+
153+ String getFormattedDifferenceOfMillis (long value1 , long value2 ) {
154+ String valueFormatted = String .format ("%04d" , (value1 - value2 ));
155+ return valueFormatted .substring (0 , valueFormatted .length () - 3 ) + "," + valueFormatted .substring (valueFormatted .length () -3 );
156+ }
157+
158+ void waitFor (long seconds ) {
159+ try {
160+ Thread .sleep (seconds * 1000 );
161+ } catch (InterruptedException exc ) {
162+ Throwables .INSTANCE .throwException (exc );
163+ }
164+ }
165+
166+ public static void logError (Supplier <String > classNameSupplier , String message , Throwable exc ) {
167+ System .err .println (classNameSupplier .get () + " - " + message );
168+ exc .printStackTrace ();
169+ }
170+
171+ public static void logInfo (Supplier <String > classNameSupplier , String ... arguments ) {
172+ System .out .println (classNameSupplier .get () + " - " + String .join ("; " , arguments ));
173+ }
174+
175+ public static void logDebug (Supplier <String > classNameSupplier , String message , Object ... arguments ) {
176+ System .out .println (classNameSupplier .get () + " - " + Strings .INSTANCE .compile (message , arguments ));
177+ }
178+
179+ }
0 commit comments