testdev/ite/src/com.nokia.testfw.launch/src/com/nokia/testfw/launch/processor/STFProcessor.java
changeset 1 96906a986c3b
equal deleted inserted replaced
0:f1112f777ce9 1:96906a986c3b
       
     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 
       
    18 /**
       
    19  * @author ferao
       
    20  * 
       
    21  */
       
    22 package com.nokia.testfw.launch.processor;
       
    23 
       
    24 import java.io.BufferedReader;
       
    25 import java.io.File;
       
    26 import java.io.IOException;
       
    27 import java.io.InputStream;
       
    28 import java.io.StringReader;
       
    29 import java.util.ArrayList;
       
    30 import java.util.Date;
       
    31 import java.text.DateFormat;
       
    32 import java.text.SimpleDateFormat;
       
    33 
       
    34 import org.eclipse.core.runtime.CoreException;
       
    35 import org.eclipse.debug.core.ILaunchConfiguration;
       
    36 
       
    37 import com.nokia.testfw.core.model.result.TestResult.TestStatus;
       
    38 import com.nokia.testfw.core.model.result.TestCaseResult;
       
    39 import com.nokia.testfw.core.model.result.TestResult;
       
    40 import com.nokia.testfw.core.model.result.TestRunResult;
       
    41 import com.nokia.testfw.core.utils.STFScriptUtils;
       
    42 import com.nokia.testfw.launch.LaunchConfigurationConstants;
       
    43 import com.nokia.testfw.launch.TFWLaunchPlugin;
       
    44 import com.nokia.testfw.launch.monitor.TailFileInputStream;
       
    45 import com.nokia.testfw.resultview.model.IDataProcessor;
       
    46 
       
    47 public class STFProcessor implements IDataProcessor {
       
    48 
       
    49 	public static final String DEFAULT_LOG = "c:\\logs\\TestFramework\\testengine\\testengine.html";
       
    50 	public static final String START_TEST_CASE = ".... Starting testcase";
       
    51 	public static final String END_TEST_CASE = "finished with verdict";
       
    52 	public static final String END_CASE_TIME_DELIMITER = ".... TestCase ";
       
    53 	public static final String INFO_START_FLAG = "[";
       
    54 	public static final String INFO_END_FLAG = "]";
       
    55 	public static final String RESULT_MESSAGE = "Message";
       
    56 	public static final String FILE = "FILE";
       
    57 	public static final String LINE = "LINE";
       
    58 	public static final String TEST_START = "############################################################";
       
    59 	public static final String TEST_FINISHED = "---------------- Log Ended ----------------";
       
    60 	public static final String DATE_FORMAT = "dd.MMM.yyyy HH:mm:ss.SSS";
       
    61 
       
    62 	public void close() throws IOException {
       
    63 		if (iInputStream != null) {
       
    64 			iInputStream.close();
       
    65 		}
       
    66 	}
       
    67 
       
    68 	public void initTestResult(TestRunResult result, ILaunchConfiguration config) {
       
    69 		ArrayList<String> testCases = STFScriptUtils
       
    70 				.getTestCasesFromScript(getScriptPath(config));
       
    71 		for (String caseName : testCases) {
       
    72 			addTestCase(result, caseName);
       
    73 		}
       
    74 	}
       
    75 
       
    76 	public ILaunchConfiguration createRetestLaunchConfiguration(
       
    77 			ILaunchConfiguration oldConfiguration, TestResult[] retestResults) {
       
    78 		return oldConfiguration;
       
    79 	}
       
    80 
       
    81 	public InputStream getInputStream(ILaunchConfiguration config) {
       
    82 		if (iInputStream == null)
       
    83 			iInputStream = createTailFileInputStream(getLogPath(config));
       
    84 		;
       
    85 		return iInputStream;
       
    86 	}
       
    87 
       
    88 	public boolean isDealType(ILaunchConfiguration config) {
       
    89 		if (config == null)
       
    90 			return false;
       
    91 
       
    92 		// test whether the launch defines the STF attributes
       
    93 		String idStr = null;
       
    94 		try {
       
    95 			idStr = config.getAttribute(
       
    96 					LaunchConfigurationConstants.SCRIPT_HOST_PATH,
       
    97 					(String) null);
       
    98 		} catch (CoreException e) {
       
    99 			TFWLaunchPlugin.log(e);
       
   100 		}
       
   101 		if (idStr == null)
       
   102 			return false;
       
   103 		return true;
       
   104 	}
       
   105 
       
   106 	public void process(TestRunResult result, String info) {
       
   107 		BufferedReader reader = new BufferedReader(new StringReader(info));
       
   108 		String line;
       
   109 		int index;
       
   110 		try {
       
   111 			while ((line = reader.readLine()) != null) {
       
   112 				if ((index = line.indexOf(TEST_START)) > -1) {
       
   113 					result.testStarted();
       
   114 				} else if ((index = line.indexOf(START_TEST_CASE)) > -1) {
       
   115 					String lInfo = line.substring(
       
   116 							index + START_TEST_CASE.length()).trim();
       
   117 					startTestCase(result, lInfo, getDate(
       
   118 							line.substring(0, index)).getTime());
       
   119 				} else if ((index = line.indexOf(END_TEST_CASE)) > -1) {
       
   120 					int delimiterIndex = line.indexOf(END_CASE_TIME_DELIMITER);
       
   121 					double time = -1;
       
   122 					if (delimiterIndex > -1) {
       
   123 						time = getDate(line.substring(0, delimiterIndex))
       
   124 								.getTime();
       
   125 					}
       
   126 					String lInfo = line.substring(
       
   127 							delimiterIndex + END_CASE_TIME_DELIMITER.length())
       
   128 							.trim();
       
   129 					endTestCase(result, lInfo, time);
       
   130 				} else if ((index = line.indexOf(TEST_FINISHED)) > -1) {
       
   131 					result.testFinished();
       
   132 				}
       
   133 			}
       
   134 		} catch (IOException e) {
       
   135 			TFWLaunchPlugin.log(e);
       
   136 		}
       
   137 	}
       
   138 
       
   139 	// private methods
       
   140 	private String getLogPath(ILaunchConfiguration config) {
       
   141 		if (iLogFile == null) {
       
   142 			try {
       
   143 				String epocRoot = config.getAttribute(
       
   144 						"com.nokia.cdt.debug.cw.symbian.Epoc_Root",
       
   145 						(String) null);
       
   146 				File logFile = new File(
       
   147 						epocRoot
       
   148 								+ "/epoc32/winscw/c/logs/TestFramework/testengine/testengine.html");
       
   149 				File logDir = new File(epocRoot
       
   150 						+ "/epoc32/winscw/c/logs/TestFramework/testengine");
       
   151 				if (!logDir.exists()) {
       
   152 					logDir.mkdir();
       
   153 				}
       
   154 				try {
       
   155 					if (!logFile.exists()) {
       
   156 						logFile.createNewFile();
       
   157 					}
       
   158 				} catch (IOException e) {
       
   159 					TFWLaunchPlugin.log(e);
       
   160 				}
       
   161 				iLogFile = logFile.getAbsolutePath();
       
   162 			} catch (CoreException e) {
       
   163 				TFWLaunchPlugin.log(e);
       
   164 			}
       
   165 		}
       
   166 		return iLogFile;
       
   167 	}
       
   168 
       
   169 	private String getScriptPath(ILaunchConfiguration config) {
       
   170 		String scriptPath = null;
       
   171 		try {
       
   172 			String tmp = config.getAttribute(
       
   173 					LaunchConfigurationConstants.SCRIPT_HOST_PATH,
       
   174 					(String) null);
       
   175 			File scriptFile = new File(tmp);
       
   176 			scriptPath = scriptFile.getAbsolutePath();
       
   177 		} catch (CoreException e) {
       
   178 			TFWLaunchPlugin.log(e);
       
   179 		}
       
   180 		return scriptPath;
       
   181 	}
       
   182 
       
   183 	private TailFileInputStream createTailFileInputStream(String filepath) {
       
   184 		TailFileInputStream tailStream = null;
       
   185 		try {
       
   186 			tailStream = new TailFileInputStream(filepath);
       
   187 		} catch (IOException e) {
       
   188 			TFWLaunchPlugin.log(e);
       
   189 		}
       
   190 		return tailStream;
       
   191 	}
       
   192 
       
   193 	private TestCaseResult startTestCase(TestRunResult result, String info,
       
   194 			double time) {
       
   195 		return result.updateCaseStatus(null, getValue(info),
       
   196 				TestStatus.STARTED, time);
       
   197 	}
       
   198 
       
   199 	private TestCaseResult endTestCase(TestRunResult result, String info,
       
   200 			double time) {
       
   201 		TestStatus status = null;
       
   202 
       
   203 		// Get case name.
       
   204 		String caseName = getValue(info);
       
   205 
       
   206 		// Get error code.
       
   207 		int errIndex = info.indexOf(END_TEST_CASE);
       
   208 		if ("0".equalsIgnoreCase(getValue(info.substring(errIndex
       
   209 				+ END_TEST_CASE.length())))) {
       
   210 			status = TestStatus.SUCCESS;
       
   211 		} else {
       
   212 			status = TestStatus.FAILURE;
       
   213 		}
       
   214 
       
   215 		// Get error msg.
       
   216 		int msgIndex = info.indexOf(RESULT_MESSAGE);
       
   217 		String msg = info.substring(msgIndex + RESULT_MESSAGE.length());
       
   218 
       
   219 		double timeSpent = -1;
       
   220 		TestResult[] caseResult = result.getResults();
       
   221 		for (TestResult cur : caseResult) {
       
   222 			if (cur.getName().equalsIgnoreCase(caseName)) {
       
   223 				timeSpent = (time - cur.getTime()) / 1000;
       
   224 				failedTestCase(cur, msg);
       
   225 			}
       
   226 		}
       
   227 
       
   228 		return result.updateCaseStatus(null, caseName, status, timeSpent);
       
   229 	}
       
   230 
       
   231 	private String getValue(String content) {
       
   232 		int startIndex = content.indexOf(INFO_START_FLAG);
       
   233 		int endIndex = content.indexOf(INFO_END_FLAG);
       
   234 		if (startIndex < endIndex) {
       
   235 			return content.substring(startIndex + 1, endIndex);
       
   236 		} else {
       
   237 			return null;
       
   238 		}
       
   239 	}
       
   240 
       
   241 	private Date getDate(String info) {
       
   242 		Date date = null;
       
   243 		DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
       
   244 		try {
       
   245 			date = dateFormat.parse(info);
       
   246 		} catch (java.text.ParseException e) {
       
   247 			TFWLaunchPlugin.log(e);
       
   248 		}
       
   249 		return date;
       
   250 	}
       
   251 
       
   252 	private TestCaseResult addTestCase(TestRunResult result, String info) {
       
   253 		// String lTestCaseName = getValue(info);
       
   254 		return result.addTestCase(null, info);
       
   255 	}
       
   256 
       
   257 	private void failedTestCase(TestResult result, String msg) {
       
   258 		int index;
       
   259 		if ((index = msg.indexOf(FILE)) > -1) {
       
   260 			String lcontent = msg.substring(index + FILE.length());
       
   261 			String file = getValue(lcontent);
       
   262 			result.setFile(file);
       
   263 			if ((index = msg.indexOf(LINE)) > -1) {
       
   264 				lcontent = msg.substring(index + LINE.length());
       
   265 				String line = getValue(lcontent);
       
   266 				result.setLine(Integer.valueOf(line));
       
   267 			}
       
   268 		} else {
       
   269 			String message = msg.substring(msg.indexOf(INFO_START_FLAG)
       
   270 					+ INFO_START_FLAG.length(), msg.lastIndexOf(INFO_END_FLAG));
       
   271 			result.setMessage(message);
       
   272 		}
       
   273 	}
       
   274 
       
   275 	// private members
       
   276 	private String iLogFile = null;
       
   277 	private InputStream iInputStream = null;
       
   278 }