core/com.nokia.carbide.cpp.codescanner.tests/src/com/nokia/carbide/cpp/codescanner/tests/CSErrorParserTest.java
author cawthron
Fri, 03 Apr 2009 14:27:34 -0500
changeset 40 0ca05f80360c
parent 2 d760517a8095
permissions -rw-r--r--
put branch name in .branch.txt
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
cawthron
parents:
diff changeset
     3
* All rights reserved.
cawthron
parents:
diff changeset
     4
* This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
* which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
*
cawthron
parents:
diff changeset
     9
* Initial Contributors:
cawthron
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
*
cawthron
parents:
diff changeset
    12
* Contributors:
cawthron
parents:
diff changeset
    13
*
cawthron
parents:
diff changeset
    14
* Description: 
cawthron
parents:
diff changeset
    15
*
cawthron
parents:
diff changeset
    16
*/
cawthron
parents:
diff changeset
    17
cawthron
parents:
diff changeset
    18
package com.nokia.carbide.cpp.codescanner.tests;
cawthron
parents:
diff changeset
    19
cawthron
parents:
diff changeset
    20
import java.io.*;
cawthron
parents:
diff changeset
    21
cawthron
parents:
diff changeset
    22
import com.nokia.carbide.cpp.internal.codescanner.error.parsers.*;
cawthron
parents:
diff changeset
    23
import com.nokia.carbide.cpp.project.core.ProjectCorePlugin;
cawthron
parents:
diff changeset
    24
import org.eclipse.cdt.core.ErrorParserManager;
cawthron
parents:
diff changeset
    25
import org.eclipse.cdt.core.IMarkerGenerator;
cawthron
parents:
diff changeset
    26
import org.eclipse.cdt.core.ProblemMarkerInfo;
cawthron
parents:
diff changeset
    27
import org.eclipse.core.resources.IProject;
cawthron
parents:
diff changeset
    28
import org.eclipse.core.resources.IResource;
cawthron
parents:
diff changeset
    29
import org.eclipse.core.runtime.CoreException;
cawthron
parents:
diff changeset
    30
import org.eclipse.core.runtime.IPath;
cawthron
parents:
diff changeset
    31
import org.eclipse.core.runtime.NullProgressMonitor;
cawthron
parents:
diff changeset
    32
cawthron
parents:
diff changeset
    33
import junit.framework.TestCase;
cawthron
parents:
diff changeset
    34
cawthron
parents:
diff changeset
    35
/**
cawthron
parents:
diff changeset
    36
 * Test cases for class CSErrorParser.
cawthron
parents:
diff changeset
    37
 *
cawthron
parents:
diff changeset
    38
 */
cawthron
parents:
diff changeset
    39
public class CSErrorParserTest extends TestCase {
cawthron
parents:
diff changeset
    40
	
cawthron
parents:
diff changeset
    41
	private class testMarkerGenerator implements IMarkerGenerator {
cawthron
parents:
diff changeset
    42
		public void addMarker(ProblemMarkerInfo problemMarkerInfo) {
cawthron
parents:
diff changeset
    43
		}
cawthron
parents:
diff changeset
    44
		public void addMarker(IResource file, int lineNumber, String errorDesc, int severity, String errorVar) {
cawthron
parents:
diff changeset
    45
		}
cawthron
parents:
diff changeset
    46
	}
cawthron
parents:
diff changeset
    47
cawthron
parents:
diff changeset
    48
	@Override
cawthron
parents:
diff changeset
    49
	protected void setUp() throws Exception {
cawthron
parents:
diff changeset
    50
		super.setUp();
cawthron
parents:
diff changeset
    51
	}
cawthron
parents:
diff changeset
    52
cawthron
parents:
diff changeset
    53
	@Override
cawthron
parents:
diff changeset
    54
	protected void tearDown() throws Exception {
cawthron
parents:
diff changeset
    55
		super.tearDown();
cawthron
parents:
diff changeset
    56
	}
cawthron
parents:
diff changeset
    57
cawthron
parents:
diff changeset
    58
	public void testProcessLine() {
cawthron
parents:
diff changeset
    59
		
cawthron
parents:
diff changeset
    60
		try {
cawthron
parents:
diff changeset
    61
			IProject project = ProjectCorePlugin.createProject("codescanner_test1", null);
cawthron
parents:
diff changeset
    62
			assertNotNull(project);
cawthron
parents:
diff changeset
    63
			IPath workingDir = project.getLocation();
cawthron
parents:
diff changeset
    64
			IPath filePath = workingDir.append("MyTest.cpp");
cawthron
parents:
diff changeset
    65
			testMarkerGenerator markGen = new testMarkerGenerator();
cawthron
parents:
diff changeset
    66
			NullProgressMonitor monitor = new NullProgressMonitor();
cawthron
parents:
diff changeset
    67
			String[] errorParserIds = new String[0];				
cawthron
parents:
diff changeset
    68
			ErrorParserManager errorParserManager = new ErrorParserManager(project, workingDir, markGen, errorParserIds);
cawthron
parents:
diff changeset
    69
			CSErrorParser errorParser = new CSErrorParser();
cawthron
parents:
diff changeset
    70
cawthron
parents:
diff changeset
    71
			createTestFile(filePath);
cawthron
parents:
diff changeset
    72
			
cawthron
parents:
diff changeset
    73
			// test with different messages
cawthron
parents:
diff changeset
    74
			String msg = filePath.toString() + "(1) : warning: this is a warning message";
cawthron
parents:
diff changeset
    75
			assertTrue(errorParser.processLine(msg, errorParserManager));
cawthron
parents:
diff changeset
    76
			msg = filePath.toString() + "(2) : error: this is an error message";
cawthron
parents:
diff changeset
    77
			assertTrue(errorParser.processLine(msg, errorParserManager));
cawthron
parents:
diff changeset
    78
			assertFalse(errorParser.processLine("the system cannot find the path specified", errorParserManager));
cawthron
parents:
diff changeset
    79
			assertFalse(errorParser.processLine("a random message", errorParserManager));
cawthron
parents:
diff changeset
    80
			
cawthron
parents:
diff changeset
    81
			project.delete(true, true, monitor);
cawthron
parents:
diff changeset
    82
		} catch (CoreException e) {
cawthron
parents:
diff changeset
    83
			fail();
cawthron
parents:
diff changeset
    84
		}
cawthron
parents:
diff changeset
    85
	}
cawthron
parents:
diff changeset
    86
cawthron
parents:
diff changeset
    87
	private void createTestFile(IPath filePath) {
cawthron
parents:
diff changeset
    88
		try {
cawthron
parents:
diff changeset
    89
			FileOutputStream fout = new FileOutputStream (filePath.toOSString());
cawthron
parents:
diff changeset
    90
		    new PrintStream(fout).println ("This is only a test.");
cawthron
parents:
diff changeset
    91
		    new PrintStream(fout).println ("There is no need to panic.");
cawthron
parents:
diff changeset
    92
		    fout.close();		
cawthron
parents:
diff changeset
    93
		}
cawthron
parents:
diff changeset
    94
		catch (IOException e) {
cawthron
parents:
diff changeset
    95
			fail();
cawthron
parents:
diff changeset
    96
		}
cawthron
parents:
diff changeset
    97
	}
cawthron
parents:
diff changeset
    98
}