Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / layouts-test.js
1 "use strict";
2 var vows = require('vows')
3 , assert = require('assert')
4 , os =  require('os')
5 , semver = require('semver')
6 , EOL = os.EOL || '\n';
7
8 //used for patternLayout tests.
9 function test(args, pattern, value) {
10   var layout = args[0]
11   , event = args[1]
12   , tokens = args[2];
13
14   assert.equal(layout(pattern, tokens)(event), value);
15 }
16
17 vows.describe('log4js layouts').addBatch({
18   'colouredLayout': {
19     topic: function() {
20       return require('../lib/layouts').colouredLayout;
21     },
22
23     'should apply level colour codes to output': function(layout) {
24       var output = layout({
25         data: ["nonsense"],
26         startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
27         categoryName: "cheese",
28         level: {
29           toString: function() { return "ERROR"; }
30         }
31       });
32       assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mnonsense');
33     },
34     'should support the console.log format for the message': function(layout) {
35       var output = layout({
36         data: ["thing %d", 2],
37         startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
38         categoryName: "cheese",
39         level: {
40           toString: function() { return "ERROR"; }
41         }
42       });
43       assert.equal(output, '\x1B[31m[2010-12-05 14:18:30.045] [ERROR] cheese - \x1B[39mthing 2');
44     }
45   },
46
47   'messagePassThroughLayout': {
48     topic: function() {
49       return require('../lib/layouts').messagePassThroughLayout;
50     },
51     'should take a logevent and output only the message' : function(layout) {
52       assert.equal(layout({
53         data: ["nonsense"],
54         startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
55         categoryName: "cheese",
56         level: {
57           colour: "green",
58           toString: function() { return "ERROR"; }
59         }
60       }), "nonsense");
61     },
62     'should support the console.log format for the message' : function(layout) {
63       assert.equal(layout({
64         data: ["thing %d", 1, "cheese"],
65         startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
66         categoryName: "cheese",
67         level : {
68           colour: "green",
69           toString: function() { return "ERROR"; }
70         }
71       }), "thing 1 cheese");
72     },
73     'should output the first item even if it is not a string': function(layout) {
74       assert.equal(layout({
75         data: [ { thing: 1} ],
76         startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
77         categoryName: "cheese",
78         level: {
79           colour: "green",
80           toString: function() { return "ERROR"; }
81         }
82       }), "{ thing: 1 }");
83     },
84     'should print the stacks of a passed error objects': function(layout) {
85       assert.isArray(layout({
86         data: [ new Error() ],
87         startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
88         categoryName: "cheese",
89         level: {
90           colour: "green",
91           toString: function() { return "ERROR"; }
92         }
93       }).match(/Error\s+at Object\..*\s+\((.*)test[\\\/]layouts-test\.js\:\d+\:\d+\)\s+at runTest/)
94                      , 'regexp did not return a match');
95     },
96     'with passed augmented errors': {
97       topic: function(layout){
98         var e = new Error("My Unique Error Message");
99         e.augmented = "My Unique attribute value";
100         e.augObj = { at1: "at2" };
101         return layout({
102           data: [ e ],
103           startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
104           categoryName: "cheese",
105           level: {
106             colour: "green",
107             toString: function() { return "ERROR"; }
108           }
109         });
110       },
111       'should print error the contained error message': function(layoutOutput) {
112         var m = layoutOutput.match(/Error: My Unique Error Message/);
113         assert.isArray(m);
114       },
115       'should print error augmented string attributes': function(layoutOutput) {
116         var m = layoutOutput.match(/augmented:\s'My Unique attribute value'/);
117         assert.isArray(m);
118       },
119       'should print error augmented object attributes': function(layoutOutput) {
120         var m = layoutOutput.match(/augObj:\s\{ at1: 'at2' \}/);
121         assert.isArray(m);
122       }
123     }
124
125
126   },
127
128   'basicLayout': {
129     topic: function() {
130       var layout = require('../lib/layouts').basicLayout,
131       event = {
132         data: ['this is a test'],
133         startTime: new Date(2010, 11, 5, 14, 18, 30, 45),
134         categoryName: "tests",
135         level: {
136           toString: function() { return "DEBUG"; }
137         }
138       };
139       return [layout, event];
140     },
141     'should take a logevent and output a formatted string': function(args) {
142       var layout = args[0], event = args[1];
143       assert.equal(layout(event), "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test");
144     },
145     'should output a stacktrace, message if the event has an error attached': function(args) {
146       var i, layout = args[0], event = args[1], output, lines,
147       error = new Error("Some made-up error"),
148       stack = error.stack.split(/\n/);
149
150       event.data = ['this is a test', error];
151       output = layout(event);
152       lines = output.split(/\n/);
153
154       if (semver.satisfies(process.version, '>=6')) {
155         assert.equal(lines.length, stack.length);
156         assert.equal(
157           lines[0],
158           "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test Error: Some made-up error"
159         );
160         for (i = 1; i < stack.length; i++) {
161           assert.equal(lines[i], stack[i]);
162         }
163       } else {
164         assert.equal(lines.length - 1, stack.length);
165         assert.equal(
166           lines[0],
167           "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test [Error: Some made-up error]"
168         );
169         for (i = 1; i < stack.length; i++) {
170           assert.equal(lines[i+2], stack[i+1]);
171         }
172       }
173
174     },
175     'should output any extra data in the log event as util.inspect strings': function(args) {
176       var layout = args[0], event = args[1], output, lines;
177       event.data = ['this is a test', {
178         name: 'Cheese',
179         message: 'Gorgonzola smells.'
180       }];
181       output = layout(event);
182       assert.equal(
183         output,
184         "[2010-12-05 14:18:30.045] [DEBUG] tests - this is a test " +
185           "{ name: 'Cheese', message: 'Gorgonzola smells.' }"
186       );
187     }
188   },
189
190   'patternLayout': {
191     topic: function() {
192       var event = {
193         data: ['this is a test'],
194         startTime: new Date('2010-12-05T14:18:30.045Z'), //new Date(2010, 11, 5, 14, 18, 30, 45),
195         categoryName: "multiple.levels.of.tests",
196         level: {
197           toString: function() { return "DEBUG"; }
198         }
199       }, layout = require('../lib/layouts').patternLayout
200       , tokens = {
201         testString: 'testStringToken',
202         testFunction: function() { return 'testFunctionToken'; },
203         fnThatUsesLogEvent: function(logEvent) { return logEvent.level.toString(); }
204       };
205
206       //override getTimezoneOffset
207       event.startTime.getTimezoneOffset = function() { return 0; };
208       return [layout, event, tokens];
209     },
210
211     'should default to "time logLevel loggerName - message"': function(args) {
212       test(args, null, "14:18:30 DEBUG multiple.levels.of.tests - this is a test" + EOL);
213     },
214     '%r should output time only': function(args) {
215       test(args, '%r', '14:18:30');
216     },
217     '%p should output the log level': function(args) {
218       test(args, '%p', 'DEBUG');
219     },
220     '%c should output the log category': function(args) {
221       test(args, '%c', 'multiple.levels.of.tests');
222     },
223     '%m should output the log data': function(args) {
224       test(args, '%m', 'this is a test');
225     },
226     '%n should output a new line': function(args) {
227       test(args, '%n', EOL);
228     },
229     '%h should output hostname' : function(args) {
230       test(args, '%h', os.hostname().toString());
231     },
232     '%z should output pid' : function(args) {
233       test(args, '%z', process.pid);
234     },
235     '%c should handle category names like java-style package names': function(args) {
236       test(args, '%c{1}', 'tests');
237       test(args, '%c{2}', 'of.tests');
238       test(args, '%c{3}', 'levels.of.tests');
239       test(args, '%c{4}', 'multiple.levels.of.tests');
240       test(args, '%c{5}', 'multiple.levels.of.tests');
241       test(args, '%c{99}', 'multiple.levels.of.tests');
242     },
243     '%d should output the date in ISO8601 format': function(args) {
244       test(args, '%d', '2010-12-05 14:18:30.045');
245     },
246     '%d should allow for format specification': function(args) {
247       test(args, '%d{ISO8601_WITH_TZ_OFFSET}', '2010-12-05T14:18:30-0000');
248       test(args, '%d{ISO8601}', '2010-12-05 14:18:30.045');
249       test(args, '%d{ABSOLUTE}', '14:18:30.045');
250       test(args, '%d{DATE}', '05 12 2010 14:18:30.045');
251       test(args, '%d{yy MM dd hh mm ss}', '10 12 05 14 18 30');
252       test(args, '%d{yyyy MM dd}', '2010 12 05');
253       test(args, '%d{yyyy MM dd hh mm ss SSS}', '2010 12 05 14 18 30 045');
254     },
255     '%% should output %': function(args) {
256       test(args, '%%', '%');
257     },
258     'should output anything not preceded by % as literal': function(args) {
259       test(args, 'blah blah blah', 'blah blah blah');
260     },
261     'should output the original string if no replacer matches the token': function(args) {
262       test(args, '%a{3}', 'a{3}');
263     },
264     'should handle complicated patterns': function(args) {
265       test(args,
266            '%m%n %c{2} at %d{ABSOLUTE} cheese %p%n',
267            'this is a test'+ EOL +' of.tests at 14:18:30.045 cheese DEBUG' + EOL
268           );
269     },
270     'should truncate fields if specified': function(args) {
271       test(args, '%.4m', 'this');
272       test(args, '%.7m', 'this is');
273       test(args, '%.9m', 'this is a');
274       test(args, '%.14m', 'this is a test');
275       test(args, '%.2919102m', 'this is a test');
276     },
277     'should pad fields if specified': function(args) {
278       test(args, '%10p', '     DEBUG');
279       test(args, '%8p', '   DEBUG');
280       test(args, '%6p', ' DEBUG');
281       test(args, '%4p', 'DEBUG');
282       test(args, '%-4p', 'DEBUG');
283       test(args, '%-6p', 'DEBUG ');
284       test(args, '%-8p', 'DEBUG   ');
285       test(args, '%-10p', 'DEBUG     ');
286     },
287     '%[%r%] should output colored time': function(args) {
288       test(args, '%[%r%]', '\x1B[36m14:18:30\x1B[39m');
289     },
290     '%x{testString} should output the string stored in tokens': function(args) {
291       test(args, '%x{testString}', 'testStringToken');
292     },
293     '%x{testFunction} should output the result of the function stored in tokens': function(args) {
294       test(args, '%x{testFunction}', 'testFunctionToken');
295     },
296     '%x{doesNotExist} should output the string stored in tokens': function(args) {
297       test(args, '%x{doesNotExist}', 'null');
298     },
299     '%x{fnThatUsesLogEvent} should be able to use the logEvent': function(args) {
300       test(args, '%x{fnThatUsesLogEvent}', 'DEBUG');
301     },
302     '%x should output the string stored in tokens': function(args) {
303       test(args, '%x', 'null');
304     }
305   },
306   'layout makers': {
307     topic: require('../lib/layouts'),
308     'should have a maker for each layout': function(layouts) {
309       assert.ok(layouts.layout("messagePassThrough"));
310       assert.ok(layouts.layout("basic"));
311       assert.ok(layouts.layout("colored"));
312       assert.ok(layouts.layout("coloured"));
313       assert.ok(layouts.layout("pattern"));
314     }
315   },
316   'add layout': {
317     topic: require('../lib/layouts'),
318     'should be able to add a layout': function(layouts) {
319       layouts.addLayout('test_layout', function(config){
320         assert.equal(config, 'test_config');
321         return function(logEvent) {
322           return "TEST LAYOUT >"+logEvent.data;
323         };
324       });
325       var serializer = layouts.layout('test_layout', 'test_config');
326       assert.ok(serializer);
327       assert.equal(serializer({data: "INPUT"}), "TEST LAYOUT >INPUT");
328     }
329   }
330 }).export(module);