Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / streams / DateRollingFileStream-test.js
1 "use strict";
2 var vows = require('vows')
3 , assert = require('assert')
4 , fs = require('fs')
5 , semver = require('semver')
6 , streams
7 , DateRollingFileStream
8 , testTime = new Date(2012, 8, 12, 10, 37, 11);
9
10 if (semver.satisfies(process.version, '>=0.10.0')) {
11   streams = require('stream');
12 } else {
13   streams = require('readable-stream');
14 }
15 DateRollingFileStream = require('../../lib/streams').DateRollingFileStream;
16
17 function cleanUp(filename) {
18   return function() {
19     fs.unlink(filename);
20   };
21 }
22
23 function now() {
24   return testTime.getTime();
25 }
26
27 vows.describe('DateRollingFileStream').addBatch({
28   'arguments': {
29     topic: new DateRollingFileStream(
30       __dirname + '/test-date-rolling-file-stream-1', 
31       'yyyy-mm-dd.hh'
32     ),
33     teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-1'),
34     
35     'should take a filename and a pattern and return a WritableStream': function(stream) {
36       assert.equal(stream.filename, __dirname + '/test-date-rolling-file-stream-1');
37       assert.equal(stream.pattern, 'yyyy-mm-dd.hh');
38       assert.instanceOf(stream, streams.Writable);
39     },
40     'with default settings for the underlying stream': function(stream) {
41       assert.equal(stream.theStream.mode, 420);
42       assert.equal(stream.theStream.flags, 'a');
43       //encoding is not available on the underlying stream
44       //assert.equal(stream.encoding, 'utf8');
45     }
46   },
47   
48   'default arguments': {
49     topic: new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-2'),
50     teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-2'),
51     
52     'pattern should be .yyyy-MM-dd': function(stream) {
53       assert.equal(stream.pattern, '.yyyy-MM-dd');
54     }
55   },
56
57   'with stream arguments': {
58     topic: new DateRollingFileStream(
59       __dirname + '/test-date-rolling-file-stream-3', 
60       'yyyy-MM-dd', 
61       { mode: parseInt('0666', 8) }
62     ),
63     teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-3'),
64     
65     'should pass them to the underlying stream': function(stream) {
66       assert.equal(stream.theStream.mode, parseInt('0666', 8));
67     }
68   },
69
70   'with stream arguments but no pattern': {
71     topic: new DateRollingFileStream(
72       __dirname + '/test-date-rolling-file-stream-4', 
73       { mode: parseInt('0666', 8) }
74     ),
75     teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-4'),
76     
77     'should pass them to the underlying stream': function(stream) {
78       assert.equal(stream.theStream.mode, parseInt('0666', 8));
79     },
80     'should use default pattern': function(stream) {
81       assert.equal(stream.pattern, '.yyyy-MM-dd');
82     }
83   },
84
85   'with a pattern of .yyyy-MM-dd': {
86     topic: function() {
87       var that = this,
88       stream = new DateRollingFileStream(
89         __dirname + '/test-date-rolling-file-stream-5', '.yyyy-MM-dd', 
90         null, 
91         now
92       );
93       stream.write("First message\n", 'utf8', function() {
94         that.callback(null, stream);
95       });
96     },
97     teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5'),
98     
99     'should create a file with the base name': {
100       topic: function(stream) {
101         fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback);
102       },
103       'file should contain first message': function(result) {
104         assert.equal(result.toString(), "First message\n");
105       }
106     },
107
108     'when the day changes': {
109       topic: function(stream) {
110         testTime = new Date(2012, 8, 13, 0, 10, 12);
111         stream.write("Second message\n", 'utf8', this.callback);
112       },
113       teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5.2012-09-12'),
114
115       
116       'the number of files': {
117         topic: function() {
118           fs.readdir(__dirname, this.callback);
119         },
120         'should be two': function(files) {
121           assert.equal(
122             files.filter(
123               function(file) { 
124                 return file.indexOf('test-date-rolling-file-stream-5') > -1; 
125               }
126             ).length, 
127             2
128           );
129         }
130       },
131       
132       'the file without a date': {
133         topic: function() {
134           fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback);
135         },
136         'should contain the second message': function(contents) {
137           assert.equal(contents.toString(), "Second message\n");
138         }
139       },
140       
141       'the file with the date': {
142         topic: function() {
143           fs.readFile(__dirname + '/test-date-rolling-file-stream-5.2012-09-12', this.callback);
144         },
145         'should contain the first message': function(contents) {
146           assert.equal(contents.toString(), "First message\n");
147         }
148       }
149     }
150   },
151   
152   'with alwaysIncludePattern': {
153     topic: function() {
154       var that = this,
155       testTime = new Date(2012, 8, 12, 0, 10, 12),
156       stream = new DateRollingFileStream(
157         __dirname + '/test-date-rolling-file-stream-pattern', 
158         '.yyyy-MM-dd', 
159         {alwaysIncludePattern: true}, 
160         now
161       );
162       stream.write("First message\n", 'utf8', function() {
163         that.callback(null, stream);
164       });
165     },
166     teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12'),
167     
168     'should create a file with the pattern set': {
169       topic: function(stream) {
170         fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', this.callback);
171       },
172       'file should contain first message': function(result) {
173         assert.equal(result.toString(), "First message\n");
174       }
175     },
176     
177     'when the day changes': {
178       topic: function(stream) {
179         testTime = new Date(2012, 8, 13, 0, 10, 12);
180         stream.write("Second message\n", 'utf8', this.callback);
181       },
182       teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13'),
183       
184       
185       'the number of files': {
186         topic: function() {
187           fs.readdir(__dirname, this.callback);
188         },
189         'should be two': function(files) {
190           assert.equal(
191             files.filter(
192               function(file) { 
193                 return file.indexOf('test-date-rolling-file-stream-pattern') > -1; 
194               }
195             ).length, 
196             2
197           );
198         }
199       },
200       
201       'the file with the later date': {
202         topic: function() {
203           fs.readFile(
204             __dirname + '/test-date-rolling-file-stream-pattern.2012-09-13', 
205             this.callback
206           );
207         },
208         'should contain the second message': function(contents) {
209           assert.equal(contents.toString(), "Second message\n");
210         }
211       },
212       
213       'the file with the date': {
214         topic: function() {
215           fs.readFile(
216             __dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', 
217             this.callback
218           );
219         },
220         'should contain the first message': function(contents) {
221           assert.equal(contents.toString(), "First message\n");
222         }
223       }
224     }
225   }
226
227 }).exportTo(module);