Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 :
10 : #include <boost/scoped_ptr.hpp>
11 :
12 : #include <cppunit/extensions/HelperMacros.h>
13 :
14 : #include <comphelper/processfactory.hxx>
15 :
16 : #include <ucbhelper/content.hxx>
17 :
18 : #include <test/bootstrapfixture.hxx>
19 :
20 : #include <DirectoryStream.hxx>
21 :
22 : namespace ucb = com::sun::star::ucb;
23 : namespace uno = com::sun::star::uno;
24 :
25 : using boost::scoped_ptr;
26 :
27 : using librevenge::RVNGInputStream;
28 :
29 : using writerperfect::DirectoryStream;
30 :
31 : namespace
32 : {
33 :
34 8 : class DirectoryStreamTest : public test::BootstrapFixture
35 : {
36 : public:
37 : DirectoryStreamTest();
38 :
39 : public:
40 2 : CPPUNIT_TEST_SUITE(DirectoryStreamTest);
41 1 : CPPUNIT_TEST(testConstruction);
42 1 : CPPUNIT_TEST(testDetection);
43 1 : CPPUNIT_TEST(testDataOperations);
44 1 : CPPUNIT_TEST(testStructuredOperations);
45 5 : CPPUNIT_TEST_SUITE_END();
46 :
47 : private:
48 : void testConstruction();
49 : void testDetection();
50 : void testDataOperations();
51 : void testStructuredOperations();
52 :
53 : private:
54 : uno::Reference<ucb::XContent> m_xDir;
55 : uno::Reference<ucb::XContent> m_xFile;
56 : uno::Reference<ucb::XContent> m_xNonexistent;
57 : };
58 :
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";
62 :
63 4 : DirectoryStreamTest::DirectoryStreamTest()
64 : {
65 4 : const uno::Reference<ucb::XCommandEnvironment> xCmdEnv;
66 8 : const uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
67 :
68 : using ucbhelper::Content;
69 :
70 4 : m_xDir = Content(getURLFromSrc(g_aDirPath), xCmdEnv, xContext).get();
71 4 : m_xFile = Content(getURLFromSrc(g_aNondirPath), xCmdEnv, xContext).get();
72 8 : m_xNonexistent = Content(getURLFromSrc(g_aNonexistentPath), xCmdEnv, xContext).get();
73 4 : }
74 :
75 1 : void DirectoryStreamTest::testConstruction()
76 : {
77 1 : const scoped_ptr<DirectoryStream> pDir(DirectoryStream::createForParent(m_xFile));
78 1 : CPPUNIT_ASSERT(bool(pDir));
79 1 : CPPUNIT_ASSERT(pDir->isStructured());
80 :
81 : // this should work for dirs too
82 2 : const scoped_ptr<DirectoryStream> pDir2(DirectoryStream::createForParent(m_xDir));
83 1 : CPPUNIT_ASSERT(bool(pDir2));
84 1 : CPPUNIT_ASSERT(pDir2->isStructured());
85 :
86 : // for nonexistent dirs nothing is created
87 2 : const scoped_ptr<DirectoryStream> pNondir(DirectoryStream::createForParent(m_xNonexistent));
88 1 : CPPUNIT_ASSERT(!pNondir);
89 :
90 : // even if we try harder, just an empty shell is created
91 2 : DirectoryStream aNondir2(m_xNonexistent);
92 2 : CPPUNIT_ASSERT(!aNondir2.isStructured());
93 1 : }
94 :
95 1 : void DirectoryStreamTest::testDetection()
96 : {
97 1 : CPPUNIT_ASSERT(DirectoryStream::isDirectory(m_xDir));
98 1 : CPPUNIT_ASSERT(!DirectoryStream::isDirectory(m_xFile));
99 1 : CPPUNIT_ASSERT(!DirectoryStream::isDirectory(m_xNonexistent));
100 1 : }
101 :
102 2 : void lcl_testDataOperations(RVNGInputStream &rStream)
103 : {
104 2 : CPPUNIT_ASSERT(rStream.isEnd());
105 2 : CPPUNIT_ASSERT_EQUAL(0L, rStream.tell());
106 2 : CPPUNIT_ASSERT_EQUAL(-1, rStream.seek(0, librevenge::RVNG_SEEK_CUR));
107 :
108 2 : unsigned long numBytesRead = 0;
109 2 : CPPUNIT_ASSERT(0 == rStream.read(1, numBytesRead));
110 2 : CPPUNIT_ASSERT_EQUAL(0UL, numBytesRead);
111 2 : }
112 :
113 1 : void DirectoryStreamTest::testDataOperations()
114 : {
115 : // data operations do not make sense on a directory -> just dummy
116 : // impls.
117 1 : DirectoryStream aDir(m_xDir);
118 1 : lcl_testDataOperations(aDir);
119 :
120 : // ... and they are equally empty if we try to pass a file
121 2 : DirectoryStream aFile(m_xFile);
122 2 : lcl_testDataOperations(aFile);
123 1 : }
124 :
125 2 : void lcl_testStructuredOperations(RVNGInputStream &rStream)
126 : {
127 2 : CPPUNIT_ASSERT(rStream.isStructured());
128 2 : scoped_ptr<RVNGInputStream> pSubstream(rStream.getSubStreamByName("mimetype"));
129 2 : CPPUNIT_ASSERT(bool(pSubstream));
130 :
131 : // TODO: test for other operations when they are implemented =)
132 2 : }
133 :
134 1 : void DirectoryStreamTest::testStructuredOperations()
135 : {
136 1 : DirectoryStream aDir(m_xDir);
137 1 : lcl_testStructuredOperations(aDir);
138 :
139 2 : scoped_ptr<DirectoryStream> pDir(DirectoryStream::createForParent(m_xFile));
140 1 : CPPUNIT_ASSERT(bool(pDir));
141 2 : lcl_testStructuredOperations(*pDir.get());
142 1 : }
143 :
144 1 : CPPUNIT_TEST_SUITE_REGISTRATION(DirectoryStreamTest);
145 :
146 3 : }
147 :
148 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|