Added AbstractConsoleDownloadMonitor to avoid code duplication

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@425102 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Carlos Sanchez Gonzalez 2006-07-24 16:01:07 +00:00
parent c5864a895d
commit 20b7d33b07
6 changed files with 275 additions and 84 deletions

View File

@ -0,0 +1,88 @@
package org.apache.maven.cli;
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.MavenTransferListener;
import org.apache.maven.wagon.WagonConstants;
import org.apache.maven.wagon.events.TransferEvent;
import org.codehaus.plexus.logging.AbstractLogEnabled;
/**
* Abstract console download progress meter.
*
* @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
* @version $Id$
* @since 2.0.5
*/
public abstract class AbstractConsoleDownloadMonitor
extends AbstractLogEnabled
implements MavenTransferListener
{
public void transferInitiated( TransferEvent transferEvent )
{
String message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";
String url = transferEvent.getWagon().getRepository().getUrl();
// TODO: can't use getLogger() because this isn't currently instantiated as a component
System.out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
}
/**
* Do nothing
*/
public void transferStarted( TransferEvent transferEvent )
{
// This space left intentionally blank
}
/**
* Do nothing
*/
public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
{
// This space left intentionally blank
}
public void transferCompleted( TransferEvent transferEvent )
{
long contentLength = transferEvent.getResource().getContentLength();
if ( contentLength != WagonConstants.UNKNOWN_LENGTH )
{
String type = ( transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" );
String l = contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b";
System.out.println( l + " " + type );
}
}
public void transferError( TransferEvent transferEvent )
{
// TODO: can't use getLogger() because this isn't currently instantiated as a component
transferEvent.getException().printStackTrace();
}
/**
* Do nothing
*/
public void debug( String message )
{
// TODO: can't use getLogger() because this isn't currently instantiated as a component
// getLogger().debug( message );
}
}

View File

@ -1,7 +1,7 @@
package org.apache.maven.cli; package org.apache.maven.cli;
/* /*
* Copyright 2001-2005 The Apache Software Foundation. * Copyright 2001-2006 The Apache Software Foundation.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,11 +16,7 @@ package org.apache.maven.cli;
* limitations under the License. * limitations under the License.
*/ */
import org.apache.maven.wagon.WagonConstants;
import org.apache.maven.wagon.events.TransferEvent; import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.events.TransferListener;
import org.apache.maven.MavenTransferListener;
import org.codehaus.plexus.logging.AbstractLogEnabled;
/** /**
* Console download progress meter. * Console download progress meter.
@ -29,8 +25,7 @@ import org.codehaus.plexus.logging.AbstractLogEnabled;
* @version $Id$ * @version $Id$
*/ */
public class BatchModeDownloadMonitor public class BatchModeDownloadMonitor
extends AbstractLogEnabled extends AbstractConsoleDownloadMonitor
implements MavenTransferListener
{ {
public void transferInitiated( TransferEvent transferEvent ) public void transferInitiated( TransferEvent transferEvent )
{ {
@ -41,40 +36,4 @@ public class BatchModeDownloadMonitor
// TODO: can't use getLogger() because this isn't currently instantiated as a component // TODO: can't use getLogger() because this isn't currently instantiated as a component
System.out.println( message + ": " + url + "/" + transferEvent.getResource().getName() ); System.out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
} }
public void transferStarted( TransferEvent transferEvent )
{
// This space left intentionally blank
}
public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
{
// This space left intentionally blank
}
public void transferCompleted( TransferEvent transferEvent )
{
long contentLength = transferEvent.getResource().getContentLength();
if ( contentLength != WagonConstants.UNKNOWN_LENGTH )
{
String type = ( transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" );
String l = contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b";
System.out.println( l + " " + type );
}
}
public void transferError( TransferEvent transferEvent )
{
// TODO: can't use getLogger() because this isn't currently instantiated as a component
transferEvent.getException().printStackTrace();
}
public void debug( String message )
{
// TODO: can't use getLogger() because this isn't currently instantiated as a component
// getLogger().debug( message );
}
} }

View File

@ -18,9 +18,6 @@ package org.apache.maven.cli;
import org.apache.maven.wagon.WagonConstants; import org.apache.maven.wagon.WagonConstants;
import org.apache.maven.wagon.events.TransferEvent; import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.events.TransferListener;
import org.apache.maven.MavenTransferListener;
import org.codehaus.plexus.logging.AbstractLogEnabled;
/** /**
* Console download progress meter. * Console download progress meter.
@ -29,28 +26,17 @@ import org.codehaus.plexus.logging.AbstractLogEnabled;
* @version $Id$ * @version $Id$
*/ */
public class ConsoleDownloadMonitor public class ConsoleDownloadMonitor
extends AbstractLogEnabled extends AbstractConsoleDownloadMonitor
implements MavenTransferListener
{ {
private long complete; private long complete;
public void transferInitiated( TransferEvent transferEvent ) public void transferInitiated( TransferEvent transferEvent )
{ {
String message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading"; super.transferInitiated( transferEvent );
String url = transferEvent.getWagon().getRepository().getUrl();
// TODO: can't use getLogger() because this isn't currently instantiated as a component
System.out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
complete = 0; complete = 0;
} }
public void transferStarted( TransferEvent transferEvent )
{
// This space left intentionally blank
}
public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length ) public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
{ {
long total = transferEvent.getResource().getContentLength(); long total = transferEvent.getResource().getContentLength();
@ -67,30 +53,5 @@ public class ConsoleDownloadMonitor
System.out.print( complete + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : total + "b" ) + "\r" ); System.out.print( complete + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : total + "b" ) + "\r" );
} }
} }
public void transferCompleted( TransferEvent transferEvent )
{
long contentLength = transferEvent.getResource().getContentLength();
if ( contentLength != WagonConstants.UNKNOWN_LENGTH )
{
String type = ( transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" );
String l = contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b";
System.out.println( l + " " + type );
}
}
public void transferError( TransferEvent transferEvent )
{
// TODO: can't use getLogger() because this isn't currently instantiated as a component
transferEvent.getException().printStackTrace();
}
public void debug( String message )
{
// TODO: can't use getLogger() because this isn't currently instantiated as a component
// getLogger().debug( message );
}
} }

