tracefw/tracecompiler/test/src/MacroWrappersTest.java
branchRCL_3
changeset 20 ca8a1b6995f6
equal deleted inserted replaced
19:07b41fa8d1dd 20:ca8a1b6995f6
       
     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 * JUnit tests for OST MacroWrappers
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 import static org.junit.Assert.assertTrue;
       
    22 
       
    23 import java.util.ArrayList;
       
    24 import org.junit.Assert;
       
    25 import org.junit.Test;
       
    26 import com.nokia.tracecompiler.source.*;
       
    27 import com.nokia.tracecompiler.document.*;
       
    28 
       
    29 public class MacroWrappersTest {
       
    30 
       
    31 	public static void main(String[] args) {
       
    32 		org.junit.runner.JUnitCore.main(MacroWrappersTest.class.getName());
       
    33 	}
       
    34 	
       
    35 	@Test
       
    36 	public void testMacroWrappers(){
       
    37 		
       
    38 		// this string should parse down to the following members in list	
       
    39 		final String REFERENCE_STR = 
       
    40 			"TRACE_DEBUG_ONLY ( mystuff,…, OstTraceExt3( TRACE_DEBUG,DUP7__CONSTRUCTL,\"::ConstructL;x=%d;y=%d;z=%d\",x,y,z ) );";
       
    41 		FileDocumentMonitor monitor = new FileDocumentMonitor();		
       
    42 		SourceDocumentFactory factory = monitor.getFactory();
       
    43 
       
    44 		// construct the reference parameter array
       
    45 		ArrayList<String> expectedTokens = new ArrayList<String>();
       
    46 		expectedTokens.add("TRACE_DEBUG");
       
    47 		expectedTokens.add("DUP7__CONSTRUCTL");
       
    48 		expectedTokens.add("\"::ConstructL;x=%d;y=%d;z=%d\"");
       
    49 		expectedTokens.add("x");
       
    50 		expectedTokens.add("y");
       
    51 		expectedTokens.add("z");
       
    52 
       
    53 		// construct the trace string from reference parameters
       
    54 		final String TRACE_STR = "OstTraceExt3( "+ expectedTokens.get(0) + 
       
    55 								 "," + expectedTokens.get(1) + 
       
    56 								 "," + expectedTokens.get(2) +
       
    57 								 "," + expectedTokens.get(3) + 
       
    58 								 "," + expectedTokens.get(4) +
       
    59 								 "," + expectedTokens.get(5) + " )";
       
    60 				
       
    61 		// first test the parser on an unwrapped string
       
    62 		SourceParser sourceParser = new SourceParser(factory, factory.createDocument(TRACE_STR));
       
    63 		ArrayList<String> actualTokens = new ArrayList<String>();
       
    64 		// parse string
       
    65 		try{
       
    66 			sourceParser.tokenizeParameters(0, actualTokens, true);			
       
    67 		}catch(SourceParserException e){
       
    68 			Assert.fail(e.getMessage());
       
    69 		}
       
    70 						
       
    71 		checkContents(actualTokens, expectedTokens); 
       
    72 						
       
    73 		// Create the wrapped OstTrace...  	
       
    74 		final String WRAPPED_STR = "TRACE_DEBUG_ONLY ( mystuff,…, " + TRACE_STR + " );";
       
    75 		
       
    76 		// do a quick check to see that the reference string matches the string generated from list
       
    77 		// we could use java.util.StringTokenizer to generate the expected list, 
       
    78 		// but this is test code for tracecompiler after all
       
    79 		assertTrue(REFERENCE_STR.compareTo(WRAPPED_STR) == 0 );
       
    80 
       
    81 		sourceParser = new SourceParser(factory, factory.createDocument(WRAPPED_STR));
       
    82 		
       
    83 
       
    84 		actualTokens.clear();
       
    85 		// this string should parse down to the following members in list
       
    86 		// [0]	"mystuff"	
       
    87 		// [1]	"…"	
       
    88 		// [2]	"OstTraceExt3( TRACE_DEBUG, DUP7__CONSTRUCTL, "::ConstructL;x=%d;y=%d;z=%d",x,y,z )"	
       
    89 		try{
       
    90 			sourceParser.tokenizeParameters(0, actualTokens, true);			
       
    91 		}catch(SourceParserException e){
       
    92 			Assert.fail(e.getMessage());
       
    93 		}
       
    94 
       
    95 		assertTrue(actualTokens.size() == 3);
       
    96 		assertTrue(((String)actualTokens.get(0)).compareTo("mystuff") == 0);
       
    97 		assertTrue(((String)actualTokens.get(1)).compareTo("…") == 0);
       
    98 		
       
    99 		// now check the extracted OstTrace part
       
   100 		String ostStr = (String)actualTokens.get(2);
       
   101 		assertTrue(ostStr.compareTo(TRACE_STR) == 0);
       
   102 		
       
   103 		sourceParser = new SourceParser(factory, factory.createDocument(ostStr));
       
   104 		actualTokens.clear();
       
   105 
       
   106 		try{
       
   107 			sourceParser.tokenizeParameters(0, actualTokens, true);			
       
   108 		}catch(SourceParserException e){
       
   109 			Assert.fail(e.getMessage());
       
   110 		}
       
   111 						
       
   112 		checkContents(actualTokens, expectedTokens); 
       
   113 	}
       
   114 	
       
   115 	
       
   116 	@Test
       
   117 	public void testMultipleMacroWrappers(){
       
   118 		
       
   119 		// testing parsing of macro in this format
       
   120 		//TRACE_DEBUG_ONLY (OstTrace0( TRACE_DETAILED, DUP5__CONSTRUCTL, "::ConstructL" )
       
   121 		// , OstTraceExt3( TRACE_BORDER, DUP99__CONSTRUCTL, "::ConstructL;x=%x;y=%x;z=%x",x,y, (x+y) ));
       
   122 		
       
   123 		final String REFERENCE_STR = 
       
   124 			"TRACE_DEBUG_ONLY (OstTrace0( TRACE_DETAILED,DUP5__CONSTRUCTL ),OstTraceExt3( TRACE_BORDER,DUP99__CONSTRUCTL,\"::ConstructL;x=%x;y=%x;z=%x\",x,y,(x+y) ));";
       
   125 
       
   126 		FileDocumentMonitor monitor = new FileDocumentMonitor();		
       
   127 		SourceDocumentFactory factory = monitor.getFactory();
       
   128 
       
   129 		
       
   130 		// construct the reference parameter array
       
   131 		ArrayList<String> expectedTokens1 = new ArrayList<String>();
       
   132 		expectedTokens1.add("TRACE_DETAILED");
       
   133 		expectedTokens1.add("DUP5__CONSTRUCTL");
       
   134 		
       
   135 		
       
   136 		ArrayList<String> expectedTokens2 = new ArrayList<String>();
       
   137 		expectedTokens2.add("TRACE_BORDER");
       
   138 		expectedTokens2.add("DUP99__CONSTRUCTL");
       
   139 		expectedTokens2.add("\"::ConstructL;x=%x;y=%x;z=%x\"");
       
   140 		expectedTokens2.add("x");
       
   141 		expectedTokens2.add("y");
       
   142 		expectedTokens2.add("(x+y)");
       
   143 
       
   144 		// construct the trace strings from reference parameters
       
   145 		final String TRACE_STR1 = "OstTrace0( "+ expectedTokens1.get(0) + 
       
   146 								 "," + expectedTokens1.get(1) + " )";
       
   147 
       
   148 		final String TRACE_STR2 = "OstTraceExt3( "+ expectedTokens2.get(0) + 
       
   149 								  "," + expectedTokens2.get(1) + 
       
   150 								  "," + expectedTokens2.get(2) +
       
   151 								  "," + expectedTokens2.get(3) + 
       
   152 								  "," + expectedTokens2.get(4) +
       
   153 								  "," + expectedTokens2.get(5) + " )";
       
   154 
       
   155 		// check it works with TRACE_STR1
       
   156 		SourceParser sourceParser = new SourceParser(factory, factory.createDocument(TRACE_STR1));
       
   157 		ArrayList<String> actualTokens = new ArrayList<String>();
       
   158 		// parse string
       
   159 		try{
       
   160 			sourceParser.tokenizeParameters(0, actualTokens, true);			
       
   161 		}catch(SourceParserException e){
       
   162 			Assert.fail(e.getMessage());
       
   163 		}
       
   164 						
       
   165 		checkContents(actualTokens, expectedTokens1); 
       
   166 
       
   167 		// check it works with TRACE_STR2
       
   168 		sourceParser = new SourceParser(factory, factory.createDocument(TRACE_STR2));
       
   169 		actualTokens.clear();
       
   170 		
       
   171 		// parse string
       
   172 		try{
       
   173 			sourceParser.tokenizeParameters(0, actualTokens, true);			
       
   174 		}catch(SourceParserException e){
       
   175 			Assert.fail(e.getMessage());
       
   176 		}
       
   177 						
       
   178 		checkContents(actualTokens, expectedTokens2); 
       
   179 
       
   180 		// create the composite string
       
   181 		String TRACE_STR3 = "TRACE_DEBUG_ONLY (" + TRACE_STR1 + "," + TRACE_STR2 + ");";
       
   182 
       
   183 		// do a quick check to see that the reference string matches the string generated from list
       
   184 		// we could use java.util.StringTokenizer to generate the expected list, 
       
   185 		// but this is test code for tracecompiler after all
       
   186 		assertTrue(REFERENCE_STR.compareTo(TRACE_STR3) == 0);
       
   187 		
       
   188 		sourceParser = new SourceParser(factory, factory.createDocument(TRACE_STR3));
       
   189 		actualTokens.clear();
       
   190 
       
   191 		// this string should parse down to the following members in list
       
   192 		// [0]	"OstTraceExt0( TRACE_DETAILED,DUP5__CONSTRUCTL )" (id=77)	
       
   193 		// [1]	"OstTraceExt3( TRACE_BORDER,DUP99__CONSTRUCTL,"::ConstructL;x=%d;y=%d;z=%d",x,y,z )" (id=78)	
       
   194 		try{
       
   195 			sourceParser.tokenizeParameters(0, actualTokens, true);			
       
   196 		}catch(SourceParserException e){
       
   197 			Assert.fail(e.getMessage());
       
   198 		}
       
   199 		
       
   200 		assertTrue(actualTokens.size() == 2);
       
   201 		final String OST_STR1 = actualTokens.get(0);
       
   202 		assertTrue(OST_STR1.compareTo(TRACE_STR1) == 0);
       
   203 		final String OST_STR2 = actualTokens.get(1);
       
   204 		assertTrue(OST_STR2.compareTo(TRACE_STR2) == 0);
       
   205 		
       
   206 		
       
   207 		// check parsing of first OST macro
       
   208 		sourceParser = new SourceParser(factory, factory.createDocument(OST_STR1));
       
   209 		actualTokens.clear();
       
   210 		try{
       
   211 			sourceParser.tokenizeParameters(0, actualTokens, true);			
       
   212 		}catch(SourceParserException e){
       
   213 			Assert.fail(e.getMessage());
       
   214 		}
       
   215 
       
   216 		checkContents(actualTokens, expectedTokens1);
       
   217 		
       
   218 
       
   219 		// check parsing of second OST macro
       
   220 		sourceParser = new SourceParser(factory, factory.createDocument(OST_STR2));
       
   221 		actualTokens.clear();
       
   222 		try{
       
   223 			sourceParser.tokenizeParameters(0, actualTokens, true);			
       
   224 		}catch(SourceParserException e){
       
   225 			Assert.fail(e.getMessage());
       
   226 		}
       
   227 
       
   228 		checkContents(actualTokens, expectedTokens2);
       
   229 		
       
   230 	}
       
   231 
       
   232 	
       
   233 	private static void checkContents(final ArrayList<String> ACTUAL, final ArrayList<String> EXPECTED ){
       
   234 		assertTrue(ACTUAL.size() == EXPECTED.size());
       
   235 		for(int i=0;i < ACTUAL.size();i++){
       
   236 			assertTrue( ACTUAL.get(i).compareTo(EXPECTED.get(i)) == 0);			
       
   237 		}		
       
   238 	}
       
   239 }