From a62878ff86ecb38f9ea9fbe99f06518ed6016566 Mon Sep 17 00:00:00 2001 From: Andreas Heinisch Date: Thu, 28 Jan 2021 18:34:18 +0100 Subject: tdf#60237 - correct the behaviour of the Overwrite property If the Overwrite property of the MediaDescriptor is set to false, try to write the file before trying to rename or copy it. If the file already exists, raise an error (ERRCODE_IO_ALREADYEXISTS). In addition, the documentation about the default option was corrected. Change-Id: I1031dca3a039343fc599d194fcaa99a20dd13e2a Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110089 Tested-by: Jenkins Reviewed-by: Miklos Vajna --- sfx2/qa/cppunit/test_misc.cxx | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'sfx2/qa') diff --git a/sfx2/qa/cppunit/test_misc.cxx b/sfx2/qa/cppunit/test_misc.cxx index f6039b0e68d8..02c2ed356f88 100644 --- a/sfx2/qa/cppunit/test_misc.cxx +++ b/sfx2/qa/cppunit/test_misc.cxx @@ -25,11 +25,13 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -192,6 +194,37 @@ CPPUNIT_TEST_FIXTURE(MiscTest, testHardLinks) #endif } +CPPUNIT_TEST_FIXTURE(MiscTest, testOverwrite) +{ + // tdf#60237 - try to overwrite an existing file using the different settings of the Overwrite option + utl::TempFile aTempFile; + aTempFile.EnableKillingFile(); + uno::Reference xComponent + = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument"); + CPPUNIT_ASSERT(xComponent.is()); + uno::Reference xStorable(xComponent, uno::UNO_QUERY); + CPPUNIT_ASSERT(xStorable.is()); + + // overwrite the file using the default case of the Overwrite option (true) + CPPUNIT_ASSERT_NO_THROW(xStorable->storeToURL(aTempFile.GetURL(), {})); + + // explicitly overwrite the file using the Overwrite option + CPPUNIT_ASSERT_NO_THROW(xStorable->storeToURL( + aTempFile.GetURL(), + comphelper::InitPropertySequence({ { "Overwrite", uno::makeAny(true) } }))); + + try + { + // overwrite an existing file with the Overwrite flag set to false + xStorable->storeToURL(aTempFile.GetURL(), comphelper::InitPropertySequence( + { { "Overwrite", uno::makeAny(false) } })); + CPPUNIT_ASSERT_MESSAGE("We expect an exception on overwriting an existing file", false); + } + catch (const css::uno::Exception&) + { + } +} + } CPPUNIT_PLUGIN_IMPLEMENT(); -- cgit