/***************************************************************************** * Copyright (c) 2008 g-Eclipse Consortium * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Initial development of the original code was made for the * g-Eclipse project founded by European Union * project number: FP6-IST-034327 http://www.geclipse.eu/ * * Contributors: * Mathias Stuempert - initial API and implementation *****************************************************************************/ package eu.geclipse.core.reporting; import static org.junit.Assert.*; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.junit.BeforeClass; import org.junit.Test; /** * Test class for the ProblemException. */ public class ProblemException_PDETest { private static String staticProblemID; private static Throwable staticInnerException; private static String staticPluginID; private static String staticMailTo; private static String[] staticReasons; private String description; private Throwable innerException; /** * JUnit autogenerated class. * * @throws Exception If an exception occurs. */ @BeforeClass public static void setUpBeforeClass() throws Exception { staticProblemID = Messages.getString("ProblemException_PDETest.ExampleProblemID"); //$NON-NLS-1$ staticInnerException = new Exception( Messages.getString("ProblemException_PDETest.InnerExceptionText") ); //$NON-NLS-1$ staticPluginID = Messages.getString("ProblemException_PDETest.PluginID"); //$NON-NLS-1$ staticMailTo = Messages.getString("ProblemException_PDETest.MailTo"); //$NON-NLS-1$ staticReasons = new String[] { Messages.getString("ProblemException_PDETest.ExampleReason1"), //$NON-NLS-1$ Messages.getString("ProblemException_PDETest.ExampleReason2") //$NON-NLS-1$ }; } /** * Test for the constructor. */ @Test public void testProblemExceptionStringString() { this.description = Messages.getString("ProblemException_PDETest.ExampleProblemDescription"); //$NON-NLS-1$ this.innerException = null; ProblemException exc = new ProblemException( staticProblemID, staticPluginID ); setupReasons( exc ); testExampleException( exc ); } /** * Test for the constructor. */ @Test public void testProblemExceptionStringThrowableString() { this.description = Messages.getString("ProblemException_PDETest.ExampleProblemDescription"); //$NON-NLS-1$ this.innerException = staticInnerException; ProblemException exc = new ProblemException( staticProblemID, this.innerException, staticPluginID ); setupReasons( exc ); testExampleException( exc ); } /** * Test for the constructor. */ @Test public void testProblemExceptionStringStringString() { this.description = Messages.getString("ProblemException_PDETest.CustomProblemDescription"); //$NON-NLS-1$ this.innerException = null; ProblemException exc = new ProblemException( staticProblemID, this.description, staticPluginID ); setupReasons( exc ); testExampleException( exc ); } /** * Test for the constructor. */ @Test public void testProblemExceptionStringStringThrowableString() { this.description = Messages.getString("ProblemException_PDETest.CustomProblemDescription"); //$NON-NLS-1$ this.innerException = staticInnerException; ProblemException exc = new ProblemException( staticProblemID, this.description, this.innerException, staticPluginID ); setupReasons( exc ); testExampleException( exc ); } /** * Test for the constructor. */ @Test public void testProblemExceptionIProblem() { this.description = Messages.getString("ProblemException_PDETest.ExampleProblemDescription"); //$NON-NLS-1$ this.innerException = null; IReportingService reportingService = ReportingPlugin.getReportingService(); IProblem problem = reportingService.getProblem( staticProblemID, null, null, staticPluginID ); ProblemException exc = new ProblemException( problem ); setupReasons( exc ); testExampleException( exc ); } /** * Test for the constructor. */ @Test public void testProblemExceptionIStatus() { this.description = Messages.getString("ProblemException_PDETest.CustomProblemDescription"); //$NON-NLS-1$ this.innerException = staticInnerException; IStatus status = new Status( IStatus.ERROR, staticPluginID, 0, this.description, this.innerException ); ProblemException exc = new ProblemException( status ); testExampleStatus( exc.getStatus() ); } /** * Test for getProblem. */ @Test public void testGetProblem() { this.description = Messages.getString("ProblemException_PDETest.CustomProblemDescription"); //$NON-NLS-1$ this.innerException = staticInnerException; ProblemException exc = new ProblemException( staticProblemID, this.description, this.innerException, staticPluginID ); setupReasons( exc ); testExampleProblem( exc.getProblem() ); } ////////////////////////////////////// // Down here are the helper methods // ////////////////////////////////////// private void setupReasons( final ProblemException exc ) { IProblem problem = exc.getProblem(); for ( String reason : staticReasons ) { problem.addReason( reason ); } } private void testExampleException( final ProblemException exc ) { testExampleProblem( exc.getProblem() ); testExampleStatus( exc.getStatus() ); } private void testExampleProblem( final IProblem problem ) { assertEquals( this.description, problem.getDescription() ); assertEquals( this.innerException, problem.getException() ); assertEquals( staticMailTo, problem.getMailTo() ); assertEquals( staticPluginID, problem.getPluginID() ); assertArrayEquals( staticReasons, problem.getReasons() ); testExampleSolutions( problem.getSolutions() ); testExampleStatus( problem.getStatus() ); } private void testExampleSolutions( final ISolution[] solutions ) { assertEquals( 2, solutions.length, 0 ); ISolution solution1 = null; ISolution solution2 = null; if ( solutions[0].getID().equals( "eu.geclipse.core.reporting.test.ExampleSolution1" ) ) { solution1 = solutions[0]; solution2 = solutions[1]; } else { solution1 = solutions[1]; solution2 = solutions[0]; } testExampleSolution1( solution1 ); testExampleSolution2( solution2 ); } private void testExampleSolution1( final ISolution solution ) { assertEquals( "This is the Example Solution 1", solution.getDescription() ); assertEquals( "eu.geclipse.core.reporting.test.ExampleSolution1", solution.getID() ); assertTrue( ! solution.isActive() ); } private void testExampleSolution2( final ISolution solution ) { assertEquals( "This is the Example Solution 2", solution.getDescription() ); assertEquals( "eu.geclipse.core.reporting.test.ExampleSolution2", solution.getID() ); assertTrue( ! solution.isActive() ); } private void testExampleStatus( final IStatus status ) { assertEquals( this.description, status.getMessage() ); assertEquals( this.innerException, status.getException() ); assertEquals( staticPluginID, status.getPlugin() ); } }