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 test.javafx.concurrent; 27 28 import java.util.Arrays; 29 import java.util.Collection; 30 import java.util.concurrent.atomic.AtomicBoolean; 31 import javafx.beans.value.ChangeListener; 32 import javafx.beans.value.ObservableValue; 33 import javafx.concurrent.Worker; 34 import test.javafx.concurrent.mocks.EpicFailTask; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.junit.runners.Parameterized; 38 39 import static org.junit.Assert.*; 40 41 /** 42 * This tests that state is correct when a Task throws an exception partway through. 43 * In this particular case, the progress is updated to 50% before the exception 44 * occurs. 45 */ 46 @RunWith(Parameterized.class) 47 public class ServiceExceptionTest extends ServiceTestBase { 48 @Parameterized.Parameters public static Collection implementations() { 49 return Arrays.asList(new Object[][]{ 50 { new Exception("Exception") }, 51 { new IllegalArgumentException("IAE") }, 52 { new NullPointerException("NPE") }, 53 { new RuntimeException("RuntimeException") } 54 }); 55 } 56 57 private Exception exception; 58 59 public ServiceExceptionTest(Exception th) { 60 this.exception = th; 61 } 62 63 @Override protected TestServiceFactory setupServiceFactory() { 64 return new TestServiceFactory() { 65 @Override public AbstractTask createTestTask() { 66 return new EpicFailTask(ServiceExceptionTest.this.exception); 67 } 68 }; 69 } 70 71 /************************************************************************ 72 * Run the concurrent and check that the exception property is set, and that 73 * the value property is null. The progress fields may be in some 74 * arbitrary state. 75 ***********************************************************************/ 76 77 /** 78 * Whenever the exception occurs we should have the exception property set 79 */ 80 @Test public void exceptionShouldBeSet() { 81 service.start(); 82 handleEvents(); 83 assertSame(exception, service.getException()); 84 assertSame(exception, service.exceptionProperty().get()); 85 } 86 87 @Test public void exceptionPropertyNotification() { 88 final AtomicBoolean passed = new AtomicBoolean(false); 89 service.exceptionProperty().addListener((o, oldValue, newValue) -> passed.set(newValue == exception)); 90 service.start(); 91 handleEvents(); 92 assertTrue(passed.get()); 93 } 94 95 /** 96 * The value should always be null if an exception occurs before the end of the Task. 97 */ 98 @Test public void valueShouldBeNull() { 99 service.start(); 100 handleEvents(); 101 assertNull(service.getValue()); 102 assertNull(service.valueProperty().get()); 103 } 104 105 @Test public void runningShouldBeFalse() { 106 service.start(); 107 handleEvents(); 108 assertFalse(service.isRunning()); 109 assertFalse(service.runningProperty().get()); 110 } 111 112 @Test public void runningPropertyNotification() { 113 final AtomicBoolean passed = new AtomicBoolean(false); 114 service.runningProperty().addListener((o, oldValue, newValue) -> passed.set(!newValue)); 115 service.start(); 116 handleEvents(); 117 assertTrue(passed.get()); 118 } 119 120 @Test public void workDoneShouldBeTen() { 121 service.start(); 122 handleEvents(); 123 assertEquals(10, service.getWorkDone(), 0); 124 assertEquals(10, service.workDoneProperty().get(), 0); 125 } 126 127 @Test public void workDonePropertyNotification() { 128 final AtomicBoolean passed = new AtomicBoolean(false); 129 service.workDoneProperty().addListener((observable, oldValue, newValue) -> passed.set(newValue.doubleValue() == 10)); 130 service.start(); 131 handleEvents(); 132 assertTrue(passed.get()); 133 } 134 135 @Test public void totalWorkShouldBeTwenty() { 136 service.start(); 137 handleEvents(); 138 assertEquals(20, service.getTotalWork(), 0); 139 assertEquals(20, service.totalWorkProperty().get(), 0); 140 } 141 142 @Test public void totalWorkPropertyNotification() { 143 final AtomicBoolean passed = new AtomicBoolean(false); 144 service.totalWorkProperty().addListener((observable, oldValue, newValue) -> passed.set(newValue.doubleValue() == 20)); 145 service.start(); 146 handleEvents(); 147 assertTrue(passed.get()); 148 } 149 150 @Test public void afterRunningProgressShouldBe_FiftyPercent() { 151 service.start(); 152 handleEvents(); 153 assertEquals(.5, service.getProgress(), 0); 154 assertEquals(.5, service.progressProperty().get(), 0); 155 } 156 157 @Test public void progressPropertyNotification() { 158 final AtomicBoolean passed = new AtomicBoolean(false); 159 service.progressProperty().addListener((observable, oldValue, newValue) -> passed.set(newValue.doubleValue() == .5)); 160 service.start(); 161 handleEvents(); 162 assertTrue(passed.get()); 163 } 164 165 @Test public void stateShouldBe_FAILED() { 166 service.start(); 167 handleEvents(); 168 assertSame(Worker.State.FAILED, service.getState()); 169 assertSame(Worker.State.FAILED, service.stateProperty().get()); 170 } 171 172 @Test public void statePropertyNotification() { 173 final AtomicBoolean passed = new AtomicBoolean(false); 174 service.stateProperty().addListener((observable, oldValue, newValue) -> passed.set(newValue == Worker.State.FAILED)); 175 service.start(); 176 handleEvents(); 177 assertTrue(passed.get()); 178 } 179 180 @Test public void messageShouldBeLastSetValue() { 181 service.start(); 182 handleEvents(); 183 assertEquals("About to fail", service.getMessage()); 184 assertEquals("About to fail", service.messageProperty().get()); 185 } 186 187 @Test public void messagePropertyNotification() { 188 final AtomicBoolean passed = new AtomicBoolean(false); 189 service.messageProperty().addListener((observable, oldValue, newValue) -> passed.set("About to fail".equals(service.getMessage()))); 190 service.start(); 191 handleEvents(); 192 assertTrue(passed.get()); 193 } 194 195 @Test public void titleShouldBeLastSetValue() { 196 service.start(); 197 handleEvents(); 198 assertEquals("Epic Fail", service.getTitle()); 199 assertEquals("Epic Fail", service.titleProperty().get()); 200 } 201 202 @Test public void titlePropertyNotification() { 203 final AtomicBoolean passed = new AtomicBoolean(false); 204 service.titleProperty().addListener((observable, oldValue, newValue) -> passed.set("Epic Fail".equals(service.getTitle()))); 205 service.start(); 206 handleEvents(); 207 assertTrue(passed.get()); 208 } 209 }