testdev/ite/src/com.nokia.testfw.launch/src/com/nokia/testfw/launch/monitor/LogXmlHandler.java
changeset 1 96906a986c3b
equal deleted inserted replaced
0:f1112f777ce9 1:96906a986c3b
       
     1 /*
       
     2  * Copyright (c) 2005-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.launch.monitor;
       
    18 
       
    19 import java.text.SimpleDateFormat;
       
    20 
       
    21 import org.eclipse.core.runtime.IStatus;
       
    22 import org.xml.sax.Attributes;
       
    23 import org.xml.sax.SAXException;
       
    24 import org.xml.sax.helpers.DefaultHandler;
       
    25 
       
    26 import com.nokia.testfw.core.model.result.TestCaseResult;
       
    27 import com.nokia.testfw.core.model.result.TestResult;
       
    28 import com.nokia.testfw.core.model.result.TestRunResult;
       
    29 import com.nokia.testfw.core.model.result.TestSuiteResult;
       
    30 import com.nokia.testfw.core.model.result.TestResult.TestStatus;
       
    31 import com.nokia.testfw.launch.TFWLaunchPlugin;
       
    32 
       
    33 /**
       
    34  * @author xiaoma
       
    35  * 
       
    36  */
       
    37 public class LogXmlHandler extends DefaultHandler {
       
    38 
       
    39 	TestRunResult result;
       
    40 	TestSuiteResult curSuiteResult;
       
    41 	TestCaseResult curCaseResult;
       
    42 	boolean isDefinition = false;
       
    43 	String suiteStartTime;
       
    44 	String caseStartTime;
       
    45 	boolean isSuite = false;
       
    46 	boolean isCase = false;
       
    47 	boolean isStartTime = false;
       
    48 	boolean isFinishTime = false;
       
    49 
       
    50 	public LogXmlHandler(TestRunResult result) {
       
    51 		this.result = result;
       
    52 		curSuiteResult = null;
       
    53 		curCaseResult = null;
       
    54 	}
       
    55 
       
    56 	public void startDocument() throws SAXException {
       
    57 		TFWLaunchPlugin.log(IStatus.INFO, "start process result xml log");
       
    58 		result.testStarted();
       
    59 	}
       
    60 
       
    61 	public void endDocument() throws SAXException {
       
    62 		TFWLaunchPlugin.log(IStatus.INFO, "end process result xml log");
       
    63 		result.testFinished();
       
    64 	}
       
    65 
       
    66 	public void startElement(String uri, String localName, String qName,
       
    67 			Attributes attributes) throws SAXException {
       
    68 		if (qName.equals("testsuite")) {
       
    69 			processTestSuiteStart(attributes);
       
    70 			isSuite = true;
       
    71 		} else if (qName.equals("test")) {
       
    72 			processTestStart(attributes);
       
    73 			isCase = true;
       
    74 		} else if (qName.equals("status")) {
       
    75 			processTestStatus(attributes);
       
    76 		} else if (qName.equals("location")) {
       
    77 			processLocation(attributes);
       
    78 		} else if (qName.equals("definition")) {
       
    79 			isDefinition = true;
       
    80 		} else if (qName.equals("started")) {
       
    81 			isStartTime = true;
       
    82 		} else if (qName.equals("finished")) {
       
    83 			isFinishTime = true;
       
    84 		}
       
    85 	}
       
    86 
       
    87 	public void endElement(String uri, String localName, String qName)
       
    88 			throws SAXException {
       
    89 		if (qName.equals("testsuite")) {
       
    90 			curSuiteResult = null;
       
    91 			isSuite = false;
       
    92 		} else if (qName.equals("test")) {
       
    93 			curCaseResult = null;
       
    94 			isCase = false;
       
    95 		} else if (qName.equals("definition")) {
       
    96 			isDefinition = false;
       
    97 		} else if (qName.equals("started")) {
       
    98 			isStartTime = false;
       
    99 		} else if (qName.equals("finished")) {
       
   100 			isFinishTime = false;
       
   101 		}
       
   102 	}
       
   103 
       
   104 	private void processTestSuiteStart(Attributes attributes) {
       
   105 		String name = attributes.getValue("name");
       
   106 		curSuiteResult = result.addTestSuite(name);
       
   107 	}
       
   108 
       
   109 	private void processTestStart(Attributes attributes) {
       
   110 		String suiteName = null;
       
   111 		if (curSuiteResult != null) {
       
   112 			suiteName = curSuiteResult.getName();
       
   113 		}
       
   114 		String caseName = attributes.getValue("name");
       
   115 		curCaseResult = result.addTestCase(suiteName, caseName);
       
   116 		// start test case
       
   117 		result.updateCaseStatus(suiteName, caseName, TestStatus.STARTED, 0);
       
   118 
       
   119 	}
       
   120 
       
   121 	private void processTestStatus(Attributes attributes) {
       
   122 		String type = attributes.getValue("type");
       
   123 		TestStatus status = null;
       
   124 		if (type.equals("pass")) {
       
   125 			status = TestStatus.SUCCESS;
       
   126 		} else if (type.equals("crash")) {
       
   127 			status = TestStatus.FAILURE;
       
   128 		} else if (type.equals("fail")) {
       
   129 			status = TestStatus.FAILURE;
       
   130 		} else {
       
   131 			System.err.println("unknow status status:" + type);
       
   132 		}
       
   133 
       
   134 		result.updateCaseStatus(curSuiteResult.getName(), curCaseResult
       
   135 				.getName(), status, 0);
       
   136 		// the current schema has problem
       
   137 	}
       
   138 
       
   139 	private void processLocation(Attributes attributes) {
       
   140 		if (!isDefinition) {
       
   141 			return;
       
   142 		}
       
   143 		TestResult tr = null;
       
   144 		if (curCaseResult != null) {
       
   145 			tr = curCaseResult;
       
   146 		} else if (curSuiteResult != null) {
       
   147 			tr = curSuiteResult;
       
   148 		}
       
   149 		String file = attributes.getValue("file");
       
   150 		String line = attributes.getValue("line");
       
   151 		String col = attributes.getValue("column");
       
   152 
       
   153 		if (tr != null) {
       
   154 			tr.setFile(file);
       
   155 			if (line != null) {
       
   156 				tr.setLine(Integer.parseInt(line));
       
   157 			}
       
   158 			if (col != null) {
       
   159 				tr.setColumn(Integer.parseInt(col));
       
   160 			}
       
   161 		}
       
   162 	}
       
   163 
       
   164 	public void characters(char ch[], int start, int length)
       
   165 			throws SAXException {
       
   166 
       
   167 		if (isStartTime) {
       
   168 			String time = new String(ch, start, length);
       
   169 			// processStartTime
       
   170 			if (isCase) {
       
   171 				caseStartTime = time;
       
   172 			} else if (isSuite) {
       
   173 				suiteStartTime = time;
       
   174 			}
       
   175 		} else if (isFinishTime) {
       
   176 
       
   177 			try {
       
   178 				// process finish time
       
   179 				String time = new String(ch, start, length);
       
   180 				SimpleDateFormat formatter = new SimpleDateFormat(
       
   181 						"yyyy-MM-dd'T'HH:mm:ss");
       
   182 				if (isCase) {
       
   183 					int duration = (int) (formatter.parse(time).getTime() - formatter
       
   184 							.parse(caseStartTime).getTime()) / 1000;
       
   185 					curCaseResult.setTime(duration);
       
   186 				} else if (isSuite) {
       
   187 					int duration = (int) (formatter.parse(time).getTime() - formatter
       
   188 							.parse(suiteStartTime).getTime()) / 1000;
       
   189 					curSuiteResult.setTime(duration);
       
   190 				}
       
   191 			} catch (Exception e) {
       
   192 				TFWLaunchPlugin.log(e);
       
   193 				e.printStackTrace();
       
   194 			}
       
   195 		}
       
   196 	}
       
   197 
       
   198 }