OpenWalnut  1.4.0
WDataHandler_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 WDATAHANDLER_TEST_H
00026 #define WDATAHANDLER_TEST_H
00027 
00028 #include <boost/shared_ptr.hpp>
00029 
00030 #include <cxxtest/TestSuite.h>
00031 
00032 #include "../../common/WLogger.h"
00033 #include "../WSubject.h"
00034 #include "../WDataHandler.h"
00035 
00036 /**
00037  * Test important functionality of WDataHandler class
00038  */
00039 class WDataHandlerTest : public CxxTest::TestSuite
00040 {
00041 public:
00042     /**
00043      * Setup logger and other stuff for each test.
00044      */
00045     void setUp()
00046     {
00047         WLogger::startup();
00048     }
00049 
00050     /**
00051      * Instatiation should throw nothing.
00052      */
00053     void testInstantiation()
00054     {
00055         TS_ASSERT_THROWS_NOTHING( WDataHandler() );
00056     }
00057 
00058     /**
00059      * Singleton getter should create an instance
00060      */
00061     void testSingleton()
00062     {
00063         TS_ASSERT_THROWS_NOTHING( WDataHandler::getDataHandler() );
00064         TS_ASSERT( WDataHandler::getDataHandler().get() );
00065     }
00066 
00067     /**
00068      * Test adding and iterating subjects
00069      */
00070     void testAddSubjects()
00071     {
00072         boost::shared_ptr< WDataHandler > dh = WDataHandler::getDataHandler();
00073 
00074         WPersonalInformation testInfo( WPersonalInformation::createDummyInformation() );
00075         testInfo.setSubjectID( 1 );
00076         testInfo.setLastName( "Testname" );
00077 
00078         WSubject* s = new WSubject( testInfo );
00079         TS_ASSERT_THROWS_NOTHING( dh->addSubject( boost::shared_ptr< WSubject >( s ) ) );
00080         TS_ASSERT_EQUALS( 2, dh->m_subjects.size() );   // note: this is 2 since the datahandler always provides a default subject
00081 
00082         // test iteration
00083         WDataHandler::SubjectSharedContainerType::ReadTicket a = dh->getSubjects();
00084 
00085         // iterate the list and find all textures
00086         int count = 0;
00087         for( WDataHandler::SubjectContainerType::const_iterator iter = a->get().begin(); iter != a->get().end(); ++iter )
00088         {
00089             count++;
00090 
00091             // the second one needs to be the added subject
00092             TS_ASSERT( ( count == 1 ) || ( s == ( *iter ).get() ) );
00093         }
00094 
00095         TS_ASSERT( count == 2 );
00096     }
00097 
00098     /**
00099      * Test the remove and clean functionality.
00100      */
00101     void testRemoveSubjects()
00102     {
00103         boost::shared_ptr< WDataHandler > dh = WDataHandler::getDataHandler();
00104 
00105         WPersonalInformation testInfo( WPersonalInformation::createDummyInformation() );
00106         testInfo.setSubjectID( 2 );
00107         testInfo.setLastName( "Testname2" );
00108 
00109         boost::shared_ptr< WSubject > s( new WSubject( testInfo ) );
00110         dh->addSubject( s );
00111 
00112         // now there should be 3 subjects (one from testAddSubjects, the above added one and the default subject)
00113         TS_ASSERT_EQUALS( 3, dh->m_subjects.size() );   // note: this is 2 since the datahandler always provides a default subject
00114         dh->removeSubject( s );
00115         TS_ASSERT_EQUALS( 2, dh->m_subjects.size() );   // note: this is 2 since the datahandler always provides a default subject
00116         dh->clear();
00117         TS_ASSERT_EQUALS( 0, dh->m_subjects.size() );   // note: this is 2 since the datahandler always provides a default subject
00118     }
00119 };
00120 
00121 #endif  // WDATAHANDLER_TEST_H
00122