1 /* 2 * Copyright (c) 2010, 2014, 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.beans.value.ChangeListener; 29 import javafx.beans.value.ObservableValue; 30 import java.util.ArrayList; 31 import java.util.List; 32 33 import javafx.concurrent.mocks.SimpleTask; 34 import org.junit.Before; 35 import org.junit.Test; 36 37 import static org.junit.Assert.*; 38 39 /** 40 * A Task which simply completes normally. It never throws an Exception. 41 * It also never increments the progress or touches messages or anything 42 * else. 43 */ 44 public class TaskSimpleTest { 45 private Task<String> task; 46 47 @Before public void setup() { 48 task = new SimpleTask(); 49 50 // Checks that the running property is always correct. 51 task.runningProperty().addListener((o, oldValue, newValue) -> { 52 Worker.State s = task.getState(); 53 if (newValue) { 54 assertTrue(s == Worker.State.SCHEDULED || s == Worker.State.RUNNING); 55 } else { 56 assertTrue(s != Worker.State.SCHEDULED && s != Worker.State.RUNNING); 57 } 58 }); 59 } 60 61 /************************************************************************ 62 * Test the initial values for the Task, 63 ***********************************************************************/ 64 65 @Test public void stateShouldBe_READY_ByDefault() { 66 assertEquals(Task.State.READY, task.getState()); 67 } 68 69 @Test public void workDoneShouldBe_Indeterminate_ByDefault() { 70 assertEquals(-1, task.getWorkDone(), 0); 71 } 72 73 @Test public void totalWorkShouldBe_Indeterminate_ByDefault() { 74 assertEquals(-1, task.getTotalWork(), 0); 75 } 76 77 @Test public void progressShouldBe_Indeterminate_ByDefault() { 78 assertEquals(-1, task.getWorkDone(), 0); 79 } 80 81 @Test public void valueShouldBe_Null_ByDefault() { 82 assertNull(task.getValue()); 83 } 84 85 @Test public void exceptionShouldBe_Null_ByDefault() { 86 assertNull(task.getException()); 87 } 88 89 @Test public void runningShouldBe_False_ByDefault() { 90 assertEquals(false, task.isRunning()); 91 } 92 93 @Test public void messageShouldBe_EmptyString_ByDefault() { 94 assertEquals("", task.getMessage()); 95 } 96 97 @Test public void titleShouldBe_EmptyString_ByDefault() { 98 assertEquals("", task.getTitle()); 99 } 100 101 @Test public void isCancelledShouldBe_False_ByDefault() { 102 assertEquals(false, task.isCancelled()); 103 } 104 105 @Test public void isDoneShouldBe_False_ByDefault() { 106 assertEquals(false, task.isDone()); 107 } 108 109 /************************************************************************ 110 * Run the task and make sure that the states SCHEDULED, RUNNING, and 111 * SUCCEEDED were all encountered in order. Check the condition at the 112 * end of execution. Progress should equal maxProgress (-1 in this case), 113 * percentDone should still be -1. value should be "Sentinel" and 114 * exception should be null, running should be false. 115 ***********************************************************************/ 116 117 @Test public void afterRunningStatesShouldHaveBeen_SCHEDULED_RUNNING_SUCCEEDED() { 118 final List<Worker.State> states = new ArrayList<Worker.State>(); 119 task.stateProperty().addListener((observable, oldValue, newValue) -> { 120 states.add(newValue); 121 }); 122 123 task.run(); 124 125 assertArrayEquals(states.toArray(), new Worker.State[]{ 126 Worker.State.SCHEDULED, 127 Worker.State.RUNNING, 128 Worker.State.SUCCEEDED 129 }); 130 } 131 132 @Test public void afterRunningWorkDoneShouldBe_Indeterminate() { 133 task.run(); 134 assertEquals(-1, task.getWorkDone(), 0); 135 } 136 137 @Test public void afterRunningTotalWorkShouldBe_Indeterminate() { 138 task.run(); 139 assertEquals(-1, task.getTotalWork(), 0); 140 } 141 142 @Test public void afterRunningProgressShouldBe_Indeterminate() { 143 task.run(); 144 assertEquals(-1, task.getWorkDone(), 0); 145 } 146 147 @Test public void afterRunningValueShouldBe_Finished() { 148 task.run(); 149 assertEquals("Sentinel", task.getValue()); 150 } 151 152 @Test public void afterRunningExceptionShouldBe_Null() { 153 task.run(); 154 assertNull(task.getException()); 155 } 156 157 @Test public void afterRunningMessageShouldBe_EmptyString() { 158 task.run(); 159 assertEquals("", task.getMessage()); 160 } 161 162 @Test public void afterRunningTitleShouldBe_EmptyString() { 163 task.run(); 164 assertEquals("", task.getTitle()); 165 } 166 167 @Test public void afterRunning_isCancelled_ShouldBe_False() { 168 task.run(); 169 assertEquals(false, task.isCancelled()); 170 } 171 172 @Test public void afterRunning_isDone_ShouldBe_True() { 173 task.run(); 174 assertEquals(true, task.isDone()); 175 } 176 177 /************************************************************************ 178 * TODO Need to resolve what should happen when get() is called 179 * The following few tests are setup so that we can test that 180 * invoking "get" instead of "run" still results in the state 181 * being set correctly for the Task. In theory, the exception, 182 * value, and other fields should be updated when get is called (?) 183 ***********************************************************************/ 184 185 }