/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 * Copyright (C)  2013 Frederik Heber. All rights reserved.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    MoleCuilder is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MoleCuilder.  If not, see .
 */
/*
 * ParserXmlUnitTest.cpp
 *
 *  Created on: Mar 24, 2012
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "ParserXmlUnitTest.hpp"
#include 
#include 
#include 
#include "Atom/atom.hpp"
#include "Atom/AtomObserver.hpp"
#include "CodePatterns/Log.hpp"
#include "Descriptors/AtomTypeDescriptor.hpp"
#include "Element/element.hpp"
#include "Element/periodentafel.hpp"
#include "Parser/ChangeTracker.hpp"
#include "Parser/XmlParser.hpp"
#include "World.hpp"
#ifdef HAVE_TESTRUNNER
#include "UnitTestMain.hpp"
#endif /*HAVE_TESTRUNNER*/
using namespace std;
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( ParserXmlUnitTest );
static string waterXml = "\
\n\
\n\
\t\n\
\t\t\n\
\t\t\t\n\
\t\t\t\n\
\t\t\t\n\
\t\t\n\
\t\n";
static string waterMultiXml = "\n";
void ParserXmlUnitTest::setUp() {
  World::getInstance();
  parser = new FormatParser();
  setVerbosity(2);
  // we need hydrogens and oxygens in the following tests
  CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(1) != NULL);
  CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(8) != NULL);
}
void ParserXmlUnitTest::tearDown()
{
  delete parser;
  ChangeTracker::purgeInstance();
  World::purgeInstance();
  AtomObserver::purgeInstance();
}
/************************************ tests ***********************************/
void ParserXmlUnitTest::readwriteXmlTest() {
  cout << "Testing the XML parser." << endl;
  stringstream input;
  input << waterXml;
  parser->load(&input);
  input.clear();
  CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
  // store and parse in again
  {
//    std::string first;
//    std::string second;
    std::stringstream output;
    std::vector atoms = World::getInstance().getAllAtoms();
    parser->save(&output, atoms);
    std::cout << output.str();
    {
      delete parser;
      parser = new FormatParser();
      std::stringstream input(waterXml);
      parser->load(&input);
    }
    FormatParser *parser_control = new FormatParser();
    parser_control->load(&output);
    CPPUNIT_ASSERT( parser->data == parser_control->data );
    delete parser_control;
  }
}