tracefw/tracecompiler/test/src/ErrorLogsTestBase.java
author hgs
Mon, 23 Aug 2010 15:29:36 +0300
changeset 41 838cdffd57ce
permissions -rw-r--r--
201031
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description: 
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
hgs
parents:
diff changeset
    19
import static org.junit.Assert.fail;
hgs
parents:
diff changeset
    20
hgs
parents:
diff changeset
    21
import java.io.BufferedReader;
hgs
parents:
diff changeset
    22
import java.io.DataInputStream;
hgs
parents:
diff changeset
    23
import java.io.File;
hgs
parents:
diff changeset
    24
import java.io.FileInputStream;
hgs
parents:
diff changeset
    25
import java.io.FileOutputStream;
hgs
parents:
diff changeset
    26
import java.io.IOException;
hgs
parents:
diff changeset
    27
import java.io.InputStream;
hgs
parents:
diff changeset
    28
import java.io.InputStreamReader;
hgs
parents:
diff changeset
    29
import java.io.PrintStream;
hgs
parents:
diff changeset
    30
hgs
parents:
diff changeset
    31
public class ErrorLogsTestBase {
hgs
parents:
diff changeset
    32
	
hgs
parents:
diff changeset
    33
	/****************************************************UTILITY FUNCTIONS FOR TESTS************************************************************/
hgs
parents:
diff changeset
    34
	
hgs
parents:
diff changeset
    35
	/**
hgs
parents:
diff changeset
    36
	 * This function gets an environment variable value given the name.
hgs
parents:
diff changeset
    37
	 * @param variablename The name of the variable for which the value is required.
hgs
parents:
diff changeset
    38
	 * @return The variable value.
hgs
parents:
diff changeset
    39
	 */
hgs
parents:
diff changeset
    40
	public String GetEnvironmentVariablePath(String variablename) {
hgs
parents:
diff changeset
    41
		String variablevalue = System.getenv(variablename);
hgs
parents:
diff changeset
    42
		if(variablevalue == null || (variablevalue.length()==0)){
hgs
parents:
diff changeset
    43
			fail();
hgs
parents:
diff changeset
    44
		}
hgs
parents:
diff changeset
    45
		
hgs
parents:
diff changeset
    46
		// need to check that the path ends in a backslash
hgs
parents:
diff changeset
    47
		if(!variablevalue.endsWith(File.separator)){ 
hgs
parents:
diff changeset
    48
			variablevalue += File.separator; 
hgs
parents:
diff changeset
    49
		}
hgs
parents:
diff changeset
    50
		return variablevalue;
hgs
parents:
diff changeset
    51
	}
hgs
parents:
diff changeset
    52
	
hgs
parents:
diff changeset
    53
	/**
hgs
parents:
diff changeset
    54
	 * This function invokes the desired compiler for a particular test case depending
hgs
parents:
diff changeset
    55
	 * on the string passed
hgs
parents:
diff changeset
    56
	 * @param compiler String specifying the compiler to run, and also specifying where
hgs
parents:
diff changeset
    57
	 * 		  		   the logs should be stored.
hgs
parents:
diff changeset
    58
	 */
hgs
parents:
diff changeset
    59
	public void InvokeCompiler(String compiler) {
hgs
parents:
diff changeset
    60
		System.out.println ("InvokeCompiler()");
hgs
parents:
diff changeset
    61
		//get epocroot location
hgs
parents:
diff changeset
    62
		String e32=GetEnvironmentVariablePath("EPOCROOT");
hgs
parents:
diff changeset
    63
		
hgs
parents:
diff changeset
    64
		String TraceErrorAppPath = e32 + "testdata" + File.separator + "TraceErrorApp" + File.separator;
hgs
parents:
diff changeset
    65
		try{
hgs
parents:
diff changeset
    66
			File compilerpath = new File (e32 + "epoc32" + File.separator + "tools" + File.separator + "tracecompiler" + File.separator);
hgs
parents:
diff changeset
    67
			
hgs
parents:
diff changeset
    68
			if (compiler.compareTo("_sbs")==0){//raptor
hgs
parents:
diff changeset
    69
				compilerpath = new File (e32 + "testdata" + File.separator + "TraceErrorApp" + File.separator +"group" + File.separator);
hgs
parents:
diff changeset
    70
			}
hgs
parents:
diff changeset
    71
			else if (compiler.compareTo("_mult_cpp")==0){//raptor compiling project with multiple source files
hgs
parents:
diff changeset
    72
				compilerpath = new File (e32 + "testdata" + File.separator + "MultipleCppTraceErrorApp" + File.separator + "group" + File.separator);
hgs
parents:
diff changeset
    73
			}
hgs
parents:
diff changeset
    74
			else if (compiler.compareTo("_mult_mmp")==0){//raptor compiling project with multiple source files
hgs
parents:
diff changeset
    75
				compilerpath = new File (e32 + "testdata" + File.separator + "MultipleMmpTraceErrorApps" + File.separator + "group" + File.separator);
hgs
parents:
diff changeset
    76
			}
hgs
parents:
diff changeset
    77
			else if (compiler.compareTo("_tc")!=0){
hgs
parents:
diff changeset
    78
				System.out.println("Error: Unrecognised test case. Use either _tc, _sbs, _mult_cpp or _mult_mmp");
hgs
parents:
diff changeset
    79
				fail();
hgs
parents:
diff changeset
    80
			}
hgs
parents:
diff changeset
    81
			
hgs
parents:
diff changeset
    82
			//set up the process builder object
hgs
parents:
diff changeset
    83
	 		ProcessBuilder pb; 
hgs
parents:
diff changeset
    84
	 		//commands to be used to invoke the compiler
hgs
parents:
diff changeset
    85
	 		if (compiler.compareTo("_tc")==0){//tracecompiler
hgs
parents:
diff changeset
    86
	 			// These EMMA flags are added, because of EMMA reporting in Hudson
hgs
parents:
diff changeset
    87
				// EMMA lfags are commented out, because it does not work in Hudson at the moment
hgs
parents:
diff changeset
    88
//	 			String emmaOutputFileFlag = "-Demma.coverage.out.file=" + e32 + "testdata" + File.separator + "reports" + File.separator + "emma" + File.separator + "coverage.emma";
hgs
parents:
diff changeset
    89
//	 			String emmaOutputMergeFlag = "-Demma.coverage.out.merge=true";
hgs
parents:
diff changeset
    90
	 			String emmaOutputFileFlag = "";
hgs
parents:
diff changeset
    91
	 			String emmaOutputMergeFlag = "";
hgs
parents:
diff changeset
    92
	 			pb = new ProcessBuilder("java", emmaOutputFileFlag, emmaOutputMergeFlag, "-classpath",".", "com.nokia.tracecompiler.TraceCompiler", "E8576D92", "TraceErrorApp",TraceErrorAppPath+"group"+File.separator+"TraceErrorApp.mmp",TraceErrorAppPath+"src"+File.separator+"TraceErrorApp.cpp");
hgs
parents:
diff changeset
    93
	 		}
hgs
parents:
diff changeset
    94
	 		else /*if (compiler.compareTo("_sbs")==0)*/{//for all other raptor cases
hgs
parents:
diff changeset
    95
	 			pb = new ProcessBuilder("sbs.bat","-c","armv5.tracecompiler");
hgs
parents:
diff changeset
    96
	 		}
hgs
parents:
diff changeset
    97
	 		
hgs
parents:
diff changeset
    98
			pb.directory(compilerpath);
hgs
parents:
diff changeset
    99
			pb.redirectErrorStream(true);
hgs
parents:
diff changeset
   100
			
hgs
parents:
diff changeset
   101
			//start the compiler
hgs
parents:
diff changeset
   102
			Process p = pb.start();
hgs
parents:
diff changeset
   103
			InputStream inp = p.getInputStream();
hgs
parents:
diff changeset
   104
	        int c = -1;
hgs
parents:
diff changeset
   105
	        
hgs
parents:
diff changeset
   106
	        
hgs
parents:
diff changeset
   107
	        String str = "";
hgs
parents:
diff changeset
   108
	        
hgs
parents:
diff changeset
   109
	        /*
hgs
parents:
diff changeset
   110
	        //read the output from the compiler into the input stream
hgs
parents:
diff changeset
   111
	        while ((c = inp.read()) != -1) {
hgs
parents:
diff changeset
   112
	            str= str +((char)c);
hgs
parents:
diff changeset
   113
	        }
hgs
parents:
diff changeset
   114
	        */
hgs
parents:
diff changeset
   115
	        
hgs
parents:
diff changeset
   116
	        StringBuilder sb = new StringBuilder();
hgs
parents:
diff changeset
   117
	        //read the output from the compiler into the input stream
hgs
parents:
diff changeset
   118
	        while ((c = inp.read()) != -1) {
hgs
parents:
diff changeset
   119
	        	sb.append(((char)c));
hgs
parents:
diff changeset
   120
	        }
hgs
parents:
diff changeset
   121
	        str = sb.toString();
hgs
parents:
diff changeset
   122
	        
hgs
parents:
diff changeset
   123
	        
hgs
parents:
diff changeset
   124
	    	//Declare output file to write logs to
hgs
parents:
diff changeset
   125
	    	FileOutputStream outstream; 
hgs
parents:
diff changeset
   126
			PrintStream ps; 
hgs
parents:
diff changeset
   127
			System.out.println ("creating output logs in buildlog"+compiler+".txt");
hgs
parents:
diff changeset
   128
			outstream = new FileOutputStream(e32+"epoc32"+File.separator+"build"+File.separator+"buildlog"+compiler+".txt");
hgs
parents:
diff changeset
   129
			ps = new PrintStream( outstream );
hgs
parents:
diff changeset
   130
			ps.println (str);
hgs
parents:
diff changeset
   131
			
hgs
parents:
diff changeset
   132
			//check return code if tracecompiler.. we don't check error code for raptor:
hgs
parents:
diff changeset
   133
			int ret = p.exitValue();
hgs
parents:
diff changeset
   134
			if ((ret==0)&&(compiler.compareTo("_tc")==0)){
hgs
parents:
diff changeset
   135
				System.out.println("FAIL: TraceCompiler does not exit with error");
hgs
parents:
diff changeset
   136
				fail();
hgs
parents:
diff changeset
   137
			}
hgs
parents:
diff changeset
   138
			inp.close();
hgs
parents:
diff changeset
   139
			outstream.close();
hgs
parents:
diff changeset
   140
		}
hgs
parents:
diff changeset
   141
		
hgs
parents:
diff changeset
   142
    	catch (IOException e){//Catch IOException
hgs
parents:
diff changeset
   143
    		System.err.println("Error: " + e.getMessage());
hgs
parents:
diff changeset
   144
    		fail();
hgs
parents:
diff changeset
   145
        }
hgs
parents:
diff changeset
   146
		
hgs
parents:
diff changeset
   147
	}
hgs
parents:
diff changeset
   148
	
hgs
parents:
diff changeset
   149
	/**
hgs
parents:
diff changeset
   150
	 * This function parses the log files (as specified by the passed string) for errors and warnings
hgs
parents:
diff changeset
   151
	 *  and stores them in a separate file (also specified by the same passed string)
hgs
parents:
diff changeset
   152
	 * 
hgs
parents:
diff changeset
   153
	 * @param pathext  String specifying the part of the name of the build log file to parse which is 
hgs
parents:
diff changeset
   154
	 * 				   also part of the name of the file where parsed logs are written to.	  		   
hgs
parents:
diff changeset
   155
	 */
hgs
parents:
diff changeset
   156
	public void ParseLogFile(String pathext) {
hgs
parents:
diff changeset
   157
		System.out.println ("ParseLogFile()");
hgs
parents:
diff changeset
   158
		//get epocroot location
hgs
parents:
diff changeset
   159
		String e32=GetEnvironmentVariablePath("EPOCROOT");
hgs
parents:
diff changeset
   160
		
hgs
parents:
diff changeset
   161
		
hgs
parents:
diff changeset
   162
	    try{
hgs
parents:
diff changeset
   163
	    	// Open the file
hgs
parents:
diff changeset
   164
	    	System.out.println ("opening buildlog"+pathext+".txt");
hgs
parents:
diff changeset
   165
	    	FileInputStream fstream = new FileInputStream(e32+"epoc32"+File.separator+"build"+File.separator+"buildlog"+pathext+".txt");
hgs
parents:
diff changeset
   166
hgs
parents:
diff changeset
   167
	    	// Get the object of DataInputStream
hgs
parents:
diff changeset
   168
	    	//System.out.println ("creating input stream");
hgs
parents:
diff changeset
   169
	    	DataInputStream in = new DataInputStream(fstream);
hgs
parents:
diff changeset
   170
	        BufferedReader br = new BufferedReader(new InputStreamReader(in));
hgs
parents:
diff changeset
   171
	    	String strLine;
hgs
parents:
diff changeset
   172
	    	
hgs
parents:
diff changeset
   173
	    	//Declare output file to write parsed logs to
hgs
parents:
diff changeset
   174
	    	FileOutputStream out; 
hgs
parents:
diff changeset
   175
			PrintStream p; 
hgs
parents:
diff changeset
   176
			//System.out.println ("creating output stream");
hgs
parents:
diff changeset
   177
			out = new FileOutputStream(e32+"epoc32"+File.separator+"build"+File.separator+"parsedlogs"+pathext+".txt");
hgs
parents:
diff changeset
   178
			p = new PrintStream( out );
hgs
parents:
diff changeset
   179
	    	
hgs
parents:
diff changeset
   180
	    	//Read File Line By Line and write to parsedlogs.txt
hgs
parents:
diff changeset
   181
	    	System.out.println ("Writing to parsedlogs"+pathext+".txt");
hgs
parents:
diff changeset
   182
	    	while ((strLine = br.readLine()) != null&&(strLine.length()) >= 9)   {
hgs
parents:
diff changeset
   183
	    		//remove any leading white spaces because there are spaces when printed to standard output.
hgs
parents:
diff changeset
   184
	    		strLine=strLine.trim();
hgs
parents:
diff changeset
   185
		    	if((strLine.startsWith("error:")==true)||(strLine.startsWith("warning:")==true)){
hgs
parents:
diff changeset
   186
			    	// Write to a separate file
hgs
parents:
diff changeset
   187
			    	p.println (strLine);
hgs
parents:
diff changeset
   188
	    	}
hgs
parents:
diff changeset
   189
      	
hgs
parents:
diff changeset
   190
    	}
hgs
parents:
diff changeset
   191
    	
hgs
parents:
diff changeset
   192
    	//Close the input and output streams
hgs
parents:
diff changeset
   193
    	in.close();
hgs
parents:
diff changeset
   194
    	out.close();
hgs
parents:
diff changeset
   195
    	}
hgs
parents:
diff changeset
   196
    	
hgs
parents:
diff changeset
   197
    	catch (IOException e){//Catch IOException
hgs
parents:
diff changeset
   198
    		System.err.println("Error: " + e.getMessage());
hgs
parents:
diff changeset
   199
    		fail();
hgs
parents:
diff changeset
   200
    	}
hgs
parents:
diff changeset
   201
	
hgs
parents:
diff changeset
   202
	}
hgs
parents:
diff changeset
   203
	
hgs
parents:
diff changeset
   204
	/**
hgs
parents:
diff changeset
   205
	 * This function validates the parsed log file by comparing it to a reference file containg
hgs
parents:
diff changeset
   206
	 * the knows warnings and errors 
hgs
parents:
diff changeset
   207
	 * @param project String specifying the name of the project folder that was compiled.
hgs
parents:
diff changeset
   208
	 * @param pathext String specifying the part of the name of the parsed log file to validate.	   
hgs
parents:
diff changeset
   209
	 */
hgs
parents:
diff changeset
   210
	public void ValidateLogs(String project, String pathext) {
hgs
parents:
diff changeset
   211
		System.out.println ("ValidateLogs()");
hgs
parents:
diff changeset
   212
		//get epocroot location
hgs
parents:
diff changeset
   213
		String e32=GetEnvironmentVariablePath("EPOCROOT");
hgs
parents:
diff changeset
   214
		
hgs
parents:
diff changeset
   215
	try{
hgs
parents:
diff changeset
   216
		// Open the files
hgs
parents:
diff changeset
   217
		System.out.println ("opening referencelog.txt and parsedlogs"+pathext+".txt");
hgs
parents:
diff changeset
   218
		FileInputStream fstream1 = new FileInputStream(e32+"testdata"+File.separator+project+File.separator+"data"+File.separator+"referencelog.txt");
hgs
parents:
diff changeset
   219
		FileInputStream fstream2 = new FileInputStream(e32+"epoc32"+File.separator+"build"+File.separator+"parsedlogs"+pathext+".txt");
hgs
parents:
diff changeset
   220
	
hgs
parents:
diff changeset
   221
		// Get the objects of DataInputStream
hgs
parents:
diff changeset
   222
		//System.out.println ("creating input streams");
hgs
parents:
diff changeset
   223
		DataInputStream in1 = new DataInputStream(fstream1);
hgs
parents:
diff changeset
   224
		DataInputStream in2 = new DataInputStream(fstream2);
hgs
parents:
diff changeset
   225
		//System.out.println ("creating buffered readers");
hgs
parents:
diff changeset
   226
	    BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
hgs
parents:
diff changeset
   227
	    BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
hgs
parents:
diff changeset
   228
		String strLine1;
hgs
parents:
diff changeset
   229
		String strLine2;
hgs
parents:
diff changeset
   230
		int i=1;
hgs
parents:
diff changeset
   231
		
hgs
parents:
diff changeset
   232
		//compare the two files
hgs
parents:
diff changeset
   233
		while (((strLine1 = br1.readLine()) != null)&&(((strLine2 = br2.readLine()) != null)))   {
hgs
parents:
diff changeset
   234
			if (pathext.compareTo("_mult_mmp")!=0){//if this is not the multiple mmp test, then compare the whole error string
hgs
parents:
diff changeset
   235
				if(strLine1.compareTo(strLine2)==0){
hgs
parents:
diff changeset
   236
					System.out.println ("Validated line "+i+": "+strLine1);
hgs
parents:
diff changeset
   237
				}
hgs
parents:
diff changeset
   238
				else{
hgs
parents:
diff changeset
   239
					System.out.println ("Line "+i+" ("+strLine2+") is not correct.");
hgs
parents:
diff changeset
   240
					fail();
hgs
parents:
diff changeset
   241
				}
hgs
parents:
diff changeset
   242
			}
hgs
parents:
diff changeset
   243
			else{//else if this is the multiple mmp test, compare only the last 40 characters of the string because
hgs
parents:
diff changeset
   244
				//raptor does not have a specified order in which it compiles mmps using trace compiler so we can't validate exactly
hgs
parents:
diff changeset
   245
				if(strLine1.substring(strLine1.length()-40, strLine1.length()).compareTo(strLine2.substring(strLine1.length()-40, strLine2.length()))==0){
hgs
parents:
diff changeset
   246
					System.out.println ("Validated line "+i+": "+strLine1);
hgs
parents:
diff changeset
   247
				}
hgs
parents:
diff changeset
   248
				else{
hgs
parents:
diff changeset
   249
					System.out.println ("Line "+i+" ("+strLine2+") is not correct.");
hgs
parents:
diff changeset
   250
					fail();
hgs
parents:
diff changeset
   251
				}			
hgs
parents:
diff changeset
   252
			}
hgs
parents:
diff changeset
   253
		i++;
hgs
parents:
diff changeset
   254
		}
hgs
parents:
diff changeset
   255
		
hgs
parents:
diff changeset
   256
		if (i==1) {//never entered loop therefore there were no errors or warnings in the build logs
hgs
parents:
diff changeset
   257
			System.out.println("FAIL: Compiler did not output any correct errors to logs");
hgs
parents:
diff changeset
   258
			fail();
hgs
parents:
diff changeset
   259
		}
hgs
parents:
diff changeset
   260
		in1.close();
hgs
parents:
diff changeset
   261
		in2.close();
hgs
parents:
diff changeset
   262
	}
hgs
parents:
diff changeset
   263
	
hgs
parents:
diff changeset
   264
	catch (IOException e){//Catch IOException
hgs
parents:
diff changeset
   265
		System.err.println("Error: " + e.getMessage());
hgs
parents:
diff changeset
   266
		fail();
hgs
parents:
diff changeset
   267
    }
hgs
parents:
diff changeset
   268
	}
hgs
parents:
diff changeset
   269
}