Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / logging-test.js
1 "use strict";
2 var vows = require('vows')
3 , assert = require('assert')
4 , sandbox = require('sandboxed-module');
5
6 function setupConsoleTest() {
7   var fakeConsole = {}
8   , logEvents = []
9   , log4js;
10
11   ['trace','debug','log','info','warn','error'].forEach(function(fn) {
12     fakeConsole[fn] = function() {
13       throw new Error("this should not be called.");
14     };
15   });
16
17   log4js = sandbox.require(
18     '../lib/log4js',
19     {
20       globals: {
21         console: fakeConsole
22       }
23     }
24   );
25
26   log4js.clearAppenders();
27   log4js.addAppender(function(evt) {
28     logEvents.push(evt);
29   });
30
31   return { log4js: log4js, logEvents: logEvents, fakeConsole: fakeConsole };
32 }
33
34 vows.describe('log4js').addBatch({
35
36     'getBufferedLogger': {
37         topic: function () {
38             var log4js = require('../lib/log4js');
39             log4js.clearAppenders();
40             var logger = log4js.getBufferedLogger('tests');
41             return logger;
42         },
43
44         'should take a category and return a logger': function (logger) {
45             assert.equal(logger.target.category, 'tests');
46             assert.isFunction(logger.flush);
47             assert.isFunction(logger.trace);
48             assert.isFunction(logger.debug);
49             assert.isFunction(logger.info);
50             assert.isFunction(logger.warn);
51             assert.isFunction(logger.error);
52             assert.isFunction(logger.fatal);
53         },
54
55         'cache events': {
56             topic: function () {
57                 var log4js = require('../lib/log4js');
58                 log4js.clearAppenders();
59                 var logger = log4js.getBufferedLogger('tests1');
60                 var events = [];
61                 logger.target.addListener("log", function (logEvent) { events.push(logEvent); });
62                 logger.debug("Debug event");
63                 logger.trace("Trace event 1");
64                 logger.trace("Trace event 2");
65                 logger.warn("Warning event");
66                 logger.error("Aargh!", new Error("Pants are on fire!"));
67                 logger.error(
68                   "Simulated CouchDB problem",
69                   { err: 127, cause: "incendiary underwear" }
70                 );
71                 return events;
72             },
73
74             'should not emit log events if .flush() is not called.': function (events) {
75                 assert.equal(events.length, 0);
76             }
77         },
78
79         'log events after flush() is called': {
80             topic: function () {
81                 var log4js = require('../lib/log4js');
82                 log4js.clearAppenders();
83                 var logger = log4js.getBufferedLogger('tests2');
84                 logger.target.setLevel("TRACE");
85                 var events = [];
86                 logger.target.addListener("log", function (logEvent) { events.push(logEvent); });
87                 logger.debug("Debug event");
88                 logger.trace("Trace event 1");
89                 logger.trace("Trace event 2");
90                 logger.warn("Warning event");
91                 logger.error("Aargh!", new Error("Pants are on fire!"));
92                 logger.error(
93                   "Simulated CouchDB problem",
94                   { err: 127, cause: "incendiary underwear" }
95                 );
96                 logger.flush();
97                 return events;
98             },
99
100             'should emit log events when .flush() is called.': function (events) {
101                 assert.equal(events.length, 6);
102             }
103         }
104     },
105
106
107   'getLogger': {
108     topic: function() {
109       var log4js = require('../lib/log4js');
110       log4js.clearAppenders();
111       var logger = log4js.getLogger('tests');
112       logger.setLevel("DEBUG");
113       return logger;
114     },
115
116     'should take a category and return a logger': function(logger) {
117       assert.equal(logger.category, 'tests');
118       assert.equal(logger.level.toString(), "DEBUG");
119       assert.isFunction(logger.debug);
120       assert.isFunction(logger.info);
121       assert.isFunction(logger.warn);
122       assert.isFunction(logger.error);
123       assert.isFunction(logger.fatal);
124     },
125
126     'log events' : {
127       topic: function(logger) {
128         var events = [];
129         logger.addListener("log", function (logEvent) { events.push(logEvent); });
130         logger.debug("Debug event");
131         logger.trace("Trace event 1");
132         logger.trace("Trace event 2");
133         logger.warn("Warning event");
134         logger.error("Aargh!", new Error("Pants are on fire!"));
135         logger.error("Simulated CouchDB problem", { err: 127, cause: "incendiary underwear" });
136         return events;
137       },
138
139       'should emit log events': function(events) {
140         assert.equal(events[0].level.toString(), 'DEBUG');
141         assert.equal(events[0].data[0], 'Debug event');
142         assert.instanceOf(events[0].startTime, Date);
143       },
144
145       'should not emit events of a lower level': function(events) {
146         assert.equal(events.length, 4);
147         assert.equal(events[1].level.toString(), 'WARN');
148       },
149
150       'should include the error if passed in': function(events) {
151         assert.instanceOf(events[2].data[1], Error);
152         assert.equal(events[2].data[1].message, 'Pants are on fire!');
153       }
154     }
155   },
156
157   'when shutdown is called': {
158     topic: function() {
159       var callback = this.callback;
160       var events = {
161        appenderShutdownCalled: false,
162        shutdownCallbackCalled: false
163       },
164       log4js = sandbox.require(
165         '../lib/log4js',
166         {
167           requires: {
168             './appenders/file':
169             {
170               name: "file",
171               appender: function() {},
172               configure: function(configuration) {
173                 return function() {};
174               },
175               shutdown: function(cb) {
176                 events.appenderShutdownCalled = true;
177                 cb();
178               }
179             }
180           }
181         }
182       ),
183       config = { appenders:
184                  [ { "type" : "file",
185                      "filename" : "cheesy-wotsits.log",
186                      "maxLogSize" : 1024,
187                      "backups" : 3
188                    }
189                  ]
190                };
191
192       log4js.configure(config);
193       log4js.shutdown(function shutdownCallback() {
194          events.shutdownCallbackCalled = true;
195          // Re-enable log writing so other tests that use logger are not
196          // affected.
197          require('../lib/logger').enableAllLogWrites();
198          callback(null, events);
199       });
200     },
201
202     'should invoke appender shutdowns': function(events) {
203       assert.ok(events.appenderShutdownCalled);
204     },
205
206     'should call callback': function(events) {
207       assert.ok(events.shutdownCallbackCalled);
208     }
209   },
210
211   'invalid configuration': {
212     'should throw an exception': function() {
213       assert.throws(function() {
214         require('log4js').configure({ "type": "invalid" });
215       });
216     }
217   },
218
219   'configuration when passed as object': {
220     topic: function() {
221       var appenderConfig,
222       log4js = sandbox.require(
223         '../lib/log4js',
224         {
225           requires: {
226             './appenders/file':
227             {
228               name: "file",
229               appender: function() {},
230               configure: function(configuration) {
231                 appenderConfig = configuration;
232                 return function() {};
233               }
234             }
235           }
236         }
237       ),
238       config = { appenders:
239                  [ { "type" : "file",
240                      "filename" : "cheesy-wotsits.log",
241                      "maxLogSize" : 1024,
242                      "backups" : 3
243                    }
244                  ]
245                };
246       log4js.configure(config);
247       return appenderConfig;
248     },
249     'should be passed to appender config': function(configuration) {
250       assert.equal(configuration.filename, 'cheesy-wotsits.log');
251     }
252   },
253
254   'configuration that causes an error': {
255     topic: function() {
256       var log4js = sandbox.require(
257         '../lib/log4js',
258         {
259           requires: {
260             './appenders/file':
261             {
262               name: "file",
263               appender: function() {},
264               configure: function(configuration) {
265                 throw new Error("oh noes");
266               }
267             }
268           }
269         }
270       ),
271       config = { appenders:
272                  [ { "type" : "file",
273                      "filename" : "cheesy-wotsits.log",
274                      "maxLogSize" : 1024,
275                      "backups" : 3
276                    }
277                  ]
278                };
279       try {
280         log4js.configure(config);
281       } catch (e) {
282         return e;
283       }
284     },
285     'should wrap error in a meaningful message': function(e) {
286       assert.ok(e.message.indexOf('log4js configuration problem for') > -1);
287     }
288   },
289
290   'configuration when passed as filename': {
291     topic: function() {
292       var appenderConfig,
293       configFilename,
294       log4js = sandbox.require(
295         '../lib/log4js',
296         { requires:
297           { 'fs':
298             { statSync:
299               function() {
300                 return { mtime: Date.now() };
301               },
302               readFileSync:
303               function(filename) {
304                 configFilename = filename;
305                 return JSON.stringify({
306                   appenders: [
307                     { type: "file"
308                       , filename: "whatever.log"
309                     }
310                   ]
311                 });
312               },
313               readdirSync:
314               function() {
315                 return ['file'];
316               }
317             },
318             './appenders/file':
319             { name: "file",
320               appender: function() {},
321               configure: function(configuration) {
322                 appenderConfig = configuration;
323                 return function() {};
324               }
325             }
326           }
327         }
328       );
329       log4js.configure("/path/to/cheese.json");
330       return [ configFilename, appenderConfig ];
331     },
332     'should read the config from a file': function(args) {
333       assert.equal(args[0], '/path/to/cheese.json');
334     },
335     'should pass config to appender': function(args) {
336       assert.equal(args[1].filename, "whatever.log");
337     }
338   },
339
340   'with no appenders defined' : {
341     topic: function() {
342       var logger,
343       that = this,
344       fakeConsoleAppender = {
345         name: "console",
346         appender: function() {
347           return function(evt) {
348             that.callback(null, evt);
349           };
350         },
351         configure: function() {
352           return fakeConsoleAppender.appender();
353         }
354       },
355       log4js = sandbox.require(
356         '../lib/log4js',
357         {
358           requires: {
359             './appenders/console': fakeConsoleAppender
360           }
361         }
362       );
363       logger = log4js.getLogger("some-logger");
364       logger.debug("This is a test");
365     },
366     'should default to the console appender': function(evt) {
367       assert.equal(evt.data[0], "This is a test");
368     }
369   },
370
371   'addAppender' : {
372     topic: function() {
373       var log4js = require('../lib/log4js');
374       log4js.clearAppenders();
375       return log4js;
376     },
377     'without a category': {
378       'should register the function as a listener for all loggers': function (log4js) {
379         var appenderEvent,
380         appender = function(evt) { appenderEvent = evt; },
381         logger = log4js.getLogger("tests");
382
383         log4js.addAppender(appender);
384         logger.debug("This is a test");
385         assert.equal(appenderEvent.data[0], "This is a test");
386         assert.equal(appenderEvent.categoryName, "tests");
387         assert.equal(appenderEvent.level.toString(), "DEBUG");
388       },
389       'if an appender for a category is defined': {
390         'should register for that category': function (log4js) {
391           var otherEvent,
392           appenderEvent,
393           cheeseLogger;
394
395           log4js.addAppender(function (evt) { appenderEvent = evt; });
396           log4js.addAppender(function (evt) { otherEvent = evt; }, 'cheese');
397
398           cheeseLogger = log4js.getLogger('cheese');
399           cheeseLogger.debug('This is a test');
400           assert.deepEqual(appenderEvent, otherEvent);
401           assert.equal(otherEvent.data[0], 'This is a test');
402           assert.equal(otherEvent.categoryName, 'cheese');
403
404           otherEvent = undefined;
405           appenderEvent = undefined;
406           log4js.getLogger('pants').debug("this should not be propagated to otherEvent");
407           assert.isUndefined(otherEvent);
408           assert.equal(appenderEvent.data[0], "this should not be propagated to otherEvent");
409         }
410       }
411     },
412
413     'with a category': {
414       'should only register the function as a listener for that category': function(log4js) {
415         var appenderEvent,
416         appender = function(evt) { appenderEvent = evt; },
417         logger = log4js.getLogger("tests");
418
419         log4js.addAppender(appender, 'tests');
420         logger.debug('this is a category test');
421         assert.equal(appenderEvent.data[0], 'this is a category test');
422
423         appenderEvent = undefined;
424         log4js.getLogger('some other category').debug('Cheese');
425         assert.isUndefined(appenderEvent);
426       }
427     },
428
429     'with multiple categories': {
430       'should register the function as a listener for all the categories': function(log4js) {
431         var appenderEvent,
432         appender = function(evt) { appenderEvent = evt; },
433         logger = log4js.getLogger('tests');
434
435         log4js.addAppender(appender, 'tests', 'biscuits');
436
437         logger.debug('this is a test');
438         assert.equal(appenderEvent.data[0], 'this is a test');
439         appenderEvent = undefined;
440
441         var otherLogger = log4js.getLogger('biscuits');
442         otherLogger.debug("mmm... garibaldis");
443         assert.equal(appenderEvent.data[0], "mmm... garibaldis");
444
445         appenderEvent = undefined;
446
447         log4js.getLogger("something else").debug("pants");
448         assert.isUndefined(appenderEvent);
449       },
450       'should register the function when the list of categories is an array': function(log4js) {
451         var appenderEvent,
452         appender = function(evt) { appenderEvent = evt; };
453
454         log4js.addAppender(appender, ['tests', 'pants']);
455
456         log4js.getLogger('tests').debug('this is a test');
457         assert.equal(appenderEvent.data[0], 'this is a test');
458
459         appenderEvent = undefined;
460
461         log4js.getLogger('pants').debug("big pants");
462         assert.equal(appenderEvent.data[0], "big pants");
463
464         appenderEvent = undefined;
465
466         log4js.getLogger("something else").debug("pants");
467         assert.isUndefined(appenderEvent);
468       }
469     }
470   },
471
472   'default setup': {
473     topic: function() {
474       var appenderEvents = [],
475       fakeConsole = {
476         'name': 'console',
477         'appender': function () {
478           return function(evt) {
479             appenderEvents.push(evt);
480           };
481         },
482         'configure': function (config) {
483           return fakeConsole.appender();
484         }
485       },
486       globalConsole = {
487         log: function() { }
488       },
489       log4js = sandbox.require(
490         '../lib/log4js',
491         {
492           requires: {
493             './appenders/console': fakeConsole
494           },
495           globals: {
496             console: globalConsole
497           }
498         }
499       ),
500       logger = log4js.getLogger('a-test');
501
502       logger.debug("this is a test");
503       globalConsole.log("this should not be logged");
504
505       return appenderEvents;
506     },
507
508     'should configure a console appender': function(appenderEvents) {
509       assert.equal(appenderEvents[0].data[0], 'this is a test');
510     },
511
512     'should not replace console.log with log4js version': function(appenderEvents) {
513       assert.equal(appenderEvents.length, 1);
514     }
515   },
516
517   'console' : {
518     topic: setupConsoleTest,
519
520     'when replaceConsole called': {
521       topic: function(test) {
522         test.log4js.replaceConsole();
523
524         test.fakeConsole.log("Some debug message someone put in a module");
525         test.fakeConsole.debug("Some debug");
526         test.fakeConsole.error("An error");
527         test.fakeConsole.info("some info");
528         test.fakeConsole.warn("a warning");
529
530         test.fakeConsole.log("cheese (%s) and biscuits (%s)", "gouda", "garibaldis");
531         test.fakeConsole.log({ lumpy: "tapioca" });
532         test.fakeConsole.log("count %d", 123);
533         test.fakeConsole.log("stringify %j", { lumpy: "tapioca" });
534
535         return test.logEvents;
536       },
537
538       'should replace console.log methods with log4js ones': function(logEvents) {
539         assert.equal(logEvents.length, 9);
540         assert.equal(logEvents[0].data[0], "Some debug message someone put in a module");
541         assert.equal(logEvents[0].level.toString(), "INFO");
542         assert.equal(logEvents[1].data[0], "Some debug");
543         assert.equal(logEvents[1].level.toString(), "DEBUG");
544         assert.equal(logEvents[2].data[0], "An error");
545         assert.equal(logEvents[2].level.toString(), "ERROR");
546         assert.equal(logEvents[3].data[0], "some info");
547         assert.equal(logEvents[3].level.toString(), "INFO");
548         assert.equal(logEvents[4].data[0], "a warning");
549         assert.equal(logEvents[4].level.toString(), "WARN");
550         assert.equal(logEvents[5].data[0], "cheese (%s) and biscuits (%s)");
551         assert.equal(logEvents[5].data[1], "gouda");
552         assert.equal(logEvents[5].data[2], "garibaldis");
553       }
554     },
555     'when turned off': {
556       topic: function(test) {
557         test.log4js.restoreConsole();
558         try {
559           test.fakeConsole.log("This should cause the error described in the setup");
560         } catch (e) {
561           return e;
562         }
563       },
564       'should call the original console methods': function (err) {
565         assert.instanceOf(err, Error);
566         assert.equal(err.message, "this should not be called.");
567       }
568     }
569   },
570   'console configuration': {
571     topic: setupConsoleTest,
572     'when disabled': {
573       topic: function(test) {
574         test.log4js.replaceConsole();
575         test.log4js.configure({ replaceConsole: false });
576         try {
577           test.fakeConsole.log("This should cause the error described in the setup");
578         } catch (e) {
579           return e;
580         }
581       },
582       'should allow for turning off console replacement': function (err) {
583         assert.instanceOf(err, Error);
584         assert.equal(err.message, 'this should not be called.');
585       }
586     },
587     'when enabled': {
588       topic: function(test) {
589         test.log4js.restoreConsole();
590         test.log4js.configure({ replaceConsole: true });
591         //log4js.configure clears all appenders
592         test.log4js.addAppender(function(evt) {
593           test.logEvents.push(evt);
594         });
595
596         test.fakeConsole.debug("Some debug");
597         return test.logEvents;
598       },
599
600       'should allow for turning on console replacement': function (logEvents) {
601         assert.equal(logEvents.length, 1);
602         assert.equal(logEvents[0].level.toString(), "DEBUG");
603         assert.equal(logEvents[0].data[0], "Some debug");
604       }
605     }
606   },
607   'configuration persistence' : {
608     topic: function() {
609       var logEvent,
610       firstLog4js = require('../lib/log4js'),
611       secondLog4js;
612
613       firstLog4js.clearAppenders();
614       firstLog4js.addAppender(function(evt) { logEvent = evt; });
615
616       secondLog4js = require('../lib/log4js');
617       secondLog4js.getLogger().info("This should go to the appender defined in firstLog4js");
618
619       return logEvent;
620     },
621     'should maintain appenders between requires': function (logEvent) {
622       assert.equal(logEvent.data[0], "This should go to the appender defined in firstLog4js");
623     }
624   },
625
626   'getDefaultLogger': {
627     topic: function() {
628       return require('../lib/log4js').getDefaultLogger();
629     },
630     'should return a logger': function(logger) {
631       assert.ok(logger.info);
632       assert.ok(logger.debug);
633       assert.ok(logger.error);
634     }
635   }
636 }).export(module);