OpenWalnut  1.4.0
WException_test.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WEXCEPTION_TEST_H
00026 #define WEXCEPTION_TEST_H
00027 
00028 #include <string>
00029 
00030 #include <boost/shared_ptr.hpp>
00031 #include <cxxtest/TestSuite.h>
00032 
00033 #include "../WException.h"
00034 
00035 /**
00036  * Test WException
00037  */
00038 class WExceptionTest : public CxxTest::TestSuite
00039 {
00040 public:
00041     /**
00042      * An instantiation should never throw an exception.
00043      */
00044     void testInstantiation( void )
00045     {
00046         TS_ASSERT_THROWS_NOTHING( WException e() );
00047         TS_ASSERT_THROWS_NOTHING( WException e( std::string( "Some message" ) ) );
00048     }
00049 
00050     /**
00051      * Getting the message means every trace element should be returned.
00052      */
00053     void testGetMessage( void )
00054     {
00055         WException e( std::string( "Dummy exception" ) );
00056         e.m_trace.push_back( "first" );
00057         e.m_trace.push_back( "second" );
00058         std::string expected = "Dummy exception\n\ntrace: first\ntrace: second";
00059         TS_ASSERT_EQUALS( expected, e.getTrace() );
00060         WException f;
00061         TS_ASSERT_EQUALS( std::string(), f.getTrace() );
00062     }
00063 
00064     /**
00065      * Test backtrace. This test always passes on platforms other than Linux!
00066      */
00067     void testBacktrace( void )
00068     {
00069 #if ( defined( __linux__ ) && defined( __GNUC__ ) )
00070         try
00071         {
00072             new WException();
00073         }
00074         catch( const WException& e )
00075         {
00076             std::string bt = e.getBacktrace();
00077             // how to test this? Since the trace is different in release and debug mode, we simply test for
00078             // non empty string here.
00079             TS_ASSERT( bt.length() );
00080         }
00081 #else
00082         // every platform not Linux will pass this test since only Linux is supported
00083         TS_ASSERT( true );
00084 #endif
00085     }
00086 };
00087 
00088 #endif  // WEXCEPTION_TEST_H