MatrixBerryCore
mlunit_test_log4jconfigurator.m
Go to the documentation of this file.
1 classdef mlunit_test_log4jconfigurator < matlab.unittest.TestCase
2  properties
3  configurationProp
4  end
5  methods
6  function self = mlunit_test_log4jconfigurator(varargin)
7  [reg,prop] = mxberry.core.parseparams(varargin,...
8  {'parallelConfiguration'});
9  nReg = length(reg);
10  self = self@matlab.unittest.TestCase(reg{1:min(nReg,2)});
11  if ~isempty(prop)
12  self.configurationProp = {'configuration', prop{2}};
13  else
14  self.configurationProp = {};
15  end
16  end
17  end
18  methods (Test)
19  %
20  function test_configuration_persistence(self)
21  if ~isempty(which('mxberry.pcalc.auxdfeval'))
22  mxberry.pcalc.auxdfeval(...
23  @(x)self.aux_test_configuration_persistence,...
24  cell(1,1), 'alwaysFork', true,...
25  self.configurationProp{:});
26  end
27  end
28  %
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);
35  end
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);
45  end
46  end
47  methods (Access=private)
48  %
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));
55  %
56  NL = sprintf('\n');
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');
77  end
78  if isempty( regexp(outputText, 'in .* at line \d+', 'once') )
79  self.assertFail('Log4jConfigurator.configure did not print a stack trace');
80  end
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');
85  end
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');
90  end
91  if isempty( strfind(outputText,'WARN') )
92  self.assertFail('Log4jConfigurator.configureSimply should have issued at least 1 warning');
93  end
94  if isempty( regexp(outputText, 'in .* at line \d+', 'once') )
95  self.assertFail('Log4jConfigurator.configureSimply did not print a stack trace');
96  end
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');
102  end
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');
110  end
111  self.verifyEqual(true,Log4jConfigurator.isLocked(),...
112  'Failed to lock configuration using isLockAfterConfigure property');
113  function restoreConf(confStr,isLocked)
114  s=warning('off',...
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);
119  warning(s);
120  end
121  end
122  end
123 end