blob: 3698b79872b13948a3ce2deb3aa6e6f3b4e18d68 [file] [log] [blame]
Bernardo Rufino1f1bae92018-09-19 10:21:02 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server.backup.keyvalue;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import static org.testng.Assert.expectThrows;
22
23import android.app.backup.BackupTransport;
24import android.platform.test.annotations.Presubmit;
25
Bernardo Rufino1f1bae92018-09-19 10:21:02 +010026import org.junit.Test;
27import org.junit.runner.RunWith;
James Lemieux38d86c22018-10-30 16:50:39 -070028import org.robolectric.RobolectricTestRunner;
Bernardo Rufino1f1bae92018-09-19 10:21:02 +010029
30import java.io.IOException;
31
James Lemieux38d86c22018-10-30 16:50:39 -070032@RunWith(RobolectricTestRunner.class)
Bernardo Rufino1f1bae92018-09-19 10:21:02 +010033@Presubmit
34public class TaskExceptionTest {
35 @Test
36 public void testStateCompromised() {
37 TaskException exception = TaskException.stateCompromised();
38
39 assertThat(exception.isStateCompromised()).isTrue();
40 assertThat(exception.getStatus()).isEqualTo(BackupTransport.TRANSPORT_ERROR);
41 }
42
43 @Test
44 public void testStateCompromised_whenCauseInstanceOfTaskException() {
45 Exception cause = TaskException.forStatus(BackupTransport.TRANSPORT_NOT_INITIALIZED);
46
47 TaskException exception = TaskException.stateCompromised(cause);
48
49 assertThat(exception.isStateCompromised()).isTrue();
50 assertThat(exception.getStatus()).isEqualTo(BackupTransport.TRANSPORT_NOT_INITIALIZED);
51 assertThat(exception.getCause()).isEqualTo(cause);
52 }
53
54 @Test
55 public void testStateCompromised_whenCauseNotInstanceOfTaskException() {
56 Exception cause = new IOException();
57
58 TaskException exception = TaskException.stateCompromised(cause);
59
60 assertThat(exception.isStateCompromised()).isTrue();
61 assertThat(exception.getStatus()).isEqualTo(BackupTransport.TRANSPORT_ERROR);
62 assertThat(exception.getCause()).isEqualTo(cause);
63 }
64
65 @Test
66 public void testForStatus_whenTransportOk_throws() {
67 expectThrows(
68 IllegalArgumentException.class,
69 () -> TaskException.forStatus(BackupTransport.TRANSPORT_OK));
70 }
71
72 @Test
73 public void testForStatus_whenTransportNotInitialized() {
74 TaskException exception =
75 TaskException.forStatus(BackupTransport.TRANSPORT_NOT_INITIALIZED);
76
77 assertThat(exception.isStateCompromised()).isFalse();
78 assertThat(exception.getStatus()).isEqualTo(BackupTransport.TRANSPORT_NOT_INITIALIZED);
79 }
80
81 @Test
82 public void testCausedBy_whenCauseInstanceOfTaskException_returnsCause() {
83 Exception cause = TaskException.forStatus(BackupTransport.TRANSPORT_NOT_INITIALIZED);
84
85 TaskException exception = TaskException.causedBy(cause);
86
87 assertThat(exception).isEqualTo(cause);
88 }
89
90 @Test
91 public void testCausedBy_whenCauseNotInstanceOfTaskException() {
92 Exception cause = new IOException();
93
94 TaskException exception = TaskException.causedBy(cause);
95
96 assertThat(exception).isNotEqualTo(cause);
97 assertThat(exception.isStateCompromised()).isFalse();
98 assertThat(exception.getStatus()).isEqualTo(BackupTransport.TRANSPORT_ERROR);
99 assertThat(exception.getCause()).isEqualTo(cause);
100 }
101
102 @Test
103 public void testCreate() {
104 TaskException exception = TaskException.create();
105
106 assertThat(exception.isStateCompromised()).isFalse();
107 assertThat(exception.getStatus()).isEqualTo(BackupTransport.TRANSPORT_ERROR);
108 }
109
110 @Test
111 public void testIsStateCompromised_whenStateCompromised_returnsTrue() {
112 TaskException taskException = TaskException.stateCompromised();
113
114 boolean stateCompromised = taskException.isStateCompromised();
115
116 assertThat(stateCompromised).isTrue();
117 }
118
119 @Test
120 public void testIsStateCompromised_whenCreatedWithCreate_returnsFalse() {
121 TaskException taskException = TaskException.create();
122
123 boolean stateCompromised = taskException.isStateCompromised();
124
125 assertThat(stateCompromised).isFalse();
126 }
127
128 @Test
129 public void testGetStatus_whenStatusIsTransportPackageRejected() {
130 TaskException taskException =
131 TaskException.forStatus(BackupTransport.TRANSPORT_PACKAGE_REJECTED);
132
133 int status = taskException.getStatus();
134
135 assertThat(status).isEqualTo(BackupTransport.TRANSPORT_PACKAGE_REJECTED);
136 }
137
138 @Test
139 public void testGetStatus_whenStatusIsTransportNotInitialized() {
140 TaskException taskException =
141 TaskException.forStatus(BackupTransport.TRANSPORT_NOT_INITIALIZED);
142
143 int status = taskException.getStatus();
144
145 assertThat(status).isEqualTo(BackupTransport.TRANSPORT_NOT_INITIALIZED);
146 }
147}