1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 // autogenerated file with codegen.pl 23 24 #include <testshl/simpleheader.hxx> 25 26 #include <osl/process.h> 27 #include <ucbhelper/contentbroker.hxx> 28 #include <cppuhelper/bootstrap.hxx> 29 #include <com/sun/star/ucb/XSimpleFileAccess.hpp> 30 #include <com/sun/star/io/XInputStream.hpp> 31 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 32 #include <com/sun/star/lang/XMultiComponentFactory.hpp> 33 #include <com/sun/star/uno/Any.hxx> 34 #include <com/sun/star/container/XNameContainer.hpp> 35 36 #include <doctok/exceptions.hxx> 37 #include <doctok/WW8Document.hxx> 38 #include <resourcemodel/WW8ResourceModel.hxx> 39 40 #include <iostream> 41 42 namespace testdoctok 43 { 44 using namespace ::std; 45 using namespace ::com::sun::star; 46 using namespace doctok; 47 48 uno::Reference<io::XInputStream> xStream; 49 uno::Reference<uno::XComponentContext> xContext; 50 WW8Document::Pointer_t pDocument; 51 uno::Reference< ::com::sun::star::ucb::XSimpleFileAccess > xSimpleFileAccess; 52 53 class test : public CppUnit::TestFixture 54 { 55 56 public: 57 // initialise your test code values here. setUp()58 void setUp() 59 { 60 } 61 tearDown()62 void tearDown() 63 { 64 } 65 testInitUno()66 void testInitUno() 67 { 68 bool bResult = false; 69 70 // initialise UCB-Broker 71 uno::Reference<uno::XComponentContext> 72 xComponentContext 73 (::cppu::defaultBootstrap_InitialComponentContext()); 74 OSL_ASSERT( xComponentContext.is() ); 75 76 xContext = xComponentContext; 77 78 uno::Reference<lang::XMultiComponentFactory> 79 xFactory(xComponentContext->getServiceManager() ); 80 OSL_ASSERT(xFactory.is()); 81 82 uno::Sequence<uno::Any> aUcbInitSequence(2); 83 aUcbInitSequence[0] <<= 84 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Local")); 85 aUcbInitSequence[1] <<= 86 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office")); 87 88 uno::Reference<lang::XMultiServiceFactory> 89 xServiceFactory(xFactory, uno::UNO_QUERY); 90 OSL_ASSERT( xServiceFactory.is() ); 91 92 if (xServiceFactory.is()) 93 { 94 sal_Bool bRet = 95 ::ucb::ContentBroker::initialize(xServiceFactory, 96 aUcbInitSequence); 97 98 OSL_ASSERT(bRet); 99 if (bRet) 100 { 101 uno::Reference< ::com::sun::star::ucb::XSimpleFileAccess > 102 xNameContainer(xFactory->createInstanceWithContext 103 (::rtl::OUString::createFromAscii 104 ("com.sun.star.ucb.SimpleFileAccess" ), 105 xComponentContext), uno::UNO_QUERY ); 106 107 if (xNameContainer.is()) 108 { 109 xSimpleFileAccess = xNameContainer; 110 111 bResult = true; 112 } 113 } 114 } 115 116 CPPUNIT_ASSERT_MESSAGE("UNO initialization failed", 117 bResult); 118 } 119 120 // insert your test code here. testOpenFile()121 void testOpenFile() 122 { 123 try 124 { 125 rtl_uString *dir=NULL; 126 osl_getProcessWorkingDir(&dir); 127 rtl_uString *fname=NULL; 128 //rtl_uString_newFromAscii(&fname, "/OpenDocument-v1.doc"); 129 rtl_uString_newFromAscii(&fname, "/test.doc"); 130 //rtl_uString_newFromAscii(&fname, "/numbers.doc"); 131 rtl_uString *absfile=NULL; 132 rtl_uString_newConcat(&absfile, dir, fname); 133 134 rtl::OUString sInputFileURL( absfile ); 135 136 for (sal_uInt32 n = 0; n < sInputFileURL.getLength(); ++n) 137 { 138 sal_uChar nC = sInputFileURL[n]; 139 140 if (nC < 0xff && isprint(nC)) 141 clog << static_cast<char>(nC); 142 else 143 clog << "."; 144 } 145 146 clog << endl; 147 148 xStream = xSimpleFileAccess->openFileRead(sInputFileURL); 149 150 WW8Stream::Pointer_t pStream = 151 WW8DocumentFactory::createStream(xContext, xStream); 152 153 pDocument = WW8DocumentFactory::createDocument(pStream); 154 } 155 catch (doctok::Exception e) 156 { 157 clog << "Exception!!" << endl; 158 } 159 160 CPPUNIT_ASSERT_MESSAGE("creating document failed", 161 pDocument != NULL); 162 163 #if 1 164 } 165 testTraversal()166 void testTraversal() 167 { 168 #endif 169 sal_uInt32 nResult = 0; 170 171 try 172 { 173 WW8DocumentIterator::Pointer_t pIt = pDocument->begin(); 174 WW8DocumentIterator::Pointer_t pItEnd = pDocument->end(); 175 176 while (! pIt->equal(*pItEnd)) 177 { 178 pIt->dump(clog); 179 180 clog << endl; 181 182 WW8PropertySet::Pointer_t pAttrs = pIt->getProperties(); 183 184 if (pAttrs != NULL) 185 { 186 pAttrs->dump(clog); 187 } 188 189 pIt->getText().dump(clog); 190 ++(*pIt); 191 ++nResult; 192 } 193 } 194 catch (doctok::Exception e) 195 { 196 clog << "Exception!!" << endl; 197 } 198 199 char sBuffer[256]; 200 snprintf(sBuffer, 255, "%d", nResult); 201 clog << "Iterator steps:" << sBuffer << endl; 202 203 CPPUNIT_ASSERT_MESSAGE("traversing document failed", 204 nResult > 0); 205 } 206 testEvents()207 void testEvents() 208 { 209 try 210 { 211 Stream::Pointer_t pStream = doctok::createStreamHandler(); 212 213 pDocument->resolve(*pStream); 214 } 215 catch (doctok::Exception e) 216 { 217 clog << "Exception!!" << endl; 218 } 219 } 220 testEnd()221 void testEnd() 222 { 223 ::ucb::ContentBroker::deinitialize(); 224 } 225 226 // Change the following lines only, if you add, remove or rename 227 // member functions of the current class, 228 // because these macros are need by auto register mechanism. 229 230 CPPUNIT_TEST_SUITE(test); 231 CPPUNIT_TEST(testInitUno); 232 CPPUNIT_TEST(testOpenFile); 233 //CPPUNIT_TEST(testTraversal); 234 CPPUNIT_TEST(testEvents); 235 CPPUNIT_TEST(testEnd); 236 CPPUNIT_TEST_SUITE_END(); 237 }; // class test 238 239 // ----------------------------------------------------------------------------- 240 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(testdoctok::test, "doctok"); 241 } // namespace doctok 242 243 244 // ----------------------------------------------------------------------------- 245 246 // this macro creates an empty function, which will called by the RegisterAllFunctions() 247 // to let the user the possibility to also register some functions by hand. 248 NOADDITIONAL; 249 250