LibreOffice Module test (master)  1
xnamecontainer.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
11 
12 #include <com/sun/star/container/ElementExistException.hpp>
13 #include <com/sun/star/container/NoSuchElementException.hpp>
14 #include <com/sun/star/container/XNameContainer.hpp>
15 #include <com/sun/star/lang/IllegalArgumentException.hpp>
16 
17 #include <com/sun/star/uno/Any.hxx>
18 #include <com/sun/star/uno/Reference.hxx>
19 
20 #include <cppunit/TestAssert.h>
21 
22 using namespace css;
23 
24 namespace apitest
25 {
26 void XNameContainer::testInsertByName()
27 {
28  uno::Reference<container::XNameContainer> xNameContainer(init(), uno::UNO_QUERY_THROW);
29 
30  CPPUNIT_ASSERT(!xNameContainer->hasByName(m_aName + "Insert"));
31  xNameContainer->insertByName(m_aName + "Insert", m_aElement);
32  CPPUNIT_ASSERT(xNameContainer->hasByName(m_aName + "Insert"));
33 }
34 
35 void XNameContainer::testInsertByNameEmptyName()
36 {
37  uno::Reference<container::XNameContainer> xNameContainer(init(), uno::UNO_QUERY_THROW);
38 
39  uno::Any aAny;
40  CPPUNIT_ASSERT_THROW(xNameContainer->insertByName("", aAny), lang::IllegalArgumentException);
41 }
42 
43 void XNameContainer::testInsertByNameInvalidElement()
44 {
45  uno::Reference<container::XNameContainer> xNameContainer(init(), uno::UNO_QUERY_THROW);
46 
47  // TODO: Find a way to create an invalid element.
48  // CPPUNIT_ASSERT_THROW(xNameContainer->insertByName("Dummy", nullptr),
49  // lang::IllegalArgumentException);
50 }
51 
52 void XNameContainer::testInsertByNameDuplicate()
53 {
54  uno::Reference<container::XNameContainer> xNameContainer(init(), uno::UNO_QUERY_THROW);
55 
56  CPPUNIT_ASSERT(!xNameContainer->hasByName(m_aName + "Duplicate"));
57  xNameContainer->insertByName(m_aName + "Duplicate", m_aElement);
58  CPPUNIT_ASSERT(xNameContainer->hasByName(m_aName + "Duplicate"));
59 
60  bool bExceptionThrown = false;
61  try
62  {
63  xNameContainer->insertByName(m_aName + "Duplicate", m_aElement);
64  }
65  catch (const container::ElementExistException&)
66  {
67  bExceptionThrown = true;
68  }
69  catch (const lang::IllegalArgumentException&)
70  {
71  bExceptionThrown = true;
72  }
73  CPPUNIT_ASSERT(bExceptionThrown);
74 }
75 
76 void XNameContainer::testRemoveByName()
77 {
78  uno::Reference<container::XNameContainer> xNameContainer(init(), uno::UNO_QUERY_THROW);
79  CPPUNIT_ASSERT(xNameContainer->hasByName(m_aName));
80  xNameContainer->removeByName(m_aName);
81  CPPUNIT_ASSERT(!xNameContainer->hasByName(m_aName));
82 }
83 
84 void XNameContainer::testRemoveByNameEmptyName()
85 {
86  uno::Reference<container::XNameContainer> xNameContainer(init(), uno::UNO_QUERY_THROW);
87 
88  CPPUNIT_ASSERT_THROW(xNameContainer->removeByName(""), lang::IllegalArgumentException);
89 }
90 
91 void XNameContainer::testRemoveByNameNoneExistingElement()
92 {
93  uno::Reference<container::XNameContainer> xNameContainer(init(), uno::UNO_QUERY_THROW);
94 
95  CPPUNIT_ASSERT_THROW(xNameContainer->removeByName("UnitTest"),
96  container::NoSuchElementException);
97 }
98 
99 } // namespace apitest
100 
101 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
OUString m_aName
FILE * init(int, char **)