1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package com.sun.star.comp.ucb; 25 26 import complexlib.ComplexTestCase; 27 import com.sun.star.lang.IllegalArgumentException; 28 import com.sun.star.lang.XMultiServiceFactory; 29 import com.sun.star.task.XInteractionAbort; 30 import com.sun.star.task.XInteractionHandler; 31 import com.sun.star.task.XInteractionRequest; 32 import com.sun.star.task.XInteractionContinuation; 33 import com.sun.star.ucb.Command; 34 import com.sun.star.ucb.GlobalTransferCommandArgument; 35 import com.sun.star.ucb.NameClash; 36 import com.sun.star.ucb.NameClashResolveRequest; 37 import com.sun.star.ucb.TransferCommandOperation; 38 import com.sun.star.ucb.XCommandEnvironment; 39 import com.sun.star.ucb.XCommandProcessor; 40 import com.sun.star.ucb.XInteractionReplaceExistingData; 41 import com.sun.star.ucb.XInteractionSupplyName; 42 import com.sun.star.ucb.XProgressHandler; 43 import com.sun.star.uno.AnyConverter; 44 import com.sun.star.uno.UnoRuntime; 45 import java.io.PrintWriter; 46 47 public final class GlobalTransfer_Test extends ComplexTestCase { 48 49 static private final String fileName 50 = "testcase-do-not-remove.sxw"; 51 static private final String httpSourceDir 52 = "http://so-berlin/~webdav/"; 53 static private final String httpTargetDir 54 = "http://so-berlin/~webdav/"; 55 static private final String fileSourceDir 56 = "file:///d:/temp/source/"; 57 static private final String fileTargetDir 58 = "file:///d:/temp/"; 59 getTestObjectName()60 public String getTestObjectName() { 61 return getClass().getName(); 62 } 63 getTestMethodNames()64 public String[] getTestMethodNames() { 65 return new String[] { "testNameClashASK" }; 66 } 67 testNameClashASK()68 public void testNameClashASK() throws Exception { 69 Object oObj = null; 70 try { 71 XMultiServiceFactory xMSF = (XMultiServiceFactory)param.getMSF(); 72 oObj 73 = xMSF.createInstance( "com.sun.star.ucb.UniversalContentBroker" ); 74 } 75 catch(com.sun.star.uno.Exception e) { 76 e.printStackTrace((PrintWriter)log); 77 78 // After this exception the test has failed and cannot continue. 79 failed( "Cannot create service instance: com.sun.star.ucb." + 80 "UniversalContentBroker. message:" + e.getMessage() ); 81 return; 82 } 83 84 if ( oObj == null ) { 85 failed( "Cannot create service instance: com.sun.star.ucb." + 86 "UniversalContentBroker"); 87 return; 88 } 89 90 XCommandProcessor xCmdProc 91 = (XCommandProcessor)UnoRuntime.queryInterface( 92 XCommandProcessor.class, oObj ); 93 assure( "UCB does not implement mandatory interface XCommandProcessor!", 94 xCmdProc != null); 95 96 ResourceCopier cp = new ResourceCopier( xCmdProc ); 97 98 try { 99 cp.copyResource( httpSourceDir, fileTargetDir, fileName ); 100 } 101 catch(com.sun.star.uno.Exception e) { 102 e.printStackTrace((PrintWriter)log); 103 104 // After this exception the test has failed and cannot continue. 105 failed( "Could not copy resource:" + e.getMessage() ); 106 } 107 } 108 109 private final class ResourceCopier { 110 private XCommandProcessor m_cmdProc = null; 111 private CommandEnvironment m_env = new CommandEnvironment(); 112 ResourceCopier( XCommandProcessor oCmdProc )113 ResourceCopier( XCommandProcessor oCmdProc ) 114 { 115 m_cmdProc = oCmdProc; 116 } 117 copyResource( String sourceDir, String targetDir, String fileName )118 public void copyResource( 119 String sourceDir, String targetDir, String fileName ) 120 throws Exception { 121 122 GlobalTransferCommandArgument transferArg 123 = new GlobalTransferCommandArgument( 124 TransferCommandOperation.COPY, 125 sourceDir + fileName, 126 targetDir, 127 "", 128 NameClash.ASK ); 129 130 Command cmd = new Command( "globalTransfer", -1, transferArg ); 131 132 m_cmdProc.execute( cmd, 0, m_env ); 133 } 134 } 135 136 private final class CommandEnvironment implements XCommandEnvironment { 137 private final XInteractionHandler m_InteractionHandler 138 = new InteractionHadler(); 139 getInteractionHandler()140 public XInteractionHandler getInteractionHandler() { 141 return m_InteractionHandler; 142 } 143 getProgressHandler()144 public XProgressHandler getProgressHandler() { 145 // not needed for the test. 146 return null; 147 } 148 } 149 150 private final class InteractionHadler implements XInteractionHandler { handle( XInteractionRequest Request )151 public void handle( /*IN*/XInteractionRequest Request ) { 152 153 log.println( "Interaction Handler called." ); 154 155 try { 156 NameClashResolveRequest req = (NameClashResolveRequest) 157 AnyConverter.toObject( 158 NameClashResolveRequest.class, Request.getRequest() ); 159 160 log.println( "Interaction Handler: NameClashResolveRequest: " 161 + req.ClashingName ); 162 163 XInteractionContinuation[] continuations 164 = Request.getContinuations(); 165 for ( int i = 0; i < continuations.length; ++i ) { 166 /* 167 XInteractionAbort xAbort 168 = (XInteractionAbort)UnoRuntime.queryInterface( 169 XInteractionAbort.class, continuations[ i ] ); 170 if ( xAbort != null ) { 171 log.println( "Interaction Handler selects: ABORT" ); 172 xAbort.select(); 173 return; 174 } 175 */ 176 /* 177 XInteractionReplaceExistingData xReplace 178 = (XInteractionReplaceExistingData) 179 UnoRuntime.queryInterface( 180 XInteractionReplaceExistingData.class, 181 continuations[ i ] ); 182 if ( xReplace != null ) { 183 log.println( "Interaction Handler selects: REPLACE" ); 184 xReplace.select(); 185 return; 186 } 187 */ 188 XInteractionSupplyName xSupplyName 189 = (XInteractionSupplyName) 190 UnoRuntime.queryInterface( 191 XInteractionSupplyName.class, 192 continuations[ i ] ); 193 if ( xSupplyName != null ) { 194 String newname = "renamed_" + req.ClashingName; 195 log.println( "Interaction Handler selects: NEW NAME: " 196 + newname ); 197 xSupplyName.setName( newname ); 198 xSupplyName.select(); 199 return; 200 } 201 } 202 } 203 catch ( IllegalArgumentException e ) 204 { 205 e.printStackTrace(); 206 } 207 } 208 } 209 210 } 211