Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / qa / unit / dataproviders_test.cxx
blob5b1aa76b95ed3a9be0b20fe0667564ca0ec038bb
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
9 #include <rtl/ustring.hxx>
11 #include "helper/qahelper.hxx"
12 #include <document.hxx>
13 #include <address.hxx>
14 #include <dbdata.hxx>
15 #include <datamapper.hxx>
16 #include <vcl/scheduler.hxx>
17 #include <orcusxml.hxx>
19 #include <memory>
21 using namespace ::com::sun::star;
22 using namespace ::com::sun::star::uno;
24 class ScDataProvidersTest : public ScModelTestBase
26 public:
27 ScDataProvidersTest();
29 void testCSVImport();
30 void testDataLargerThanDB();
31 void testHTMLImport();
32 void testXMLImport();
33 // void testBaseImport();
35 CPPUNIT_TEST_SUITE(ScDataProvidersTest);
36 CPPUNIT_TEST(testCSVImport);
37 CPPUNIT_TEST(testDataLargerThanDB);
38 CPPUNIT_TEST(testHTMLImport);
39 CPPUNIT_TEST(testXMLImport);
40 // CPPUNIT_TEST(testBaseImport);
41 CPPUNIT_TEST_SUITE_END();
44 void ScDataProvidersTest::testCSVImport()
46 createScDoc();
48 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 10, 10);
49 ScDocument* pDoc = getScDoc();
50 bool bInserted
51 = pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
52 CPPUNIT_ASSERT(bInserted);
54 OUString aFileURL = createFileURL(u"csv/test1.csv");
55 sc::ExternalDataSource aDataSource(aFileURL, "org.libreoffice.calc.csv", pDoc);
56 aDataSource.setDBData(pDBData->GetName());
58 pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
59 auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
60 CPPUNIT_ASSERT(!rDataSources.empty());
62 rDataSources[0].refresh(pDoc, true);
63 Scheduler::ProcessEventsToIdle();
65 CPPUNIT_ASSERT_EQUAL(1.0, pDoc->GetValue(0, 0, 0));
66 CPPUNIT_ASSERT_EQUAL(2.0, pDoc->GetValue(1, 0, 0));
67 CPPUNIT_ASSERT_EQUAL(3.0, pDoc->GetValue(2, 0, 0));
68 CPPUNIT_ASSERT_EQUAL(4.0, pDoc->GetValue(3, 0, 0));
69 CPPUNIT_ASSERT_EQUAL(OUString("test1"), pDoc->GetString(0, 1, 0));
70 CPPUNIT_ASSERT_EQUAL(OUString("test2"), pDoc->GetString(1, 1, 0));
71 CPPUNIT_ASSERT_EQUAL(OUString("test3"), pDoc->GetString(2, 1, 0));
74 void ScDataProvidersTest::testDataLargerThanDB()
76 createScDoc();
78 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 1, 1);
79 ScDocument* pDoc = getScDoc();
80 bool bInserted
81 = pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
82 CPPUNIT_ASSERT(bInserted);
84 OUString aFileURL = createFileURL(u"csv/test1.csv");
85 sc::ExternalDataSource aDataSource(aFileURL, "org.libreoffice.calc.csv", pDoc);
86 aDataSource.setDBData(pDBData->GetName());
88 pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
89 auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
90 CPPUNIT_ASSERT(!rDataSources.empty());
92 rDataSources[0].refresh(pDoc, true);
93 Scheduler::ProcessEventsToIdle();
95 CPPUNIT_ASSERT_EQUAL(1.0, pDoc->GetValue(0, 0, 0));
96 CPPUNIT_ASSERT_EQUAL(2.0, pDoc->GetValue(1, 0, 0));
97 CPPUNIT_ASSERT_EQUAL(0.0, pDoc->GetValue(2, 0, 0));
98 CPPUNIT_ASSERT_EQUAL(0.0, pDoc->GetValue(3, 0, 0));
99 CPPUNIT_ASSERT_EQUAL(OUString("test1"), pDoc->GetString(0, 1, 0));
100 CPPUNIT_ASSERT_EQUAL(OUString("test2"), pDoc->GetString(1, 1, 0));
101 CPPUNIT_ASSERT_EQUAL(OUString(), pDoc->GetString(2, 1, 0));
104 void ScDataProvidersTest::testHTMLImport()
106 createScDoc();
108 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 10, 10);
109 ScDocument* pDoc = getScDoc();
110 bool bInserted
111 = pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
112 CPPUNIT_ASSERT(bInserted);
114 OUString aFileURL = createFileURL(u"html/test1.html");
115 sc::ExternalDataSource aDataSource(aFileURL, "org.libreoffice.calc.html", pDoc);
116 aDataSource.setID("//table");
117 aDataSource.setDBData(pDBData->GetName());
119 pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
120 auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
121 CPPUNIT_ASSERT(!rDataSources.empty());
123 rDataSources[0].refresh(pDoc, true);
124 Scheduler::ProcessEventsToIdle();
126 std::vector<OUString> aCarManufacturers = { "Audi", "GM", "Nissan", "Ferrari", "Peugeot" };
127 std::vector<OUString> aCities = { "Berlin", "San Francisco", "Tokyo", "Rome", "Paris" };
128 std::vector<double> aFirstCol = { 1, 10, -100, -0.11111, 1 };
129 std::vector<double> aSecondCol = {
130 2, 2.1, 40179, 2, 2,
131 }; // 40179 is equal to 2010-1-1
133 CPPUNIT_ASSERT_EQUAL(OUString("Col1"), pDoc->GetString(0, 0, 0));
134 CPPUNIT_ASSERT_EQUAL(OUString("Col2"), pDoc->GetString(1, 0, 0));
135 CPPUNIT_ASSERT_EQUAL(OUString("Col3"), pDoc->GetString(2, 0, 0));
136 CPPUNIT_ASSERT_EQUAL(OUString("Col4"), pDoc->GetString(3, 0, 0));
138 for (SCROW nRow = 0; nRow <= 4; ++nRow)
140 ASSERT_DOUBLES_EQUAL(aFirstCol[nRow], pDoc->GetValue(0, nRow + 1, 0));
141 ASSERT_DOUBLES_EQUAL(aSecondCol[nRow], pDoc->GetValue(1, nRow + 1, 0));
142 CPPUNIT_ASSERT_EQUAL(aCarManufacturers[nRow], pDoc->GetString(2, nRow + 1, 0));
143 CPPUNIT_ASSERT_EQUAL(aCities[nRow], pDoc->GetString(3, nRow + 1, 0));
147 void ScDataProvidersTest::testXMLImport()
149 createScDoc();
151 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 10, 10);
152 ScDocument* pDoc = getScDoc();
153 bool bInserted
154 = pDoc->GetDBCollection()->getNamedDBs().insert(std::unique_ptr<ScDBData>(pDBData));
155 CPPUNIT_ASSERT(bInserted);
157 ScOrcusImportXMLParam aParam;
159 ScOrcusImportXMLParam::RangeLink aRangeLink;
160 aRangeLink.maPos = ScAddress(0, 0, 0);
161 aRangeLink.maFieldPaths.push_back("/bookstore/book/title"_ostr);
162 aRangeLink.maFieldPaths.push_back("/bookstore/book/author"_ostr);
163 aRangeLink.maRowGroups.push_back("/bookstore/book"_ostr);
164 aParam.maRangeLinks.push_back(aRangeLink);
166 OUString aFileURL = createFileURL(u"xml/test1.xml");
167 sc::ExternalDataSource aDataSource(aFileURL, "org.libreoffice.calc.xml", pDoc);
168 aDataSource.setDBData("testDB");
169 aDataSource.setXMLImportParam(aParam);
171 pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
172 auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
173 CPPUNIT_ASSERT(!rDataSources.empty());
175 rDataSources[0].refresh(pDoc, true);
176 Scheduler::ProcessEventsToIdle();
178 CPPUNIT_ASSERT_EQUAL(OUString("title"), pDoc->GetString(0, 0, 0));
179 CPPUNIT_ASSERT_EQUAL(OUString("author"), pDoc->GetString(1, 0, 0));
180 CPPUNIT_ASSERT_EQUAL(1.0, pDoc->GetValue(0, 1, 0));
181 CPPUNIT_ASSERT_EQUAL(OUString("test1"), pDoc->GetString(1, 1, 0));
182 CPPUNIT_ASSERT_EQUAL(2.0, pDoc->GetValue(0, 2, 0));
183 CPPUNIT_ASSERT_EQUAL(OUString("test2"), pDoc->GetString(1, 2, 0));
184 CPPUNIT_ASSERT_EQUAL(3.0, pDoc->GetValue(0, 3, 0));
185 CPPUNIT_ASSERT_EQUAL(OUString("test3"), pDoc->GetString(1, 3, 0));
186 CPPUNIT_ASSERT_EQUAL(4.0, pDoc->GetValue(0, 4, 0));
187 CPPUNIT_ASSERT_EQUAL(OUString("test4"), pDoc->GetString(1, 4, 0));
191 void ScDataProvidersTest::testBaseImport()
193 createScDoc();
195 ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 10, 10);
196 ScDocument* pDoc = getScDoc();
197 bool bInserted = pDoc->GetDBCollection()->getNamedDBs().insert(pDBData);
198 CPPUNIT_ASSERT(bInserted);
200 sc::ExternalDataSource aDataSource("~/dummy.file", "org.libreoffice.calc.sql", pDoc);
201 aDataSource.setDBData("testDB");
202 aDataSource.setID("biblio@Bibliography");
205 pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
206 auto& rDataSources = pDoc->GetExternalDataMapper().getDataSources();
207 CPPUNIT_ASSERT(!rDataSources.empty());
209 rDataSources[0].refresh(pDoc, true);
210 Scheduler::ProcessEventsToIdle();
212 CPPUNIT_ASSERT_EQUAL(OUString("ARJ00"), pDoc->GetString(0, 0, 0));
213 CPPUNIT_ASSERT_EQUAL(OUString("AVV00"), pDoc->GetString(1, 1, 0));
217 ScDataProvidersTest::ScDataProvidersTest()
218 : ScModelTestBase("sc/qa/unit/data/dataprovider")
222 CPPUNIT_TEST_SUITE_REGISTRATION(ScDataProvidersTest);
224 CPPUNIT_PLUGIN_IMPLEMENT();
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */