1 package be
.nikiroo
.utils
.test
;
3 import be
.nikiroo
.utils
.Progress
;
5 class ProgressTest
extends TestLauncher
{
6 public ProgressTest(String
[] args
) {
7 super("Progress reporting", args
);
9 addSeries(new TestLauncher("Simple progress", args
) {
11 addTest(new TestCase("Relative values and direct values") {
13 public void test() throws Exception
{
14 Progress p
= new Progress();
15 assertEquals(0, p
.getProgress());
16 assertEquals(0, p
.getRelativeProgress());
18 assertEquals(33, p
.getProgress());
19 assertEquals(0.33, p
.getRelativeProgress());
22 assertEquals(1, p
.getProgress());
24 generateAssertMessage("0.33..",
25 p
.getRelativeProgress()), true,
26 p
.getRelativeProgress() >= 0.332);
28 generateAssertMessage("0.33..",
29 p
.getRelativeProgress()), true,
30 p
.getRelativeProgress() <= 0.334);
34 addTest(new TestCase("Listeners at first level") {
38 public void test() throws Exception
{
39 Progress p
= new Progress();
40 p
.addProgressListener(new Progress
.ProgressListener() {
41 public void progress(Progress progress
, String name
) {
42 pg
= progress
.getProgress();
55 addSeries(new TestLauncher("Progress with children", args
) {
57 addTest(new TestCase("One child") {
59 public void test() throws Exception
{
60 Progress p
= new Progress();
61 Progress child
= new Progress();
63 p
.addProgress(child
, 100);
65 child
.setProgress(42);
66 assertEquals(42, p
.getProgress());
70 addTest(new TestCase("Multiple children") {
72 public void test() throws Exception
{
73 Progress p
= new Progress();
74 Progress child1
= new Progress();
75 Progress child2
= new Progress();
76 Progress child3
= new Progress();
78 p
.addProgress(child1
, 20);
79 p
.addProgress(child2
, 60);
80 p
.addProgress(child3
, 20);
82 child1
.setProgress(50);
83 assertEquals(10, p
.getProgress());
84 child2
.setProgress(100);
85 assertEquals(70, p
.getProgress());
86 child3
.setProgress(100);
87 assertEquals(90, p
.getProgress());
88 child1
.setProgress(100);
89 assertEquals(100, p
.getProgress());
93 addTest(new TestCase("Listeners with children") {
97 public void test() throws Exception
{
98 final Progress p
= new Progress();
99 Progress child1
= new Progress();
100 Progress child2
= new Progress();
101 p
.addProgress(child1
, 50);
102 p
.addProgress(child2
, 50);
104 p
.addProgressListener(new Progress
.ProgressListener() {
105 public void progress(Progress progress
, String name
) {
106 pg
= p
.getProgress();
110 child1
.setProgress(50);
111 assertEquals(25, pg
);
112 child2
.setProgress(100);
113 assertEquals(75, pg
);
114 child1
.setProgress(100);
115 assertEquals(100, pg
);
119 addTest(new TestCase("Listeners with children, not 1-100") {
123 public void test() throws Exception
{
124 final Progress p
= new Progress();
127 Progress child1
= new Progress();
130 Progress child2
= new Progress();
131 p
.addProgress(child1
, 500);
132 p
.addProgress(child2
, 500);
134 p
.addProgressListener(new Progress
.ProgressListener() {
135 public void progress(Progress progress
, String name
) {
136 pg
= p
.getProgress();
140 child1
.setProgress(1);
141 assertEquals(250, pg
);
142 child2
.setProgress(100);
143 assertEquals(750, pg
);
144 child1
.setProgress(2);
145 assertEquals(1000, pg
);
149 addTest(new TestCase(
150 "Listeners with children, not 1-100, local progress") {
154 public void test() throws Exception
{
155 final Progress p
= new Progress();
158 Progress child1
= new Progress();
161 Progress child2
= new Progress();
162 p
.addProgress(child1
, 400);
163 p
.addProgress(child2
, 400);
164 // 200 = local progress
166 p
.addProgressListener(new Progress
.ProgressListener() {
167 public void progress(Progress progress
, String name
) {
168 pg
= p
.getProgress();
172 child1
.setProgress(1);
173 assertEquals(200, pg
);
174 child2
.setProgress(100);
175 assertEquals(600, pg
);
177 assertEquals(700, pg
);
178 child1
.setProgress(2);
179 assertEquals(900, pg
);
181 assertEquals(1000, pg
);
185 addTest(new TestCase("Listeners with children, multi-thread") {
188 Object lock1
= new Object();
189 Object lock2
= new Object();
194 public void test() throws Exception
{
195 final Progress p
= new Progress(0, 200);
197 final Progress child1
= new Progress();
198 final Progress child2
= new Progress();
199 p
.addProgress(child1
, 100);
200 p
.addProgress(child2
, 100);
202 p
.addProgressListener(new Progress
.ProgressListener() {
203 public void progress(Progress progress
, String name
) {
204 int now
= p
.getProgress();
212 // Run 200 concurrent threads, 2 at a time allowed to
213 // make progress (each on a different child)
214 for (int i
= 0; i
<= 100; i
++) {
216 new Thread(new Runnable() {
218 synchronized (lock1
) {
219 if (step
> currentStep1
) {
221 child1
.setProgress(step
);
227 new Thread(new Runnable() {
229 synchronized (lock2
) {
230 if (step
> currentStep2
) {
232 child2
.setProgress(step
);
240 int timeout
= 20; // in 1/10th of seconds
241 for (i
= 0; i
< timeout
242 && (currentStep1
+ currentStep2
) < 200; i
++) {
246 assertEquals("The test froze at step " + currentStep1
247 + " + " + currentStep2
, true, i
< timeout
);
249 "There should not have any decresing steps",
251 assertEquals("The progress should have reached 200",
252 200, p
.getProgress());
254 "The progress should have reached completion",