tracesrv/tracecompiler/test/src/PluginTracePropertyVerifierTest.java
changeset 56 aa2539c91954
parent 41 838cdffd57ce
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 /*
       
     2  * Copyright (c) 2009-2010 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 PluginTracePropertyVerifier
       
    17  *
       
    18  */
       
    19 import java.lang.reflect.InvocationTargetException;
       
    20 import java.lang.reflect.Method;
       
    21 import java.util.ArrayList;
       
    22 import java.util.List;
       
    23 
       
    24 import junit.framework.Assert;
       
    25 import junit.framework.TestCase;
       
    26 
       
    27 import org.junit.After;
       
    28 import org.junit.AfterClass;
       
    29 import org.junit.Before;
       
    30 import org.junit.BeforeClass;
       
    31 import org.junit.Test;
       
    32 
       
    33 public class PluginTracePropertyVerifierTest extends TestCase {
       
    34 
       
    35 	/**
       
    36 	 * Class to PluginTracePropertyVerifier
       
    37 	 */
       
    38 	static Class<?> pluginTracePropertyVerifier;
       
    39 
       
    40 	/**
       
    41 	 * PluginTracePropertyVerifier object
       
    42 	 */
       
    43 	static Object ptpvObject;
       
    44 
       
    45 	/**
       
    46 	 * Empty string
       
    47 	 */
       
    48 	private static final String EMPTY_STRING = ""; //$NON-NLS-1$
       
    49 
       
    50 	/**
       
    51 	 * White space
       
    52 	 */
       
    53 	private static final String WHITE_SPACE = " "; //$NON-NLS-1$
       
    54 
       
    55 	/**
       
    56 	 * Enum for different functions to test
       
    57 	 * 
       
    58 	 */
       
    59 	private enum FunctionToTest {
       
    60 		ISVALIDDATA
       
    61 	}
       
    62 
       
    63 	/**
       
    64 	 * TestData class
       
    65 	 * 
       
    66 	 */
       
    67 	private class TestData {
       
    68 		String testString;
       
    69 		boolean expectedIsValidDataResult;
       
    70 
       
    71 		/**
       
    72 		 * 
       
    73 		 * Constructor
       
    74 		 * 
       
    75 		 * @param testStringParam
       
    76 		 *            the test string
       
    77 		 * @param expectedIsValidDataResultParam
       
    78 		 *            the expected return value in case of isValidData
       
    79 		 *            method
       
    80 		 */
       
    81 		TestData(String testStringParam, boolean expectedIsValidDataResultParam) {
       
    82 			this.testString = testStringParam;
       
    83 			this.expectedIsValidDataResult = expectedIsValidDataResultParam;
       
    84 		}
       
    85 	}
       
    86 
       
    87 	/**
       
    88 	 * List of test data
       
    89 	 */
       
    90 	List<TestData> testDataList = new ArrayList<TestData>();
       
    91 
       
    92 	/**
       
    93 	 * main
       
    94 	 * 
       
    95 	 * @param args
       
    96 	 */
       
    97 	public static void main(String[] args) {
       
    98 		org.junit.runner.JUnitCore
       
    99 				.main(PluginTracePropertyVerifierTest.class.getName());
       
   100 	}
       
   101 
       
   102 	/**
       
   103 	 * setUp
       
   104 	 */
       
   105 	@Before
       
   106 	public void setUp() {
       
   107 
       
   108 		if (testDataList.size() == 0) {
       
   109 			initializeTestDataList();
       
   110 		}
       
   111 
       
   112 		if (pluginTracePropertyVerifier == null && ptpvObject == null) {
       
   113 			try {
       
   114 				pluginTracePropertyVerifier = Class
       
   115 						.forName("com.nokia.tracecompiler.engine.plugin.PluginTracePropertyVerifier"); //$NON-NLS-1$
       
   116 				ptpvObject = pluginTracePropertyVerifier.newInstance();
       
   117 			} catch (Exception e) {
       
   118 				Assert.fail(e.toString());
       
   119 			}
       
   120 		}
       
   121 
       
   122 	}
       
   123 
       
   124 	/**
       
   125 	 * Initialize test data list
       
   126 	 */
       
   127 	private void initializeTestDataList() {
       
   128 		char VTAB = 0xB; //vertical TAB
       
   129 		testDataList.add(new TestData("Some valid text", true)); //valid text //$NON-NLS-1$
       
   130 		testDataList.add(new TestData("Some text with non-valid character - vertical TAB - : " + String.valueOf(VTAB) , false)); //$NON-NLS-1$
       
   131 		testDataList.add(new TestData("Some valid text with TAB	: " , true)); //$NON-NLS-1$
       
   132 		testDataList.add(new TestData("Some valid text with TABs				: " , true)); //$NON-NLS-1$
       
   133 	}
       
   134 
       
   135 	/**
       
   136 	 * Execute tests
       
   137 	 * 
       
   138 	 * @param nameOfFunctionUnderTest
       
   139 	 *            the name of the function that should be tested
       
   140 	 * @param testType
       
   141 	 *            the number of expected result column
       
   142 	 */
       
   143 	private void executeTests(String nameOfFunctionUnderTest,
       
   144 			FunctionToTest testType) {
       
   145 		for (int i = 0; i < testDataList.size(); i++) {
       
   146 			String testString = testDataList.get(i).testString;
       
   147 			boolean expectedResult = false;
       
   148 
       
   149 			switch (testType) {
       
   150 			case ISVALIDDATA:
       
   151 				expectedResult = testDataList.get(i).expectedIsValidDataResult;
       
   152 				break;
       
   153 			}
       
   154 
       
   155 			executeTest(nameOfFunctionUnderTest, testString, expectedResult);
       
   156 		}
       
   157 	}
       
   158 
       
   159 	/**
       
   160 	 * Execute test
       
   161 	 * 
       
   162 	 * @param nameOfFunctionUnderTest
       
   163 	 *            the name of the function that should be tested
       
   164 	 * @param testString
       
   165 	 *            the test string that is used in test
       
   166 	 * @param columNumberOfExpectedResult
       
   167 	 *            the number of expected result column
       
   168 	 */
       
   169 	private void executeTest(String nameOfFunctionUnderTest, String testString,
       
   170 			boolean expectedResult) {
       
   171 		Method functionUnderTest = null;
       
   172 		try {
       
   173 			functionUnderTest = pluginTracePropertyVerifier.getDeclaredMethod(
       
   174 					nameOfFunctionUnderTest, String.class);
       
   175 			// change access of the function under test because otherwise we can
       
   176 			// test to private functions
       
   177 			functionUnderTest.setAccessible(true);
       
   178 			// invoke the function and get result
       
   179 			Object retObj = null;
       
   180 
       
   181 			// remove white spaces from test string, because those those does
       
   182 			// not exist in real life either
       
   183 			retObj = functionUnderTest.invoke(ptpvObject, testString.replace(
       
   184 					WHITE_SPACE, EMPTY_STRING));
       
   185 
       
   186 			// cast the result to the expected return type.
       
   187 			Boolean res = (Boolean) retObj;
       
   188 
       
   189 			String msg = functionUnderTest.getName() + "  invoked on \"" //$NON-NLS-1$
       
   190 					+ testString + "\" returned " + res.toString(); //$NON-NLS-1$
       
   191 			Assert.assertEquals(msg, expectedResult, res.booleanValue());
       
   192 		} catch (Exception e) {
       
   193 			Assert.fail(e.getMessage());
       
   194 		}
       
   195 	}
       
   196 
       
   197 	/**
       
   198 	 * Test isValidData method
       
   199 	 */
       
   200 	@Test
       
   201 	public void testIsValidDataMethod() {
       
   202 		executeTests("isValidData", FunctionToTest.ISVALIDDATA); //$NON-NLS-1$
       
   203 	}
       
   204 
       
   205 	//add future methods tests after this point.
       
   206 	
       
   207 	/**
       
   208 	 * tearDown
       
   209 	 */
       
   210 	@After
       
   211 	public void tearDown() {
       
   212 		// nothing to do
       
   213 	}
       
   214 
       
   215 }