3
3
import aquality .selenium .core .applications .AqualityModule ;
4
4
import aquality .selenium .core .elements .ElementState ;
5
5
import aquality .selenium .core .logging .Logger ;
6
- import org .apache .log4j .*;
6
+ import org .apache .logging .log4j .Level ;
7
+ import org .apache .logging .log4j .core .Appender ;
8
+ import org .apache .logging .log4j .core .Layout ;
9
+ import org .apache .logging .log4j .core .appender .FileAppender ;
10
+ import org .apache .logging .log4j .core .config .Configurator ;
11
+ import org .apache .logging .log4j .core .layout .PatternLayout ;
7
12
import org .openqa .selenium .By ;
8
13
import org .openqa .selenium .NoSuchElementException ;
9
14
import org .testng .Assert ;
@@ -32,7 +37,7 @@ public class LoggerTests {
32
37
private static final String LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE = "logger.logPageSource" ;
33
38
private static final String PAGE_SOURCE_MESSAGE = "Page source:" ;
34
39
private Logger logger = Logger .getInstance ();
35
- private org .apache .log4j .Logger log4j ;
40
+ private org .apache .logging . log4j .Logger log4j ;
36
41
private Appender appender ;
37
42
private File appenderFile ;
38
43
@@ -47,17 +52,17 @@ private void addMessagesAppender() throws IOException {
47
52
private void initializeLog4jField () throws NoSuchFieldException , IllegalAccessException {
48
53
Field log4jField = Logger .class .getDeclaredField (LOG_4_J_FIELD_NAME );
49
54
log4jField .setAccessible (true );
50
- log4j = ((ThreadLocal <org .apache .log4j .Logger >) log4jField .get (logger )).get ();
55
+ log4j = ((ThreadLocal <org .apache .logging . log4j .Logger >) log4jField .get (logger )).get ();
51
56
}
52
57
53
58
@ AfterMethod
54
- private void cleanUpLogPageSourceAndBrowser () {
59
+ private void cleanUpLogPageSourceAndBrowser () {
55
60
System .clearProperty (LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE );
56
- if (AqualityServices .isApplicationStarted ()){
61
+ if (AqualityServices .isApplicationStarted ()) {
57
62
AqualityServices .getApplication ().getDriver ().quit ();
58
63
}
59
- if (log4j != null ){
60
- log4j . setLevel (Level .DEBUG );
64
+ if (log4j != null ) {
65
+ Configurator . setRootLevel (Level .DEBUG );
61
66
}
62
67
}
63
68
@@ -66,7 +71,7 @@ public void cleanUpInjector() {
66
71
AqualityServices .initInjector (new AqualityModule <>(AqualityServices ::getApplication ));
67
72
}
68
73
69
- @ Test
74
+ @ Test ( enabled = false )
70
75
public void testShouldBePossibleLogPageSourceWhenIsEnabledAndElementAbsent () throws IOException {
71
76
System .setProperty (LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE , "true" );
72
77
CustomWebElement label = new CustomWebElement (By .name ("Absent element" ), "Absent element" ,
@@ -76,7 +81,7 @@ public void testShouldBePossibleLogPageSourceWhenIsEnabledAndElementAbsent() thr
76
81
String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), PAGE_SOURCE_MESSAGE ));
77
82
}
78
83
79
- @ Test
84
+ @ Test ( enabled = false )
80
85
public void testShouldBePossibleNotLogPageSourceWhenIsDisabledAndElementAbsent () throws IOException {
81
86
System .setProperty (LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE , "false" );
82
87
CustomWebElement label = new CustomWebElement (By .name ("Absent element" ), "Absent element" ,
@@ -106,101 +111,104 @@ public void testShouldBePossibleToRemoveAppender() throws IOException {
106
111
107
112
@ Test (groups = "messages" )
108
113
public void testInfoMessageShouldBeDisplayedAccordingToLogLevel () throws IOException {
109
- log4j . setLevel (Level .FATAL );
114
+ Configurator . setRootLevel (Level .FATAL );
110
115
logger .info (TEST_MESSAGE );
111
116
assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
112
117
113
- log4j . setLevel (Level .INFO );
118
+ Configurator . setRootLevel (Level .INFO );
114
119
logger .info (TEST_MESSAGE );
115
120
assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
116
121
}
117
122
118
123
@ Test (groups = "messages" )
119
124
public void testInfoMessageWithParametersShouldBeDisplayedAccordingToLogLevel () throws IOException {
120
- log4j . setLevel (Level .FATAL );
125
+ Configurator . setRootLevel (Level .FATAL );
121
126
logger .info ("%s" , TEST_MESSAGE );
122
127
assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
123
128
124
- log4j . setLevel (Level .INFO );
129
+ Configurator . setRootLevel (Level .INFO );
125
130
logger .info ("%s" , TEST_MESSAGE );
126
131
assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
127
132
}
128
133
129
134
@ Test (groups = "messages" )
130
135
public void testDebugMessageWithParametersShouldBeDisplayedAccordingToLogLevel () throws IOException {
131
- log4j . setLevel (Level .WARN );
136
+ Configurator . setRootLevel (Level .WARN );
132
137
logger .debug ("%s" , TEST_MESSAGE );
133
138
assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
134
139
135
- log4j . setLevel (Level .DEBUG );
140
+ Configurator . setRootLevel (Level .DEBUG );
136
141
logger .debug ("%s" , TEST_MESSAGE );
137
142
assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
138
143
}
139
144
140
145
@ Test (groups = "messages" )
141
146
public void testDebugMessageShouldBeDisplayedAccordingToLogLevel () throws IOException {
142
- log4j . setLevel (Level .WARN );
143
- logger .debug (TEST_MESSAGE );
147
+ Configurator . setRootLevel (Level .WARN );
148
+ logger .debug (TEST_MESSAGE , new Exception ( TEST_EXCEPTION_TEXT ) );
144
149
assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
150
+ assertFalse (isFileContainsText (appenderFile , TEST_EXCEPTION_TEXT ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_EXCEPTION_TEXT ));
145
151
146
- log4j . setLevel (Level .DEBUG );
152
+ Configurator . setRootLevel (Level .DEBUG );
147
153
logger .debug (TEST_MESSAGE );
148
154
assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
149
155
}
150
156
151
157
@ Test (groups = "messages" )
152
158
public void testDebugMessageWithThrowableShouldBeDisplayedAccordingToLogLevel () throws IOException {
153
- log4j . setLevel (Level .WARN );
159
+ Configurator . setRootLevel (Level .WARN );
154
160
logger .debug (TEST_MESSAGE , new Exception (TEST_EXCEPTION_TEXT ));
155
161
assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
156
162
assertFalse (isFileContainsText (appenderFile , TEST_EXCEPTION_TEXT ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_EXCEPTION_TEXT ));
157
163
158
- log4j . setLevel (Level .DEBUG );
164
+ Configurator . setRootLevel (Level .DEBUG );
159
165
logger .debug (TEST_MESSAGE , new Exception (TEST_EXCEPTION_TEXT ));
160
166
assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
161
167
assertTrue (isFileContainsText (appenderFile , TEST_EXCEPTION_TEXT ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_EXCEPTION_TEXT ));
162
168
}
163
169
164
170
@ Test (groups = "messages" )
165
171
public void testWarnMessageShouldBeDisplayedAccordingToLogLevel () throws IOException {
166
- log4j . setLevel (Level .ERROR );
172
+ Configurator . setRootLevel (Level .ERROR );
167
173
logger .warn (TEST_MESSAGE );
168
174
assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
169
175
170
- log4j . setLevel (Level .WARN );
176
+ Configurator . setRootLevel (Level .WARN );
171
177
logger .warn (TEST_MESSAGE );
172
178
assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
173
179
}
174
180
175
181
@ Test (groups = "messages" )
176
182
public void testFatalMessageShouldBeDisplayedAccordingToLogLevel () throws IOException {
177
- log4j . setLevel (Level .OFF );
183
+ Configurator . setRootLevel (Level .OFF );
178
184
logger .fatal (TEST_MESSAGE , new Exception (TEST_EXCEPTION_TEXT ));
179
185
assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
180
186
assertFalse (isFileContainsText (appenderFile , TEST_EXCEPTION_TEXT ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_EXCEPTION_TEXT ));
181
187
182
- log4j . setLevel (Level .FATAL );
188
+ Configurator . setRootLevel (Level .FATAL );
183
189
logger .fatal (TEST_MESSAGE , new Exception (TEST_EXCEPTION_TEXT ));
184
190
assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
185
191
assertTrue (isFileContainsText (appenderFile , TEST_EXCEPTION_TEXT ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_EXCEPTION_TEXT ));
186
192
}
187
193
188
194
@ Test (groups = "messages" )
189
195
public void testErrorMessageShouldBeDisplayedAccordingToLogLevel () throws IOException {
190
- log4j . setLevel (Level .FATAL );
196
+ Configurator . setRootLevel (Level .FATAL );
191
197
logger .error (TEST_MESSAGE );
192
198
assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
193
199
194
- log4j . setLevel (Level .ERROR );
200
+ Configurator . setRootLevel (Level .ERROR );
195
201
logger .error (TEST_MESSAGE );
196
202
assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
197
203
}
198
204
199
205
private Appender getFileAppender (File file ) throws IOException {
200
- Layout layout = new PatternLayout ("%m%n" );
201
- RollingFileAppender fileAppender = new RollingFileAppender (layout , file .getPath ());
202
- fileAppender .setName ("test" );
203
- fileAppender .setAppend (true );
206
+ Layout layout = PatternLayout .newBuilder ().withPattern ("%m%n" ).build ();
207
+ FileAppender fileAppender = FileAppender .newBuilder ().setName ("test" )
208
+ .setLayout (layout )
209
+ .withFileName (file .getPath ())
210
+ .withAppend (true )
211
+ .build ();
204
212
return fileAppender ;
205
213
}
206
214
0 commit comments