aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/TextProgressMonitorTest.java
blob: 55ca2cdea356238045fdfa2706d95a7faec0a9d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (C) 2023, SAP SE or an SAP affiliate company and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.lib;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class TextProgressMonitorTest {

	private TextProgressMonitor m;

	private ByteArrayOutputStream buf;

	@Before
	public void setup() {
		buf = new ByteArrayOutputStream();
		m = new TextProgressMonitor(
				new OutputStreamWriter(buf, StandardCharsets.UTF_8));
	}

	@Test
	public void testSimple() throws Exception {
		m.beginTask("task", 10);
		for (int i = 0; i < 10; i++) {
			m.update(1);
		}
		m.endTask();
		Assert.assertArrayEquals(
				new String[] { "", "task:                    10% ( 1/10)",
						"task:                    20% ( 2/10)",
						"task:                    30% ( 3/10)",
						"task:                    40% ( 4/10)",
						"task:                    50% ( 5/10)",
						"task:                    60% ( 6/10)",
						"task:                    70% ( 7/10)",
						"task:                    80% ( 8/10)",
						"task:                    90% ( 9/10)",
						"task:                   100% (10/10)",
						"task:                   100% (10/10)\n" },
				bufLines());
	}

	@Test
	public void testLargeNumbers() throws Exception {
		m.beginTask("task", 1_000_000_000);
		for (int i = 0; i < 10; i++) {
			m.update(100_000_000);
		}
		m.endTask();
		Assert.assertArrayEquals(
				new String[] { "",
						"task:                    10% ( 100000000/1000000000)",
						"task:                    20% ( 200000000/1000000000)",
						"task:                    30% ( 300000000/1000000000)",
						"task:                    40% ( 400000000/1000000000)",
						"task:                    50% ( 500000000/1000000000)",
						"task:                    60% ( 600000000/1000000000)",
						"task:                    70% ( 700000000/1000000000)",
						"task:                    80% ( 800000000/1000000000)",
						"task:                    90% ( 900000000/1000000000)",
						"task:                   100% (1000000000/1000000000)",
						"task:                   100% (1000000000/1000000000)\n" },
				bufLines());
	}

	String[] bufLines() throws UnsupportedEncodingException {
		String s = new String(buf.toString(StandardCharsets.UTF_8.name()));
		return s.split("\r");
	}
}