1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <boost/scoped_ptr.hpp>
12 #include <cppunit/extensions/HelperMacros.h>
14 #include <comphelper/processfactory.hxx>
16 #include <ucbhelper/content.hxx>
18 #include <test/bootstrapfixture.hxx>
20 #include <DirectoryStream.hxx>
22 namespace ucb
= com::sun::star::ucb
;
23 namespace uno
= com::sun::star::uno
;
25 using boost::scoped_ptr
;
27 using librevenge::RVNGInputStream
;
29 using writerperfect::DirectoryStream
;
34 class DirectoryStreamTest
: public test::BootstrapFixture
37 DirectoryStreamTest();
40 CPPUNIT_TEST_SUITE(DirectoryStreamTest
);
41 CPPUNIT_TEST(testConstruction
);
42 CPPUNIT_TEST(testDetection
);
43 CPPUNIT_TEST(testDataOperations
);
44 CPPUNIT_TEST(testStructuredOperations
);
45 CPPUNIT_TEST_SUITE_END();
48 void testConstruction();
50 void testDataOperations();
51 void testStructuredOperations();
54 uno::Reference
<ucb::XContent
> m_xDir
;
55 uno::Reference
<ucb::XContent
> m_xFile
;
56 uno::Reference
<ucb::XContent
> m_xNonexistent
;
59 static const char g_aDirPath
[] = "/writerperfect/qa/unit/data/stream/test.dir";
60 static const char g_aNondirPath
[] = "/writerperfect/qa/unit/data/stream/test.dir/mimetype";
61 static const char g_aNonexistentPath
[] = "/writerperfect/qa/unit/data/stream/foo/bar";
63 DirectoryStreamTest::DirectoryStreamTest()
65 const uno::Reference
<ucb::XCommandEnvironment
> xCmdEnv
;
66 const uno::Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext());
68 using ucbhelper::Content
;
70 m_xDir
= Content(getURLFromSrc(g_aDirPath
), xCmdEnv
, xContext
).get();
71 m_xFile
= Content(getURLFromSrc(g_aNondirPath
), xCmdEnv
, xContext
).get();
72 m_xNonexistent
= Content(getURLFromSrc(g_aNonexistentPath
), xCmdEnv
, xContext
).get();
75 void DirectoryStreamTest::testConstruction()
77 const scoped_ptr
<DirectoryStream
> pDir(DirectoryStream::createForParent(m_xFile
));
78 CPPUNIT_ASSERT(bool(pDir
));
79 CPPUNIT_ASSERT(pDir
->isStructured());
81 // this should work for dirs too
82 const scoped_ptr
<DirectoryStream
> pDir2(DirectoryStream::createForParent(m_xDir
));
83 CPPUNIT_ASSERT(bool(pDir2
));
84 CPPUNIT_ASSERT(pDir2
->isStructured());
86 // for nonexistent dirs nothing is created
87 const scoped_ptr
<DirectoryStream
> pNondir(DirectoryStream::createForParent(m_xNonexistent
));
88 CPPUNIT_ASSERT(!pNondir
);
90 // even if we try harder, just an empty shell is created
91 DirectoryStream
aNondir2(m_xNonexistent
);
92 CPPUNIT_ASSERT(!aNondir2
.isStructured());
95 void DirectoryStreamTest::testDetection()
97 CPPUNIT_ASSERT(DirectoryStream::isDirectory(m_xDir
));
98 CPPUNIT_ASSERT(!DirectoryStream::isDirectory(m_xFile
));
99 CPPUNIT_ASSERT(!DirectoryStream::isDirectory(m_xNonexistent
));
102 void lcl_testDataOperations(RVNGInputStream
&rStream
)
104 CPPUNIT_ASSERT(rStream
.isEnd());
105 CPPUNIT_ASSERT_EQUAL(0L, rStream
.tell());
106 CPPUNIT_ASSERT_EQUAL(-1, rStream
.seek(0, librevenge::RVNG_SEEK_CUR
));
108 unsigned long numBytesRead
= 0;
109 CPPUNIT_ASSERT(0 == rStream
.read(1, numBytesRead
));
110 CPPUNIT_ASSERT_EQUAL(0UL, numBytesRead
);
113 void DirectoryStreamTest::testDataOperations()
115 // data operations do not make sense on a directory -> just dummy
117 DirectoryStream
aDir(m_xDir
);
118 lcl_testDataOperations(aDir
);
120 // ... and they are equally empty if we try to pass a file
121 DirectoryStream
aFile(m_xFile
);
122 lcl_testDataOperations(aFile
);
125 void lcl_testStructuredOperations(RVNGInputStream
&rStream
)
127 CPPUNIT_ASSERT(rStream
.isStructured());
128 scoped_ptr
<RVNGInputStream
> pSubstream(rStream
.getSubStreamByName("mimetype"));
129 CPPUNIT_ASSERT(bool(pSubstream
));
131 // TODO: test for other operations when they are implemented =)
134 void DirectoryStreamTest::testStructuredOperations()
136 DirectoryStream
aDir(m_xDir
);
137 lcl_testStructuredOperations(aDir
);
139 scoped_ptr
<DirectoryStream
> pDir(DirectoryStream::createForParent(m_xFile
));
140 CPPUNIT_ASSERT(bool(pDir
));
141 lcl_testStructuredOperations(*pDir
.get());
144 CPPUNIT_TEST_SUITE_REGISTRATION(DirectoryStreamTest
);
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */