1*b1cdbd2cSJim Jagielski /************************************************************** 2*b1cdbd2cSJim Jagielski * 3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski * distributed with this work for additional information 6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski * 13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski * under the License. 19*b1cdbd2cSJim Jagielski * 20*b1cdbd2cSJim Jagielski *************************************************************/ 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski package complex.storages; 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski import com.sun.star.uno.XInterface; 25*b1cdbd2cSJim Jagielski import com.sun.star.lang.XMultiServiceFactory; 26*b1cdbd2cSJim Jagielski import com.sun.star.lang.XSingleServiceFactory; 27*b1cdbd2cSJim Jagielski 28*b1cdbd2cSJim Jagielski import com.sun.star.bridge.XUnoUrlResolver; 29*b1cdbd2cSJim Jagielski import com.sun.star.uno.UnoRuntime; 30*b1cdbd2cSJim Jagielski import com.sun.star.uno.XInterface; 31*b1cdbd2cSJim Jagielski 32*b1cdbd2cSJim Jagielski import com.sun.star.embed.*; 33*b1cdbd2cSJim Jagielski import com.sun.star.container.XNameAccess; 34*b1cdbd2cSJim Jagielski 35*b1cdbd2cSJim Jagielski import share.LogWriter; 36*b1cdbd2cSJim Jagielski import complex.storages.TestHelper; 37*b1cdbd2cSJim Jagielski import complex.storages.StorageTest; 38*b1cdbd2cSJim Jagielski 39*b1cdbd2cSJim Jagielski public class Test03 implements StorageTest { 40*b1cdbd2cSJim Jagielski 41*b1cdbd2cSJim Jagielski XMultiServiceFactory m_xMSF; 42*b1cdbd2cSJim Jagielski XSingleServiceFactory m_xStorageFactory; 43*b1cdbd2cSJim Jagielski TestHelper m_aTestHelper; 44*b1cdbd2cSJim Jagielski Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )45*b1cdbd2cSJim Jagielski public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter ) 46*b1cdbd2cSJim Jagielski { 47*b1cdbd2cSJim Jagielski m_xMSF = xMSF; 48*b1cdbd2cSJim Jagielski m_xStorageFactory = xStorageFactory; 49*b1cdbd2cSJim Jagielski m_aTestHelper = new TestHelper( aLogWriter, "Test03: " ); 50*b1cdbd2cSJim Jagielski } 51*b1cdbd2cSJim Jagielski test()52*b1cdbd2cSJim Jagielski public boolean test() 53*b1cdbd2cSJim Jagielski { 54*b1cdbd2cSJim Jagielski try 55*b1cdbd2cSJim Jagielski { 56*b1cdbd2cSJim Jagielski // create temporary storage based on arbitrary medium 57*b1cdbd2cSJim Jagielski // after such a storage is closed it is lost 58*b1cdbd2cSJim Jagielski Object oTempStorage = m_xStorageFactory.createInstance(); 59*b1cdbd2cSJim Jagielski XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage ); 60*b1cdbd2cSJim Jagielski if ( xTempStorage == null ) 61*b1cdbd2cSJim Jagielski { 62*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Can't create temporary storage representation!" ); 63*b1cdbd2cSJim Jagielski return false; 64*b1cdbd2cSJim Jagielski } 65*b1cdbd2cSJim Jagielski 66*b1cdbd2cSJim Jagielski // open a new substorage 67*b1cdbd2cSJim Jagielski XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage, 68*b1cdbd2cSJim Jagielski "SubStorage1", 69*b1cdbd2cSJim Jagielski ElementModes.WRITE ); 70*b1cdbd2cSJim Jagielski if ( xTempSubStorage == null ) 71*b1cdbd2cSJim Jagielski { 72*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Can't create substorage!" ); 73*b1cdbd2cSJim Jagielski return false; 74*b1cdbd2cSJim Jagielski } 75*b1cdbd2cSJim Jagielski 76*b1cdbd2cSJim Jagielski byte pBigBytes[] = new byte[33000]; 77*b1cdbd2cSJim Jagielski for ( int nInd = 0; nInd < 33000; nInd++ ) 78*b1cdbd2cSJim Jagielski pBigBytes[nInd] = (byte)( nInd % 128 ); 79*b1cdbd2cSJim Jagielski 80*b1cdbd2cSJim Jagielski // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 81*b1cdbd2cSJim Jagielski if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) ) 82*b1cdbd2cSJim Jagielski return false; 83*b1cdbd2cSJim Jagielski 84*b1cdbd2cSJim Jagielski // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 85*b1cdbd2cSJim Jagielski if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) ) 86*b1cdbd2cSJim Jagielski return false; 87*b1cdbd2cSJim Jagielski 88*b1cdbd2cSJim Jagielski byte pBytes1[] = { 1, 1, 1, 1, 1 }; 89*b1cdbd2cSJim Jagielski 90*b1cdbd2cSJim Jagielski // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 91*b1cdbd2cSJim Jagielski if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) ) 92*b1cdbd2cSJim Jagielski return false; 93*b1cdbd2cSJim Jagielski 94*b1cdbd2cSJim Jagielski byte pBytes2[] = { 2, 2, 2, 2, 2 }; 95*b1cdbd2cSJim Jagielski 96*b1cdbd2cSJim Jagielski // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 97*b1cdbd2cSJim Jagielski if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) ) 98*b1cdbd2cSJim Jagielski return false; 99*b1cdbd2cSJim Jagielski 100*b1cdbd2cSJim Jagielski // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 101*b1cdbd2cSJim Jagielski if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage, 102*b1cdbd2cSJim Jagielski "MediaType3", 103*b1cdbd2cSJim Jagielski false, 104*b1cdbd2cSJim Jagielski ElementModes.WRITE ) ) 105*b1cdbd2cSJim Jagielski return false; 106*b1cdbd2cSJim Jagielski 107*b1cdbd2cSJim Jagielski if ( !m_aTestHelper.commitStorage( xTempSubStorage ) ) 108*b1cdbd2cSJim Jagielski return false; 109*b1cdbd2cSJim Jagielski 110*b1cdbd2cSJim Jagielski if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) ) 111*b1cdbd2cSJim Jagielski return false; 112*b1cdbd2cSJim Jagielski 113*b1cdbd2cSJim Jagielski // ================================================ 114*b1cdbd2cSJim Jagielski // check storage hyerarchy tree 115*b1cdbd2cSJim Jagielski // ================================================ 116*b1cdbd2cSJim Jagielski 117*b1cdbd2cSJim Jagielski // check that isStorageElement() and isStreamElement reacts to nonexisting object correctly 118*b1cdbd2cSJim Jagielski try { 119*b1cdbd2cSJim Jagielski xTempStorage.isStorageElement( "does not exist" ); 120*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" ); 121*b1cdbd2cSJim Jagielski return false; 122*b1cdbd2cSJim Jagielski } 123*b1cdbd2cSJim Jagielski catch( com.sun.star.container.NoSuchElementException ne ) 124*b1cdbd2cSJim Jagielski { 125*b1cdbd2cSJim Jagielski } 126*b1cdbd2cSJim Jagielski catch( Exception e ) 127*b1cdbd2cSJim Jagielski { 128*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e ); 129*b1cdbd2cSJim Jagielski return false; 130*b1cdbd2cSJim Jagielski } 131*b1cdbd2cSJim Jagielski 132*b1cdbd2cSJim Jagielski try { 133*b1cdbd2cSJim Jagielski xTempStorage.isStreamElement( "does not exist" ); 134*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" ); 135*b1cdbd2cSJim Jagielski return false; 136*b1cdbd2cSJim Jagielski } 137*b1cdbd2cSJim Jagielski catch( com.sun.star.container.NoSuchElementException ne ) 138*b1cdbd2cSJim Jagielski { 139*b1cdbd2cSJim Jagielski } 140*b1cdbd2cSJim Jagielski catch( Exception e ) 141*b1cdbd2cSJim Jagielski { 142*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e ); 143*b1cdbd2cSJim Jagielski return false; 144*b1cdbd2cSJim Jagielski } 145*b1cdbd2cSJim Jagielski 146*b1cdbd2cSJim Jagielski XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage ); 147*b1cdbd2cSJim Jagielski if ( xRootNameAccess == null ) 148*b1cdbd2cSJim Jagielski { 149*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" ); 150*b1cdbd2cSJim Jagielski return false; 151*b1cdbd2cSJim Jagielski } 152*b1cdbd2cSJim Jagielski 153*b1cdbd2cSJim Jagielski try { 154*b1cdbd2cSJim Jagielski if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) ) 155*b1cdbd2cSJim Jagielski { 156*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" ); 157*b1cdbd2cSJim Jagielski return false; 158*b1cdbd2cSJim Jagielski } 159*b1cdbd2cSJim Jagielski 160*b1cdbd2cSJim Jagielski if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) ) 161*b1cdbd2cSJim Jagielski { 162*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" ); 163*b1cdbd2cSJim Jagielski return false; 164*b1cdbd2cSJim Jagielski } 165*b1cdbd2cSJim Jagielski } 166*b1cdbd2cSJim Jagielski catch( Exception e ) 167*b1cdbd2cSJim Jagielski { 168*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Child's type can not be detected, exception: " + e ); 169*b1cdbd2cSJim Jagielski return false; 170*b1cdbd2cSJim Jagielski } 171*b1cdbd2cSJim Jagielski 172*b1cdbd2cSJim Jagielski 173*b1cdbd2cSJim Jagielski // check that root storage contents are represented correctly 174*b1cdbd2cSJim Jagielski String sRootCont[] = xRootNameAccess.getElementNames(); 175*b1cdbd2cSJim Jagielski 176*b1cdbd2cSJim Jagielski if ( sRootCont.length != 3 ) 177*b1cdbd2cSJim Jagielski { 178*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Root storage contains wrong amount of children!" ); 179*b1cdbd2cSJim Jagielski return false; 180*b1cdbd2cSJim Jagielski } 181*b1cdbd2cSJim Jagielski 182*b1cdbd2cSJim Jagielski int nFlag = 0; 183*b1cdbd2cSJim Jagielski for ( int nInd = 0; nInd < sRootCont.length; nInd++ ) 184*b1cdbd2cSJim Jagielski { 185*b1cdbd2cSJim Jagielski if ( sRootCont[nInd].equals( "SubStorage1" ) ) 186*b1cdbd2cSJim Jagielski nFlag |= 1; 187*b1cdbd2cSJim Jagielski else if ( sRootCont[nInd].equals( "SubStream1" ) ) 188*b1cdbd2cSJim Jagielski nFlag |= 2; 189*b1cdbd2cSJim Jagielski else if ( sRootCont[nInd].equals( "BigSubStream1" ) ) 190*b1cdbd2cSJim Jagielski nFlag |= 4; 191*b1cdbd2cSJim Jagielski } 192*b1cdbd2cSJim Jagielski 193*b1cdbd2cSJim Jagielski if ( nFlag != 7 || !( xRootNameAccess.hasByName( "BigSubStream1" ) && xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) ) 194*b1cdbd2cSJim Jagielski { 195*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Root storage contains wrong list of children!" ); 196*b1cdbd2cSJim Jagielski return false; 197*b1cdbd2cSJim Jagielski } 198*b1cdbd2cSJim Jagielski 199*b1cdbd2cSJim Jagielski // get storage through XNameAccess 200*b1cdbd2cSJim Jagielski XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" ); 201*b1cdbd2cSJim Jagielski if ( xResultSubStorage == null ) 202*b1cdbd2cSJim Jagielski return false; 203*b1cdbd2cSJim Jagielski 204*b1cdbd2cSJim Jagielski if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) ) 205*b1cdbd2cSJim Jagielski return false; 206*b1cdbd2cSJim Jagielski 207*b1cdbd2cSJim Jagielski XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage ); 208*b1cdbd2cSJim Jagielski if ( xChildAccess == null ) 209*b1cdbd2cSJim Jagielski { 210*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" ); 211*b1cdbd2cSJim Jagielski return false; 212*b1cdbd2cSJim Jagielski } 213*b1cdbd2cSJim Jagielski 214*b1cdbd2cSJim Jagielski if ( !( xChildAccess.hasByName( "SubStream2" ) && xChildAccess.hasByName( "BigSubStream2" ) ) 215*b1cdbd2cSJim Jagielski || !xResultSubStorage.isStreamElement( "SubStream2" ) 216*b1cdbd2cSJim Jagielski || !xResultSubStorage.isStreamElement( "BigSubStream2" ) ) 217*b1cdbd2cSJim Jagielski { 218*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" ); 219*b1cdbd2cSJim Jagielski return false; 220*b1cdbd2cSJim Jagielski } 221*b1cdbd2cSJim Jagielski 222*b1cdbd2cSJim Jagielski return true; 223*b1cdbd2cSJim Jagielski } 224*b1cdbd2cSJim Jagielski catch( Exception e ) 225*b1cdbd2cSJim Jagielski { 226*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Exception: " + e ); 227*b1cdbd2cSJim Jagielski return false; 228*b1cdbd2cSJim Jagielski } 229*b1cdbd2cSJim Jagielski } 230*b1cdbd2cSJim Jagielski getStorageFromNameAccess( XNameAccess xAccess, String sName )231*b1cdbd2cSJim Jagielski public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName ) 232*b1cdbd2cSJim Jagielski { 233*b1cdbd2cSJim Jagielski try 234*b1cdbd2cSJim Jagielski { 235*b1cdbd2cSJim Jagielski Object oStorage = xAccess.getByName( sName ); 236*b1cdbd2cSJim Jagielski XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage ); 237*b1cdbd2cSJim Jagielski 238*b1cdbd2cSJim Jagielski if ( xResult != null ) 239*b1cdbd2cSJim Jagielski return xResult; 240*b1cdbd2cSJim Jagielski else 241*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" ); 242*b1cdbd2cSJim Jagielski } 243*b1cdbd2cSJim Jagielski catch( Exception e ) 244*b1cdbd2cSJim Jagielski { 245*b1cdbd2cSJim Jagielski m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e ); 246*b1cdbd2cSJim Jagielski } 247*b1cdbd2cSJim Jagielski 248*b1cdbd2cSJim Jagielski return null; 249*b1cdbd2cSJim Jagielski } 250*b1cdbd2cSJim Jagielski 251*b1cdbd2cSJim Jagielski } 252*b1cdbd2cSJim Jagielski 253