OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WDataHandler_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WDATAHANDLER_TEST_H
26 #define WDATAHANDLER_TEST_H
27 
28 #include <boost/shared_ptr.hpp>
29 
30 #include <cxxtest/TestSuite.h>
31 
32 #include "../../common/WLogger.h"
33 #include "../WSubject.h"
34 #include "../WDataHandler.h"
35 
36 /**
37  * Test important functionality of WDataHandler class
38  */
39 class WDataHandlerTest : public CxxTest::TestSuite
40 {
41 public:
42 
43  /**
44  * Setup logger and other stuff for each test.
45  */
46  void setUp()
47  {
49  }
50 
51  /**
52  * Instatiation should throw nothing.
53  */
55  {
56  TS_ASSERT_THROWS_NOTHING( WDataHandler() );
57  }
58 
59  /**
60  * Singleton getter should create an instance
61  */
63  {
64  TS_ASSERT_THROWS_NOTHING( WDataHandler::getDataHandler() );
65  TS_ASSERT( WDataHandler::getDataHandler().get() );
66  }
67 
68  /**
69  * Test adding and iterating subjects
70  */
72  {
73  boost::shared_ptr< WDataHandler > dh = WDataHandler::getDataHandler();
74 
76  testInfo.setSubjectID( 1 );
77  testInfo.setLastName( "Testname" );
78 
79  WSubject* s = new WSubject( testInfo );
80  TS_ASSERT_THROWS_NOTHING( dh->addSubject( boost::shared_ptr< WSubject >( s ) ) );
81  TS_ASSERT_EQUALS( 2, dh->m_subjects.size() ); // note: this is 2 since the datahandler always provides a default subject
82 
83  // test iteration
85 
86  // iterate the list and find all textures
87  int count = 0;
88  for( WDataHandler::SubjectContainerType::const_iterator iter = a->get().begin(); iter != a->get().end(); ++iter )
89  {
90  count++;
91 
92  // the second one needs to be the added subject
93  TS_ASSERT( ( count == 1 ) || ( s == ( *iter ).get() ) );
94  }
95 
96  TS_ASSERT( count == 2 );
97  }
98 
99  /**
100  * Test the remove and clean functionality.
101  */
103  {
104  boost::shared_ptr< WDataHandler > dh = WDataHandler::getDataHandler();
105 
107  testInfo.setSubjectID( 2 );
108  testInfo.setLastName( "Testname2" );
109 
110  boost::shared_ptr< WSubject > s = boost::shared_ptr< WSubject >( new WSubject( testInfo ) );
111  dh->addSubject( s );
112 
113  // now there should be 3 subjects (one from testAddSubjects, the above added one and the default subject)
114  TS_ASSERT_EQUALS( 3, dh->m_subjects.size() ); // note: this is 2 since the datahandler always provides a default subject
115  dh->removeSubject( s );
116  TS_ASSERT_EQUALS( 2, dh->m_subjects.size() ); // note: this is 2 since the datahandler always provides a default subject
117  dh->clear();
118  TS_ASSERT_EQUALS( 0, dh->m_subjects.size() ); // note: this is 2 since the datahandler always provides a default subject
119  }
120 };
121 
122 #endif // WDATAHANDLER_TEST_H
123