|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 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 | + |
| 17 | +package com.google.cloud; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertFalse; |
| 20 | +import static org.junit.Assert.assertSame; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | +import static org.junit.Assert.fail; |
| 23 | + |
| 24 | +import org.easymock.EasyMock; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +public class BatchResultTest { |
| 29 | + |
| 30 | + private BatchResult<Boolean, BaseServiceException> result; |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setUp() { |
| 34 | + result = new BatchResult<Boolean, BaseServiceException>() {}; |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testSuccess() { |
| 39 | + assertFalse(result.completed()); |
| 40 | + try { |
| 41 | + result.get(); |
| 42 | + fail("This was not completed yet."); |
| 43 | + } catch (IllegalStateException ex) { |
| 44 | + // expected |
| 45 | + } |
| 46 | + result.success(true); |
| 47 | + assertTrue(result.get()); |
| 48 | + // test that null is allowed |
| 49 | + result.success(null); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testError() { |
| 54 | + assertFalse(result.completed()); |
| 55 | + try { |
| 56 | + result.get(); |
| 57 | + fail("This was not completed yet."); |
| 58 | + } catch (IllegalStateException ex) { |
| 59 | + // expected |
| 60 | + } |
| 61 | + try { |
| 62 | + result.error(null); |
| 63 | + fail(); |
| 64 | + } catch (NullPointerException exc) { |
| 65 | + // expected |
| 66 | + } |
| 67 | + BaseServiceException ex = new BaseServiceException(0, "message", "reason", false); |
| 68 | + result.error(ex); |
| 69 | + try { |
| 70 | + result.get(); |
| 71 | + fail("This is a failed operation and should have thrown a DnsException."); |
| 72 | + } catch (BaseServiceException real) { |
| 73 | + assertSame(ex, real); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testNotifyError() { |
| 79 | + final BaseServiceException ex = new BaseServiceException(0, "message", "reason", false); |
| 80 | + assertFalse(result.completed()); |
| 81 | + BatchResult.Callback<Boolean, BaseServiceException> callback = |
| 82 | + EasyMock.createStrictMock(BatchResult.Callback.class); |
| 83 | + callback.error(ex); |
| 84 | + EasyMock.replay(callback); |
| 85 | + result.notify(callback); |
| 86 | + result.error(ex); |
| 87 | + try { |
| 88 | + result.notify(callback); |
| 89 | + fail("The batch has been completed."); |
| 90 | + } catch (IllegalStateException exception) { |
| 91 | + // expected |
| 92 | + } |
| 93 | + EasyMock.verify(callback); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testNotifySuccess() { |
| 98 | + assertFalse(result.completed()); |
| 99 | + BatchResult.Callback<Boolean, BaseServiceException> callback = |
| 100 | + EasyMock.createStrictMock(BatchResult.Callback.class); |
| 101 | + callback.success(true); |
| 102 | + EasyMock.replay(callback); |
| 103 | + result.notify(callback); |
| 104 | + result.success(true); |
| 105 | + try { |
| 106 | + result.notify(callback); |
| 107 | + fail("The batch has been completed."); |
| 108 | + } catch (IllegalStateException exception) { |
| 109 | + // expected |
| 110 | + } |
| 111 | + EasyMock.verify(callback); |
| 112 | + } |
| 113 | +} |
0 commit comments