PluginFile implements PluginInterface { void executeAction( Action action ) throws PluginException { try { switch( action.actionType ) { case DELETE: this.logger.fine( "Deleting " + action.parameter + "..." ); File f = new File( action.parameter ); Utils.deleteFilesRecursively( f ); break; case DOWNLOAD: this.logger.fine( "Downloading " + action.parameter + "..." ); URI uri = UriUtils.urlToUri( action.parameter ); File targetFile = new File( System.getProperty( "java.io.tmpdir" ), TMP_FILE ); InputStream in = null; try { in = uri.toURL().openStream(); Utils.copyStream( in, targetFile ); } finally { Utils.closeQuietly( in ); } break; case MOVE: List<String> parts = Utils.splitNicely( action.parameter, "->" ); if( parts.size() != 2 ) { this.logger.warning( "Invalid syntax for 'move' action. " + action.parameter ); } else { File source = new File( parts.get( 0 )); File target = new File( parts.get( 1 )); this.logger.fine( "Moving " + source + " to " + target + "..." ); if( ! source.renameTo( target )) throw new IOException( source + " could not be moved to " + target ); } break; case COPY: parts = Utils.splitNicely( action.parameter, "->" ); if( parts.size() != 2 ) { this.logger.warning( "Invalid syntax for 'copy' action. " + action.parameter ); } else { File source = new File( parts.get( 0 )); File target = new File( parts.get( 1 )); this.logger.fine( "Copying " + source + " to " + target + "..." ); Utils.copyStream( source, target ); } break; default: this.logger.fine( "Ignoring the action..." ); break; } } catch( Exception e ) { throw new PluginException( e ); } }  @Override String getPluginName(); @Override void setNames( String applicationName, String rootInstanceName ); @Override void initialize( Instance instance ); @Override void deploy( Instance instance ); @Override void start( Instance instance ); @Override void update( Instance instance, Import importChanged, InstanceStatus statusChanged ); @Override void stop( Instance instance ); @Override void undeploy( Instance instance ); static final String PLUGIN_NAME; }
@Test public void testInvalidCopyParameters() throws Exception { Action action = new Action( 1, ActionType.COPY, "my target only" ); new PluginFile().executeAction( action ); }
@Test public void testInvalidMoveParameters() throws Exception { Action action = new Action( 1, ActionType.MOVE, "1 -> 2 -> 3" ); new PluginFile().executeAction( action ); }