diff options
author | kadertarlan <kadertarlan1@gmail.com> | 2016-03-15 03:09:34 +0200 |
---|---|---|
committer | jan iversen <jani@documentfoundation.org> | 2016-03-18 08:02:40 +0000 |
commit | b27aabc4ea3692a6ffbf28d8da0f75ad51feece9 (patch) | |
tree | 0f1d2ce78e1ffc17d00454d1317bda4ce2139932 /sw/qa | |
parent | eef1d712c409878678b4ffbf33e44cac6d564cc2 (diff) |
tdf#97362: Convert Java unit test to Python(load_save_test.py)
Change-Id: I7c3f2e786f94137b850770c77fbd24578f56b0af
Reviewed-on: https://gerrit.libreoffice.org/23254
Reviewed-by: jan iversen <jani@documentfoundation.org>
Tested-by: jan iversen <jani@documentfoundation.org>
Diffstat (limited to 'sw/qa')
-rw-r--r-- | sw/qa/complex/writer/LoadSaveTest.java | 215 | ||||
-rw-r--r-- | sw/qa/python/load_save_test.py | 129 |
2 files changed, 129 insertions, 215 deletions
diff --git a/sw/qa/complex/writer/LoadSaveTest.java b/sw/qa/complex/writer/LoadSaveTest.java deleted file mode 100644 index e2ae7ab21b48..000000000000 --- a/sw/qa/complex/writer/LoadSaveTest.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * 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/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -package complex.writer; - -import com.sun.star.uno.UnoRuntime; -import com.sun.star.uno.XComponentContext; -import com.sun.star.lang.EventObject; -import com.sun.star.lang.XMultiServiceFactory; -import com.sun.star.lang.XComponent; -import com.sun.star.beans.PropertyValue; -import com.sun.star.beans.Pair; -import com.sun.star.frame.XGlobalEventBroadcaster; -import com.sun.star.frame.XStorable; -import com.sun.star.frame.theGlobalEventBroadcaster; -import com.sun.star.document.DocumentEvent; -import com.sun.star.document.XDocumentEventListener; -import org.openoffice.test.OfficeConnection; - -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -import java.util.List; -import java.util.ArrayList; -import java.io.File; - -/** - * a small program to load documents from one directory (recursively) - * and store them in another, implemented as a complex test. - */ -public class LoadSaveTest -{ - private static final OfficeConnection connection = new OfficeConnection(); - - @BeforeClass public static void setUpConnection() throws Exception { - connection.setUp(); - } - - @AfterClass public static void tearDownConnection() - throws InterruptedException, com.sun.star.uno.Exception - { - connection.tearDown(); - } - - private XMultiServiceFactory m_xMSF = null; - private XGlobalEventBroadcaster m_xGEB = null; - - private static final String m_fileURL = "file://"; - // these should be parameters or something? - private static final String m_SourceDir = "FIXME"; - private static final String m_TargetDir = "/tmp/out"; - - @Before public void before() throws Exception - { - XComponentContext xContext = connection.getComponentContext(); - assertNotNull("could not get component context.", xContext); - m_xMSF = UnoRuntime.queryInterface( - XMultiServiceFactory.class, xContext.getServiceManager()); - m_xGEB = theGlobalEventBroadcaster.get(xContext); - String sTmpDir = util.utils.getOfficeTemp/*Dir*/(m_xMSF); - System.out.println("tempdir: " + sTmpDir); - System.out.println("sourcedir: " + m_SourceDir); - System.out.println("targetdir: " + m_TargetDir); - } - - @Test public void testLoadStore() throws Exception - { - Pair<List<String>, List<String>> dirsFiles = - getDirAndFileNames(m_SourceDir); - makeDirs(m_TargetDir, dirsFiles.First); - for (String fileName : dirsFiles.Second) - { - try { - testDoc(fileName); - } catch (Exception e) { - report(e); - } - } - } - - public void testDoc(String fileName) throws Exception - { - XComponent xDoc = null; - EventListener xListener = new EventListener(); - try { - m_xGEB.addDocumentEventListener(xListener); - - System.out.println("Loading document: " + fileName + " ..."); - - PropertyValue[] loadProps = new PropertyValue[1]; - loadProps[0] = new PropertyValue(); - loadProps[0].Name = "ReadOnly"; - loadProps[0].Value = Boolean.TRUE; - - String sourceFile = m_fileURL + m_SourceDir + fileName; - - xDoc = util.DesktopTools.loadDoc(m_xMSF, sourceFile, loadProps); - - System.out.println("... done"); - - { - // apparently OnLayoutFinished is not sent for every doc??? - // 10 seconds is evidently not enough for large documents - int time = 0; - while (!xListener.IsLayoutFinished() && (time < 30000)) { - Thread.sleep(100); - time += 100; - } - if (time >= 30000) { - System.out.println("timeout: no OnLayoutFinished received"); - } - } - - System.out.println("Storing document: " + fileName + " ..."); - - XStorable xStor = UnoRuntime.queryInterface( - XStorable.class, xDoc); - - String targetFile = m_fileURL + m_TargetDir + fileName; - - xStor.storeToURL(targetFile, new PropertyValue[0]); - - System.out.println("... done"); - - } finally { - if (xDoc != null) { - util.DesktopTools.closeDoc(xDoc); - } - m_xGEB.removeDocumentEventListener(xListener); - } - } - - private class EventListener implements XDocumentEventListener - { - private boolean m_isLayoutFinished = false; - private boolean IsLayoutFinished() { return m_isLayoutFinished; } - public void documentEventOccured(DocumentEvent Event) - { - if ("OnLayoutFinished".equals(Event.EventName)) - { - // we only have one doc at any time, so no need to check - m_isLayoutFinished = true; - } - } - public void disposing(EventObject Event) { } - } - - void report(Exception e) { - System.out.println("Exception occurred:"); - System.out.println(e.toString()); - e.printStackTrace(System.err); -// failed(); - } - - Pair<List<String>, List<String>> getDirAndFileNames(String dir) - { - List<String> dirs = new ArrayList<String>(); - List<String> files = new ArrayList<String>(); - File root = new File(dir); - getDirAndFileNames(root, "", dirs, files); - return new Pair<List<String>, List<String>>(dirs, files); - } - - void getDirAndFileNames(File file, String relPath, - List<String> dirs, List<String> files) - { - assertTrue("does not exist: " + relPath, file.exists()); - if (file.isDirectory()) { - dirs.add(relPath); - File[] subfiles = file.listFiles(); - for (File subfile : subfiles) - { - String subfileName = - relPath + File.separator + subfile.getName(); - getDirAndFileNames(subfile, subfileName, dirs, files); - } - } - else if (file.isFile()) { - if (file.getName().endsWith(".odt")) { - files.add(relPath); - } - } - } - - void makeDirs(String target, List<String> dirs) throws Exception - { - for (String dir : dirs) { - File f = new File(target + dir); - if (!f.exists()) { - if (!f.mkdir()) { - throw new Exception("cannot mkdir: " + target + dir); - } - } - } - } -} - diff --git a/sw/qa/python/load_save_test.py b/sw/qa/python/load_save_test.py new file mode 100644 index 000000000000..29aa76391358 --- /dev/null +++ b/sw/qa/python/load_save_test.py @@ -0,0 +1,129 @@ +''' + 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/. + + This file incorporates work covered by the following license notice: + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed + with this work for additional information regarding copyright + ownership. The ASF licenses this file to you under the Apache + License, Version 2.0 (the "License"); you may not use this file + except in compliance with the License. You may obtain a copy of + the License at http://www.apache.org/licenses/LICENSE-2.0 . +''' + +import unittest +import unohelper +import os +import os.path +import time +import uno +from com.sun.star.lang import EventObject +from com.sun.star.lang import XMultiServiceFactory +from com.sun.star.lang import XComponent +from com.sun.star.beans import PropertyValue +from com.sun.star.frame import XGlobalEventBroadcaster +from com.sun.star.frame import XStorable +from com.sun.star.document import DocumentEvent +from com.sun.star.document import XDocumentEventListener +from org.libreoffice.unotest import UnoInProcess +from urllib.parse import quote + + +class LoadSaveTest(unittest.TestCase): + + def mkPropertyValue(self, name, value): + return uno.createUnoStruct("com.sun.star.beans.PropertyValue", name, 0, value, 0) + + @classmethod + def setUpClass(cls): + cls._uno = UnoInProcess() + cls._uno.setUp() + cls.xDoc = cls._uno.openEmptyWriterDoc() + cls.xContext = cls._uno.getContext() + + cls.m_fileURL = "file:/" + cls.m_SourceDir = "FIXME" + cls.m_TargetDir = "/tmp/out/" + cls.dirs= [] + cls.files = [] + cls.fileName = "" + + @classmethod + def tearDownClass(cls): + cls._uno.tearDown() + + def testLoadStore(self): + self.dirs, self.files = self.getDirAndFile(self.m_SourceDir) + self.makeDirs(self.m_TargetDir) + for self.fileName in self.files: + self.tstDoc() + + def tstDoc(self): + try: + props = [("ReadOnly", True)] + loadProps = tuple([self.mkPropertyValue(name, value) for (name, value) in props]) + + m_xMSF = self.xContext.ServiceManager + desktop = m_xMSF.createInstanceWithContext('com.sun.star.frame.Desktop', self.xContext) + + #path = os.getenv("TDOC") + filepath = os.path.abspath("FIXME") + if os.name == "nt": + sourceFile = "file:///" + filepath + "/" + quote(self.fileName) + else: + sourceFile = "file://" + quote(filepath) + "/" + quote(self.fileName) + self.xDoc = desktop.loadComponentFromURL(sourceFile ,"_blank", 0 , loadProps) + assert(self.xDoc) + + if os.name == "nt": + targetFile = "file:///" + self.m_TargetDir + quote(self.m_SourceDir) + "/" + quote(self.fileName) + else: + targetFile = "file://" + quote(self.m_TargetDir) + quote(self.m_SourceDir) + "/" + quote(self.fileName) + + p1 = PropertyValue() + PropValue = uno.Any("[]com.sun.star.beans.PropertyValue", (p1,)) + uno.invoke(self.xDoc, "storeToURL", (targetFile, PropValue)) + + except Exception: + raise + + + def getDirAndFile(self,dir): + + root2 = os.mkdir(dir) + root = open(dir+ "/" + dir + ".odt",'a') + + self.getDirAndFileNames(dir) + return self.dirs, self.files + + def getDirAndFileNames( self, fdName): + + if os.path.isdir(fdName): + self.dirs.append(fdName) + subfiles = os.listdir(fdName) + + if not fdName[-1] == "/": + fdName += "/" + for subfile in subfiles: + subfileName = fdName + subfile + self.getDirAndFileNames(subfileName) + + if os.path.isfile(fdName): + self.files.append(fdName.split('/')[-1]); + + def makeDirs(self, target): + if not os.path.exists(target): + os.mkdir(target) + self.assertTrue( os.path.exists(target)) + + for dir in self.dirs: + if not os.path.exists(target + dir): + f = os.mkdir(target + dir) + self.assertTrue( os.path.exists(target + dir)) + root = open(target + dir + "/" + self.m_SourceDir + ".odt",'a') + filepath = os.path.abspath(target + dir + "/" + self.m_SourceDir + ".odt") |