From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- sfx2/qa/complex/sfx2/DocumentEvents.java | 221 +++ sfx2/qa/complex/sfx2/DocumentMetadataAccess.java | 1228 ++++++++++++++++ sfx2/qa/complex/sfx2/DocumentProperties.java | 516 +++++++ sfx2/qa/complex/sfx2/GlobalEventBroadcaster.java | 249 ++++ sfx2/qa/complex/sfx2/UndoManager.java | 1459 ++++++++++++++++++++ sfx2/qa/complex/sfx2/testdocuments/CUSTOM.odt | Bin 0 -> 1021 bytes sfx2/qa/complex/sfx2/testdocuments/TEST.odt | Bin 0 -> 13803 bytes sfx2/qa/complex/sfx2/testdocuments/TESTRDFA.odt | Bin 0 -> 7540 bytes sfx2/qa/complex/sfx2/testdocuments/empty.rdf | 13 + sfx2/qa/complex/sfx2/tools/TestDocument.java | 34 + sfx2/qa/complex/sfx2/tools/WriterHelper.java | 210 +++ sfx2/qa/complex/sfx2/undo/CalcDocumentTest.java | 113 ++ sfx2/qa/complex/sfx2/undo/ChartDocumentTest.java | 257 ++++ sfx2/qa/complex/sfx2/undo/DocumentTest.java | 77 ++ sfx2/qa/complex/sfx2/undo/DocumentTestBase.java | 44 + sfx2/qa/complex/sfx2/undo/DrawDocumentTest.java | 35 + .../undo/DrawingOrPresentationDocumentTest.java | 207 +++ sfx2/qa/complex/sfx2/undo/ImpressDocumentTest.java | 35 + sfx2/qa/complex/sfx2/undo/WriterDocumentTest.java | 121 ++ 19 files changed, 4819 insertions(+) create mode 100644 sfx2/qa/complex/sfx2/DocumentEvents.java create mode 100644 sfx2/qa/complex/sfx2/DocumentMetadataAccess.java create mode 100644 sfx2/qa/complex/sfx2/DocumentProperties.java create mode 100644 sfx2/qa/complex/sfx2/GlobalEventBroadcaster.java create mode 100644 sfx2/qa/complex/sfx2/UndoManager.java create mode 100644 sfx2/qa/complex/sfx2/testdocuments/CUSTOM.odt create mode 100644 sfx2/qa/complex/sfx2/testdocuments/TEST.odt create mode 100644 sfx2/qa/complex/sfx2/testdocuments/TESTRDFA.odt create mode 100644 sfx2/qa/complex/sfx2/testdocuments/empty.rdf create mode 100644 sfx2/qa/complex/sfx2/tools/TestDocument.java create mode 100644 sfx2/qa/complex/sfx2/tools/WriterHelper.java create mode 100644 sfx2/qa/complex/sfx2/undo/CalcDocumentTest.java create mode 100644 sfx2/qa/complex/sfx2/undo/ChartDocumentTest.java create mode 100644 sfx2/qa/complex/sfx2/undo/DocumentTest.java create mode 100644 sfx2/qa/complex/sfx2/undo/DocumentTestBase.java create mode 100644 sfx2/qa/complex/sfx2/undo/DrawDocumentTest.java create mode 100644 sfx2/qa/complex/sfx2/undo/DrawingOrPresentationDocumentTest.java create mode 100644 sfx2/qa/complex/sfx2/undo/ImpressDocumentTest.java create mode 100644 sfx2/qa/complex/sfx2/undo/WriterDocumentTest.java (limited to 'sfx2/qa/complex') diff --git a/sfx2/qa/complex/sfx2/DocumentEvents.java b/sfx2/qa/complex/sfx2/DocumentEvents.java new file mode 100644 index 000000000..32c7ba5ae --- /dev/null +++ b/sfx2/qa/complex/sfx2/DocumentEvents.java @@ -0,0 +1,221 @@ +package complex.sfx2; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openoffice.test.OfficeConnection; +import org.openoffice.test.tools.OfficeDocument; + +import com.sun.star.document.DocumentEvent; +import com.sun.star.document.XDocumentEventBroadcaster; +import com.sun.star.document.XDocumentEventListener; +import com.sun.star.lang.EventObject; +import com.sun.star.lang.XEventListener; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.util.CloseVetoException; +import com.sun.star.util.XCloseListener; +import com.sun.star.util.XCloseable; + +public class DocumentEvents +{ + private static final OfficeConnection m_connection = new OfficeConnection(); + + @BeforeClass + public static void setUpConnection() throws Exception + { + m_connection.setUp(); + } + + + @AfterClass + public static void tearDownConnection() throws InterruptedException, com.sun.star.uno.Exception + { + m_connection.tearDown(); + } + + @Before + public void beforeTest() throws Exception + { + final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface( + XMultiServiceFactory.class, m_connection.getComponentContext().getServiceManager() ); + m_document = OfficeDocument.blankTextDocument( xMSF1 ); + } + + @After + public void afterTest() + { + if ( m_document != null ) + { + assertTrue( "closing the test document failed", m_document.close() ); + m_document = null; + } + } + + /** + * sets up the environment for a test which checks the behavior upon closing a doc + */ + private void impl_setupDocCloseTest() + { + m_observedCloseEvents.clear(); + + final XDocumentEventBroadcaster docEventBroadcaster = UnoRuntime.queryInterface( + XDocumentEventBroadcaster.class, m_document.getDocument() ); + docEventBroadcaster.addDocumentEventListener( new DocumentEventListener() ); + + final XCloseable docCloseable = UnoRuntime.queryInterface( XCloseable.class, + m_document.getDocument() ); + docCloseable.addCloseListener( new CloseListener() ); + + m_document.getDocument().addEventListener( new DocDisposeListener() ); + } + + /** + * sets up the environment for a test which checks the behavior upon closing a doc + */ + private void impl_tearDownDocCloseTest( final String i_docCloseMethod ) + { + synchronized( m_document ) + { + try + { + m_document.wait(10000); + } + catch (InterruptedException ex) + { + // don't continue the test if somebody interrupted us ... + return; + } + } + + m_document = null; + synchronized( m_observedCloseEvents ) + { + assertArrayEquals( + "wrong order of events when closing a doc " + i_docCloseMethod, + new CloseEventType[] { CloseEventType.OnUnload, CloseEventType.NotifyClosing, CloseEventType.Disposing }, + m_observedCloseEvents.toArray( new CloseEventType[0] ) + ); + } + } + + @Test + public void testCloseWinEvents() throws Exception + { + impl_setupDocCloseTest(); + m_document.getCurrentView().dispatch( ".uno:CloseWin" ); + impl_tearDownDocCloseTest( "via .uno:CloseWin" ); + } + + //@Test + public void testCloseDocEvents() throws Exception + { + impl_setupDocCloseTest(); + m_document.getCurrentView().dispatch( ".uno:CloseDoc" ); + impl_tearDownDocCloseTest( "via .uno:CloseDoc" ); + } + + //@Test + public void testCloseByAPI() + { + impl_setupDocCloseTest(); + // closing the doc by API is synchronous, so do this in a separate thread, else we will get a deadlock + // when the document tries to call back our listener (well, I admit I didn't understand *why* we get this + // deadlock ... :-\ ) + (new DocCloser()).start(); + impl_tearDownDocCloseTest( "by API" ); + } + + private class DocumentEventListener implements XDocumentEventListener + { + + public void documentEventOccured( DocumentEvent i_documentEvent ) + { + if ( i_documentEvent.EventName.equals( "OnUnload" ) ) + { + synchronized( m_observedCloseEvents ) + { + m_observedCloseEvents.add( CloseEventType.OnUnload ); + } + } + } + + public void disposing(EventObject eo) + { + // not interested in + } + } + + private class CloseListener implements XCloseListener + { + + public void queryClosing(EventObject eo, boolean bln) throws CloseVetoException + { + // not interested in + } + + public void notifyClosing(EventObject eo) + { + synchronized( m_observedCloseEvents ) + { + m_observedCloseEvents.add( CloseEventType.NotifyClosing ); + } + } + + public void disposing(EventObject eo) + { + // not interested in + } + } + + private class DocDisposeListener implements XEventListener + { + public void disposing(EventObject eo) + { + synchronized( m_observedCloseEvents ) + { + m_observedCloseEvents.add( CloseEventType.Disposing ); + } + synchronized ( m_document ) + { + m_document.notifyAll(); + } + } + } + + private class DocCloser extends Thread + { + @Override + public void run() + { + try + { + final XCloseable docCloseable = UnoRuntime.queryInterface(XCloseable.class, m_document.getDocument()); + docCloseable.close(true); + } + catch (CloseVetoException ex) + { + Logger.getLogger(DocumentEvents.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + + private enum CloseEventType + { + OnUnload, + NotifyClosing, + Disposing + } + + private OfficeDocument m_document = null; + final private ArrayList< CloseEventType > m_observedCloseEvents = new ArrayList(); +} diff --git a/sfx2/qa/complex/sfx2/DocumentMetadataAccess.java b/sfx2/qa/complex/sfx2/DocumentMetadataAccess.java new file mode 100644 index 000000000..5697bba07 --- /dev/null +++ b/sfx2/qa/complex/sfx2/DocumentMetadataAccess.java @@ -0,0 +1,1228 @@ +/* + * 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.sfx2; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import helper.StreamSimulator; + +import lib.TestParameters; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openoffice.test.OfficeConnection; + +import com.sun.star.beans.Pair; +import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.StringPair; +import com.sun.star.beans.XPropertySet; +import com.sun.star.container.XEnumeration; +import com.sun.star.container.XEnumerationAccess; +import com.sun.star.frame.XStorable; +import com.sun.star.io.XInputStream; +import com.sun.star.lang.IllegalArgumentException; +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.rdf.BlankNode; +import com.sun.star.rdf.FileFormat; +import com.sun.star.rdf.Literal; +import com.sun.star.rdf.Statement; +import com.sun.star.rdf.URI; +import com.sun.star.rdf.URIs; +import com.sun.star.rdf.XBlankNode; +import com.sun.star.rdf.XDocumentMetadataAccess; +import com.sun.star.rdf.XDocumentRepository; +import com.sun.star.rdf.XLiteral; +import com.sun.star.rdf.XMetadatable; +import com.sun.star.rdf.XNamedGraph; +import com.sun.star.rdf.XNode; +import com.sun.star.rdf.XQuerySelectResult; +import com.sun.star.rdf.XRepository; +import com.sun.star.rdf.XRepositorySupplier; +import com.sun.star.rdf.XURI; +import com.sun.star.text.XText; +import com.sun.star.text.XTextDocument; +import com.sun.star.text.XTextRange; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import com.sun.star.util.XCloseable; +import complex.sfx2.tools.TestDocument; + +/** + * Test case for interface com.sun.star.rdf.XDocumentMetadataAccess + * Currently, this service is implemented in + * sfx2/source/doc/DocumentMetadataAccess.cxx + * + * Actually, this is not a service, so we need to create a document and + * go from there... + * + */ +public class DocumentMetadataAccess +{ + XMultiServiceFactory xMSF; + XComponentContext xContext; + String tempDir; + + String nsRDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; + String nsRDFS = "http://www.w3.org/2000/01/rdf-schema#"; + String nsPkg="http://docs.oasis-open.org/opendocument/meta/package/common#"; + String nsODF ="http://docs.oasis-open.org/opendocument/meta/package/odf#"; + + XURI foo; + XURI bar; + XURI baz; + + static XURI rdf_type; + static XURI rdfs_label; + static XURI pkg_Document; + static XURI pkg_hasPart; + static XURI pkg_MetadataFile; + static XURI odf_ContentFile; + static XURI odf_StylesFile; + static XURI odf_Element; + static XBlankNode blank1; + static XBlankNode blank2; + static XBlankNode blank3; + static XBlankNode blank4; + static String manifestPath = "manifest.rdf"; + static String contentPath = "content.xml"; + static String stylesPath = "styles.xml"; + static String fooPath = "foo.rdf"; + static String fooBarPath = "meta/foo/bar.rdf"; + + XRepository xRep; + XRepositorySupplier xRS; + XDocumentMetadataAccess xDMA; + + /** + * The test parameters + */ + private static TestParameters param = null; + + @Before public void before() throws Exception + { + xMSF = getMSF(); + param = new TestParameters(); + param.put("ServiceFactory", xMSF); // important for param.getMSF() + + assertNotNull("could not create MultiServiceFactory.", xMSF); + XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xMSF); + Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext"); + xContext = UnoRuntime.queryInterface(XComponentContext.class, defaultCtx); + assertNotNull("could not get component context.", xContext); + + tempDir = util.utils.getOfficeTemp/*Dir*/(xMSF); + System.out.println("tempdir: " + tempDir); + + foo = URI.create(xContext, "uri:foo"); + assertNotNull("foo", foo); + bar = URI.create(xContext, "uri:bar"); + assertNotNull("bar", bar); + baz = URI.create(xContext, "uri:baz"); + assertNotNull("baz", baz); + + blank1 = BlankNode.create(xContext, "_:1"); + assertNotNull("blank1", blank1); + blank2 = BlankNode.create(xContext, "_:2"); + assertNotNull("blank2", blank2); + blank3 = BlankNode.create(xContext, "_:3"); + assertNotNull("blank3", blank3); + blank4 = BlankNode.create(xContext, "_:4"); + assertNotNull("blank4", blank4); + rdf_type = URI.createKnown(xContext, URIs.RDF_TYPE); + assertNotNull("rdf_type", rdf_type); + rdfs_label = URI.createKnown(xContext, URIs.RDFS_LABEL); + assertNotNull("rdfs_label", rdfs_label); + pkg_Document = URI.createKnown(xContext, URIs.PKG_DOCUMENT); + assertNotNull("pkg_Document", pkg_Document); + pkg_hasPart = URI.createKnown(xContext, URIs.PKG_HASPART); + assertNotNull("pkg_hasPart", pkg_hasPart); + pkg_MetadataFile = URI.createKnown(xContext, URIs.PKG_METADATAFILE); + assertNotNull("pkg_MetadataFile", pkg_MetadataFile); + odf_ContentFile = URI.createKnown(xContext, URIs.ODF_CONTENTFILE); + assertNotNull("odf_ContentFile", odf_ContentFile); + odf_StylesFile = URI.createKnown(xContext, URIs.ODF_STYLESFILE); + assertNotNull("odf_StylesFile", odf_StylesFile); + odf_Element = URI.createKnown(xContext, URIs.ODF_ELEMENT); + assertNotNull("odf_Element", odf_Element); + } + + @After public void after() + { + xRep = null; + xRS = null; + xDMA = null; + } + + @Test public void check() throws Exception + { + XComponent xComp = null; + XComponent xComp2 = null; + try { + XEnumeration xStmtsEnum; + XNamedGraph xManifest; + + System.out.println("Creating document with Repository..."); + + // we cannot create a XDMA directly, we must create + // a document and get it from there :( + // create document + PropertyValue[] loadProps = new PropertyValue[1]; + loadProps[0] = new PropertyValue(); + loadProps[0].Name = "Hidden"; + loadProps[0].Value = true; + xComp = util.DesktopTools.openNewDoc(xMSF, "swriter", loadProps); + XTextDocument xText = UnoRuntime.queryInterface(XTextDocument.class, xComp); + + XRepositorySupplier xRepoSupplier = UnoRuntime.queryInterface(XRepositorySupplier.class, xComp); + assertNotNull("xRS null", xRepoSupplier); + XDocumentMetadataAccess xDocMDAccess = UnoRuntime.queryInterface(XDocumentMetadataAccess.class, xRepoSupplier); + assertNotNull("xDMA null", xDocMDAccess); + xRep = xRepoSupplier.getRDFRepository(); + assertNotNull("xRep null", xRep); + + System.out.println("...done"); + + System.out.println("Checking that new repository is initialized..."); + + XURI xBaseURI = xDocMDAccess; + String baseURI = xBaseURI.getStringValue(); + assertNotNull("new: baseURI", xBaseURI ); + assertTrue("new: baseURI", !xBaseURI.getStringValue().equals("")); + + assertTrue("new: # graphs", 1 == xRep.getGraphNames().length); + XURI manifest = URI.createNS(xContext, xBaseURI.getStringValue(), + manifestPath); + xManifest = xRep.getGraph(manifest); + assertTrue("new: manifest graph", null != xManifest); + + Statement[] manifestStmts = getManifestStmts(xBaseURI); + xStmtsEnum = xRep.getStatements(null, null, null); + assertTrue("new: manifest graph", eq(xStmtsEnum, manifestStmts)); + + System.out.println("...done"); + + System.out.println("Checking some invalid args..."); + + String content = "behold, for I am the content."; + new TestRange(content); + + try { + xDocMDAccess.getElementByURI(null); + fail("getElementByURI: null allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.getMetadataGraphsWithType(null); + fail("getMetadataGraphsWithType: null URI allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addMetadataFile("", new XURI[0]); + fail("addMetadataFile: empty filename allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addMetadataFile("/foo", new XURI[0]); + fail("addMetadataFile: absolute filename allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addMetadataFile("fo\"o", new XURI[0]); + fail("addMetadataFile: invalid filename allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addMetadataFile("../foo", new XURI[0]); + fail("addMetadataFile: filename with .. allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addMetadataFile("foo/../../bar", new XURI[0]); + fail("addMetadataFile: filename with nest .. allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addMetadataFile("foo/././bar", new XURI[0]); + fail("addMetadataFile: filename with nest . allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addMetadataFile("content.xml", new XURI[0]); + fail("addMetadataFile: content.xml allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addMetadataFile("styles.xml", new XURI[0]); + fail("addMetadataFile: styles.xml allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addMetadataFile("meta.xml", new XURI[0]); + fail("addMetadataFile: meta.xml allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addMetadataFile("settings.xml", new XURI[0]); + fail("addMetadataFile: settings.xml allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, null, "foo", + foo, new XURI[0]); + fail("importMetadataFile: null stream allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + + final String sEmptyRDF = TestDocument.getUrl("empty.rdf"); + try { + XInputStream xFooIn = new StreamSimulator(sEmptyRDF, true, param); + xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, xFooIn, "", + foo, new XURI[0]); + fail("importMetadataFile: empty filename allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + XInputStream xFooIn = + new StreamSimulator(sEmptyRDF, true, param); + xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, xFooIn, "meta.xml", + foo, new XURI[0]); + fail("importMetadataFile: meta.xml filename allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + XInputStream xFooIn = + new StreamSimulator(sEmptyRDF, true, param); + xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, + xFooIn, "foo", null, new XURI[0]); + fail("importMetadataFile: null base URI allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + XInputStream xFooIn = + new StreamSimulator(sEmptyRDF, true, param); + xDocMDAccess.importMetadataFile(FileFormat.RDF_XML, + xFooIn, "foo", rdf_type, new XURI[0]); + fail("importMetadataFile: non-absolute base URI allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.removeMetadataFile(null); + fail("removeMetadataFile: null URI allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addContentOrStylesFile(""); + fail("addContentOrStylesFile: empty filename allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addContentOrStylesFile("/content.xml"); + fail("addContentOrStylesFile: absolute filename allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.addContentOrStylesFile("foo.rdf"); + fail("addContentOrStylesFile: invalid filename allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.removeContentOrStylesFile(""); + fail("removeContentOrStylesFile: empty filename allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.loadMetadataFromStorage(null, foo, null); + fail("loadMetadataFromStorage: null storage allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.storeMetadataToStorage(null/*, base*/); + fail("storeMetadataToStorage: null storage allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.loadMetadataFromMedium(new PropertyValue[0]); + fail("loadMetadataFromMedium: empty medium allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + try { + xDocMDAccess.storeMetadataToMedium(new PropertyValue[0]); + fail("storeMetadataToMedium: empty medium allowed"); + } catch (IllegalArgumentException e) { + // ignore + } + + System.out.println("...done"); + + System.out.println("Checking file addition/removal..."); + + xDocMDAccess.removeContentOrStylesFile(contentPath); + xStmtsEnum = xManifest.getStatements(null, null, null); + assertTrue("removeContentOrStylesFile (content)", + eq(xStmtsEnum, new Statement[] { + manifestStmts[0], manifestStmts[2], manifestStmts[4] + })); + + xDocMDAccess.addContentOrStylesFile(contentPath); + xStmtsEnum = xManifest.getStatements(null, null, null); + assertTrue("addContentOrStylesFile (content)", + eq(xStmtsEnum, manifestStmts)); + + xDocMDAccess.removeContentOrStylesFile(stylesPath); + xStmtsEnum = xManifest.getStatements(null, null, null); + assertTrue("removeContentOrStylesFile (styles)", + eq(xStmtsEnum, new Statement[] { + manifestStmts[0], manifestStmts[1], manifestStmts[3] + })); + + xDocMDAccess.addContentOrStylesFile(stylesPath); + xStmtsEnum = xManifest.getStatements(null, null, null); + assertTrue("addContentOrStylesFile (styles)", + eq(xStmtsEnum, manifestStmts)); + + XURI xFoo = URI.createNS(xContext, xBaseURI.getStringValue(), + fooPath); + Statement xM_BaseHaspartFoo = + new Statement(xBaseURI, pkg_hasPart, xFoo, manifest); + Statement xM_FooTypeMetadata = + new Statement(xFoo, rdf_type, pkg_MetadataFile, manifest); + Statement xM_FooTypeBar = + new Statement(xFoo, rdf_type, bar, manifest); + xDocMDAccess.addMetadataFile(fooPath, new XURI[] { bar }); + xStmtsEnum = xManifest.getStatements(null, null, null); + assertTrue("addMetadataFile", + eq(xStmtsEnum, merge(manifestStmts, new Statement[] { + xM_BaseHaspartFoo, xM_FooTypeMetadata, xM_FooTypeBar + }))); + + XURI[] graphsBar = xDocMDAccess.getMetadataGraphsWithType(bar); + assertTrue("getMetadataGraphsWithType", + graphsBar.length == 1 && eq(graphsBar[0], xFoo)); + + + xDocMDAccess.removeMetadataFile(xFoo); + xStmtsEnum = xManifest.getStatements(null, null, null); + assertTrue("removeMetadataFile", + eq(xStmtsEnum, manifestStmts)); + + System.out.println("...done"); + + System.out.println("Checking mapping..."); + + XEnumerationAccess xTextEnum = UnoRuntime.queryInterface(XEnumerationAccess.class, xText.getText()); + Object o = xTextEnum.createEnumeration().nextElement(); + XMetadatable xMeta1 = UnoRuntime.queryInterface(XMetadatable.class, o); + + XMetadatable xMeta; + xMeta = xDocMDAccess.getElementByURI(xMeta1); + assertTrue("getElementByURI: null", null != xMeta); + String XmlId = xMeta.getMetadataReference().Second; + String XmlId1 = xMeta1.getMetadataReference().Second; + assertTrue("getElementByURI: no xml id", !XmlId.equals("")); + assertTrue("getElementByURI: different xml id", XmlId.equals(XmlId1)); + + System.out.println("...done"); + + System.out.println("Checking storing and loading..."); + + XURI xFoobar = URI.createNS(xContext, xBaseURI.getStringValue(), + fooBarPath); + Statement[] metadataStmts = getMetadataFileStmts(xBaseURI, + fooBarPath); + xDocMDAccess.addMetadataFile(fooBarPath, new XURI[0]); + xStmtsEnum = xRep.getStatements(null, null, null); + assertTrue("addMetadataFile", + eq(xStmtsEnum, merge(manifestStmts, metadataStmts ))); + + Statement xFoobar_FooBarFoo = + new Statement(foo, bar, foo, xFoobar); + xRep.getGraph(xFoobar).addStatement(foo, bar, foo); + xStmtsEnum = xRep.getStatements(null, null, null); + assertTrue("addStatement", + eq(xStmtsEnum, merge(manifestStmts, merge(metadataStmts, + new Statement[] { xFoobar_FooBarFoo })))); + + PropertyValue noMDNoContentFile = new PropertyValue(); + noMDNoContentFile.Name = "URL"; + noMDNoContentFile.Value = TestDocument.getUrl("CUSTOM.odt"); + PropertyValue noMDFile = new PropertyValue(); + noMDFile.Name = "URL"; + noMDFile.Value = TestDocument.getUrl("TEST.odt"); + PropertyValue file = new PropertyValue(); + file.Name = "URL"; + file.Value = tempDir + "TESTDMA.odt"; + PropertyValue mimetype = new PropertyValue(); + mimetype.Name = "MediaType"; + mimetype.Value = "application/vnd.oasis.opendocument.text"; + PropertyValue[] argsEmptyNoContent = { mimetype, noMDNoContentFile}; + PropertyValue[] argsEmpty = { mimetype, noMDFile }; + PropertyValue[] args = { mimetype, file }; + + xStmtsEnum = xRep.getStatements(null, null, null); + XURI[] graphs = xRep.getGraphNames(); + + xDocMDAccess.storeMetadataToMedium(args); + + // this should re-init + xDocMDAccess.loadMetadataFromMedium(argsEmptyNoContent); + xRep = xRepoSupplier.getRDFRepository(); + assertTrue("xRep null", null != xRep); + assertTrue("baseURI still tdoc?", + !baseURI.equals(xDocMDAccess.getStringValue())); + Statement[] manifestStmts2 = getManifestStmts(xDocMDAccess); + xStmtsEnum = xRep.getStatements(null, null, null); + // there is no content or styles file in here, so we have just + // the package stmt + assertTrue("loadMetadataFromMedium (no metadata, no content)", + eq(xStmtsEnum, new Statement[] { manifestStmts2[0] })); + + // this should re-init + xDocMDAccess.loadMetadataFromMedium(argsEmpty); + xRep = xRepoSupplier.getRDFRepository(); + assertTrue("xRep null", null != xRep); + assertTrue("baseURI still tdoc?", + !baseURI.equals(xDocMDAccess.getStringValue())); + Statement[] manifestStmts3 = getManifestStmts(xDocMDAccess); + + xStmtsEnum = xRep.getStatements(null, null, null); + assertTrue("loadMetadataFromMedium (no metadata)", + eq(xStmtsEnum, manifestStmts3)); + + xDocMDAccess.loadMetadataFromMedium(args); + xRep = xRepoSupplier.getRDFRepository(); + assertTrue("xRep null", null != xRep); + Statement[] manifestStmts4 = getManifestStmts(xDocMDAccess); + Statement[] metadataStmts4 = getMetadataFileStmts(xDocMDAccess, + fooBarPath); + + xStmtsEnum = xRep.getStatements(null, null, null); + assertTrue("some graph(s) not reloaded", + graphs.length == xRep.getGraphNames().length); + + XURI xFoobar4 = URI.createNS(xContext, xDocMDAccess.getStringValue(), + fooBarPath); + Statement xFoobar_FooBarFoo4 = + new Statement(foo, bar, foo, xFoobar4); + assertTrue("loadMetadataFromMedium (re-load)", + eq(xStmtsEnum, merge(manifestStmts4, merge(metadataStmts4, + new Statement[] { xFoobar_FooBarFoo4 })))); + + System.out.println("...done"); + + System.out.println("Checking storing and loading via model..."); + + String f = tempDir + "TESTPARA.odt"; + + XStorable xStor = UnoRuntime.queryInterface(XStorable.class, xRepoSupplier); + + xStor.storeToURL(f, new PropertyValue[0]); + + xComp2 = util.DesktopTools.loadDoc(xMSF, f, loadProps); + + XDocumentMetadataAccess xDMA2 = UnoRuntime.queryInterface(XDocumentMetadataAccess.class, xComp2); + assertTrue("xDMA2 null", null != xDMA2); + + XRepositorySupplier xRS2 = UnoRuntime.queryInterface(XRepositorySupplier.class, xComp2); + assertTrue("xRS2 null", null != xRS2); + + XRepository xRep2 = xRS2.getRDFRepository(); + assertTrue("xRep2 null", null != xRep2); + + Statement[] manifestStmts5 = getManifestStmts(xDMA2); + Statement[] metadataStmts5 = getMetadataFileStmts(xDMA2, + fooBarPath); + XURI xFoobar5 = URI.createNS(xContext, xDMA2.getStringValue(), + fooBarPath); + Statement xFoobar_FooBarFoo5 = + new Statement(foo, bar, foo, xFoobar5); + xStmtsEnum = xRep.getStatements(null, null, null); + XEnumeration xStmtsEnum2 = xRep2.getStatements(null, null, null); + assertTrue("load: repository differs", + eq(xStmtsEnum2, merge(manifestStmts5, merge(metadataStmts5, + new Statement[] { xFoobar_FooBarFoo5 })))); + + System.out.println("...done"); + + } finally { + close(xComp); + close(xComp2); + } + } + + @Test public void checkRDFa() throws Exception + { + XComponent xComp = null; + try { + final String file = TestDocument.getUrl("TESTRDFA.odt"); + xComp = loadRDFa(file); + if (xComp != null) + { + final String sNewFile = tempDir + "TESTRDFA.odt"; + storeRDFa(xComp, sNewFile); + close(xComp); + + xComp = loadRDFa(sNewFile); + } + } finally { + close(xComp); + } + } + + @Test + public void checkTdf123293() throws Exception + { + XComponent xComp = null; + try { + xComp = util.DesktopTools.loadDocUsingStream(xMSF, TestDocument.getPath("TESTRDFA.odt")); + + // Metadata was discarded when loading from stream, make sure it's there now + XRepositorySupplier xRepoSupplier = UnoRuntime.queryInterface(XRepositorySupplier.class, xComp); + assertNotNull("No metadata loaded", xRepoSupplier); + } finally { + close(xComp); + } + } + + private void storeRDFa(XComponent xComp, String file) throws com.sun.star.io.IOException + { + System.out.println("Storing test document..."); + + XStorable xStor = UnoRuntime.queryInterface(XStorable.class, xComp); + + xStor.storeToURL(file, new PropertyValue[0]); + + System.out.println("...done"); + } + + private XComponent loadRDFa(String file) throws Exception + { + XComponent xComp = null; + + System.out.println("Loading test document..."); + + PropertyValue[] loadProps = new PropertyValue[1]; + loadProps[0] = new PropertyValue(); + loadProps[0].Name = "Hidden"; + loadProps[0].Value = true; + + + + xComp = util.DesktopTools.loadDoc(xMSF, file, loadProps); + + XRepositorySupplier xRepoSupplier = UnoRuntime.queryInterface(XRepositorySupplier.class, xComp); + assertTrue("xRS null", null != xRepoSupplier); + + XDocumentRepository xDocRepository = UnoRuntime.queryInterface(XDocumentRepository.class, xRepoSupplier.getRDFRepository()); + assertTrue("xRep null", null != xDocRepository); + + XTextDocument xTextDoc = UnoRuntime.queryInterface(XTextDocument.class, xComp); + + XText xText = xTextDoc.getText(); + + XEnumerationAccess xEA = UnoRuntime.queryInterface(XEnumerationAccess.class, xText); + XEnumeration xEnum = xEA.createEnumeration(); + + System.out.println("...done"); + + System.out.println("Checking RDFa in loaded test document..."); + + XMetadatable xPara; + Pair result; + + Statement x_FooBarLit1 = new Statement(foo, bar, mkLit("1"), null); + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 1", + !result.Second && + eq(result.First, new Statement[] { + x_FooBarLit1 + })); + + Statement x_FooBarLit2 = new Statement(foo, bar, mkLit("2"), null); + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 2", + !result.Second && + eq(result.First, new Statement[] { + x_FooBarLit2 + })); + + Statement x_BlankBarLit3 = + new Statement(blank1, bar, mkLit("3"), null); + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 3", + !result.Second && + eq(result.First, new Statement[] { + x_BlankBarLit3 + })); + XBlankNode b3 = UnoRuntime.queryInterface(XBlankNode.class, result.First[0].Subject); + + Statement x_BlankBarLit4 = + new Statement(blank2, bar, mkLit("4"), null); + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 4", + !result.Second && + eq(result.First, new Statement[] { + x_BlankBarLit4 + })); + XBlankNode b4 = UnoRuntime.queryInterface(XBlankNode.class, result.First[0].Subject); + + Statement x_BlankBarLit5 = + new Statement(blank1, bar, mkLit("5"), null); + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 5", + !result.Second && + eq(result.First, new Statement[] { + x_BlankBarLit5 + })); + XBlankNode b5 = UnoRuntime.queryInterface(XBlankNode.class, result.First[0].Subject); + + assertTrue("RDFa: 3 != 4", + !b3.getStringValue().equals(b4.getStringValue())); + assertTrue("RDFa: 3 == 5", + b3.getStringValue().equals(b5.getStringValue())); + + Statement x_FooBarLit6 = new Statement(foo, bar, mkLit("6"), null); + Statement x_FooBazLit6 = new Statement(foo, baz, mkLit("6"), null); + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 6", + !result.Second && + eq(result.First, new Statement[] { + x_FooBarLit6, x_FooBazLit6 + })); + + Statement x_FooBarLit7 = new Statement(foo, bar, mkLit("7"), null); + Statement x_FooBazLit7 = new Statement(foo, baz, mkLit("7"), null); + Statement x_FooFooLit7 = new Statement(foo, foo, mkLit("7"), null); + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 7", + !result.Second && + eq(result.First, new Statement[] { + x_FooBarLit7, x_FooBazLit7, x_FooFooLit7 + })); + + XNode lit = mkLit("a fooish bar"); + XNode lit_type= mkLit("a fooish bar", bar); + Statement x_FooBarLit = new Statement(foo, bar, lit, null); + Statement x_FooBarLittype = new Statement(foo, bar, lit_type, null); + + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 8", + result.Second && + eq(result.First, new Statement[] { + x_FooBarLit + })); + + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 9", + result.Second && + eq(result.First, new Statement[] { + x_FooBarLit + })); + + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 10", + result.Second && + eq(result.First, new Statement[] { + x_FooBarLittype + })); + + Statement x_FooBarLit11 + = new Statement(foo, bar, mkLit("11", bar), null); + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 11", + !result.Second && + eq(result.First, new Statement[] { + x_FooBarLit11 + })); + + XURI xFile = URI.createNS(xContext, file, "/" + contentPath); + Statement x_FileBarLit12 = + new Statement(xFile, bar, mkLit("12"), null); + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 12", + !result.Second && + eq(result.First, new Statement[] { + x_FileBarLit12 + })); + + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 13", + result.Second && + eq(result.First, new Statement[] { + x_FooBarLit + })); + + new Statement(foo, rdfs_label, mkLit("14"), null); + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 14", + result.Second && + eq(result.First, new Statement[] { + /* x_FooLabelLit14 */ x_FooBarLit + })); + + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 15", eq(result.First, new Statement[] { } )); + + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 16", eq(result.First, new Statement[] { } )); + + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 17", eq(result.First, new Statement[] { } )); + + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 18", eq(result.First, new Statement[] { } )); + + xPara = UnoRuntime.queryInterface(XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 19", eq(result.First, new Statement[] { } )); + + xPara = UnoRuntime.queryInterface( + XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 20", eq(result.First, new Statement[] { } )); + + xPara = UnoRuntime.queryInterface( + XMetadatable.class, xEnum.nextElement()); + result = xDocRepository.getStatementRDFa(xPara); + assertTrue("RDFa: 21", eq(result.First, new Statement[] { } )); + + System.out.println("...done"); + + return xComp; + } + + +// utilities ------------------------------------------------------------- + + static void close(XComponent i_comp) + { + try { + XCloseable xClos = UnoRuntime.queryInterface(XCloseable.class, i_comp); + if (xClos != null) + { + xClos.close(true); + } + } catch (Exception e) { + } + } + + XLiteral mkLit(String i_content) + { + return Literal.create(xContext, i_content); + } + + XLiteral mkLit(String i_content, XURI i_uri) + { + return Literal.createWithType(xContext, i_content, i_uri); + } + + static Statement[] merge(Statement[] i_A1, Statement[] i_A2) + { + // bah, java sucks... + Statement[] ret = new Statement[i_A1.length + i_A2.length]; + for (int i = 0; i < i_A1.length; ++i) { + ret[i] = i_A1[i]; + } + for (int i = 0; i < i_A2.length; ++i) { + ret[i+i_A1.length] = i_A2[i]; + } + return ret; + } + + public static String toS(XNode n) { + if (null == n) + { + return "< null >"; + } + return n.getStringValue(); + } + + static boolean isBlank(XNode i_node) + { + XBlankNode blank = UnoRuntime.queryInterface(XBlankNode.class, i_node); + return blank != null; + } + + + static Statement[] toSeq(XEnumeration i_Enum) throws Exception + { + java.util.Collection c = new java.util.ArrayList(); + while (i_Enum.hasMoreElements()) { + Statement s = (Statement) i_Enum.nextElement(); + c.add(s); + } + // java sucks + Object[] arr = c.toArray(); + Statement[] ret = new Statement[arr.length]; + for (int i = 0; i < arr.length; ++i) { + ret[i] = (Statement) arr[i]; + } + return ret; + } + + static XNode[][] toSeqs(XEnumeration i_Enum) throws Exception + { + java.util.Collection c = new java.util.ArrayList(); + while (i_Enum.hasMoreElements()) { + XNode[] s = (XNode[]) i_Enum.nextElement(); + c.add(s); + } + Object[] arr = c.toArray(); + XNode[][] ret = new XNode[arr.length][]; + for (int i = 0; i < arr.length; ++i) { + ret[i] = (XNode[]) arr[i]; + } + return ret; + } + + private static class BindingComp implements java.util.Comparator + { + public int compare(XNode[] left, XNode[] right) + { + if (left.length != right.length) + { + throw new RuntimeException(); + } + for (int i = 0; i < left.length; ++i) { + int eq = (left[i].getStringValue().compareTo( + right[i].getStringValue())); + if (eq != 0) + { + return eq; + } + } + return 0; + } + } + + private static class StmtComp implements java.util.Comparator + { + public int compare(Statement left, Statement right) + { + int eq; + if ((eq = cmp(left.Graph, right.Graph )) != 0) return eq; + if ((eq = cmp(left.Subject, right.Subject )) != 0) return eq; + if ((eq = cmp(left.Predicate, right.Predicate)) != 0) return eq; + if ((eq = cmp(left.Object, right.Object )) != 0) return eq; + return 0; + } + + private int cmp(XNode i_Left, XNode i_Right) + { + if (isBlank(i_Left)) { + return isBlank(i_Right) ? 0 : 1; + } else { + if (isBlank(i_Right)) { + return -1; + } else { + return toS(i_Left).compareTo(toS(i_Right)); + } + } + } + } + + static boolean eq(Statement i_Left, Statement i_Right) + { + XURI lG = i_Left.Graph; + XURI rG = i_Right.Graph; + if (!eq(lG, rG)) { + System.out.println("Graphs differ: " + toS(lG) + " != " + toS(rG)); + return false; + } + if (!eq(i_Left.Subject, i_Right.Subject)) { + System.out.println("Subjects differ: " + + i_Left.Subject.getStringValue() + " != " + + i_Right.Subject.getStringValue()); + return false; + } + if (!eq(i_Left.Predicate, i_Right.Predicate)) { + System.out.println("Predicates differ: " + + i_Left.Predicate.getStringValue() + " != " + + i_Right.Predicate.getStringValue()); + return false; + } + if (!eq(i_Left.Object, i_Right.Object)) { + System.out.println("Objects differ: " + + i_Left.Object.getStringValue() + " != " + + i_Right.Object.getStringValue()); + return false; + } + return true; + } + + static boolean eq(Statement[] i_Result, Statement[] i_Expected) + { + if (i_Result.length != i_Expected.length) { + System.out.println("eq: different lengths: " + i_Result.length + " " + + i_Expected.length); + return false; + } + Statement[] expected = i_Expected.clone(); + java.util.Arrays.sort(i_Result, new StmtComp()); + java.util.Arrays.sort(expected, new StmtComp()); + for (int i = 0; i < expected.length; ++i) + { + // This is better for debug! + final Statement a = i_Result[i]; + final Statement b = expected[i]; + final boolean cond = eq(a, b); + if (!cond) return false; + } + return true; + } + + static boolean eq(XEnumeration i_Enum, Statement[] i_Expected) + throws Exception + { + Statement[] current = toSeq(i_Enum); + return eq(current, i_Expected); + } + + static boolean eq(XNode i_Left, XNode i_Right) + { + if (i_Left == null) { + return (i_Right == null); + } else { + return (i_Right != null) && + (i_Left.getStringValue().equals(i_Right.getStringValue()) + // FIXME: hack: blank nodes considered equal + || (isBlank(i_Left) && isBlank(i_Right))); + } + } + + static boolean eq(XQuerySelectResult i_Result, + String[] i_Vars, XNode[][] i_Bindings) throws Exception + { + String[] vars = i_Result.getBindingNames(); + XEnumeration iter = i_Result; + XNode[][] bindings = toSeqs(iter); + if (vars.length != i_Vars.length) { + System.out.println("var lengths differ"); + return false; + } + if (bindings.length != i_Bindings.length) { + System.out.println("binding lengths differ: " + i_Bindings.length + + " vs " + bindings.length ); + return false; + } + java.util.Arrays.sort(bindings, new BindingComp()); + java.util.Arrays.sort(i_Bindings, new BindingComp()); + for (int i = 0; i < i_Bindings.length; ++i) { + if (i_Bindings[i].length != i_Vars.length) { + System.out.println("TEST ERROR!"); + throw new Exception(); + } + if (bindings[i].length != i_Vars.length) { + System.out.println("binding length and var length differ"); + return false; + } + for (int j = 0; j < i_Vars.length; ++j) { + if (!eq(bindings[i][j], i_Bindings[i][j])) { + System.out.println("bindings differ: " + + toS(bindings[i][j]) + " != " + toS(i_Bindings[i][j])); + return false; + } + } + } + for (int i = 0; i < i_Vars.length; ++i) { + if (!vars[i].equals(i_Vars[i])) { + System.out.println("variable names differ: " + + vars[i] + " != " + i_Vars[i]); + return false; + } + } + return true; + } + + static boolean eq(StringPair i_Left, StringPair i_Right) + { + return ((i_Left.First).equals(i_Right.First)) && + ((i_Left.Second).equals(i_Right.Second)); + } + + static String mkNamespace(String i_prefix, String i_namespace) + { + return "PREFIX " + i_prefix + ": <" + i_namespace + ">\n"; + } + + static String mkNss() + { + String namespaces = mkNamespace("rdf", + "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); + namespaces += mkNamespace("pkg", + "http://docs.oasis-open.org/opendocument/meta/package/common#"); + namespaces += mkNamespace("odf", + "http://docs.oasis-open.org/opendocument/meta/package/odf#"); + return namespaces; + } + + Statement[] getManifestStmts(XURI xBaseURI) throws Exception + { + XURI xManifest = URI.createNS(xContext, xBaseURI.getStringValue(), + manifestPath); + XURI xContent = URI.createNS(xContext, xBaseURI.getStringValue(), + contentPath); + XURI xStyles = URI.createNS(xContext, xBaseURI.getStringValue(), + stylesPath); + Statement xM_BaseTypeDoc = + new Statement(xBaseURI, rdf_type, pkg_Document, xManifest); + Statement xM_BaseHaspartContent = + new Statement(xBaseURI, pkg_hasPart, xContent, xManifest); + Statement xM_BaseHaspartStyles = + new Statement(xBaseURI, pkg_hasPart, xStyles, xManifest); + Statement xM_ContentTypeContent = + new Statement(xContent, rdf_type, odf_ContentFile, xManifest); + Statement xM_StylesTypeStyles = + new Statement(xStyles, rdf_type, odf_StylesFile, xManifest); + return new Statement[] { + xM_BaseTypeDoc, xM_BaseHaspartContent, xM_BaseHaspartStyles, + xM_ContentTypeContent, xM_StylesTypeStyles + }; + } + + Statement[] getMetadataFileStmts(XURI xBaseURI, String Path) + throws Exception + { + XURI xManifest = URI.createNS(xContext, xBaseURI.getStringValue(), + manifestPath); + XURI xGraph = URI.createNS(xContext, xBaseURI.getStringValue(), Path); + Statement xM_BaseHaspartGraph = + new Statement(xBaseURI, pkg_hasPart, xGraph, xManifest); + Statement xM_GraphTypeMetadata = + new Statement(xGraph, rdf_type, pkg_MetadataFile, xManifest); + return new Statement[] { xM_BaseHaspartGraph, xM_GraphTypeMetadata }; + } + + class TestRange implements XTextRange, XMetadatable, XServiceInfo + { + String m_Stream; + String m_XmlId; + String m_Text; + TestRange(String i_Str) { m_Text = i_Str; } + + public String getStringValue() { return ""; } + public String getNamespace() { return ""; } + public String getLocalName() { return ""; } + + public StringPair getMetadataReference() + { + return new StringPair(m_Stream, m_XmlId); + } + public void setMetadataReference(StringPair i_Ref) + throws IllegalArgumentException + { + m_Stream = i_Ref.First; + m_XmlId = i_Ref.Second; + } + public void ensureMetadataReference() + { + m_Stream = "content.xml"; + m_XmlId = "42"; + } + + public String getImplementationName() { return null; } + public String[] getSupportedServiceNames() { return null; } + public boolean supportsService(String i_Svc) + { + return i_Svc.equals("com.sun.star.text.Paragraph"); + } + + public XText getText() { return null; } + public XTextRange getStart() { return null; } + public XTextRange getEnd() { return null; } + public String getString() { return m_Text; } + public void setString(String i_Str) { m_Text = i_Str; } + } + + + + private XMultiServiceFactory getMSF() + { + return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); + } + + // setup and close connections + @BeforeClass public static void setUpConnection() throws Exception { + System.out.println( "------------------------------------------------------------" ); + System.out.println( "starting class: " + DocumentMetadataAccess.class.getName() ); + System.out.println( "------------------------------------------------------------" ); + connection.setUp(); + } + + @AfterClass public static void tearDownConnection() + throws InterruptedException, com.sun.star.uno.Exception + { + System.out.println( "------------------------------------------------------------" ); + System.out.println( "finishing class: " + DocumentMetadataAccess.class.getName() ); + System.out.println( "------------------------------------------------------------" ); + connection.tearDown(); + } + + private static final OfficeConnection connection = new OfficeConnection(); + +} + diff --git a/sfx2/qa/complex/sfx2/DocumentProperties.java b/sfx2/qa/complex/sfx2/DocumentProperties.java new file mode 100644 index 000000000..c5f84b1b7 --- /dev/null +++ b/sfx2/qa/complex/sfx2/DocumentProperties.java @@ -0,0 +1,516 @@ +/* + * 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.sfx2; + + +import complex.sfx2.tools.TestDocument; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import com.sun.star.lang.XInitialization; + +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.Locale; +import com.sun.star.lang.EventObject; +import com.sun.star.util.Date; +import com.sun.star.util.DateTime; +import com.sun.star.util.Time; +import com.sun.star.util.Duration; +import com.sun.star.util.XModifyListener; +import com.sun.star.util.XModifyBroadcaster; +import com.sun.star.beans.XPropertyContainer; +import com.sun.star.beans.XPropertySet; +import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.NamedValue; +import com.sun.star.beans.PropertyAttribute; +import com.sun.star.beans.UnknownPropertyException; +import com.sun.star.beans.IllegalTypeException; + +import com.sun.star.document.XDocumentProperties; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openoffice.test.OfficeConnection; +import static org.junit.Assert.*; + +/** + * Test case for the service com.sun.star.document.DocumentProperties. + * Currently, this service is implemented in + * sfx2/source/doc/SfxDocumentMetaData.cxx. + * + */ +public class DocumentProperties +{ + @After public void cleanup() { + // nothing to do + } + + // for testing modifications + private class Listener implements XModifyListener { + private boolean m_Called; + + Listener() { + m_Called = false; + } + + private boolean reset() { + boolean oldCalled = m_Called; + m_Called = false; + return oldCalled; + } + + public void modified(EventObject e) { + m_Called = true; + } + + public void disposing(EventObject e) { + } + } + + @Test public void check() throws Exception + { + XMultiServiceFactory xMSF = getMSF(); + assertNotNull("could not create MultiServiceFactory.", xMSF); + XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xMSF); + Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext"); + XComponentContext xContext = UnoRuntime.queryInterface(XComponentContext.class, defaultCtx); + assertNotNull("could not get component context.", xContext); + + // TODO: Path to temp + String temp = util.utils.getOfficeTemp/*Dir*/(xMSF); + System.out.println("tempdir: " + temp); + + PropertyValue[] noArgs = { }; + PropertyValue mimetype = new PropertyValue(); + mimetype.Name = "MediaType"; + mimetype.Value = "application/vnd.oasis.opendocument.text"; + PropertyValue[] mimeArgs = { mimetype }; + PropertyValue cfile = new PropertyValue(); + cfile.Name = "URL"; + cfile.Value = temp + "EMPTY.odt"; + PropertyValue[] mimeEmptyArgs = { mimetype, cfile }; + + System.out.println("Creating service DocumentProperties..."); + + Object oDP = + xMSF.createInstance("com.sun.star.document.DocumentProperties"); + XDocumentProperties xDP = UnoRuntime.queryInterface(XDocumentProperties.class, oDP); + + System.out.println("...done"); + + + System.out.println("Checking initialize ..."); + + XDocumentProperties xDP2 = UnoRuntime.queryInterface(XDocumentProperties.class, xMSF.createInstance("com.sun.star.document.DocumentProperties")); + XInitialization xInit = UnoRuntime.queryInterface(XInitialization.class, xDP2); + xInit.initialize(new Object[] { }); + + System.out.println("...done"); + + System.out.println("Checking storing default-initialized meta data ..."); + + xDP2.storeToMedium("", mimeEmptyArgs); + + System.out.println("...done"); + + System.out.println("Checking loading default-initialized meta data ..."); + + xDP2.loadFromMedium("", mimeEmptyArgs); + assertEquals("Author", "", xDP2.getAuthor()); + + System.out.println("...done"); + + System.out.println("Checking loading from test document..."); + + String file = TestDocument.getUrl("TEST.odt"); + xDP.loadFromMedium(file, noArgs); +/* XInputStream xStream = + new StreamSimulator("./testdocuments/TEST.odt", true, param); + Object oSF = + xMSF.createInstance("com.sun.star.embed.StorageFactory"); + XSingleServiceFactory xSF = (XSingleServiceFactory) + UnoRuntime.queryInterface(XSingleServiceFactory.class, oSF); + Object oStor = xSF.createInstanceWithArguments( + new Object[] { xStream }); + XStorage xStor = (XStorage) UnoRuntime.queryInterface( + XStorage.class, oStor); + xDP.loadFromStorage(xStor);*/ + + System.out.println("...done"); + + System.out.println("Checking meta-data import..."); + + assertEquals("Author", "Karl-Heinz Mustermann", xDP.getAuthor()); + assertEquals( + "Generator", + "StarOffice/8$Solaris_x86 OpenOffice.org_project/680m232$Build-9227", + xDP.getGenerator()); + assertEquals("CreationDate", 2007, xDP.getCreationDate().Year); + assertEquals("Title", "Urgent Memo", xDP.getTitle()); + assertEquals("Subject", "Wichtige Mitteilung", xDP.getSubject()); + assertEquals( + "Description", + "Modern internal company memorandum in full-blocked style", + xDP.getDescription()); + assertEquals( + "ModifiedBy", "Karl-Heinz Mustermann", xDP.getModifiedBy()); + assertEquals( + "ModificationDate", 10, xDP.getModificationDate().Month); + assertEquals( + "PrintedBy", "Karl-Heinz Mustermann", xDP.getPrintedBy()); + assertEquals("PrintDate", 29, xDP.getPrintDate().Day); + assertEquals("TemplateName", "Modern Memo", xDP.getTemplateName()); + assertTrue("TemplateURL", + xDP.getTemplateURL().endsWith("memmodern.ott")); + assertEquals("TemplateDate", 17, xDP.getTemplateDate().Hours); + assertTrue( + "AutoloadURL", xDP.getAutoloadURL().endsWith("/TEST.odt")); + assertEquals("AutoloadSecs", 0, xDP.getAutoloadSecs()); + assertEquals("DefaultTarget", "_blank", xDP.getDefaultTarget()); + assertEquals("EditingCycles", 3, xDP.getEditingCycles()); + assertEquals("EditingDuration", 320, xDP.getEditingDuration()); + + String[] kws = xDP.getKeywords(); + assertTrue("Keywords", fromArray(kws).containsAll( + fromArray(new Object[] { "Asien", "Memo", "Reis" }))); + + NamedValue[] ds = xDP.getDocumentStatistics(); + assertTrue("DocumentStatistics:WordCount", containsNV(ds, + new NamedValue("WordCount", Integer.valueOf(23)))); + assertTrue("DocumentStatistics:PageCount", containsNV(ds, + new NamedValue("PageCount", Integer.valueOf(1)))); + + XPropertyContainer udpc = xDP.getUserDefinedProperties(); + XPropertySet udps = UnoRuntime.queryInterface( XPropertySet.class, udpc ); + assertEquals( + "UserDefined 1", "Dies ist ein wichtiger Hinweis", + udps.getPropertyValue("Hinweis")); + assertEquals( + "UserDefined 2", "Kann Spuren von N\u00FCssen enthalten", + udps.getPropertyValue("Warnung")); + + System.out.println("...done"); + + System.out.println("Checking meta-data updates..."); + + String str; + DateTime dt = new DateTime(); + Locale l = new Locale(); + int i; + + str = "me"; + xDP.setAuthor(str); + assertEquals("setAuthor", str, xDP.getAuthor()); + str = "the computa"; + xDP.setGenerator(str); + assertEquals("setGenerator", str, xDP.getGenerator()); + dt.Year = 2038; + dt.Month = 1; + dt.Day = 1; + xDP.setCreationDate(dt); + assertEquals( + "setCreationDate", dt.Year, xDP.getCreationDate().Year); + str = "El t'itulo"; + xDP.setTitle(str); + assertEquals("setTitle", str, xDP.getTitle()); + str = "Ein verkommenes Subjekt"; + xDP.setSubject(str); + assertEquals("setSubject", str, xDP.getSubject()); + str = "Este descripci'on no es importante"; + xDP.setDescription(str); + assertEquals("setDescription", str, xDP.getDescription()); + l.Language = "en"; + l.Country = "GB"; + xDP.setLanguage(l); + Locale l2 = xDP.getLanguage(); + assertEquals("setLanguage Lang", l.Language, l2.Language); + assertEquals("setLanguage Cty", l.Country, l2.Country); + str = "myself"; + xDP.setModifiedBy(str); + assertEquals("setModifiedBy", str, xDP.getModifiedBy()); + dt.Year = 2042; + xDP.setModificationDate(dt); + assertEquals( + "setModificationDate", dt.Year, xDP.getModificationDate().Year); + str = "i did not do it"; + xDP.setPrintedBy(str); + assertEquals("setPrintedBy", str, xDP.getPrintedBy()); + dt.Year = 2024; + xDP.setPrintDate(dt); + assertEquals("setPrintDate", dt.Year, xDP.getPrintDate().Year); + str = "blah"; + xDP.setTemplateName(str); + assertEquals("setTemplateName", str, xDP.getTemplateName()); + str = "gopher://some-hole-in-the-ground/"; + xDP.setTemplateURL(str); + assertEquals("setTemplateURL", str, xDP.getTemplateURL()); + dt.Year = 2043; + xDP.setTemplateDate(dt); + assertEquals( + "setTemplateDate", dt.Year, xDP.getTemplateDate().Year); + str = "http://nowhere/"; + xDP.setAutoloadURL(str); + assertEquals("setAutoloadURL", str, xDP.getAutoloadURL()); + i = 3661; // this might not work (due to conversion via double...) + xDP.setAutoloadSecs(i); + assertEquals("setAutoloadSecs", i, xDP.getAutoloadSecs()); + str = "_blank"; + xDP.setDefaultTarget(str); + assertEquals("setDefaultTarget", str, xDP.getDefaultTarget()); + i = 42; + xDP.setEditingCycles((short) i); + assertEquals("setEditingCycles", i, xDP.getEditingCycles()); + i = 84; + xDP.setEditingDuration(i); + assertEquals("setEditingDuration", i, xDP.getEditingDuration()); + str = ""; + + String[] kws2 = new String[] { + "keywordly", "keywordlike", "keywordalicious" }; + xDP.setKeywords(kws2); + kws = xDP.getKeywords(); + assertTrue("setKeywords", fromArray(kws).containsAll(fromArray(kws2))); + + NamedValue[] ds2 = new NamedValue[] { + new NamedValue("SyllableCount", Integer.valueOf(9)), + new NamedValue("FrameCount", Integer.valueOf(2)), + new NamedValue("SentenceCount", Integer.valueOf(7)) }; + xDP.setDocumentStatistics(ds2); + ds = xDP.getDocumentStatistics(); + assertTrue("setDocumentStatistics:SyllableCount", containsNV(ds, + new NamedValue("SyllableCount", Integer.valueOf(9)))); + assertTrue("setDocumentStatistics:FrameCount", containsNV(ds, + new NamedValue("FrameCount", Integer.valueOf(2)))); + assertTrue("setDocumentStatistics:SentenceCount", containsNV(ds, + new NamedValue("SentenceCount", Integer.valueOf(7)))); + + System.out.println("...done"); + + System.out.println("Checking user-defined meta-data updates..."); + + // actually, this tests the PropertyBag service + // but maybe the DocumentProperties service will be implemented + // differently some day... + boolean b = true; + double d = 3.1415; + // note that Time is only supported for backward compatibility! + Time t = new Time(); + t.Hours = 1; + t.Minutes = 16; + Date date = new Date(); + date.Year = 2071; + date.Month = 2; + date.Day = 3; + dt.Year = 2065; + Duration dur = new Duration(); + dur.Negative = true; + dur.Years = 1001; + dur.Months = 999; + dur.Days = 888; + dur.Hours = 777; + dur.Minutes = 666; + dur.Seconds = 555; + dur.NanoSeconds = 444444444; + + udpc.addProperty("Frobnicate", PropertyAttribute.REMOVABLE, b); + udpc.addProperty("FrobDuration", PropertyAttribute.REMOVABLE, dur); + udpc.addProperty("FrobDuration2", PropertyAttribute.REMOVABLE, t); + udpc.addProperty("FrobEndDate", PropertyAttribute.REMOVABLE, date); + udpc.addProperty("FrobStartTime", PropertyAttribute.REMOVABLE, dt); + udpc.addProperty("Pi", PropertyAttribute.REMOVABLE, new Double(d)); + udpc.addProperty("Foo", PropertyAttribute.REMOVABLE, "bar"); + udpc.addProperty("Removed", PropertyAttribute.REMOVABLE, "bar"); + // #i94175#: empty property name is valid ODF 1.1 + udpc.addProperty("", PropertyAttribute.REMOVABLE, "eeeeek"); + try { + udpc.removeProperty("Info 1"); + udpc.removeProperty("Removed"); + } catch (UnknownPropertyException e) { + fail("removeProperty failed"); + } + + try { + udpc.addProperty("Forbidden", PropertyAttribute.REMOVABLE, + new String[] { "foo", "bar" }); + fail("inserting value of non-supported type did not fail"); + } catch (IllegalTypeException e) { + // ignore + } + + assertEquals( + "UserDefined bool", b, udps.getPropertyValue("Frobnicate")); + assertTrue("UserDefined duration", eqDuration(dur, (Duration) + udps.getPropertyValue("FrobDuration"))); + assertTrue("UserDefined time", eqTime(t, (Time) + udps.getPropertyValue("FrobDuration2"))); + assertTrue("UserDefined date", eqDate(date, (Date) + udps.getPropertyValue("FrobEndDate"))); + assertTrue("UserDefined datetime", eqDateTime(dt, (DateTime) + udps.getPropertyValue("FrobStartTime"))); + assertEquals("UserDefined float", d, udps.getPropertyValue("Pi")); + assertEquals( + "UserDefined string", "bar", udps.getPropertyValue("Foo")); + assertEquals( + "UserDefined empty name", "eeeeek", udps.getPropertyValue("")); + + try { + udps.getPropertyValue("Removed"); + fail("UserDefined remove didn't"); + } catch (UnknownPropertyException e) { + // ok + } + + System.out.println("...done"); + + System.out.println("Checking storing meta-data to file..."); + + xDP.storeToMedium(temp + "TEST.odt", mimeArgs); + + System.out.println("...done"); + + System.out.println("Checking loading meta-data from stored file..."); + + xDP.loadFromMedium(temp + "TEST.odt", noArgs); + + System.out.println("...done"); + + System.out.println("Checking user-defined meta-data from stored file..."); + + udpc = xDP.getUserDefinedProperties(); + udps = UnoRuntime.queryInterface( XPropertySet.class, udpc ); + + assertEquals( + "UserDefined bool", b, udps.getPropertyValue("Frobnicate")); + assertTrue("UserDefined duration", eqDuration(dur, (Duration) + udps.getPropertyValue("FrobDuration"))); + // this is now a Duration! + Duration t_dur = new Duration(false, (short)0, (short)0, (short)0, + t.Hours, t.Minutes, t.Seconds, + t.NanoSeconds); + assertTrue("UserDefined time", eqDuration(t_dur, (Duration) + udps.getPropertyValue("FrobDuration2"))); + assertTrue("UserDefined date", eqDate(date, (Date) + udps.getPropertyValue("FrobEndDate"))); + assertTrue("UserDefined datetime", eqDateTime(dt, (DateTime) + udps.getPropertyValue("FrobStartTime"))); + assertEquals("UserDefined float", d, udps.getPropertyValue("Pi")); + assertEquals( + "UserDefined string", "bar", udps.getPropertyValue("Foo")); + + try { + udps.getPropertyValue("Removed"); + fail("UserDefined remove didn't"); + } catch (UnknownPropertyException e) { + // ok + } + + System.out.println("...done"); + + System.out.println("Checking notification listener interface..."); + + Listener listener = new Listener(); + XModifyBroadcaster xMB = UnoRuntime.queryInterface( XModifyBroadcaster.class, xDP ); + xMB.addModifyListener(listener); + xDP.setAuthor("not me"); + assertTrue("Listener Author", listener.reset()); + udpc.addProperty("Listener", PropertyAttribute.REMOVABLE, "foo"); + assertTrue("Listener UserDefined Add", listener.reset()); + udps.setPropertyValue("Listener", "bar"); + assertTrue("Listener UserDefined Set", listener.reset()); + udpc.removeProperty("Listener"); + assertTrue("Listener UserDefined Remove", listener.reset()); + xMB.removeModifyListener(listener); + udpc.addProperty("Listener2", PropertyAttribute.REMOVABLE, "foo"); + assertTrue("Removed Listener UserDefined Add", !listener.reset()); + + System.out.println("...done"); + } + + // grrr... + boolean eqDateTime(DateTime a, DateTime b) { + return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day + && a.Hours == b.Hours && a.Minutes == b.Minutes + && a.Seconds == b.Seconds + && a.NanoSeconds == b.NanoSeconds; + } + + boolean eqDate(Date a, Date b) { + return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day; + } + + boolean eqTime(Time a, Time b) { + return a.Hours == b.Hours && a.Minutes == b.Minutes + && a.Seconds == b.Seconds + && a.NanoSeconds == b.NanoSeconds; + } + + boolean eqDuration(Duration a, Duration b) { + return a.Years == b.Years && a.Months == b.Months && a.Days == b.Days + && a.Hours == b.Hours && a.Minutes == b.Minutes + && a.Seconds == b.Seconds + && a.NanoSeconds == b.NanoSeconds + && a.Negative == b.Negative; + } + + java.util.Collection fromArray(Object[] os) { + java.util.Collection ret = new java.util.HashSet(); + for (int i = 0; i < os.length; ++i) { + ret.add(os[i]); + } + return ret; + } + + // bah, structs do not have proper equals(), and uno.Type is not comparable + public static boolean containsNV (NamedValue[] nvs, NamedValue nv) { + for (int i = 0; i < nvs.length; ++i) { + if (nvs[i].Name.equals(nv.Name) && nvs[i].Value.equals(nv.Value)) { + return true; + } + } + return false; + } + + private XMultiServiceFactory getMSF() + { + final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface( XMultiServiceFactory.class, connection.getComponentContext().getServiceManager() ); + return xMSF1; + } + + // setup and close connections + @BeforeClass public static void setUpConnection() throws Exception { + System.out.println( "------------------------------------------------------------" ); + System.out.println( "starting class: " + DocumentProperties.class.getName() ); + System.out.println( "------------------------------------------------------------" ); + connection.setUp(); + } + + @AfterClass public static void tearDownConnection() + throws InterruptedException, com.sun.star.uno.Exception + { + System.out.println( "------------------------------------------------------------" ); + System.out.println( "finishing class: " + DocumentProperties.class.getName() ); + System.out.println( "------------------------------------------------------------" ); + connection.tearDown(); + } + + private static final OfficeConnection connection = new OfficeConnection(); + +} + diff --git a/sfx2/qa/complex/sfx2/GlobalEventBroadcaster.java b/sfx2/qa/complex/sfx2/GlobalEventBroadcaster.java new file mode 100644 index 000000000..7c42cdf1f --- /dev/null +++ b/sfx2/qa/complex/sfx2/GlobalEventBroadcaster.java @@ -0,0 +1,249 @@ +/* + * 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.sfx2; + +import com.sun.star.awt.XWindow; +import com.sun.star.document.XEventBroadcaster; +import com.sun.star.document.XEventListener; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.text.XTextDocument; +import com.sun.star.uno.UnoRuntime; +import complex.sfx2.tools.WriterHelper; + +import java.util.ArrayList; + +import util.UITools; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openoffice.test.OfficeConnection; +import static org.junit.Assert.*; + + +/** + * This testcase checks the GlobalEventBroadcaster + * it will add an XEventListener and verify the Events + * raised when opening/changing and closing Office Documents + */ +public class GlobalEventBroadcaster { + XMultiServiceFactory m_xMSF = null; + XEventBroadcaster m_xEventBroadcaster = null; + ArrayList notifyEvents = new ArrayList(); + // XTextDocument xTextDoc; + XSpreadsheetDocument xSheetDoc; + XEventListener m_xEventListener = new EventListenerImpl(); + + @Before public void initialize() { + m_xMSF = getMSF(); + System.out.println("check whether there is a valid MultiServiceFactory"); + + assertNotNull("## Couldn't get MultiServiceFactory make sure your Office is started", m_xMSF); + + System.out.println("... done"); + + System.out.println( + "Create an instance of com.sun.star.frame.GlobalEventBroadcaster"); + + Object GlobalEventBroadcaster = null; + + try { + GlobalEventBroadcaster = m_xMSF.createInstance( + "com.sun.star.frame.GlobalEventBroadcaster"); + } catch (com.sun.star.uno.Exception e) { + fail("## Exception while creating instance"); + } + + System.out.println("... done"); + + System.out.println("check whether the created instance is valid"); + + assertNotNull("couldn't create service", GlobalEventBroadcaster); + + System.out.println("... done"); + + System.out.println( + "try to query the XEventBroadcaster from the gained Object"); + m_xEventBroadcaster = UnoRuntime.queryInterface(XEventBroadcaster.class, GlobalEventBroadcaster); + + if (util.utils.isVoid(m_xEventBroadcaster)) { + fail("couldn't get XEventBroadcaster"); + } + + System.out.println("... done"); + + System.out.println("adding Listener"); + m_xEventBroadcaster.addEventListener(m_xEventListener); + System.out.println("... done"); + } + + @Test public void checkWriter() throws Exception { + System.out.println("-- Checking Writer --"); + + WriterHelper wHelper = new WriterHelper(m_xMSF); + String[] expected; + System.out.println("opening an empty writer doc"); + notifyEvents.clear(); + { + XTextDocument xTextDoc = wHelper.openEmptyDoc(); + util.utils.waitForEventIdle(m_xMSF); + expected = new String[] { "OnUnfocus", "OnCreate", "OnViewCreated", "OnFocus" }; + + assertTrue("Wrong events fired when opening empty doc", + proveExpectation(expected)); + System.out.println("... done"); + + System.out.println("changing the writer doc"); + notifyEvents.clear(); + xTextDoc.getText().setString("GlobalEventBroadcaster"); + util.utils.waitForEventIdle(m_xMSF); + expected = new String[] { "OnModifyChanged" }; + + assertTrue("Wrong events fired when changing doc", + proveExpectation(expected)); + System.out.println("... done"); + + System.out.println("closing the empty writer doc"); + notifyEvents.clear(); + wHelper.closeDoc(xTextDoc); + util.utils.waitForEventIdle(m_xMSF); + } + expected = new String[] { "OnUnfocus", "OnFocus", "OnViewClosed", "OnUnload" }; + + assertTrue("Wrong events fired when closing empty doc", + proveExpectation(expected)); + System.out.println("... done"); + + System.out.println("opening a writer doc via Window-New Window"); + notifyEvents.clear(); + { + XTextDocument xTextDoc = wHelper.openFromDialog(".uno:NewWindow", "", false); + + util.utils.waitForEventIdle(m_xMSF); + expected = new String[] { "OnUnfocus", "OnCreate", "OnViewCreated", "OnFocus", "OnUnfocus", "OnViewCreated", "OnFocus", }; + + assertTrue("Wrong events fired when opening a writer doc via Window-New Window", + proveExpectation(expected)); + System.out.println("... done"); + + System.out.println("closing the created writer doc"); + notifyEvents.clear(); + + wHelper.closeDoc(xTextDoc); + util.utils.waitForEventIdle(m_xMSF); + } + expected = new String[] { "OnViewClosed", "OnUnfocus", "OnFocus", "OnViewClosed", "OnUnload" }; + + assertTrue("Wrong events fired when closing Window-New Window", + proveExpectation(expected)); + + System.out.println("... done"); + // TODO: It seems not possible to close the document without interactive question + // there the follow test will not be execute + if (false) { + System.out.println("Opening document with label wizard"); + XTextDocument xTextDoc = wHelper.openFromDialog("private:factory/swriter?slot=21051", "", false); + util.utils.waitForEventIdle(m_xMSF); + XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, wHelper.getToolkit().getActiveTopWindow()); + UITools ut = new UITools(xWindow); + notifyEvents.clear(); + System.out.println("pressing button 'New Document'"); + try{ + ut.clickButton ("New Document"); + } catch (Exception e) { + System.out.println("Couldn't press Button"); + } + System.out.println("... done"); + util.utils.waitForEventIdle(m_xMSF); + expected = new String[] { "OnViewClosed", "OnCreate", "OnFocus", "OnModifyChanged" }; + + assertTrue("Wrong events fired when starting labels wizard", + proveExpectation(expected)); + + System.out.println("Try to close document..."); + wHelper.closeDoc(xTextDoc); + util.utils.waitForEventIdle(m_xMSF); + wHelper.closeFromDialog(); + util.utils.waitForEventIdle(m_xMSF); + xTextDoc = null; + } + + System.out.println("-- Done Writer --"); + } + + @After public void cleanup() { + System.out.println("removing Listener"); + m_xEventBroadcaster.removeEventListener(m_xEventListener); + System.out.println("... done"); + } + + private boolean proveExpectation(String[] expected) { + boolean locRes = true; + boolean failure = false; + + System.out.println("Fired Events:"); + for (int k=0;k0; ) + undoManager.addUndoAction( new CountingUndoAction( --i, lock, actionsUndone ) ); + + // some concurrent threads which undo the actions + Thread[] threads = new Thread[threadCount]; + for ( int i=0; i + m_activeUndoContexts = new Stack(); + private String m_mostRecentlyAddedAction = null; + private String m_mostRecentlyUndone = null; + private final Object m_allContextsClosedCondition = new Object(); + } + + + private void impl_checkUndo() throws Exception + { + System.out.println( "testing: " + m_currentTestCase.getDocumentDescription() ); + m_currentDocument = m_currentTestCase.getDocument(); + m_currentTestCase.initializeDocument(); + m_currentTestCase.verifyInitialDocumentState(); + + final XUndoManager undoManager = getUndoManager(); + undoManager.clear(); + assertFalse( "clearing the Undo manager should result in the impossibility to undo anything", undoManager.isUndoPossible() ); + assertFalse( "clearing the Undo manager should result in the impossibility to redo anything", undoManager.isRedoPossible() ); + + m_undoListener = new UndoListener(); + undoManager.addUndoManagerListener( m_undoListener ); + + impl_testSingleModification( undoManager ); + impl_testMultipleModifications( undoManager ); + impl_testCustomUndoActions( undoManager ); + impl_testLocking( undoManager ); + impl_testNestedContexts( undoManager ); + impl_testErrorHandling( undoManager ); + impl_testContextHandling( undoManager ); + impl_testStackHandling( undoManager ); + impl_testClearance( undoManager ); + impl_testHiddenContexts( undoManager ); + + // close the document, ensure the Undo manager listener gets notified + m_currentTestCase.closeDocument(); + m_currentTestCase = null; + m_currentDocument = null; + assertTrue( "document is closed, but the UndoManagerListener has not been notified of the disposal", m_undoListener.isDisposed() ); + } + + + private void impl_testSingleModification( final XUndoManager i_undoManager ) throws com.sun.star.uno.Exception + { + m_currentTestCase.doSingleModification(); + m_currentTestCase.verifySingleModificationDocumentState(); + + // undo the modification, ensure the listener got the proper notifications + assertEquals( "We did not yet do an undo!", 0, m_undoListener.getUndoActionCount() ); + i_undoManager.undo(); + assertEquals( "A simple undo does not result in the proper Undo count.", + 1, m_undoListener.getUndoActionCount() ); + + // verify the document is in its initial state, again + m_currentTestCase.verifyInitialDocumentState(); + + // redo the modification, ensure the listener got the proper notifications + assertEquals( "not yet made a redo!", 0, m_undoListener.getRedoActionCount() ); + i_undoManager.redo(); + assertEquals( "made a redo, but got no notification of it!", 1, m_undoListener.getRedoActionCount() ); + // ensure the document is in the proper state, again + m_currentTestCase.verifySingleModificationDocumentState(); + + // now do an Undo via the UI (aka the dispatch API), and see if this works, and notifies the listener as + // expected + m_currentTestCase.getDocument().getCurrentView().dispatch( ".uno:Undo" ); + m_currentTestCase.verifyInitialDocumentState(); + assertEquals( "UI-Undo does not notify the listener", 2, m_undoListener.getUndoActionCount() ); + } + + + private void impl_testMultipleModifications( final XUndoManager i_undoManager ) throws com.sun.star.uno.Exception + { + m_undoListener.reset(); + assertEquals( "unexpected initial undo context depth", 0, m_undoListener.getCurrentUndoContextDepth() ); + i_undoManager.enterUndoContext( "Batch Changes" ); + assertEquals( "unexpected undo context depth after entering a context", + 1, m_undoListener.getCurrentUndoContextDepth() ); + assertEquals( "entering an Undo context has not been notified properly", + "Batch Changes", m_undoListener.getCurrentUndoContextTitle() ); + + final int modifications = m_currentTestCase.doMultipleModifications(); + assertEquals( "unexpected number of undo actions while doing batch changes to the document", + modifications, m_undoListener.getUndoActionsAdded() ); + assertEquals( "seems the document operations touched the undo context depth", + 1, m_undoListener.getCurrentUndoContextDepth() ); + + i_undoManager.leaveUndoContext(); + assertEquals( "unexpected undo context depth after leaving the last context", + 0, m_undoListener.getCurrentUndoContextDepth() ); + assertEquals( "no Undo done, yet - still the listener has been notified of an Undo action", + 0, m_undoListener.getUndoActionCount() ); + + i_undoManager.undo(); + assertEquals( "Just did an undo - the listener should have been notified", 1, m_undoListener.getUndoActionCount() ); + m_currentTestCase.verifyInitialDocumentState(); + } + + + private void impl_testCustomUndoActions( final XUndoManager i_undoManager ) throws com.sun.star.uno.Exception + { + i_undoManager.clear(); + m_undoListener.reset(); + assertFalse( "undo stack not empty after clearing the undo manager", i_undoManager.isUndoPossible() ); + assertFalse( "redo stack not empty after clearing the undo manager", i_undoManager.isRedoPossible() ); + assertArrayEquals( ">0 descriptions for an empty undo stack?", + new String[0], i_undoManager.getAllUndoActionTitles() ); + assertArrayEquals( ">0 descriptions for an empty redo stack?", + new String[0], i_undoManager.getAllRedoActionTitles() ); + + // add two actions, one directly, one within a context + final CustomUndoAction action1 = new CustomUndoAction( "UndoAction1" ); + i_undoManager.addUndoAction( action1 ); + assertEquals( "Adding an undo action not observed by the listener", 1, m_undoListener.getUndoActionsAdded() ); + assertEquals( "Adding an undo action did not notify the proper title", + action1.getTitle(), m_undoListener.getMostRecentlyAddedActionTitle() ); + final String contextTitle = "Undo Context"; + i_undoManager.enterUndoContext( contextTitle ); + final CustomUndoAction action2 = new CustomUndoAction( "UndoAction2" ); + i_undoManager.addUndoAction( action2 ); + assertEquals( "Adding an undo action not observed by the listener", + 2, m_undoListener.getUndoActionsAdded() ); + assertEquals( "Adding an undo action did not notify the proper title", + action2.getTitle(), m_undoListener.getMostRecentlyAddedActionTitle() ); + i_undoManager.leaveUndoContext(); + + // see if the manager has proper descriptions + assertArrayEquals( "unexpected Redo descriptions after adding two actions", + new String[0], i_undoManager.getAllRedoActionTitles() ); + assertArrayEquals( "unexpected Undo descriptions after adding two actions", + new String[]{contextTitle, action1.getTitle()}, i_undoManager.getAllUndoActionTitles() ); + + // undo one action + i_undoManager.undo(); + assertEquals( "improper action title notified during programmatic Undo", + contextTitle, m_undoListener.getMostRecentlyUndoneTitle() ); + assertTrue( "nested custom undo action has not been undone as expected", action2.undoCalled() ); + assertFalse( "nested custom undo action has not been undone as expected", action1.undoCalled() ); + assertArrayEquals( "unexpected Redo descriptions after undoing a nested custom action", + new String[]{contextTitle}, i_undoManager.getAllRedoActionTitles() ); + assertArrayEquals( "unexpected Undo descriptions after undoing a nested custom action", + new String[]{action1.getTitle()}, i_undoManager.getAllUndoActionTitles() ); + + // undo the second action, via UI dispatches + m_currentTestCase.getDocument().getCurrentView().dispatch( ".uno:Undo" ); + assertEquals( "improper action title notified during UI Undo", action1.getTitle(), m_undoListener.getMostRecentlyUndoneTitle() ); + assertTrue( "nested custom undo action has not been undone as expected", action1.undoCalled() ); + assertArrayEquals( "unexpected Redo descriptions after undoing the second custom action", + new String[]{action1.getTitle(), contextTitle}, i_undoManager.getAllRedoActionTitles() ); + assertArrayEquals( "unexpected Undo descriptions after undoing the second custom action", + new String[0], i_undoManager.getAllUndoActionTitles() ); + + // check the actions are disposed when the stacks are cleared + i_undoManager.clear(); + assertTrue( action1.disposed() && action2.disposed() ); + } + + + private void impl_testLocking( final XUndoManager i_undoManager ) throws com.sun.star.uno.Exception + { + i_undoManager.reset(); + m_undoListener.reset(); + + // implicit Undo actions, triggered by changes to the document + assertFalse( "unexpected initial locking state", i_undoManager.isLocked() ); + i_undoManager.lock(); + assertTrue( "just locked the manager, why does it lie?", i_undoManager.isLocked() ); + m_currentTestCase.doSingleModification(); + assertEquals( "when the Undo manager is locked, no implicit additions should happen", + 0, m_undoListener.getUndoActionsAdded() ); + assertTrue( "Undo manager gets unlocked as a side effect of performing a simple operation", i_undoManager.isLocked() ); + i_undoManager.unlock(); + assertEquals( "unlock is not expected to add collected actions - they should be discarded", + 0, m_undoListener.getUndoActionsAdded() ); + assertFalse( "just unlocked the manager, why does it lie?", i_undoManager.isLocked() ); + + // explicit Undo actions + i_undoManager.lock(); + i_undoManager.addUndoAction( new CustomUndoAction() ); + i_undoManager.unlock(); + assertEquals( "explicit Undo actions are expected to be ignored when the manager is locked", + 0, m_undoListener.getUndoActionsAdded() ); + + // Undo contexts while being locked + i_undoManager.lock(); + i_undoManager.enterUndoContext( "Dummy Context" ); + i_undoManager.enterHiddenUndoContext(); + assertEquals( "entering Undo contexts should be ignored when the manager is locked", 0, m_undoListener.getCurrentUndoContextDepth() ); + i_undoManager.leaveUndoContext(); + i_undoManager.leaveUndoContext(); + i_undoManager.unlock(); + + // |unlock| error handling + assertFalse( "internal error: manager should not be locked at this point in time", i_undoManager.isLocked() ); + boolean caughtExpected = false; + try { i_undoManager.unlock(); } catch ( final NotLockedException e ) { caughtExpected = true; } + assertTrue( "unlocking the manager when it is not locked should throw", caughtExpected ); + } + + + private void impl_testContextHandling( final XUndoManager i_undoManager ) throws com.sun.star.uno.Exception + { + + // part I: non-empty contexts + i_undoManager.reset(); + m_undoListener.reset(); + + // put one action on the undo and one on the redo stack, as precondition for the following tests + final XUndoAction undoAction1 = new CustomUndoAction( "Undo Action 1" ); + i_undoManager.addUndoAction( undoAction1 ); + final XUndoAction undoAction2 = new CustomUndoAction( "Undo Action 2" ); + i_undoManager.addUndoAction( undoAction2 ); + i_undoManager.undo(); + assertTrue( "precondition for context handling tests not met (1)", i_undoManager.isUndoPossible() ); + assertTrue( "precondition for context handling tests not met (2)", i_undoManager.isRedoPossible() ); + assertArrayEquals( new String[] { undoAction1.getTitle() }, i_undoManager.getAllUndoActionTitles() ); + assertArrayEquals( new String[] { undoAction2.getTitle() }, i_undoManager.getAllRedoActionTitles() ); + + final String[] expectedRedoActionComments = new String[] { undoAction2.getTitle() }; + assertArrayEquals( expectedRedoActionComments, i_undoManager.getAllRedoActionTitles() ); + + // enter a context + i_undoManager.enterUndoContext( "Undo Context" ); + // this should not (yet) touch the redo stack + assertArrayEquals( expectedRedoActionComments, i_undoManager.getAllRedoActionTitles() ); + assertEquals( "unexpected undo context depth after entering a context", 1, m_undoListener.getCurrentUndoContextDepth() ); + // add a single action + XUndoAction undoAction3 = new CustomUndoAction( "Undo Action 3" ); + i_undoManager.addUndoAction( undoAction3 ); + // still, the redo stack should be untouched - added at a lower level does not affect it at all + assertArrayEquals( expectedRedoActionComments, i_undoManager.getAllRedoActionTitles() ); + + // while the context is open, its title should already contribute to the stack, ... + assertEquals( "Undo Context", i_undoManager.getCurrentUndoActionTitle() ); + // ... getAllUndo/RedoActionTitles should operate on the top level, not on the level defined by the open + // context, ... + assertArrayEquals( new String[] { "Undo Context", undoAction1.getTitle() }, + i_undoManager.getAllUndoActionTitles() ); + // ... but Undo and Redo should be impossible as long as the context is open + assertFalse( i_undoManager.isUndoPossible() ); + assertFalse( i_undoManager.isRedoPossible() ); + + // leave the context, check the listener has been notified properly, and the notified context depth is correct + i_undoManager.leaveUndoContext(); + assertTrue( m_undoListener.wasContextLeft() ); + assertFalse( m_undoListener.wasHiddenContextLeft() ); + assertFalse( m_undoListener.hasContextBeenCancelled() ); + assertEquals( "unexpected undo context depth leaving a non-empty context", 0, m_undoListener.getCurrentUndoContextDepth() ); + // leaving a non-empty context should have cleared the redo stack + assertArrayEquals( new String[0], i_undoManager.getAllRedoActionTitles() ); + assertTrue( m_undoListener.wasRedoStackCleared() ); + + + // part II: empty contexts + i_undoManager.reset(); + m_undoListener.reset(); + + // enter a context, leave it immediately without adding an action to it + i_undoManager.enterUndoContext( "Undo Context" ); + i_undoManager.leaveUndoContext(); + assertFalse( m_undoListener.wasContextLeft() ); + assertFalse( m_undoListener.wasHiddenContextLeft() ); + assertTrue( m_undoListener.hasContextBeenCancelled() ); + assertFalse( "leaving an empty context should silently remove it, and not contribute to the stack", + i_undoManager.isUndoPossible() ); + } + + + private void impl_testNestedContexts( final XUndoManager i_undoManager ) throws com.sun.star.uno.Exception + { + i_undoManager.reset(); + m_undoListener.reset(); + i_undoManager.enterUndoContext( "context 1" ); + i_undoManager.enterUndoContext( "context 1.1" ); + final CustomUndoAction action1 = new CustomUndoAction( "action 1.1.1" ); + i_undoManager.addUndoAction( action1 ); + i_undoManager.enterUndoContext( "context 1.1.2" ); + final CustomUndoAction action2 = new CustomUndoAction( "action 1.1.2.1" ); + i_undoManager.addUndoAction( action2 ); + i_undoManager.leaveUndoContext(); + final CustomUndoAction action3 = new CustomUndoAction( "action 1.1.3" ); + i_undoManager.addUndoAction( action3 ); + i_undoManager.leaveUndoContext(); + i_undoManager.leaveUndoContext(); + final CustomUndoAction action4 = new CustomUndoAction( "action 1.2" ); + i_undoManager.addUndoAction( action4 ); + + i_undoManager.undo(); + assertEquals( "undoing a single action notifies a wrong title", action4.getTitle(), m_undoListener.getMostRecentlyUndoneTitle() ); + assertTrue( "custom Undo not called", action4.undoCalled() ); + assertFalse( "too many custom Undos called", action1.undoCalled() || action2.undoCalled() || action3.undoCalled() ); + i_undoManager.undo(); + assertTrue( "nested actions not properly undone", action1.undoCalled() && action2.undoCalled() && action3.undoCalled() ); + } + + + private void impl_testErrorHandling( final XUndoManager i_undoManager ) throws com.sun.star.uno.Exception + { + i_undoManager.reset(); + m_undoListener.reset(); + + // try retrieving the comments for the current Undo/Redo - this should fail + boolean caughtExpected = false; + try { i_undoManager.getCurrentUndoActionTitle(); } + catch( final EmptyUndoStackException e ) { caughtExpected = true; } + assertTrue( "trying the title of the current Undo action is expected to fail for an empty stack", caughtExpected ); + + caughtExpected = false; + try { i_undoManager.getCurrentRedoActionTitle(); } + catch( final EmptyUndoStackException e ) { caughtExpected = true; } + assertTrue( "trying the title of the current Redo action is expected to fail for an empty stack", caughtExpected ); + + caughtExpected = false; + try { i_undoManager.undo(); } catch ( final EmptyUndoStackException e ) { caughtExpected = true; } + assertTrue( "undo should throw if no Undo action is on the stack", caughtExpected ); + + caughtExpected = false; + try { i_undoManager.redo(); } catch ( final EmptyUndoStackException e ) { caughtExpected = true; } + assertTrue( "redo should throw if no Redo action is on the stack", caughtExpected ); + + caughtExpected = false; + try { i_undoManager.leaveUndoContext(); } catch ( final InvalidStateException e ) { caughtExpected = true; } + assertTrue( "leaveUndoContext should throw if no context is currently open", caughtExpected ); + + caughtExpected = false; + try { i_undoManager.addUndoAction( null ); } catch ( com.sun.star.lang.IllegalArgumentException e ) { caughtExpected = true; } + assertTrue( "adding a NULL action should be rejected", caughtExpected ); + + i_undoManager.reset(); + i_undoManager.addUndoAction( new CustomUndoAction() ); + i_undoManager.addUndoAction( new CustomUndoAction() ); + i_undoManager.undo(); + i_undoManager.enterUndoContext( "Undo Context" ); + // those methods should fail when a context is open: + final String[] methodNames = new String[] { "undo", "redo", "clear", "clearRedo" }; + for ( int i=0; i