-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from brianmcmichael/progress
Progress tracking
- Loading branch information
Showing
5 changed files
with
205 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/brianmcmichael/sagu/ui/OneFileProgressListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Simple Amazon Glacier Uploader - GUI client for Amazon Glacier | ||
* Copyright (C) 2012-2015 Brian L. McMichael, Libor Rysavy and other contributors | ||
* | ||
* This program is free software licensed under GNU General Public License | ||
* found in the LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.brianmcmichael.sagu.ui; | ||
|
||
import com.amazonaws.event.ProgressEvent; | ||
import com.amazonaws.event.ProgressTracker; | ||
|
||
/** | ||
* Listener for upload progress reporting of one file (currently being uploaded). | ||
*/ | ||
public class OneFileProgressListener extends ProgressTracker { | ||
|
||
private final UploadWindow uploadWindow; | ||
private final long totalBytesToTransfer; | ||
|
||
public OneFileProgressListener(final UploadWindow uploadWindow, final long totalBytesToTransfer) { | ||
this.uploadWindow = uploadWindow; | ||
this.totalBytesToTransfer = totalBytesToTransfer; | ||
} | ||
|
||
@Override | ||
public void progressChanged(final ProgressEvent progressEvent) { | ||
super.progressChanged(progressEvent); | ||
final int percents = getPercents(getProgress().getRequestBytesTransferred()); | ||
uploadWindow.updateOneFileProgress(percents); | ||
} | ||
|
||
int getPercents(final double bytesTransferred) { | ||
return totalBytesToTransfer <= 0 | ||
? 0 | ||
: (int) ((bytesTransferred / (double) totalBytesToTransfer) * 100.0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/test/java/com/brianmcmichael/sagu/ui/OneFileProgressListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Simple Amazon Glacier Uploader - GUI client for Amazon Glacier | ||
* Copyright (C) 2012-2015 Brian L. McMichael, Libor Rysavy and other contributors | ||
* | ||
* This program is free software licensed under GNU General Public License | ||
* found in the LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.brianmcmichael.sagu.ui; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.testng.Assert.*; | ||
|
||
public class OneFileProgressListenerTest { | ||
|
||
@Test | ||
public void getPercentsShouldReturnZeroWhenTransferredZero() throws Exception { | ||
final OneFileProgressListener listener = new OneFileProgressListener(mock(UploadWindow.class), 1000); | ||
assertThat(listener.getPercents(0), is(0)); | ||
} | ||
|
||
@Test | ||
public void getPercentsShouldReturnFiftyWhenTransferredHalf() throws Exception { | ||
final OneFileProgressListener listener = new OneFileProgressListener(mock(UploadWindow.class), 1000); | ||
assertThat(listener.getPercents(500), is(50)); | ||
} | ||
|
||
@Test | ||
public void getPercentsShouldReturnHundredWhenEverythingTransferred() throws Exception { | ||
final OneFileProgressListener listener = new OneFileProgressListener(mock(UploadWindow.class), 1000); | ||
assertThat(listener.getPercents(1000), is(100)); | ||
} | ||
|
||
@Test | ||
public void getPercentsShouldReturnZeroWhenTotalZero() throws Exception { | ||
final OneFileProgressListener listener = new OneFileProgressListener(mock(UploadWindow.class), 0); | ||
assertThat(listener.getPercents(0), is(0)); | ||
} | ||
|
||
@Test | ||
public void getPercentsShouldReturnZeroWhenTotalNegative() throws Exception { | ||
final OneFileProgressListener listener = new OneFileProgressListener(mock(UploadWindow.class), -1); | ||
assertThat(listener.getPercents(0), is(0)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/com/brianmcmichael/sagu/ui/UploadWindowTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Simple Amazon Glacier Uploader - GUI client for Amazon Glacier | ||
* Copyright (C) 2012-2015 Brian L. McMichael, Libor Rysavy and other contributors | ||
* | ||
* This program is free software licensed under GNU General Public License | ||
* found in the LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.brianmcmichael.sagu.ui; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.*; | ||
|
||
public class UploadWindowTest { | ||
|
||
// just for manual UI testing | ||
@Test(enabled = false) | ||
public void testUI() throws Exception { | ||
new UploadWindow(); | ||
Thread.sleep(10000); | ||
} | ||
} |