Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterList_XMLDuplicateSublists.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
15 #include <fstream>
16 #include <string>
17 
18 std::string filename;
19 
20 namespace Teuchos {
21 
23  {
25  "filename", &filename, "XML file to parse" );
26  }
27 
28  TEUCHOS_UNIT_TEST( ParameterList, ExistingSublistIsOkay )
29  {
30  std::string xmlstring(
31  "<ParameterList> \n"
32  " <ParameterList name=\"SubList\">\n"
33  " </ParameterList> \n"
34  "</ParameterList> \n");
35  RCP<ParameterList> plist = getParametersFromXmlString(xmlstring);
36  updateParametersFromXmlString( xmlstring, plist() );
37  }
38 
39  TEUCHOS_UNIT_TEST( ParameterList, XMLDuplicatedSublists )
40  {
41  ParameterList pl;
42  TEST_THROW( updateParametersFromXmlFile(filename, inOutArg(pl) ), DuplicateParameterSublist );
43  TEST_THROW( getParametersFromXmlFile(filename), DuplicateParameterSublist );
44  TEST_THROW( getParametersFromXmlFile(filename,null), DuplicateParameterSublist );
45  //
46  std::ifstream fin(filename.c_str());
47  std::string xmlstring( (std::istreambuf_iterator<char>(fin)),
48  std::istreambuf_iterator<char>() );
49  TEST_THROW( updateParametersFromXmlString(xmlstring,inOutArg(pl) ), DuplicateParameterSublist );
50  TEST_THROW( getParametersFromXmlString(xmlstring), DuplicateParameterSublist );
51  TEST_THROW( getParametersFromXmlString(xmlstring,null), DuplicateParameterSublist );
52  }
53 
54  TEUCHOS_UNIT_TEST( XMLParameterListReader, XMLDuplicatedSublistsThrowsError )
55  {
56  FileInputSource xmlFile(filename);
57  XMLObject xmlParams = xmlFile.getObject();
58  XMLParameterListReader xmlPLReader;
59  TEST_EQUALITY_CONST( xmlPLReader.getAllowsDuplicateSublists(), true );
60  out << "Changing policy to disallow duplicate sublists" << std::endl;
61  xmlPLReader.setAllowsDuplicateSublists( false );
62  TEST_EQUALITY_CONST( xmlPLReader.getAllowsDuplicateSublists(), false );
63  TEST_THROW( xmlPLReader.toParameterList(xmlParams), DuplicateParameterSublist );
64  }
65 
66  TEUCHOS_UNIT_TEST( XMLParameterListReader, XMLDuplicatedSublistsBackwardsCompatible )
67  {
68  FileInputSource xmlFile(filename);
69  XMLObject xmlParams = xmlFile.getObject();
70  XMLParameterListReader xmlPLReader;
71  TEST_EQUALITY_CONST( xmlPLReader.getAllowsDuplicateSublists(), true );
72  TEST_NOTHROW( xmlPLReader.toParameterList(xmlParams) );
73  }
74 
75 } // namespace Teuchos
Writes an XML object to a parameter list.
bool getAllowsDuplicateSublists() const
Specifies the current policy regarding duplicated sublists. See setAllowsDuplicateSublists() for more...
#define TEST_NOTHROW(code)
Asserr that the statement &#39;code&#39; does not thrown any excpetions.
static CommandLineProcessor & getCLP()
Return the CLP to add options to.
Optionally thrown when a sublist is set twice by either updateParametersFromXmlFile(), updateParametersFromXmlFileAndUpdate() or updateParametersFromXmlString()
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
Templated Parameter List class.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
XMLObject getObject() const
Get an object by invoking the TreeBuildingXMLHandler on the input data.
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Instantiation of XMLInputSource class for reading XML from a file.
RCP< ParameterList > toParameterList(const XMLObject &xml, RCP< DependencySheet > depSheet) const
A list of parameters of arbitrary type.
Writes an XML object to a parameter list.
Smart reference counting pointer class for automatic garbage collection.
Definition of XMLInputSource derived class for reading XML from a file.
void setAllowsDuplicateSublists(bool policy)
Set policy regarding duplicated sublists.