| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.exceptions; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.io.*; |
| 19 |
import java.util.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 24 |
|
| 25 |
public class CompositeExceptionTest extends RxJavaTest { |
| 26 |
|
| 27 |
private final Throwable ex1 = new Throwable("Ex1"); |
| 28 |
private final Throwable ex2 = new Throwable("Ex2", ex1); |
| 29 |
private final Throwable ex3 = new Throwable("Ex3", ex2); |
| 30 |
|
| 31 |
private CompositeException getNewCompositeExceptionWithEx123() { |
| 32 |
List<Throwable> throwables = new ArrayList<>(); |
| 33 |
throwables.add(ex1); |
| 34 |
throwables.add(ex2); |
| 35 |
throwables.add(ex3); |
| 36 |
return new CompositeException(throwables); |
| 37 |
} |
| 38 |
|
| 39 |
@Test |
| 40 |
public void multipleWithSameCause() { |
| 41 |
Throwable rootCause = new Throwable("RootCause"); |
| 42 |
Throwable e1 = new Throwable("1", rootCause); |
| 43 |
Throwable e2 = new Throwable("2", rootCause); |
| 44 |
Throwable e3 = new Throwable("3", rootCause); |
| 45 |
CompositeException ce = new CompositeException(e1, e2, e3); |
| 46 |
|
| 47 |
System.err.println("----------------------------- print composite stacktrace"); |
| 48 |
ce.printStackTrace(); |
| 49 |
assertEquals(3, ce.getExceptions().size()); |
| 50 |
|
| 51 |
assertNoCircularReferences(ce); |
| 52 |
assertNotNull(getRootCause(ce)); |
| 53 |
System.err.println("----------------------------- print cause stacktrace"); |
| 54 |
ce.getCause().printStackTrace(); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void emptyErrors() { |
| 59 |
try { |
| 60 |
new CompositeException(); |
| 61 |
fail("CompositeException should fail if errors is empty"); |
| 62 |
} catch (IllegalArgumentException e) { |
| 63 |
assertEquals("errors is empty", e.getMessage()); |
| 64 |
} |
| 65 |
try { |
| 66 |
new CompositeException(new ArrayList<>()); |
| 67 |
fail("CompositeException should fail if errors is empty"); |
| 68 |
} catch (IllegalArgumentException e) { |
| 69 |
assertEquals("errors is empty", e.getMessage()); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void compositeExceptionFromParentThenChild() { |
| 75 |
CompositeException cex = new CompositeException(ex1, ex2); |
| 76 |
|
| 77 |
System.err.println("----------------------------- print composite stacktrace"); |
| 78 |
cex.printStackTrace(); |
| 79 |
assertEquals(2, cex.getExceptions().size()); |
| 80 |
|
| 81 |
assertNoCircularReferences(cex); |
| 82 |
assertNotNull(getRootCause(cex)); |
| 83 |
|
| 84 |
System.err.println("----------------------------- print cause stacktrace"); |
| 85 |
cex.getCause().printStackTrace(); |
| 86 |
} |
| 87 |
|
| 88 |
@Test |
| 89 |
public void compositeExceptionFromChildThenParent() { |
| 90 |
CompositeException cex = new CompositeException(ex2, ex1); |
| 91 |
|
| 92 |
System.err.println("----------------------------- print composite stacktrace"); |
| 93 |
cex.printStackTrace(); |
| 94 |
assertEquals(2, cex.getExceptions().size()); |
| 95 |
|
| 96 |
assertNoCircularReferences(cex); |
| 97 |
assertNotNull(getRootCause(cex)); |
| 98 |
|
| 99 |
System.err.println("----------------------------- print cause stacktrace"); |
| 100 |
cex.getCause().printStackTrace(); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void compositeExceptionFromChildAndComposite() { |
| 105 |
CompositeException cex = new CompositeException(ex1, getNewCompositeExceptionWithEx123()); |
| 106 |
|
| 107 |
System.err.println("----------------------------- print composite stacktrace"); |
| 108 |
cex.printStackTrace(); |
| 109 |
assertEquals(3, cex.getExceptions().size()); |
| 110 |
|
| 111 |
assertNoCircularReferences(cex); |
| 112 |
assertNotNull(getRootCause(cex)); |
| 113 |
|
| 114 |
System.err.println("----------------------------- print cause stacktrace"); |
| 115 |
cex.getCause().printStackTrace(); |
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void compositeExceptionFromCompositeAndChild() { |
| 120 |
CompositeException cex = new CompositeException(getNewCompositeExceptionWithEx123(), ex1); |
| 121 |
|
| 122 |
System.err.println("----------------------------- print composite stacktrace"); |
| 123 |
cex.printStackTrace(); |
| 124 |
assertEquals(3, cex.getExceptions().size()); |
| 125 |
|
| 126 |
assertNoCircularReferences(cex); |
| 127 |
assertNotNull(getRootCause(cex)); |
| 128 |
|
| 129 |
System.err.println("----------------------------- print cause stacktrace"); |
| 130 |
cex.getCause().printStackTrace(); |
| 131 |
} |
| 132 |
|
| 133 |
@Test |
| 134 |
public void compositeExceptionFromTwoDuplicateComposites() { |
| 135 |
List<Throwable> exs = new ArrayList<>(); |
| 136 |
exs.add(getNewCompositeExceptionWithEx123()); |
| 137 |
exs.add(getNewCompositeExceptionWithEx123()); |
| 138 |
CompositeException cex = new CompositeException(exs); |
| 139 |
|
| 140 |
System.err.println("----------------------------- print composite stacktrace"); |
| 141 |
cex.printStackTrace(); |
| 142 |
assertEquals(3, cex.getExceptions().size()); |
| 143 |
|
| 144 |
assertNoCircularReferences(cex); |
| 145 |
assertNotNull(getRootCause(cex)); |
| 146 |
|
| 147 |
System.err.println("----------------------------- print cause stacktrace"); |
| 148 |
cex.getCause().printStackTrace(); |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
* This hijacks the Throwable.printStackTrace() output and puts it in a string, where we can look for |
| 153 |
* "CIRCULAR REFERENCE" (a String added by Throwable.printEnclosedStackTrace) |
| 154 |
*/ |
| 155 |
private static void assertNoCircularReferences(Throwable ex) { |
| 156 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 157 |
PrintStream printStream = new PrintStream(baos); |
| 158 |
ex.printStackTrace(printStream); |
| 159 |
assertFalse(baos.toString().contains("CIRCULAR REFERENCE")); |
| 160 |
} |
| 161 |
|
| 162 |
private static Throwable getRootCause(Throwable ex) { |
| 163 |
Throwable root = ex.getCause(); |
| 164 |
if (root == null) { |
| 165 |
return null; |
| 166 |
} else { |
| 167 |
while (true) { |
| 168 |
if (root.getCause() == null) { |
| 169 |
return root; |
| 170 |
} else { |
| 171 |
root = root.getCause(); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
@Test |
| 178 |
public void nullCollection() { |
| 179 |
CompositeException composite = new CompositeException((List<Throwable>)null); |
| 180 |
composite.getCause(); |
| 181 |
composite.printStackTrace(); |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void nullElement() { |
| 186 |
CompositeException composite = new CompositeException(Collections.singletonList((Throwable) null)); |
| 187 |
composite.getCause(); |
| 188 |
composite.printStackTrace(); |
| 189 |
} |
| 190 |
|
| 191 |
@Test |
| 192 |
public void compositeExceptionWithUnsupportedInitCause() { |
| 193 |
Throwable t = new Throwable() { |
| 194 |
|
| 195 |
private static final long serialVersionUID = -3282577447436848385L; |
| 196 |
|
| 197 |
@Override |
| 198 |
public synchronized Throwable initCause(Throwable cause) { |
| 199 |
throw new UnsupportedOperationException(); |
| 200 |
} |
| 201 |
}; |
| 202 |
CompositeException cex = new CompositeException(t, ex1); |
| 203 |
|
| 204 |
System.err.println("----------------------------- print composite stacktrace"); |
| 205 |
cex.printStackTrace(); |
| 206 |
assertEquals(2, cex.getExceptions().size()); |
| 207 |
|
| 208 |
assertNoCircularReferences(cex); |
| 209 |
assertNotNull(getRootCause(cex)); |
| 210 |
|
| 211 |
System.err.println("----------------------------- print cause stacktrace"); |
| 212 |
cex.getCause().printStackTrace(); |
| 213 |
} |
| 214 |
|
| 215 |
@Test |
| 216 |
public void compositeExceptionWithNullInitCause() { |
| 217 |
Throwable t = new Throwable("ThrowableWithNullInitCause") { |
| 218 |
|
| 219 |
private static final long serialVersionUID = -7984762607894527888L; |
| 220 |
|
| 221 |
@Override |
| 222 |
public synchronized Throwable initCause(Throwable cause) { |
| 223 |
return null; |
| 224 |
} |
| 225 |
}; |
| 226 |
CompositeException cex = new CompositeException(t, ex1); |
| 227 |
|
| 228 |
System.err.println("----------------------------- print composite stacktrace"); |
| 229 |
cex.printStackTrace(); |
| 230 |
assertEquals(2, cex.getExceptions().size()); |
| 231 |
|
| 232 |
assertNoCircularReferences(cex); |
| 233 |
assertNotNull(getRootCause(cex)); |
| 234 |
|
| 235 |
System.err.println("----------------------------- print cause stacktrace"); |
| 236 |
cex.getCause().printStackTrace(); |
| 237 |
} |
| 238 |
|
| 239 |
@Test |
| 240 |
public void messageCollection() { |
| 241 |
CompositeException compositeException = new CompositeException(ex1, ex3); |
| 242 |
assertEquals("2 exceptions occurred. ", compositeException.getMessage()); |
| 243 |
} |
| 244 |
|
| 245 |
@Test |
| 246 |
public void messageVarargs() { |
| 247 |
CompositeException compositeException = new CompositeException(ex1, ex2, ex3); |
| 248 |
assertEquals("3 exceptions occurred. ", compositeException.getMessage()); |
| 249 |
} |
| 250 |
|
| 251 |
@Test |
| 252 |
public void constructorWithNull() { |
| 253 |
assertTrue(new CompositeException((Throwable[])null).getExceptions().get(0) instanceof NullPointerException); |
| 254 |
|
| 255 |
assertTrue(new CompositeException((Iterable<Throwable>)null).getExceptions().get(0) instanceof NullPointerException); |
| 256 |
|
| 257 |
assertTrue(new CompositeException(null, new TestException()).getExceptions().get(0) instanceof NullPointerException); |
| 258 |
} |
| 259 |
|
| 260 |
@Test |
| 261 |
public void printStackTrace() { |
| 262 |
StringWriter sw = new StringWriter(); |
| 263 |
PrintWriter pw = new PrintWriter(sw); |
| 264 |
|
| 265 |
new CompositeException(new TestException()).printStackTrace(pw); |
| 266 |
|
| 267 |
assertTrue(sw.toString().contains("TestException")); |
| 268 |
} |
| 269 |
|
| 270 |
@Test |
| 271 |
public void badException() { |
| 272 |
Throwable e = new BadException(); |
| 273 |
assertSame(e, new CompositeException(e).getCause().getCause()); |
| 274 |
assertSame(e, new CompositeException(new RuntimeException(e)).getCause().getCause().getCause()); |
| 275 |
} |
| 276 |
|
| 277 |
@Test |
| 278 |
public void exceptionOverview() { |
| 279 |
CompositeException composite = new CompositeException( |
| 280 |
new TestException("ex1"), |
| 281 |
new TestException("ex2"), |
| 282 |
new TestException("ex3", new TestException("ex4")) |
| 283 |
); |
| 284 |
|
| 285 |
String overview = composite.getCause().getMessage(); |
| 286 |
|
| 287 |
assertTrue(overview, overview.contains("Multiple exceptions (3)")); |
| 288 |
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex1")); |
| 289 |
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex2")); |
| 290 |
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex3")); |
| 291 |
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex4")); |
| 292 |
assertTrue(overview, overview.contains("at io.reactivex.rxjava3.exceptions.CompositeExceptionTest.exceptionOverview")); |
| 293 |
} |
| 294 |
|
| 295 |
@Test |
| 296 |
public void causeWithExceptionWithoutStacktrace() { |
| 297 |
CompositeException composite = new CompositeException( |
| 298 |
new TestException("ex1"), |
| 299 |
new CompositeException.ExceptionOverview("example") |
| 300 |
); |
| 301 |
|
| 302 |
String overview = composite.getCause().getMessage(); |
| 303 |
|
| 304 |
assertTrue(overview, overview.contains("Multiple exceptions (2)")); |
| 305 |
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex1")); |
| 306 |
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.CompositeException.ExceptionOverview: example")); |
| 307 |
|
| 308 |
assertEquals(overview, 2, overview.split("at\\s").length); |
| 309 |
} |
| 310 |
|
| 311 |
@Test |
| 312 |
public void reoccurringException() { |
| 313 |
TestException ex0 = new TestException("ex0"); |
| 314 |
TestException ex1 = new TestException("ex1", ex0); |
| 315 |
CompositeException composite = new CompositeException( |
| 316 |
ex1, |
| 317 |
new TestException("ex2", ex1) |
| 318 |
); |
| 319 |
|
| 320 |
String overview = composite.getCause().getMessage(); |
| 321 |
System.err.println(overview); |
| 322 |
|
| 323 |
assertTrue(overview, overview.contains("Multiple exceptions (2)")); |
| 324 |
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex0")); |
| 325 |
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex1")); |
| 326 |
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex2")); |
| 327 |
assertTrue(overview, overview.contains("(cause not expanded again) io.reactivex.rxjava3.exceptions.TestException: ex0")); |
| 328 |
assertEquals(overview, 5, overview.split("at\\s").length); |
| 329 |
} |
| 330 |
|
| 331 |
@Test |
| 332 |
public void nestedMultilineMessage() { |
| 333 |
TestException ex1 = new TestException("ex1"); |
| 334 |
TestException ex2 = new TestException("ex2"); |
| 335 |
CompositeException composite1 = new CompositeException( |
| 336 |
ex1, |
| 337 |
ex2 |
| 338 |
); |
| 339 |
TestException ex3 = new TestException("ex3"); |
| 340 |
TestException ex4 = new TestException("ex4", composite1); |
| 341 |
|
| 342 |
CompositeException composite2 = new CompositeException( |
| 343 |
ex3, |
| 344 |
ex4 |
| 345 |
); |
| 346 |
|
| 347 |
String overview = composite2.getCause().getMessage(); |
| 348 |
System.err.println(overview); |
| 349 |
|
| 350 |
assertTrue(overview, overview.contains(" Multiple exceptions (2)")); |
| 351 |
assertTrue(overview, overview.contains(" |-- io.reactivex.rxjava3.exceptions.TestException: ex1")); |
| 352 |
assertTrue(overview, overview.contains(" |-- io.reactivex.rxjava3.exceptions.TestException: ex2")); |
| 353 |
} |
| 354 |
|
| 355 |
@Test |
| 356 |
public void singleExceptionIsTheCause() { |
| 357 |
TestException ex = new TestException("ex1"); |
| 358 |
CompositeException composite = new CompositeException(ex); |
| 359 |
|
| 360 |
assertSame(composite.getCause(), ex); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
final class BadException extends Throwable { |
| 365 |
private static final long serialVersionUID = 8999507293896399171L; |
| 366 |
|
| 367 |
@Override |
| 368 |
public synchronized Throwable getCause() { |
| 369 |
return this; |
| 370 |
} |
| 371 |
} |