1 classdef mlunit_test_log4jconfigurator < matlab.unittest.TestCase
6 function self = mlunit_test_log4jconfigurator(varargin)
8 {
'parallelConfiguration'});
10 self =
self@matlab.unittest.TestCase(reg{1:min(nReg,2)});
12 self.configurationProp = {
'configuration', prop{2}};
14 self.configurationProp = {};
20 function test_configuration_persistence(
self)
21 if ~isempty(which('
mxberry.pcalc.auxdfeval'))
23 @(x)self.aux_test_configuration_persistence,...
24 cell(1,1), 'alwaysFork', true,...
25 self.configurationProp{:});
29 function self=test_getLogger(
self)
30 logger=
mxberry.log.log4j.test.Log4jConfigurator.getLogger();
31 loggerName=
char(logger.getName());
32 logger2=
mxberry.log.log4j.test.Log4jConfigurator.getLogger(loggerName);
33 loggerName2=
char(logger2.getName());
34 self.verifyEqual(loggerName,loggerName2);
36 function self=test_getLoggerBySuffix(self)
37 logger=
mxberry.log.log4j.test.Log4jConfigurator.getLogger();
38 loggerName=
char(logger.getName());
39 logger2=
mxberry.log.log4j.test.Log4jConfigurator.getLogger(loggerName,false);
40 loggerName2=
char(logger2.getName());
41 self.verifyEqual(loggerName,loggerName2);
42 logger2=
mxberry.log.log4j.test.Log4jConfigurator.getLogger('suffix',true);
43 loggerName2=
char(logger2.getName());
44 self.verifyEqual([loggerName '.suffix'],loggerName2);
47 methods (Access=private)
49 function aux_test_configuration_persistence(self)
50 import
mxberry.log.log4j.test.Log4jConfigurator;
51 import org.apache.log4j.Level;
52 lastPropStr=
mxberry.log.log4j.Log4jConfigurator.getLastLogPropStr;
53 isLocked=
mxberry.log.log4j.Log4jConfigurator.isLocked();
54 onCln=onCleanup(@()restoreConf(lastPropStr,isLocked));
57 appenderConfStr = ['log4j.appender.stdout=org.apache.log4j.ConsoleAppender',NL,...
58 'log4j.appender.stdout.layout=org.apache.log4j.PatternLayout',NL,...
59 'log4j.appender.stdout.layout.ConversionPattern=%5p %c - %m\\n'];
60 % Unlock and reconfigure
61 Log4jConfigurator.unlockConfiguration();
62 self.verifyEqual(false,Log4jConfigurator.isLocked());
63 confStr = ['log4j.rootLogger=WARN,stdout', NL, appenderConfStr];
64 evalc('Log4jConfigurator.configure(confStr)');
65 self.verifyEqual(true,Log4jConfigurator.isConfigured());
66 self.verifyEqual(confStr,Log4jConfigurator.getLastLogPropStr());
67 % Lock configuration and try to configure log4j again, using a
68 % different level than it currently has. Log4jConfigurator
69 % should do nothing, besides issuing a warning, and the level
70 % should remain unchanged.
71 Log4jConfigurator.lockConfiguration();
72 self.verifyEqual(true,Log4jConfigurator.isLocked());
73 confStr = ['log4j.rootLogger=INFO,stdout', NL, appenderConfStr]; %
#ok<NASGU> 74 outputText = evalc(
'Log4jConfigurator.configure(confStr)');
75 if isempty( strfind(outputText,
'WARN') )
76 self.assertFail('Log4jConfigurator.configure should have issued at least 1 warning');
78 if isempty( regexp(outputText, 'in .* at line \d+', 'once') )
79 self.assertFail('Log4jConfigurator.configure did not print a stack trace');
81 % Create a logger instance and check log level
82 logger=Log4jConfigurator.getLogger();
83 if logger.isInfoEnabled()
84 self.assertFail('Locked Log4jConfigurator should not allow a configuration change');
86 % Now try to configure using configureSimply
87 outputText = evalc('Log4jConfigurator.configureSimply(''INFO'')');
88 if logger.isInfoEnabled()
89 self.assertFail('Locked Log4jConfigurator should not allow a configuration change');
91 if isempty( strfind(outputText,'WARN') )
92 self.assertFail('Log4jConfigurator.configureSimply should have issued at least 1 warning');
94 if isempty( regexp(outputText, 'in .* at line \d+', 'once') )
95 self.assertFail('Log4jConfigurator.configureSimply did not print a stack trace');
97 % Unlock the configuration and try to change it
98 Log4jConfigurator.unlockConfiguration();
99 evalc('Log4jConfigurator.configure(''log4j.rootLogger=INFO'',''isLockAfterConfigure'',true)');
100 if ~logger.isInfoEnabled()
101 self.assertFail('Log4jConfigurator failed to change configuration');
103 self.verifyEqual(true,Log4jConfigurator.isLocked(),...
104 'Failed to lock configuration using isLockAfterConfigure property');
105 % Do the same using configureSimply
106 Log4jConfigurator.unlockConfiguration();
107 evalc('Log4jConfigurator.configureSimply(''WARN'',''isLockAfterConfigure'',true)');
108 if logger.isInfoEnabled()
109 self.assertFail('Log4jConfigurator failed to change configuration');
111 self.verifyEqual(true,Log4jConfigurator.isLocked(),...
112 'Failed to lock configuration using isLockAfterConfigure property');
113 function restoreConf(confStr,isLocked)
115 'MXBERRY:LOGGING:LOG4J:LOG4JCONFIGURATOR:CONFIGUREINTERNAL:emptyConfStr');
116 mxberry.log.log4j.test.Log4jConfigurator.unlockConfiguration();
117 mxberry.log.log4j.test.Log4jConfigurator.configure(...
118 confStr,'islockafterconfigure',isLocked);