001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: * $Id: TestRuntimeFile.java 4212 2007-01-09 02:40:19Z jzhang $
024: */
025: package com.bostechcorp.cbesb.runtime.file;
026:
027: import java.io.BufferedReader;
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.FileOutputStream;
031: import java.io.FileReader;
032: import java.io.InputStream;
033: import java.io.InputStreamReader;
034: import java.io.OutputStream;
035: import java.nio.charset.Charset;
036: import java.text.SimpleDateFormat;
037: import java.util.Date;
038: import java.util.SortedMap;
039:
040: import javax.xml.transform.Source;
041:
042: import junit.framework.TestCase;
043:
044: import com.bostechcorp.cbesb.runtime.ccsl.lib.InputReader;
045: import com.bostechcorp.cbesb.runtime.ccsl.lib.OutputWriter;
046:
047: /**
048: * @author j.zhang
049: * @version 1.0.0
050: */
051: public class TestRuntimeFile extends TestCase {
052:
053: // private File fileConfigA = null;
054: // private File fileConfigB = null;
055: private File fileInA = null;
056: private File fileInB = null;
057: // private File fileOutA = null;
058: // private File fileOutB = null;
059: // private File fileResultA = null;
060: // private File fileResultB = null;
061:
062: private static final String CONFIG = "target/test-data/config";
063: private static final String IN = "target/test-data/in";
064: private static final String OUT = "target/test-data/out";
065: private static final String RESULT = "target/test-data/result";
066: private static final String filePattern = "{BASENAME}_{DATE}_{TIME}_{COUNT}.{EXT}";
067:
068: // private static final String filePattern = "{BASENAME}_{DATE}_{COUNT}.{EXT}";
069:
070: /**
071: * Constructor.
072: *
073: * @param name String
074: */
075: public TestRuntimeFile(String name) {
076: super (name);
077: }
078:
079: /* (non-Javadoc)
080: * @see junit.framework.TestCase#setUp()
081: */
082: protected void setUp() throws Exception {
083: super .setUp();
084: deleteFile();
085: }
086:
087: /* (non-Javadoc)
088: * @see junit.framework.TestCase#tearDown()
089: */
090: protected void tearDown() throws Exception {
091: super .tearDown();
092: deleteFile();
093: }
094:
095: /**
096: * Test method for {@link com.bostechcorp.cbesb.runtime.file.FileOperator#scanDir(java.lang.String)}.
097: */
098: public void testScanDir() {
099: scanDirCase1();
100: }
101:
102: /**
103: * Test method for {@link com.bostechcorp.cbesb.runtime.file.FileOperator#scanStageDir(java.lang.String, java.lang.String, java.lang.String, boolean)}.
104: */
105: public void testScanStageDir() {
106: scanStageDirCase1();
107: scanStageDirCase2();
108: scanStageDirCase3();
109: scanStageDirCase4();
110: }
111:
112: /**
113: * Test method for {@link com.bostechcorp.cbesb.runtime.file.FileOperator#moveFile(java.io.File, java.lang.String)}.
114: */
115: public void testMoveFile() {
116: moveFileCase1();
117: }
118:
119: /**
120: * Test method for {@link com.bostechcorp.cbesb.runtime.file.FileOperator#copyFile(java.io.File, java.lang.String)}.
121: */
122: public void testCopyFile() {
123: copyFileCase1();
124: }
125:
126: /**
127: * Test method for {@link com.bostechcorp.cbesb.runtime.file.FileOperator#deleteFile(java.io.File)}.
128: */
129: public void testDeleteFile() {
130: deleteFileCase1();
131: }
132:
133: /**
134: * Test method for {@link com.bostechcorp.cbesb.runtime.file.FileOperator#createFolder(java.lang.String)}.
135: */
136: public void testCreateFolder() {
137: createFolderCase1();
138: }
139:
140: /**
141: * Test method for {@link com.bostechcorp.cbesb.runtime.file.FileOperator#archiveFile(java.io.File, java.lang.String, java.lang.String)}.
142: */
143: public void testArchiveFile() {
144: archiveFileCase1();
145: }
146:
147: /**
148: * Test method for {@link com.bostechcorp.cbesb.runtime.file.FilePatternMatcher#processFilePattern(java.lang.String)}.
149: */
150: public void testProcessFilePattern() {
151: processFilePatternCase1();
152: }
153:
154: /**
155: * Test method for {@link com.bostechcorp.cbesb.runtime.file.FilePatternMatcher#matchPattern(java.lang.String, java.lang.String)}.
156: */
157: public void testMatchPattern() {
158: matchPatternCase1();
159: matchPatternCase2();
160: matchPatternCase3();
161: }
162:
163: public void testCharset() throws Exception {
164: testCharsetCase1();
165: testCharsetCase2();
166: }
167:
168: private void createFileIn() {
169: fileInA = new File(IN, "fileA");
170: fileInB = new File(IN, "fileB");
171: try {
172: OutputStream outputStreamA = new FileOutputStream(fileInA);
173: OutputStream outputStreamB = new FileOutputStream(fileInB);
174: byte[] byteA = "<Test file in A./>".getBytes();
175: byte[] byteB = "<Test file in B./>".getBytes();
176: outputStreamA.write(byteA);
177: outputStreamB.write(byteB);
178: outputStreamA.close();
179: outputStreamB.close();
180: } catch (Exception e) {
181: e.printStackTrace();
182: }
183: }
184:
185: private void deleteFile() {
186: File[] config = FileOperator.scanDir(CONFIG);
187: File[] in = FileOperator.scanDir(IN);
188: File[] out = FileOperator.scanDir(OUT);
189: File[] result = FileOperator.scanDir(RESULT);
190: for (int i = 0; i < config.length; i++) {
191: config[i].delete();
192: }
193: for (int i = 0; i < in.length; i++) {
194: in[i].delete();
195: }
196: for (int i = 0; i < out.length; i++) {
197: out[i].delete();
198: }
199: for (int i = 0; i < result.length; i++) {
200: result[i].delete();
201: }
202: }
203:
204: private void scanDirCase1() {
205: String dirName = "target/test-data/in/";
206: FileOperator.createFolder(dirName);
207: createFileIn();
208: File[] files = FileOperator.scanDir(dirName);
209:
210: assertEquals(2, files.length);
211: assertEquals(fileInA.getName(), files[0].getName());
212: assertEquals(fileInB.getName(), files[1].getName());
213:
214: deleteFile();
215: }
216:
217: private void scanStageDirCase1() {
218: createFileIn();
219: FileOperator.scanStageDir(IN, OUT, RESULT, true);
220: File[] fileIn = FileOperator.scanDir(IN);
221: File[] fileOut = FileOperator.scanDir(OUT);
222: File[] fileResult = FileOperator.scanDir(RESULT);
223:
224: assertEquals(0, fileIn.length);
225: assertEquals(2, fileOut.length);
226: assertEquals(0, fileResult.length);
227:
228: deleteFile();
229: }
230:
231: private void scanStageDirCase2() {
232: createFileIn();
233: FileOperator.scanStageDir(IN, OUT, RESULT, false);
234: File[] fileIn = FileOperator.scanDir(IN);
235: File[] fileOut = FileOperator.scanDir(OUT);
236: File[] fileResult = FileOperator.scanDir(RESULT);
237:
238: assertEquals(0, fileIn.length);
239: assertEquals(0, fileOut.length);
240: assertEquals(2, fileResult.length);
241:
242: deleteFile();
243: }
244:
245: private void scanStageDirCase3() {
246: FileOperator.scanStageDir(IN, OUT, RESULT, true);
247: File[] fileIn = FileOperator.scanDir(IN);
248: File[] fileOut = FileOperator.scanDir(OUT);
249: File[] fileResult = FileOperator.scanDir(RESULT);
250:
251: assertEquals(0, fileIn.length);
252: assertEquals(0, fileOut.length);
253: assertEquals(0, fileResult.length);
254:
255: deleteFile();
256: }
257:
258: private void scanStageDirCase4() {
259: createFileIn();
260: FileOperator.scanStageDir(IN, OUT, null, false);
261: File[] fileIn = FileOperator.scanDir(IN);
262: File[] fileOut = FileOperator.scanDir(OUT);
263: File[] fileResult = FileOperator.scanDir(RESULT);
264:
265: assertEquals(0, fileIn.length);
266: assertEquals(0, fileOut.length);
267: assertEquals(0, fileResult.length);
268:
269: deleteFile();
270: }
271:
272: private void moveFileCase1() {
273: createFileIn();
274:
275: FileOperator.moveFile(fileInA, OUT);
276: FileOperator.moveFile(fileInB, OUT);
277:
278: File[] fileIn = FileOperator.scanDir(IN);
279: File[] fileOut = FileOperator.scanDir(OUT);
280:
281: assertEquals(0, fileIn.length);
282: assertEquals(2, fileOut.length);
283:
284: deleteFile();
285: }
286:
287: private void copyFileCase1() {
288: createFileIn();
289:
290: FileOperator.copyFile(fileInA, OUT);
291: FileOperator.copyFile(fileInB, OUT);
292:
293: File[] fileIn = FileOperator.scanDir(IN);
294: File[] fileOut = FileOperator.scanDir(OUT);
295:
296: assertEquals(2, fileIn.length);
297: assertEquals(2, fileOut.length);
298:
299: deleteFile();
300: }
301:
302: private void deleteFileCase1() {
303: createFileIn();
304:
305: FileOperator.deleteFile(fileInA);
306: FileOperator.deleteFile(fileInB);
307:
308: File[] fileIn = FileOperator.scanDir(IN);
309:
310: assertEquals(0, fileIn.length);
311:
312: deleteFile();
313: }
314:
315: private void createFolderCase1() {
316: String folderName = IN + "/dir";
317:
318: File createFolder = new File(folderName);
319:
320: FileOperator.createFolder(folderName);
321:
322: assertTrue(createFolder.isDirectory());
323:
324: createFolder.delete();
325: }
326:
327: private void archiveFileCase1() {
328: createFileIn();
329:
330: FileOperator.archiveFile(fileInA, OUT, filePattern, 0);
331: FileOperator.archiveFile(fileInB, OUT, filePattern, 1);
332:
333: File[] fileIn = FileOperator.scanDir(IN);
334: File[] fileOut = FileOperator.scanDir(OUT);
335:
336: assertEquals(0, fileIn.length);
337: assertEquals(2, fileOut.length);
338:
339: deleteFile();
340: }
341:
342: private void processFilePatternCase1() {
343: String name1 = "filename1_DATE_TIME_0.xml";
344: String name2 = "filename2_DATE_TIME_1.dat";
345: String name3 = "filename3_DATE_TIME_2";
346: Date date = new Date();
347: SimpleDateFormat timeFormat = new SimpleDateFormat("hhmmss");
348: String timeString = timeFormat.format(date);
349: SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
350: String dateString = dateFormat.format(date);
351: name1 = name1.replace("DATE", dateString);
352: name2 = name2.replace("DATE", dateString);
353: name3 = name3.replace("DATE", dateString);
354: name1 = name1.replace("TIME", timeString);
355: name2 = name2.replace("TIME", timeString);
356: name3 = name3.replace("TIME", timeString);
357:
358: String filename1 = "filename1.xml";
359: String filename2 = "filename2.dat";
360: String filename3 = "filename3";
361: int i = 0;
362: String returnName1 = FilePatternMatcher.processFilePattern(
363: filename1, filePattern, i);
364: i++;
365: String returnName2 = FilePatternMatcher.processFilePattern(
366: filename2, filePattern, i);
367: i++;
368: String returnName3 = FilePatternMatcher.processFilePattern(
369: filename3, filePattern, i);
370:
371: assertEquals(name1, returnName1);
372: assertEquals(name2, returnName2);
373: assertEquals(name3, returnName3);
374: }
375:
376: private void matchPatternCase1() {
377: boolean flag = false;
378: try {
379: flag = FilePatternMatcher
380: .matchPattern("file1.xml", "*.xml");
381: } catch (Exception e) {
382: e.printStackTrace();
383: }
384:
385: assertTrue(flag);
386: }
387:
388: private void matchPatternCase2() {
389: boolean flag = true;
390: try {
391: flag = FilePatternMatcher
392: .matchPattern("file1.dat", "*.xml");
393: } catch (Exception e) {
394: e.printStackTrace();
395: }
396: assertFalse(flag);
397: }
398:
399: private void matchPatternCase3() {
400: boolean flag = false;
401: try {
402: flag = FilePatternMatcher.matchPattern("file1.xml", "*");
403: } catch (Exception e) {
404: e.printStackTrace();
405: }
406: assertTrue(flag);
407: }
408:
409: private void testCharsetCase1() throws Exception {
410: FileInputStream fis = new FileInputStream("target/utf-8.txt");
411: InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
412: BufferedReader br = new BufferedReader(isr);
413: FileReaderInputStream inStream = new FileReaderInputStream(br,
414: "UTF-8");
415:
416: Source src = InputReader.getRawString(inStream, "UTF-8");
417:
418: OutputStream os = new FileOutputStream("target/utf-16be.txt");
419: OutputWriter.processOutputStream(src, os, "raw", "UTF-16BE");
420: }
421:
422: private void testCharsetCase2() throws Exception {
423: FileInputStream fis = new FileInputStream("target/utf-8.xml");
424: InputStreamReader isr = new InputStreamReader(fis, "GB2312");
425: BufferedReader br = new BufferedReader(isr);
426: FileReaderInputStream inStream = new FileReaderInputStream(br,
427: "GB2312");
428:
429: Source src = InputReader.getRawXml(inStream);
430:
431: OutputStream os = new FileOutputStream("target/iso-8859-1.xml");
432: OutputWriter.processOutputStream(src, os, "raw", "UTF-8");
433: }
434: }
|