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