1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/config.h>
#include <set>
#include <vector>
#include <swmodeltestbase.hxx>
#include <com/sun/star/text/MailMergeType.hpp>
#include <com/sun/star/sdb/CommandType.hpp>
#include <com/sun/star/table/TableBorder.hpp>
#include <com/sun/star/text/TextContentAnchorType.hpp>
#include <com/sun/star/text/XTextTable.hpp>
#include <com/sun/star/sdbc/XRowSet.hpp>
#include <com/sun/star/sdbcx/XRowLocate.hpp>
#include <com/sun/star/task/XJob.hpp>
#include <tools/urlobj.hxx>
#include <comphelper/sequence.hxx>
#include <wrtsh.hxx>
#include <ndtxt.hxx>
#include <pagefrm.hxx>
#include <unoprnms.hxx>
#include <dbmgr.hxx>
#include <unotxdoc.hxx>
#include <docsh.hxx>
#include <IDocumentLayoutAccess.hxx>
#include <rootfrm.hxx>
/**
* Maps database URIs to the registered database names for quick lookups
*/
typedef std::map<OUString, OUString> DBuriMap;
DBuriMap aDBuriMap;
class MailMergeTestBase : public SwModelTestBase
{
public:
MailMergeTestBase()
: SwModelTestBase(u"/sw/qa/extras/mailmerge/data/"_ustr, u"writer8"_ustr)
, mnCurOutputType(0)
, maMMtestFilename(nullptr)
{
}
virtual void tearDown() override
{
if (mxSwTextDocument.is())
{
if (mnCurOutputType == text::MailMergeType::SHELL)
mxSwTextDocument->GetDocShell()->DoClose();
else
mxSwTextDocument->dispose();
}
if (mxCurResultSet.is())
{
css::uno::Reference<css::lang::XComponent>(mxCurResultSet, css::uno::UNO_QUERY_THROW)
->dispose();
}
SwModelTestBase::tearDown();
}
/**
* Helper func used by each unit test to test the 'mail merge' code.
*
* Registers the data source, loads the original file as reference,
* initializes the mail merge job and its default argument sequence.
*
* The 'verify' method actually has to execute the mail merge by
* calling executeMailMerge() after modifying the job arguments.
*/
void executeMailMergeTest(const char* filename, const char* datasource, const char* tablename,
char const* const filter, int selection, const char* column)
{
maMMtestFilename = filename;
header();
utl::TempFileNamed aTempDir(nullptr, true);
aTempDir.EnableKillingFile();
const OUString aWorkDir = aTempDir.GetURL();
const OUString aURI(createFileURL(OUString::createFromAscii(datasource)));
const OUString aPrefix = column ? OUString::createFromAscii(column) : u"LOMM_"_ustr;
const OUString aDBName = registerDBsource(aURI, aWorkDir);
initMailMergeJobAndArgs(filename, tablename, aDBName, aPrefix, aWorkDir, filter, selection,
column != nullptr);
verify();
mnCurOutputType = 0;
}
OUString registerDBsource(const OUString& aURI, const OUString& aWorkDir)
{
OUString aDBName;
DBuriMap::const_iterator pos = aDBuriMap.find(aURI);
if (pos == aDBuriMap.end())
{
aDBName = SwDBManager::LoadAndRegisterDataSource(aURI, &aWorkDir);
aDBuriMap.insert(std::pair<OUString, OUString>(aURI, aDBName));
std::cout << "New datasource name: '" << aDBName << "'" << std::endl;
}
else
{
aDBName = pos->second;
std::cout << "Old datasource name: '" << aDBName << "'" << std::endl;
}
CPPUNIT_ASSERT(!aDBName.isEmpty());
return aDBName;
}
uno::Reference<sdbc::XRowSet> getXResultFromDataset(const char* tablename,
const OUString& aDBName)
{
uno::Reference<sdbc::XRowSet> xCurResultSet;
uno::Reference<uno::XInterface> xInstance
= getMultiServiceFactory()->createInstance(u"com.sun.star.sdb.RowSet"_ustr);
uno::Reference<beans::XPropertySet> xRowSetPropSet(xInstance, uno::UNO_QUERY);
assert(xRowSetPropSet.is() && "failed to get XPropertySet interface from RowSet");
if (xRowSetPropSet.is())
{
xRowSetPropSet->setPropertyValue(u"DataSourceName"_ustr, uno::Any(aDBName));
xRowSetPropSet->setPropertyValue(u"Command"_ustr,
uno::Any(OUString::createFromAscii(tablename)));
xRowSetPropSet->setPropertyValue(u"CommandType"_ustr,
uno::Any(sdb::CommandType::TABLE));
uno::Reference<sdbc::XRowSet> xRowSet(xInstance, uno::UNO_QUERY);
if (xRowSet.is())
xRowSet->execute(); // build ResultSet from properties
xCurResultSet = xRowSet;
assert(xCurResultSet.is() && "failed to build ResultSet");
}
return xCurResultSet;
}
void initMailMergeJobAndArgs(const char* filename, const char* tablename,
const OUString& aDBName, const OUString& aPrefix,
const OUString& aWorkDir, char const* const filter, int nDataSets,
const bool bPrefixIsColumn)
{
uno::Reference<task::XJob> xJob(
getMultiServiceFactory()->createInstance(u"com.sun.star.text.MailMerge"_ustr),
uno::UNO_QUERY_THROW);
mxJob.set(xJob);
mMMargs.reserve(15);
mMMargs.emplace_back(UNO_NAME_OUTPUT_TYPE, uno::Any(filter ? text::MailMergeType::FILE
: text::MailMergeType::SHELL));
mMMargs.emplace_back(UNO_NAME_DOCUMENT_URL,
uno::Any((createFileURL(OUString::createFromAscii(filename)))));
mMMargs.emplace_back(UNO_NAME_DATA_SOURCE_NAME, uno::Any(aDBName));
mMMargs.emplace_back(UNO_NAME_OUTPUT_URL, uno::Any(aWorkDir));
if (filter)
{
mMMargs.emplace_back(UNO_NAME_FILE_NAME_PREFIX, uno::Any(aPrefix));
mMMargs.emplace_back(UNO_NAME_SAVE_FILTER, uno::Any(OUString::createFromAscii(filter)));
}
if (bPrefixIsColumn)
mMMargs.emplace_back(UNO_NAME_FILE_NAME_FROM_COLUMN, uno::Any(true));
if (tablename)
{
mMMargs.emplace_back(UNO_NAME_DAD_COMMAND_TYPE, uno::Any(sdb::CommandType::TABLE));
mMMargs.emplace_back(UNO_NAME_DAD_COMMAND,
uno::Any(OUString::createFromAscii(tablename)));
}
if (nDataSets > 0)
{
mxCurResultSet = getXResultFromDataset(tablename, aDBName);
uno::Reference<sdbcx::XRowLocate> xCurRowLocate(mxCurResultSet, uno::UNO_QUERY);
mMMargs.emplace_back(UNO_NAME_RESULT_SET, uno::Any(mxCurResultSet));
std::vector<uno::Any> vResult;
vResult.reserve(nDataSets);
sal_Int32 i;
for (i = 0, mxCurResultSet->first(); i < nDataSets; i++, mxCurResultSet->next())
{
vResult.emplace_back(xCurRowLocate->getBookmark());
}
mMMargs.emplace_back(UNO_NAME_SELECTION,
uno::Any(comphelper::containerToSequence(vResult)));
}
}
void executeMailMerge(bool bDontLoadResult = false)
{
const uno::Sequence<beans::NamedValue> aSeqMailMergeArgs
= comphelper::containerToSequence(mMMargs);
uno::Any res = mxJob->execute(aSeqMailMergeArgs);
bool bOk = true;
bool bMMFilenameFromColumn = false;
for (const beans::NamedValue& rArgument : aSeqMailMergeArgs)
{
const OUString& rName = rArgument.Name;
const uno::Any& rValue = rArgument.Value;
// all error checking was already done by the MM job execution
if (rName == UNO_NAME_OUTPUT_URL)
bOk &= rValue >>= msMailMergeOutputURL;
else if (rName == UNO_NAME_FILE_NAME_PREFIX)
bOk &= rValue >>= msMailMergeOutputPrefix;
else if (rName == UNO_NAME_OUTPUT_TYPE)
bOk &= rValue >>= mnCurOutputType;
else if (rName == UNO_NAME_FILE_NAME_FROM_COLUMN)
bOk &= rValue >>= bMMFilenameFromColumn;
else if (rName == UNO_NAME_DOCUMENT_URL)
bOk &= rValue >>= msMailMergeDocumentURL;
}
CPPUNIT_ASSERT(bOk);
// MM via UNO just works with file names. If we load the file on
// Windows before MM uses it, MM won't work, as it's already open.
// Don't move the load before the mail merge execution!
// (see gb_CppunitTest_use_instdir_configuration)
createSwDoc(maMMtestFilename);
if (mnCurOutputType == text::MailMergeType::SHELL)
{
uno::Reference<lang::XComponent> xTmp;
CPPUNIT_ASSERT(res >>= xTmp);
mxSwTextDocument = dynamic_cast<SwXTextDocument*>(xTmp.get());
CPPUNIT_ASSERT(mxSwTextDocument.is());
}
else
{
CPPUNIT_ASSERT_EQUAL(uno::Any(true), res);
if (!bMMFilenameFromColumn && !bDontLoadResult)
loadMailMergeDocument(0);
}
}
/**
* Like parseExport(), but for given mail merge document.
*/
xmlDocUniquePtr parseMailMergeExport(const OUString& rStreamName)
{
if (mnCurOutputType != text::MailMergeType::FILE)
return nullptr;
OUString name = msMailMergeOutputPrefix + OUString::number(0) + ".odt";
std::unique_ptr<SvStream> pStream(
parseExportStream(msMailMergeOutputURL + "/" + name, rStreamName));
return parseXmlStream(pStream.get());
}
void loadMailMergeDocument(const OUString& filename)
{
assert(mnCurOutputType == text::MailMergeType::FILE);
if (mxComponent.is())
mxComponent->dispose();
// Output name early, so in the case of a hang, the name of the hanging input file is visible.
std::cout << filename << ",";
mxComponent = loadFromDesktop(msMailMergeOutputURL + "/" + filename,
u"com.sun.star.text.TextDocument"_ustr);
calcLayout();
}
/**
Loads number-th document from mail merge. Requires file output from mail merge.
*/
void loadMailMergeDocument(int number, char const* const ext = ".odt")
{
OUString name;
if (!msMailMergeOutputPrefix.isEmpty())
name = msMailMergeOutputPrefix;
else
{
INetURLObject aURLObj;
aURLObj.SetSmartProtocol(INetProtocol::File);
aURLObj.SetSmartURL(msMailMergeDocumentURL);
name = aURLObj.GetBase();
}
name += OUString::number(number)
+ OStringToOUString(std::string_view(ext, strlen(ext)), RTL_TEXTENCODING_ASCII_US);
loadMailMergeDocument(name);
}
// Returns page number of the first page of a MM document inside the large MM document (used in the SHELL case).
int documentStartPageNumber(int document) const
{ // See documentStartPageNumber() .
CPPUNIT_ASSERT(mxSwTextDocument);
SwWrtShell* shell = mxSwTextDocument->GetDocShell()->GetWrtShell();
IDocumentMarkAccess* marks = shell->GetDoc()->getIDocumentMarkAccess();
// Unfortunately, the pages are marked using UNO bookmarks, which have internals names, so they cannot be referred to by their names.
// Assume that there are no other UNO bookmarks than the ones used by mail merge, and that they are in the sorted order.
IDocumentMarkAccess::const_iterator mark;
int pos = 0;
for (mark = marks->getAllMarksBegin(); mark != marks->getAllMarksEnd() && pos < document;
++mark)
{
if (IDocumentMarkAccess::GetType(**mark) == IDocumentMarkAccess::MarkType::UNO_BOOKMARK)
++pos;
}
CPPUNIT_ASSERT_EQUAL(document, pos);
sal_uInt16 page, dummy;
shell->Push();
shell->GotoMark(*mark);
shell->GetPageNum(page, dummy);
shell->Pop(SwCursorShell::PopMode::DeleteCurrent);
return page;
}
protected:
uno::Reference<css::task::XJob> mxJob;
std::vector<beans::NamedValue> mMMargs;
OUString msMailMergeDocumentURL;
OUString msMailMergeOutputURL;
OUString msMailMergeOutputPrefix;
sal_Int16 mnCurOutputType;
rtl::Reference<SwXTextDocument> mxSwTextDocument;
uno::Reference<sdbc::XRowSet> mxCurResultSet;
const char* maMMtestFilename;
};
#define DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, filter, BaseClass, \
selection, column) \
class TestName : public BaseClass \
{ \
public: \
CPPUNIT_TEST_SUITE(TestName); \
CPPUNIT_TEST(MailMerge); \
CPPUNIT_TEST_SUITE_END(); \
\
void MailMerge() \
{ \
executeMailMergeTest(filename, datasource, tablename, filter, selection, column); \
} \
void verify() override; \
}; \
CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \
void TestName::verify()
// Will generate the resulting document in mxMMDocument.
#define DECLARE_SHELL_MAILMERGE_TEST(TestName, filename, datasource, tablename) \
DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, nullptr, MailMergeTestBase, \
0, nullptr)
// Will generate documents as files, use loadMailMergeDocument().
#define DECLARE_FILE_MAILMERGE_TEST(TestName, filename, datasource, tablename) \
DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, "writer8", \
MailMergeTestBase, 0, nullptr)
#define DECLARE_SHELL_MAILMERGE_TEST_SELECTION(TestName, filename, datasource, tablename, \
selection) \
DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, nullptr, MailMergeTestBase, \
selection, nullptr)
#define DECLARE_FILE_MAILMERGE_TEST_COLUMN(TestName, filename, datasource, tablename, column) \
DECLARE_MAILMERGE_TEST(TestName, filename, datasource, tablename, "writer8", \
MailMergeTestBase, 0, column)
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|