View File

@ -0,0 +1,113 @@
package org.apache.maven.cli;
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import junit.framework.TestCase;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.authentication.AuthenticationException;
import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.providers.file.FileWagon;
import org.apache.maven.wagon.repository.Repository;
import org.apache.maven.wagon.resource.Resource;
/**
* Test for {@link AbstractConsoleDownloadMonitor}
*
* @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
* @version $Id$
*/
public abstract class AbstractConsoleDownloadMonitorTest
extends TestCase
{
private AbstractConsoleDownloadMonitor monitor;
public AbstractConsoleDownloadMonitorTest()
{
super();
}
public void setMonitor( AbstractConsoleDownloadMonitor monitor )
{
this.monitor = monitor;
}
public AbstractConsoleDownloadMonitor getMonitor()
{
return monitor;
}
public void testTransferInitiated()
throws Exception
{
monitor.transferInitiated( new TransferEventMock() );
}
public void testTransferStarted()
throws Exception
{
monitor.transferStarted( new TransferEventMock() );
}
public void testTransferProgress()
throws Exception
{
byte[] buffer = new byte[1000];
monitor.transferProgress( new TransferEventMock(), buffer, 1000 );
}
public void testTransferCompleted()
throws Exception
{
monitor.transferCompleted( new TransferEventMock() );
}
public void testTransferError()
throws Exception
{
monitor.transferError( new TransferEventMock( new RuntimeException() ) );
}
public void testDebug()
throws Exception
{
monitor.debug( "msg" );
}
private class TransferEventMock
extends TransferEvent
{
public TransferEventMock()
throws ConnectionException, AuthenticationException
{
super( new FileWagon(), new Resource(), TransferEvent.TRANSFER_INITIATED, TransferEvent.REQUEST_GET );
getResource().setContentLength( 100000 );
Repository repository = new Repository();
getWagon().connect( repository );
}
public TransferEventMock( Exception exception )
throws ConnectionException, AuthenticationException
{
super( new FileWagon(), new Resource(), exception, TransferEvent.REQUEST_GET );
getResource().setContentLength( 100000 );
Repository repository = new Repository();
getWagon().connect( repository );
}
}
}

View File

@ -0,0 +1,35 @@
package org.apache.maven.cli;
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Test for {@link BatchModeDownloadMonitor}
*
* @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
* @version $Id$
*/
public class BatchModeDownloadMonitorTest
extends AbstractConsoleDownloadMonitorTest
{
protected void setUp()
throws Exception
{
super.setMonitor( new BatchModeDownloadMonitor() );
super.setUp();
}
}

View File

@ -0,0 +1,35 @@
package org.apache.maven.cli;
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Test for {@link ConsoleDownloadMonitor}
*
* @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
* @version $Id$
*/
public class ConsoleDownloadMonitorTest
extends AbstractConsoleDownloadMonitorTest
{
protected void setUp()
throws Exception
{
super.setMonitor( new ConsoleDownloadMonitor() );
super.setUp();
}
}