xref: /AOO41X/main/sfx2/qa/cppunit/test_metadatable.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <sal/cppunit.h>
29*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/util/Duration.hpp>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <sfx2/Metadatable.hxx>
36*cdf0e10cSrcweir #include <sfx2/XmlIdRegistry.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir using namespace ::com::sun::star;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir namespace {
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir class MetadatableTest
45*cdf0e10cSrcweir     : public ::CppUnit::TestFixture
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir public:
48*cdf0e10cSrcweir     virtual void setUp();
49*cdf0e10cSrcweir     virtual void tearDown();
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir     void test();
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(MetadatableTest);
54*cdf0e10cSrcweir     CPPUNIT_TEST(test);
55*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir private:
58*cdf0e10cSrcweir };
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir void MetadatableTest::setUp()
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir }
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir void MetadatableTest::tearDown()
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir class MockMetadatable
70*cdf0e10cSrcweir     : public ::sfx2::Metadatable
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir private:
73*cdf0e10cSrcweir     ::sfx2::IXmlIdRegistry & m_rRegistry;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir public:
76*cdf0e10cSrcweir     MockMetadatable(::sfx2::IXmlIdRegistry & i_rReg,
77*cdf0e10cSrcweir             bool const i_isInClip = false)
78*cdf0e10cSrcweir         : m_rRegistry(i_rReg)
79*cdf0e10cSrcweir         , m_bInClipboard(i_isInClip), m_bInUndo(false), m_bInContent(true) {}
80*cdf0e10cSrcweir     bool m_bInClipboard;
81*cdf0e10cSrcweir     bool m_bInUndo;
82*cdf0e10cSrcweir     bool m_bInContent;
83*cdf0e10cSrcweir     virtual bool IsInClipboard() const { return m_bInClipboard; }
84*cdf0e10cSrcweir     virtual bool IsInUndo() const { return m_bInUndo; }
85*cdf0e10cSrcweir     virtual bool IsInContent() const { return m_bInContent; }
86*cdf0e10cSrcweir     virtual ::sfx2::IXmlIdRegistry& GetRegistry() { return m_rRegistry; }
87*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference<
88*cdf0e10cSrcweir         ::com::sun::star::rdf::XMetadatable > MakeUnoObject() { return 0; }
89*cdf0e10cSrcweir };
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir static bool operator==(beans::StringPair p1, beans::StringPair p2)
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir     return p1.First == p2.First && p1.Second == p2.Second;
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir void MetadatableTest::test()
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     OSL_TRACE("SwMetadatable test(): start\n");
99*cdf0e10cSrcweir     ::std::auto_ptr< ::sfx2::IXmlIdRegistry > const pReg(
100*cdf0e10cSrcweir         ::sfx2::createXmlIdRegistry(false) );
101*cdf0e10cSrcweir     ::std::auto_ptr< ::sfx2::IXmlIdRegistry > const pRegClip(
102*cdf0e10cSrcweir         ::sfx2::createXmlIdRegistry(true) );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     MockMetadatable m1(*pReg);
105*cdf0e10cSrcweir     MockMetadatable m2(*pReg);
106*cdf0e10cSrcweir     MockMetadatable m3(*pReg);
107*cdf0e10cSrcweir     MockMetadatable m4(*pReg);
108*cdf0e10cSrcweir     MockMetadatable m5(*pReg);
109*cdf0e10cSrcweir     ::rtl::OUString empty;
110*cdf0e10cSrcweir     ::rtl::OUString content( ::rtl::OUString::createFromAscii("content.xml") );
111*cdf0e10cSrcweir     ::rtl::OUString styles ( ::rtl::OUString::createFromAscii("styles.xml") );
112*cdf0e10cSrcweir     ::rtl::OUString sid1( ::rtl::OUString::createFromAscii("id1") );
113*cdf0e10cSrcweir     ::rtl::OUString sid2( ::rtl::OUString::createFromAscii("id2") );
114*cdf0e10cSrcweir     ::rtl::OUString sid3( ::rtl::OUString::createFromAscii("id3") );
115*cdf0e10cSrcweir     ::rtl::OUString sid4( ::rtl::OUString::createFromAscii("id4") );
116*cdf0e10cSrcweir     beans::StringPair id1(content, sid1);
117*cdf0e10cSrcweir     beans::StringPair id2(content, sid2);
118*cdf0e10cSrcweir     beans::StringPair id3(content, sid3);
119*cdf0e10cSrcweir     beans::StringPair id4(styles,  sid4);
120*cdf0e10cSrcweir     beans::StringPair id3e(empty,  sid3);
121*cdf0e10cSrcweir     beans::StringPair id4e(empty,  sid4);
122*cdf0e10cSrcweir     m1.SetMetadataReference(id1);
123*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("set failed", m1.GetMetadataReference() == id1);
124*cdf0e10cSrcweir     try {
125*cdf0e10cSrcweir         m2.SetMetadataReference(id1);
126*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false);
127*cdf0e10cSrcweir     } catch (lang::IllegalArgumentException) { }
128*cdf0e10cSrcweir     m1.SetMetadataReference(id1);
129*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
130*cdf0e10cSrcweir             m1.GetMetadataReference() == id1);
131*cdf0e10cSrcweir     m1.EnsureMetadataReference();
132*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
133*cdf0e10cSrcweir             m1.GetMetadataReference() == id1);
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     m2.EnsureMetadataReference();
136*cdf0e10cSrcweir     beans::StringPair m2id(m2.GetMetadataReference());
137*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed", m2id.Second.getLength());
138*cdf0e10cSrcweir     m2.EnsureMetadataReference();
139*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
140*cdf0e10cSrcweir             m2.GetMetadataReference() == m2id);
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     m1.m_bInUndo = true;
143*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("move to undo failed",
144*cdf0e10cSrcweir             !m1.GetMetadataReference().Second.getLength());
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     m1.m_bInUndo = false;
147*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("move from undo failed",
148*cdf0e10cSrcweir             m1.GetMetadataReference() == id1);
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     m1.m_bInUndo = true;
151*cdf0e10cSrcweir     try {
152*cdf0e10cSrcweir         m2.SetMetadataReference(id1); // steal!
153*cdf0e10cSrcweir     } catch (lang::IllegalArgumentException &) {
154*cdf0e10cSrcweir         CPPUNIT_FAIL("set duplicate to undo failed");
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir     m1.m_bInUndo = false;
157*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate",
158*cdf0e10cSrcweir             !m1.GetMetadataReference().Second.getLength());
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     m3.RegisterAsCopyOf(m2);
161*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
162*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
163*cdf0e10cSrcweir             !m3.GetMetadataReference().Second.getLength());
164*cdf0e10cSrcweir     m4.RegisterAsCopyOf(m3);
165*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy: source", m2.GetMetadataReference() == id1);
166*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
167*cdf0e10cSrcweir             !m3.GetMetadataReference().Second.getLength());
168*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy: duplicate",
169*cdf0e10cSrcweir             !m4.GetMetadataReference().Second.getLength());
170*cdf0e10cSrcweir     m2.m_bInUndo = true;
171*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
172*cdf0e10cSrcweir             m3.GetMetadataReference() == id1);
173*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("duplicate to undo",
174*cdf0e10cSrcweir             !m2.GetMetadataReference().Second.getLength());
175*cdf0e10cSrcweir     m2.m_bInUndo = false;
176*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
177*cdf0e10cSrcweir             m2.GetMetadataReference() == id1);
178*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("duplicate from undo",
179*cdf0e10cSrcweir             !m3.GetMetadataReference().Second.getLength());
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     m4.EnsureMetadataReference(); // new!
182*cdf0e10cSrcweir     beans::StringPair m4id(m4.GetMetadataReference());
183*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure on duplicate",
184*cdf0e10cSrcweir             m4id.Second.getLength() && !(m4id == id1));
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     MockMetadatable mc1(*pRegClip, true); // in clipboard
187*cdf0e10cSrcweir     MockMetadatable mc2(*pRegClip, true);
188*cdf0e10cSrcweir     MockMetadatable mc3(*pRegClip, true);
189*cdf0e10cSrcweir     MockMetadatable mc4(*pRegClip, true);
190*cdf0e10cSrcweir     MockMetadatable m2p(*pReg);
191*cdf0e10cSrcweir     MockMetadatable m3p(*pReg);
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     mc1.SetMetadataReference(id2);
194*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("set failed", mc1.GetMetadataReference() == id2);
195*cdf0e10cSrcweir     try {
196*cdf0e10cSrcweir         mc2.SetMetadataReference(id2);
197*cdf0e10cSrcweir         CPPUNIT_FAIL("set duplicate succeeded");
198*cdf0e10cSrcweir     } catch (lang::IllegalArgumentException) { }
199*cdf0e10cSrcweir     mc1.SetMetadataReference(id2);
200*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("set failed (existing)",
201*cdf0e10cSrcweir             mc1.GetMetadataReference() == id2);
202*cdf0e10cSrcweir     mc1.EnsureMetadataReference();
203*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)",
204*cdf0e10cSrcweir             mc1.GetMetadataReference() == id2);
205*cdf0e10cSrcweir     mc2.EnsureMetadataReference();
206*cdf0e10cSrcweir     beans::StringPair mc2id(mc2.GetMetadataReference());
207*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed", mc2id.Second.getLength());
208*cdf0e10cSrcweir     mc2.EnsureMetadataReference();
209*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)",
210*cdf0e10cSrcweir             mc2.GetMetadataReference() == mc2id);
211*cdf0e10cSrcweir     mc2.RemoveMetadataReference();
212*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove failed",
213*cdf0e10cSrcweir             !mc2.GetMetadataReference().Second.getLength());
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     // set up mc2 as copy of m2 and mc3 as copy of m3
216*cdf0e10cSrcweir     mc3.RegisterAsCopyOf(m3);
217*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)",
218*cdf0e10cSrcweir             !mc3.GetMetadataReference().Second.getLength() );
219*cdf0e10cSrcweir     mc2.RegisterAsCopyOf(m2);
220*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)",
221*cdf0e10cSrcweir             mc2.GetMetadataReference() == id1);
222*cdf0e10cSrcweir     // paste mc2 to m2p and mc3 to m3p
223*cdf0e10cSrcweir     m2p.RegisterAsCopyOf(mc2);
224*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)",
225*cdf0e10cSrcweir             !m2p.GetMetadataReference().Second.getLength() );
226*cdf0e10cSrcweir     m3p.RegisterAsCopyOf(mc3);
227*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)",
228*cdf0e10cSrcweir             !m3p.GetMetadataReference().Second.getLength() );
229*cdf0e10cSrcweir     // delete m2, m2p, m3
230*cdf0e10cSrcweir     m2.RemoveMetadataReference();
231*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove failed",
232*cdf0e10cSrcweir             !m2.GetMetadataReference().Second.getLength());
233*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)",
234*cdf0e10cSrcweir             m2p.GetMetadataReference() == id1);
235*cdf0e10cSrcweir     m2p.RemoveMetadataReference();
236*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove failed",
237*cdf0e10cSrcweir             !m2p.GetMetadataReference().Second.getLength());
238*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)",
239*cdf0e10cSrcweir             m3.GetMetadataReference() == id1);
240*cdf0e10cSrcweir     m3.RemoveMetadataReference();
241*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove failed",
242*cdf0e10cSrcweir             !m3.GetMetadataReference().Second.getLength());
243*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)",
244*cdf0e10cSrcweir             m3p.GetMetadataReference() == id1);
245*cdf0e10cSrcweir     // delete mc2
246*cdf0e10cSrcweir     mc2.SetMetadataReference(beans::StringPair());
247*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent",
248*cdf0e10cSrcweir             !mc3.GetMetadataReference().Second.getLength() );
249*cdf0e10cSrcweir     // paste mc2
250*cdf0e10cSrcweir     m2p.RegisterAsCopyOf(mc2);
251*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove-paste",
252*cdf0e10cSrcweir             !m2p.GetMetadataReference().Second.getLength());
253*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)",
254*cdf0e10cSrcweir             m3p.GetMetadataReference() == id1);
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir     // auto-detect stream
257*cdf0e10cSrcweir     m5.SetMetadataReference(id3e);
258*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("auto-detect (content)",
259*cdf0e10cSrcweir             m5.GetMetadataReference() == id3);
260*cdf0e10cSrcweir     m5.m_bInContent = false;
261*cdf0e10cSrcweir     m5.SetMetadataReference(id4e);
262*cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)",
263*cdf0e10cSrcweir             m5.GetMetadataReference() == id4);
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir     OSL_TRACE("sfx2::Metadatable test(): finished\n");
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest);
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir CPPUNIT_PLUGIN_IMPLEMENT();
274*cdf0e10cSrcweir 
275