1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.concurrent;
  27 
  28 import javafx.concurrent.mocks.ProgressingTask;
  29 import org.junit.Before;
  30 import org.junit.Test;
  31 
  32 import static org.junit.Assert.*;
  33 
  34 /**
  35  * A Task which always completes normally, but which also progresses from
  36  * 0 to 20 (inclusive). Initially the indeterminate state is used for
  37  * progress and maxProgress, but during execution the progress and
  38  * maxProgress are updated.
  39  */
  40 public class TaskProgressTest {
  41     private Task task;
  42 
  43     @Before public void setup() {
  44         task = new ProgressingTask();
  45     }
  46 
  47     /************************************************************************
  48      * Run the task and check that the final progress and maxProgress and
  49      * percentDone have correct values.
  50      ***********************************************************************/
  51 
  52     @Test public void afterRunningWorkDoneShouldBe_20() {
  53         task.run();
  54         assertEquals(20, task.getWorkDone(), 0);
  55     }
  56 
  57     @Test public void afterRunningTotalWorkShouldBe_20() {
  58         task.run();
  59         assertEquals(20, task.getTotalWork(), 0);
  60     }
  61 
  62     @Test public void afterRunningProgressShouldBe_1() {
  63         task.run();
  64         assertEquals(1, task.getProgress(), 0);
  65     }
  66 
  67     /************************************************************************
  68      * Test the updateProgress method on Task, that various inputs lead to
  69      * expected outputs according to the specification
  70      ***********************************************************************/
  71 
  72     @Test public void updateProgress_Long_0_100() {
  73         task.updateProgress(0, 100);
  74         assertEquals(0, task.getProgress(), 0);
  75         assertEquals(0, task.getWorkDone(), 0);
  76         assertEquals(100, task.getTotalWork(), 0);
  77     }
  78 
  79     @Test public void updateProgress_Long_n1_100() {
  80         task.updateProgress(-1, 100);
  81         assertEquals(-1, task.getProgress(), 0);
  82         assertEquals(-1, task.getWorkDone(), 0);
  83         assertEquals(100, task.getTotalWork(), 0);
  84     }
  85 
  86     @Test public void updateProgress_Long_n10_100() {
  87         task.updateProgress(-10, 100);
  88         assertEquals(-1, task.getProgress(), 0);
  89         assertEquals(-1, task.getWorkDone(), 0);
  90         assertEquals(100, task.getTotalWork(), 0);
  91     }
  92 
  93     @Test public void updateProgress_Long_MIN_VALUE_100() {
  94         task.updateProgress(Long.MIN_VALUE, 100);
  95         assertEquals(-1, task.getProgress(), 0);
  96         assertEquals(-1, task.getWorkDone(), 0);
  97         assertEquals(100, task.getTotalWork(), 0);
  98     }
  99 
 100     @Test public void updateProgress_Long_10_100() {
 101         task.updateProgress(10, 100);
 102         assertEquals(.1, task.getProgress(), 0);
 103         assertEquals(10, task.getWorkDone(), 0);
 104         assertEquals(100, task.getTotalWork(), 0);
 105     }
 106 
 107     @Test public void updateProgress_Long_100_100() {
 108         task.updateProgress(100, 100);
 109         assertEquals(1, task.getProgress(), 0);
 110         assertEquals(100, task.getWorkDone(), 0);
 111         assertEquals(100, task.getTotalWork(), 0);
 112     }
 113 
 114     @Test public void updateProgress_Long_110_100() {
 115         task.updateProgress(110, 100);
 116         assertEquals(1, task.getProgress(), 0);
 117         assertEquals(100, task.getWorkDone(), 0);
 118         assertEquals(100, task.getTotalWork(), 0);
 119     }
 120 
 121     @Test public void updateProgress_Long_MAX_VALUE_100() {
 122         task.updateProgress(Long.MAX_VALUE, 100);
 123         assertEquals(1, task.getProgress(), 0);
 124         assertEquals(100, task.getWorkDone(), 0);
 125         assertEquals(100, task.getTotalWork(), 0);
 126     }
 127 
 128     @Test public void updateProgress_Long_0_n1() {
 129         task.updateProgress(0, -1);
 130         assertEquals(-1, task.getProgress(), 0);
 131         assertEquals(-1, task.getWorkDone(), 0);
 132         assertEquals(-1, task.getTotalWork(), 0);
 133     }
 134 
 135     @Test public void updateProgress_Long_0_n10() {
 136         task.updateProgress(0, -10);
 137         assertEquals(-1, task.getProgress(), 0);
 138         assertEquals(-1, task.getWorkDone(), 0);
 139         assertEquals(-1, task.getTotalWork(), 0);
 140     }
 141 
 142     @Test public void updateProgress_Long_0_MIN_VALUE() {
 143         task.updateProgress(0, Long.MIN_VALUE);
 144         assertEquals(-1, task.getProgress(), 0);
 145         assertEquals(-1, task.getWorkDone(), 0);
 146         assertEquals(-1, task.getTotalWork(), 0);
 147     }
 148 
 149     @Test public void updateProgress_Long_100_10() {
 150         task.updateProgress(100, 10);
 151         assertEquals(1, task.getProgress(), 0);
 152         assertEquals(10, task.getWorkDone(), 0);
 153         assertEquals(10, task.getTotalWork(), 0);
 154     }
 155 
 156     @Test public void updateProgress_Long_100_MAX_VALUE() {
 157         task.updateProgress(100, Long.MAX_VALUE);
 158         assertEquals(100.0 / Long.MAX_VALUE, task.getProgress(), 0);
 159         assertEquals(100, task.getWorkDone(), 0);
 160         assertEquals(Long.MAX_VALUE, task.getTotalWork(), 0);
 161     }
 162 
 163     /* Now test the Double variants (Infinity, NaN)*/
 164     @Test public void updateProgress_Double_Infinity_100() {
 165         task.updateProgress(Double.POSITIVE_INFINITY, 100);
 166         assertEquals(-1, task.getProgress(), 0);
 167         assertEquals(-1, task.getWorkDone(), 0);
 168         assertEquals(100, task.getTotalWork(), 0);
 169     }
 170 
 171     @Test public void updateProgress_Double_NInfinity_100() {
 172         task.updateProgress(Double.NEGATIVE_INFINITY, 100);
 173         assertEquals(-1, task.getProgress(), 0);
 174         assertEquals(-1, task.getWorkDone(), 0);
 175         assertEquals(100, task.getTotalWork(), 0);
 176     }
 177 
 178     @Test public void updateProgress_Double_NaN_100() {
 179         task.updateProgress(Double.NaN, 100);
 180         assertEquals(-1, task.getProgress(), 0);
 181         assertEquals(-1, task.getWorkDone(), 0);
 182         assertEquals(100, task.getTotalWork(), 0);
 183     }
 184 
 185     @Test public void updateProgress_Double_0_Infinity() {
 186         task.updateProgress(0, Double.POSITIVE_INFINITY);
 187         assertEquals(-1, task.getProgress(), 0);
 188         assertEquals(-1, task.getWorkDone(), 0);
 189         assertEquals(-1, task.getTotalWork(), 0);
 190     }
 191 
 192     @Test public void updateProgress_Double_0_NInfinity() {
 193         task.updateProgress(0, Double.NEGATIVE_INFINITY);
 194         assertEquals(-1, task.getProgress(), 0);
 195         assertEquals(-1, task.getWorkDone(), 0);
 196         assertEquals(-1, task.getTotalWork(), 0);
 197     }
 198 
 199     @Test public void updateProgress_Double_0_NaN() {
 200         task.updateProgress(0, Double.NaN);
 201         assertEquals(-1, task.getProgress(), 0);
 202         assertEquals(-1, task.getWorkDone(), 0);
 203         assertEquals(-1, task.getTotalWork(), 0);
 204     }
 205 
 206     @Test public void updateProgress_Double_Infinity_Infinity() {
 207         task.updateProgress(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
 208         assertEquals(-1, task.getProgress(), 0);
 209         assertEquals(-1, task.getWorkDone(), 0);
 210         assertEquals(-1, task.getTotalWork(), 0);
 211     }
 212 
 213     @Test public void updateProgress_Double_NInfinity_Infinity() {
 214         task.updateProgress(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
 215         assertEquals(-1, task.getProgress(), 0);
 216         assertEquals(-1, task.getWorkDone(), 0);
 217         assertEquals(-1, task.getTotalWork(), 0);
 218     }
 219 
 220     @Test public void updateProgress_Double_Infinity_NInfinity() {
 221         task.updateProgress(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY);
 222         assertEquals(-1, task.getProgress(), 0);
 223         assertEquals(-1, task.getWorkDone(), 0);
 224         assertEquals(-1, task.getTotalWork(), 0);
 225     }
 226 
 227     @Test public void updateProgress_Double_NInfinity_NInfinity() {
 228         task.updateProgress(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
 229         assertEquals(-1, task.getProgress(), 0);
 230         assertEquals(-1, task.getWorkDone(), 0);
 231         assertEquals(-1, task.getTotalWork(), 0);
 232     }
 233 
 234     @Test public void updateProgress_Double_Infinity_NaN() {
 235         task.updateProgress(Double.POSITIVE_INFINITY, Double.NaN);
 236         assertEquals(-1, task.getProgress(), 0);
 237         assertEquals(-1, task.getWorkDone(), 0);
 238         assertEquals(-1, task.getTotalWork(), 0);
 239     }
 240 
 241     @Test public void updateProgress_Double_NInfinity_NaN() {
 242         task.updateProgress(Double.NEGATIVE_INFINITY, Double.NaN);
 243         assertEquals(-1, task.getProgress(), 0);
 244         assertEquals(-1, task.getWorkDone(), 0);
 245         assertEquals(-1, task.getTotalWork(), 0);
 246     }
 247 
 248     @Test public void updateProgress_Double_NaN_Infinity() {
 249         task.updateProgress(Double.NaN, Double.POSITIVE_INFINITY);
 250         assertEquals(-1, task.getProgress(), 0);
 251         assertEquals(-1, task.getWorkDone(), 0);
 252         assertEquals(-1, task.getTotalWork(), 0);
 253     }
 254 
 255     @Test public void updateProgress_Double_NaN_NInfinity() {
 256         task.updateProgress(Double.NaN, Double.NEGATIVE_INFINITY);
 257         assertEquals(-1, task.getProgress(), 0);
 258         assertEquals(-1, task.getWorkDone(), 0);
 259         assertEquals(-1, task.getTotalWork(), 0);
 260     }
 261 
 262     @Test public void updateProgress_Double_NaN_NaN() {
 263         task.updateProgress(Double.NaN, Double.NaN);
 264         assertEquals(-1, task.getProgress(), 0);
 265         assertEquals(-1, task.getWorkDone(), 0);
 266         assertEquals(-1, task.getTotalWork(), 0);
 267     }
 268 }