Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / configureNoLevels-test.js
1 "use strict";
2 // This test shows unexpected behaviour for log4js.configure() in log4js-node@0.4.3 and earlier:
3 // 1) log4js.configure(), log4js.configure(null),
4 // log4js.configure({}), log4js.configure(<some object with no levels prop>)
5 // all set all loggers levels to trace, even if they were previously set to something else.
6 // 2) log4js.configure({levels:{}}), log4js.configure({levels: {foo:
7 // bar}}) leaves previously set logger levels intact.
8 //
9
10 // Basic set up
11 var vows = require('vows');
12 var assert = require('assert');
13 var toLevel = require('../lib/levels').toLevel;
14
15 // uncomment one or other of the following to see progress (or not) while running the tests
16 // var showProgress = console.log;
17 var showProgress = function() {};
18
19
20 // Define the array of levels as string to iterate over.
21 var strLevels= ['Trace','Debug','Info','Warn','Error','Fatal'];
22
23 // setup the configurations we want to test
24 var configs = {
25   'nop': 'nop', // special case where the iterating vows generator will not call log4js.configure
26   'is undefined': undefined,
27   'is null': null,
28   'is empty': {},
29   'has no levels': {foo: 'bar'},
30   'has null levels': {levels: null},
31   'has empty levels': {levels: {}},
32   'has random levels': {levels: {foo: 'bar'}},
33   'has some valid levels': {levels: {A: 'INFO'}}
34 };
35
36 // Set up the basic vows batches for this test
37 var batches = [];
38
39
40 function getLoggerName(level) {
41   return level+'-logger';
42 }
43
44 // the common vows top-level context, whether log4js.configure is called or not
45 // just making sure that the code is common,
46 // so that there are no spurious errors in the tests themselves.
47 function getTopLevelContext(nop, configToTest, name) {
48   return {
49     topic: function() {
50       var log4js = require('../lib/log4js');
51       // create loggers for each level,
52       // keeping the level in the logger's name for traceability
53       strLevels.forEach(function(l) {
54         log4js.getLogger(getLoggerName(l)).setLevel(l);
55       });
56
57       if (!nop) {
58         showProgress('** Configuring log4js with', configToTest);
59         log4js.configure(configToTest);
60       }
61       else {
62         showProgress('** Not configuring log4js');
63       }
64       return log4js;
65     }
66   };
67 }
68
69 showProgress('Populating batch object...');
70
71 function checkForMismatch(topic) {
72   var er = topic.log4js.levels.toLevel(topic.baseLevel)
73     .isLessThanOrEqualTo(topic.log4js.levels.toLevel(topic.comparisonLevel));
74
75   assert.equal(
76     er, 
77     topic.expectedResult, 
78     'Mismatch: for setLevel(' + topic.baseLevel + 
79       ') was expecting a comparison with ' + topic.comparisonLevel + 
80       ' to be ' + topic.expectedResult
81   );
82 }
83
84 function checkExpectedResult(topic) {
85   var result = topic.log4js
86     .getLogger(getLoggerName(topic.baseLevel))
87     .isLevelEnabled(topic.log4js.levels.toLevel(topic.comparisonLevel));
88   
89   assert.equal(
90     result, 
91     topic.expectedResult, 
92     'Failed: ' + getLoggerName(topic.baseLevel) + 
93       '.isLevelEnabled( ' + topic.comparisonLevel + ' ) returned ' + result
94   );
95 }
96
97 function setupBaseLevelAndCompareToOtherLevels(baseLevel) {
98   var baseLevelSubContext = 'and checking the logger whose level was set to '+baseLevel ;
99   var subContext = { topic: baseLevel };
100   batch[context][baseLevelSubContext] = subContext;
101
102   // each logging level has strLevels sub-contexts,
103   // to exhaustively test all the combinations of 
104   // setLevel(baseLevel) and isLevelEnabled(comparisonLevel) per config
105   strLevels.forEach(compareToOtherLevels(subContext));
106 }
107
108 function compareToOtherLevels(subContext) {
109   var baseLevel = subContext.topic;
110
111   return function (comparisonLevel) {
112     var comparisonLevelSubContext = 'with isLevelEnabled('+comparisonLevel+')';
113
114     // calculate this independently of log4js, but we'll add a vow 
115     // later on to check that we're not mismatched with log4js
116     var expectedResult = strLevels.indexOf(baseLevel) <= strLevels.indexOf(comparisonLevel);
117
118     // the topic simply gathers all the parameters for the vow 
119     // into an object, to simplify the vow's work.
120     subContext[comparisonLevelSubContext] = {
121       topic: function(baseLevel, log4js) {
122         return {
123           comparisonLevel: comparisonLevel, 
124           baseLevel: baseLevel, 
125           log4js: log4js, 
126           expectedResult: expectedResult
127         };
128       }
129     };
130
131     var vow = 'should return '+expectedResult;
132     subContext[comparisonLevelSubContext][vow] = checkExpectedResult;
133     
134     // the extra vow to check the comparison between baseLevel and
135     // comparisonLevel we performed earlier matches log4js'
136     // comparison too
137     var subSubContext = subContext[comparisonLevelSubContext];
138     subSubContext['finally checking for comparison mismatch with log4js'] = checkForMismatch;
139   };
140 }
141
142 // Populating the batches programmatically, as there are 
143 // (configs.length x strLevels.length x strLevels.length) = 324 
144 // possible test combinations
145 for (var cfg in configs) {
146   var configToTest = configs[cfg];
147   var nop = configToTest === 'nop';
148   var context;
149   if (nop) {
150     context = 'Setting up loggers with initial levels, then NOT setting a configuration,';
151   }
152   else {
153     context = 'Setting up loggers with initial levels, then setting a configuration which '+cfg+',';
154   }
155
156   showProgress('Setting up the vows batch and context for '+context);
157   // each config to be tested has its own vows batch with a single top-level context
158   var batch={};
159   batch[context]= getTopLevelContext(nop, configToTest, context);
160   batches.push(batch);
161
162   // each top-level context has strLevels sub-contexts, one per logger 
163   // which has set to a specific level in the top-level context's topic
164   strLevels.forEach(setupBaseLevelAndCompareToOtherLevels);
165 }
166
167 showProgress('Running tests');
168 var v = vows.describe('log4js.configure(), with or without a "levels" property');
169
170 batches.forEach(function(batch) {v=v.addBatch(batch);});
171
172 v.export(module);
173