Add parentheses to fix UnexpectedLoaderException message logic

These were missing in 5211ff0dc1

Without these, the `": null"` is still logged (but the `"Unexpected
IllegalStateException"` bit is not), because the **whole** string
concatenation is compared to null as the boolean condition of the
ternary, and this condition is always false (the result of a string
concatentation is never `null`) i.e. (with excess parentheses for
clarity):

```
(("Unexpected " + cause.getClass().getSimpleName() + cause.getMessage()) != null)
    ? ": " + cause.getMessage()
    : ""
```

Also add a test because obviously this isn't as simple as I'd thought.

PiperOrigin-RevId: 593079975
This commit is contained in:
ibaker 2023-12-22 03:39:56 -08:00 committed by Copybara-Service
parent 09bde8053d
commit a496bbd777
2 changed files with 51 additions and 3 deletions

View file

@ -48,9 +48,9 @@ public final class Loader implements LoaderErrorThrower {
public UnexpectedLoaderException(Throwable cause) {
super(
"Unexpected " + cause.getClass().getSimpleName() + cause.getMessage() != null
? ": " + cause.getMessage()
: "",
"Unexpected "
+ cause.getClass().getSimpleName()
+ (cause.getMessage() != null ? ": " + cause.getMessage() : ""),
cause);
}
}

View file

@ -0,0 +1,48 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.exoplayer.upstream;
import static com.google.common.truth.Truth.assertThat;
import androidx.media3.exoplayer.upstream.Loader.UnexpectedLoaderException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Tests for {@link UnexpectedLoaderException}. */
@RunWith(JUnit4.class)
public class UnexpectedLoaderExceptionTest {
@Test
public void causeWithMessage_messageAppended() {
UnexpectedLoaderException unexpectedLoaderException =
new UnexpectedLoaderException(new IllegalStateException("test message"));
assertThat(unexpectedLoaderException)
.hasMessageThat()
.isEqualTo("Unexpected IllegalStateException: test message");
}
@Test
public void causeWithoutMessage_noMessageAppended() {
UnexpectedLoaderException unexpectedLoaderException =
new UnexpectedLoaderException(new IllegalStateException());
assertThat(unexpectedLoaderException)
.hasMessageThat()
.isEqualTo("Unexpected IllegalStateException");
}
}