Skip to content

Commit

Permalink
#618: improve IdeProgressBarConsoleTest (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille authored Sep 20, 2024
1 parent 8f668ea commit 2e601cd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public AbstractIdeProgressBar(long maxLength) {
this.maxLength = maxLength;
}

/**
* @return the maximum length of a progress bar.
*/
@Override
public long getMaxLength() {

return maxLength;
Expand All @@ -33,6 +31,20 @@ public long getMaxLength() {
*/
protected abstract void doStepBy(long stepSize, long currentProgress);

/**
* Sets the progress bar to given step position while making sure to avoid overflow.
*
* @param stepPosition position to set to.
*/
protected void stepTo(long stepPosition) {

if ((this.maxLength > 0) && (stepPosition > this.maxLength)) {
stepPosition = this.maxLength; // clip to max avoiding overflow
}
this.currentProgress = stepPosition;
doStepTo(stepPosition);
}

/**
* Sets the progress bar to given step position.
*
Expand All @@ -48,22 +60,28 @@ public void stepBy(long stepSize) {
// check if maximum overflow
if (this.currentProgress > this.maxLength) {
this.currentProgress = this.maxLength;
doStepTo(this.maxLength);
stepTo(this.maxLength);
return;
}
}

doStepBy(stepSize, this.currentProgress);
}

@Override
public long getCurrentProgress() {

return this.currentProgress;
}

@Override
public void close() {
if (this.maxLength < 0) {
return;
}

if (this.currentProgress < this.maxLength) {
doStepTo(this.maxLength);
stepTo(this.maxLength);
}
}

Expand Down
10 changes: 10 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/io/IdeProgressBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ public interface IdeProgressBar extends AutoCloseable {
*/
void stepBy(long stepSize);

/**
* @return the maximum value when the progress bar has reached its end or {@code -1} if the maximum is undefined.
*/
long getMaxLength();

/**
* @return the total count accumulated with {@link #stepBy(long)} or {@link #getMaxLength()} in case of overflow.
*/
long getCurrentProgress();

@Override
void close();
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
package com.devonfw.tools.ide.io;

import java.nio.file.Path;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.os.SystemInfo;
import com.devonfw.tools.ide.os.SystemInfoMock;
import com.devonfw.tools.ide.os.SystemInfoImpl;

/**
* Test of {@link IdeProgressBarConsole}.
*/
// disabled on Windows - see https://github.com/devonfw/IDEasy/issues/618
@DisabledOnOs({ OS.WINDOWS })
public class IdeProgressBarConsoleTest extends AbstractIdeContextTest {

private IdeProgressBarConsole newProgressBar(long maxSize) {
SystemInfo systemInfo = SystemInfoImpl.INSTANCE;
IdeProgressBarConsole progressBarConsole = new IdeProgressBarConsole(systemInfo, "downloading", maxSize);
return progressBarConsole;
}

@Test
public void testProgressBarMaxSizeUnknownStepBy(@TempDir Path tempDir) throws Exception {
public void testProgressBarMaxSizeUnknownStepBy() throws Exception {
// arrange
IdeTestContext context = newContext(tempDir);
long stepSize = 10000L;
IdeProgressBarConsole progressBarConsole = new IdeProgressBarConsole(context.getSystemInfo(), "downloading", -1);
IdeProgressBarConsole progressBarConsole = newProgressBar(-1);

// act
progressBarConsole.stepBy(stepSize);
Expand All @@ -29,95 +34,87 @@ public void testProgressBarMaxSizeUnknownStepBy(@TempDir Path tempDir) throws Ex
// assert
assertThat(progressBarConsole.getProgressBar().isIndefinite()).isEqualTo(true);
assertThat(progressBarConsole.getProgressBar().getMax()).isEqualTo(stepSize);
assertThat(progressBarConsole.getCurrentProgress()).isEqualTo(stepSize);
}

@Test
public void testProgressBarMaxSizeUnknownDoStepTo(@TempDir Path tempDir) throws Exception {
public void testProgressBarMaxSizeUnknownDoStepTo() throws Exception {
// arrange
IdeTestContext context = newContext(tempDir);
long stepSize = 10000L;
IdeProgressBarConsole progressBarConsole = new IdeProgressBarConsole(context.getSystemInfo(), "downloading", -1);
IdeProgressBarConsole progressBarConsole = newProgressBar(-1);

// act
progressBarConsole.doStepTo(stepSize);
progressBarConsole.stepBy(100L);
progressBarConsole.stepTo(stepSize);
progressBarConsole.close();

// assert
assertThat(progressBarConsole.getProgressBar().isIndefinite()).isEqualTo(true);
assertThat(progressBarConsole.getProgressBar().getMax()).isEqualTo(stepSize);
assertThat(progressBarConsole.getCurrentProgress()).isEqualTo(stepSize);
}


@Test
public void testProgressBarMaxSizeKnownStepBy(@TempDir Path tempDir) throws Exception {
public void testProgressBarMaxSizeKnownStepBy() throws Exception {
// arrange
IdeTestContext context = newContext(tempDir);
long maxSize = 10000L;
IdeProgressBarConsole progressBarConsole = new IdeProgressBarConsole(context.getSystemInfo(), "downloading", maxSize);
IdeProgressBarConsole progressBarConsole = newProgressBar(maxSize);

//act
progressBarConsole.stepBy(maxSize);
progressBarConsole.close();

// assert
assertThat(progressBarConsole.getProgressBar().isIndefinite()).isEqualTo(false);
assertThat(progressBarConsole.getProgressBar().getMax()).isEqualTo(maxSize);
assertThat(progressBarConsole.getMaxLength()).isEqualTo(maxSize);
assertThat(progressBarConsole.getCurrentProgress()).isEqualTo(maxSize);
}

@Test
public void testProgressBarMaxSizeKnownDoStepTo(@TempDir Path tempDir) throws Exception {
public void testProgressBarMaxSizeKnownDoStepTo() throws Exception {
// arrange
IdeTestContext context = newContext(tempDir);
long maxSize = 10000L;
IdeProgressBarConsole progressBarConsole = new IdeProgressBarConsole(context.getSystemInfo(), "downloading", maxSize);
IdeProgressBarConsole progressBarConsole = newProgressBar(maxSize);

//act
progressBarConsole.doStepTo(maxSize);
progressBarConsole.close();

// assert
assertThat(progressBarConsole.getProgressBar().isIndefinite()).isEqualTo(false);
assertThat(progressBarConsole.getProgressBar().getMax()).isEqualTo(maxSize);
assertThat(progressBarConsole.getMaxLength()).isEqualTo(maxSize);
assertThat(progressBarConsole.getCurrentProgress()).isEqualTo(maxSize);
}

@Test
public void testProgressBarMaxSizeKnownIncompleteSteps(@TempDir Path tempDir) throws Exception {
public void testProgressBarMaxSizeKnownIncompleteSteps() throws Exception {
// arrange
IdeTestContext context = newContext(tempDir);
long maxSize = 10000L;
IdeProgressBarConsole progressBarConsole = new IdeProgressBarConsole(context.getSystemInfo(), "downloading", maxSize);
IdeProgressBarConsole progressBarConsole = newProgressBar(maxSize);

// act
progressBarConsole.stepBy(1L);
// assert
assertThat(progressBarConsole.getCurrentProgress()).isEqualTo(1L);
// act
progressBarConsole.close();

// assert
assertThat(progressBarConsole.getProgressBar().getMax()).isEqualTo(maxSize);
assertThat(progressBarConsole.getMaxLength()).isEqualTo(maxSize);
}

@Test
public void testProgressBarMaxOverflow(@TempDir Path tempDir) throws Exception {
public void testProgressBarMaxOverflow() throws Exception {
// arrange
IdeTestContext context = newContext(tempDir);
long maxSize = 10000L;
IdeProgressBarConsole progressBarConsole = new IdeProgressBarConsole(context.getSystemInfo(), "downloading", maxSize);
IdeProgressBarConsole progressBarConsole = newProgressBar(maxSize);
// act
progressBarConsole.stepBy(20000L);
progressBarConsole.close();

//assert
assertThat(progressBarConsole.getProgressBar().getMax()).isEqualTo(maxSize);
assertThat(progressBarConsole.getCurrentProgress()).isEqualTo(maxSize);
}

@Test
public void testProgressBarStyleSwitchOnNonWindows(@TempDir Path tempDir) throws Exception {
// arrange
IdeTestContext context = newContext(tempDir);
SystemInfo systemInfo = SystemInfoMock.of("linux");
context.setSystemInfo(systemInfo);
long maxSize = 10000L;
IdeProgressBarConsole progressBarConsole = new IdeProgressBarConsole(context.getSystemInfo(), "downloading", maxSize);
// act
progressBarConsole.close();
}
}

0 comments on commit 2e601cd

Please sign in to comment.