Update ooo320-m1
[ooovba.git] / configmgr / qa / unit / ubootstrap.cxx
blob907f05c43b460c02c15eb014a713f202a5d4c406
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ubootstrap.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "common.hxx"
32 #include <osl/file.hxx>
34 // Do all those evil things to make the tests actually work ...
35 Magic::Magic()
37 rtl_uString *curWd = NULL;
38 osl_getProcessWorkingDir( &curWd );
40 rtl::OUString aKey;
41 rtl::OUString aValue( curWd );
43 // Unless you do this, obviously you don't want your paths to be correct
44 // or file access to work properly
45 aKey = rtl::OUString::createFromAscii( "BaseInstallation" );
46 rtl_bootstrap_set( aKey.pData, aValue.pData );
47 // Unless you do this, obviously you don't deserve to have anything work
48 aKey = rtl::OUString::createFromAscii( "CFG_INIFILE" );
49 aValue += rtl::OUString::createFromAscii( "/configmgrrc" );
50 rtl_bootstrap_set( aKey.pData, aValue.pData );
52 // FIXME: Create a safe /tmp directory - unfotunately that
53 // either requires dependencies we don't have, or some cut &
54 // paste action of unotools/source/ucbhelper/tempfile.cxx
55 if (osl::File::getTempDirURL(maTempDir) != osl::FileBase::E_None) {
56 t_print ("no tmp dir");
57 CPPUNIT_FAIL ("no tmp dir");
59 maTempDir += rtl::OUString::createFromAscii("/unittstconfig");
60 osl::FileBase::RC err = osl::Directory::create (maTempDir);
62 if (err != osl::FileBase::E_None) {
63 t_print ("tmp dir '%s' already exists\n",
64 rtl::OUStringToOString (maTempDir, RTL_TEXTENCODING_UTF8).getStr());
65 CPPUNIT_FAIL ("tmp user config dir already exists!");
68 aKey = rtl::OUString::createFromAscii( "UserInstallation" );
69 rtl_bootstrap_set( aKey.pData, maTempDir.pData );
72 void removeRecursive (const rtl::OUString& aPath)
74 sal_Int32 nMask = FileStatusMask_Type | FileStatusMask_FileURL;
76 // fprintf (stderr, "Remove recursive '%s'\n", rtl::OUStringToOString (aPath, RTL_TEXTENCODING_UTF8).getStr());
78 osl::DirectoryItem aItem;
79 osl::FileStatus aStatus( nMask );
80 osl::FileBase::RC nError;
82 nError = osl::DirectoryItem::get( aPath, aItem );
83 CPPUNIT_ASSERT_MESSAGE ("invalid path", nError == osl::FileBase::E_None);
85 nError = aItem.getFileStatus( aStatus );
86 CPPUNIT_ASSERT_MESSAGE ("invalid file", nError == osl::FileBase::E_None);
88 if (aStatus.getFileType() == osl::FileStatus::Regular ||
89 aStatus.getFileType() == osl::FileStatus::Link )
91 nError = osl::File::remove( aPath );
92 // fprintf (stderr, "Remove file '%s'\n", rtl::OUStringToOString (aPath, RTL_TEXTENCODING_UTF8).getStr());
93 CPPUNIT_ASSERT_MESSAGE ("removing file", nError == osl::FileBase::E_None);
95 else if (aStatus.getFileType() == osl::FileStatus::Directory)
97 osl::Directory aDirectory (aPath);
98 nError = aDirectory.open();
99 CPPUNIT_ASSERT_MESSAGE ("opening dir", nError == osl::FileBase::E_None);
101 rtl::OUString name;
103 while (true) {
104 nError = aDirectory.getNextItem( aItem );
105 if ( nError != osl::FileBase::E_None )
106 break;
107 nError = aItem.getFileStatus( aStatus );
108 CPPUNIT_ASSERT_MESSAGE ("getting status", nError == osl::FileBase::E_None);
109 removeRecursive (aStatus.getFileURL());
111 aDirectory.close();
113 nError = osl::Directory::remove( aPath );
114 // fprintf (stderr, "Remove dir '%s'\n", rtl::OUStringToOString (aPath, RTL_TEXTENCODING_UTF8).getStr());
116 CPPUNIT_ASSERT_MESSAGE ("removing directory", nError == osl::FileBase::E_None);
120 Magic::~Magic()
122 removeRecursive (maTempDir);