|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package com.nokia.testfw.testrunner; |
|
18 |
|
19 import java.io.*; |
|
20 |
|
21 public class LogMsgWriter { |
|
22 public static final String DEFAULT_LOG_FILE = ""; |
|
23 public static final String FMT_START_TEST = "StartTest[%s]\n"; |
|
24 public static final String END_TEST = "EndTest\n"; |
|
25 public static final String FMT_LIST_CASE = "ListCase[%s]\n"; |
|
26 public static final String FMT_START_CASE = "StartCase[%s]\n"; |
|
27 public static final String FMT_TEST_FAILED_MSG = "TestFailedMsg[%s]\n"; |
|
28 public static final String FMT_LONG_TEST_FAILED_MSG = "TestFailedMsg[%s]File[%s]Line[%d]\n"; |
|
29 public static final String FMT_END_CASE = "EndCase Result[%s]Time[%d]ms\n"; |
|
30 public static final String PASSED = "passed"; |
|
31 public static final String FAILED = "failed"; |
|
32 |
|
33 public LogMsgWriter() { |
|
34 try { |
|
35 logger = new FileWriter(DEFAULT_LOG_FILE, true); |
|
36 } catch (IOException e) { |
|
37 System.err.println(e); |
|
38 } |
|
39 } |
|
40 |
|
41 public LogMsgWriter(String logName) { |
|
42 try { |
|
43 logger = new FileWriter(logName, true); |
|
44 } catch (IOException e) { |
|
45 System.err.println(e); |
|
46 } |
|
47 } |
|
48 |
|
49 // Methods to write log. |
|
50 public void writeStartTest(String msg) { |
|
51 try { |
|
52 String line = String.format(FMT_START_CASE, msg); |
|
53 write(line); |
|
54 } catch (IOException e) { |
|
55 System.err.println(e); |
|
56 } |
|
57 } |
|
58 |
|
59 public void writeEndTest() { |
|
60 try { |
|
61 write(END_TEST); |
|
62 } catch (IOException e) { |
|
63 System.err.println(e); |
|
64 } |
|
65 } |
|
66 |
|
67 public void writeCaseList(String[] caseNames) { |
|
68 try { |
|
69 for (int i = 0; i < caseNames.length; ++i) { |
|
70 String line = String.format(FMT_LIST_CASE, caseNames[i]); |
|
71 write(line); |
|
72 } |
|
73 } catch (IOException e) { |
|
74 System.err.println(e); |
|
75 } |
|
76 } |
|
77 |
|
78 public void writeStartCase(String msg) { |
|
79 try { |
|
80 String line = String.format(FMT_START_CASE, msg); |
|
81 write(line); |
|
82 } catch (IOException e) { |
|
83 System.err.println(e); |
|
84 } |
|
85 } |
|
86 |
|
87 public void writeTestFailureMsg(String msg) { |
|
88 try { |
|
89 String line = String.format(FMT_TEST_FAILED_MSG, msg); |
|
90 write(line); |
|
91 } catch (IOException e) { |
|
92 System.err.println(e); |
|
93 } |
|
94 } |
|
95 |
|
96 public void writeLongTestFailureMsg(String msg, String fileName, int lineNo) { |
|
97 try { |
|
98 String line = String.format(FMT_LONG_TEST_FAILED_MSG, msg, fileName, lineNo); |
|
99 write(line); |
|
100 } catch (IOException e) { |
|
101 System.err.println(e); |
|
102 } |
|
103 } |
|
104 |
|
105 public void writeEndCaseResult(boolean passed, int timeSpent) { |
|
106 try { |
|
107 String line = String.format(FMT_END_CASE, passed ? PASSED : FAILED, timeSpent); |
|
108 write(line); |
|
109 } catch (IOException e) { |
|
110 System.err.println(e); |
|
111 } |
|
112 } |
|
113 |
|
114 // This should be called explicitly. |
|
115 public void close() { |
|
116 try { |
|
117 logger.flush(); |
|
118 logger.close(); |
|
119 } catch (IOException e) { |
|
120 System.err.println(e); |
|
121 } |
|
122 } |
|
123 |
|
124 private void write(String msg) throws IOException { |
|
125 logger.write(msg); |
|
126 logger.flush(); |
|
127 } |
|
128 |
|
129 private FileWriter logger = null; |
|
130 } |