| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * AtomObserverUnitTest.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Dec 19, 2011
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "AtomObserverUnitTest.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 38 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 39 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 40 | #include <iostream>
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include "World.hpp"
 | 
|---|
| 45 | #include "Atom/atom.hpp"
 | 
|---|
| 46 | #include "Atom/AtomObserver.hpp"
 | 
|---|
| 47 | #include "Element/element.hpp"
 | 
|---|
| 48 | #include "Element/periodentafel.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #include "stubs/ObserverStub.hpp"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 53 | #include "UnitTestMain.hpp"
 | 
|---|
| 54 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 55 | 
 | 
|---|
| 56 | /********************************************** Test classes **************************************/
 | 
|---|
| 57 | // Registers the fixture into the 'registry'
 | 
|---|
| 58 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomObserverTest );
 | 
|---|
| 59 | 
 | 
|---|
| 60 | // set up and tear down
 | 
|---|
| 61 | void AtomObserverTest::setUp(){
 | 
|---|
| 62 |   World::getInstance();
 | 
|---|
| 63 |   AtomObserver::getInstance();
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   setVerbosity(3);
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   // observer checks that we don't receive general updates ...
 | 
|---|
| 68 |   observer = new UpdateCountObserver;
 | 
|---|
| 69 |   // but only specific notifications in observer1 and observer2
 | 
|---|
| 70 |   observer1 = new NotificationObserver(
 | 
|---|
| 71 |       AtomObserver::getInstance().getChannel(AtomObservable::PositionChanged)
 | 
|---|
| 72 |       );
 | 
|---|
| 73 |   observer2 = new NotificationObserver(
 | 
|---|
| 74 |       AtomObserver::getInstance().getChannel(AtomObservable::ElementChanged)
 | 
|---|
| 75 |       );
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   AtomObserver::getInstance().signOn(observer);
 | 
|---|
| 78 |   AtomObserver::getInstance().signOn(observer1, AtomObservable::PositionChanged);
 | 
|---|
| 79 |   AtomObserver::getInstance().signOn(observer2, AtomObservable::ElementChanged);
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   for(int i=0;i<ATOM_COUNT;++i){
 | 
|---|
| 82 |     atoms[i]= World::getInstance().createAtom();
 | 
|---|
| 83 |     atomIds[i]= atoms[i]->getId();
 | 
|---|
| 84 |   }
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | void AtomObserverTest::tearDown()
 | 
|---|
| 88 | {
 | 
|---|
| 89 |   AtomObserver::getInstance().signOff(observer);
 | 
|---|
| 90 |   AtomObserver::getInstance().signOff(observer1, AtomObservable::PositionChanged);
 | 
|---|
| 91 |   AtomObserver::getInstance().signOff(observer2, AtomObservable::ElementChanged);
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   delete observer;
 | 
|---|
| 94 |   delete observer1;
 | 
|---|
| 95 |   delete observer2;
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   AtomObserver::purgeInstance();
 | 
|---|
| 98 |   World::purgeInstance();
 | 
|---|
| 99 | }
 | 
|---|
| 100 | 
 | 
|---|
| 101 | /** Unit test on whether adding/removing atoms informs relay and onward.
 | 
|---|
| 102 |  *
 | 
|---|
| 103 |  */
 | 
|---|
| 104 | void AtomObserverTest::AtomAddTest()
 | 
|---|
| 105 | {
 | 
|---|
| 106 |   // check for zero update
 | 
|---|
| 107 |   CPPUNIT_ASSERT_EQUAL( (int)0, observer->updates );
 | 
|---|
| 108 |   CPPUNIT_ASSERT_EQUAL( false, observer1->wasNotified );
 | 
|---|
| 109 |   CPPUNIT_ASSERT_EQUAL( false, observer2->wasNotified );
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | /** Unit test on whether moving atoms informs relay and onward.
 | 
|---|
| 113 |  *
 | 
|---|
| 114 |  */
 | 
|---|
| 115 | void AtomObserverTest::AtomMoveTest()
 | 
|---|
| 116 | {
 | 
|---|
| 117 |   // move an atom
 | 
|---|
| 118 |   atoms[0]->setPosition(Vector(1.,0.,0.));
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   // check for update
 | 
|---|
| 121 |   CPPUNIT_ASSERT_EQUAL( true, observer1->wasNotified );
 | 
|---|
| 122 |   CPPUNIT_ASSERT_EQUAL( false, observer2->wasNotified );
 | 
|---|
| 123 |   CPPUNIT_ASSERT_EQUAL( (int)0, observer->updates );
 | 
|---|
| 124 | }
 | 
|---|
| 125 | 
 | 
|---|
| 126 | /** Unit test on whether changing atom's element informs relay and onward.
 | 
|---|
| 127 |  *
 | 
|---|
| 128 |  */
 | 
|---|
| 129 | void AtomObserverTest::AtomElementTest()
 | 
|---|
| 130 | {
 | 
|---|
| 131 |   atoms[0]->setType(1);
 | 
|---|
| 132 | 
 | 
|---|
| 133 |   // check for update
 | 
|---|
| 134 |   CPPUNIT_ASSERT_EQUAL( false, observer1->wasNotified );
 | 
|---|
| 135 |   CPPUNIT_ASSERT_EQUAL( true, observer2->wasNotified );
 | 
|---|
| 136 |   CPPUNIT_ASSERT_EQUAL( (int)0, observer->updates );
 | 
|---|
| 137 | }
 | 
|---|