1 /* 2 * Copyright 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 17 package androidx.work; 18 19 import static org.hamcrest.CoreMatchers.instanceOf; 20 import static org.hamcrest.CoreMatchers.is; 21 import static org.hamcrest.MatcherAssert.assertThat; 22 23 import org.junit.Before; 24 import org.junit.Test; 25 26 import java.util.Arrays; 27 28 public class ArrayCreatingInputMergerTest { 29 30 private static final String KEY = "key"; 31 32 private static final int[] VALUE_INT_ARRAY = { 0, 1, 2 }; 33 private static final int VALUE_INT = 3; 34 private static final Long VALUE_LONG = Long.MAX_VALUE; 35 36 ArrayCreatingInputMerger mArrayCreatingInputMerger; 37 Data mDataWithIntArray; 38 Data mDataWithInt; 39 Data mDataWithLong; 40 41 @Before setUp()42 public void setUp() { 43 mArrayCreatingInputMerger = new ArrayCreatingInputMerger(); 44 mDataWithIntArray = new Data.Builder().putIntArray(KEY, VALUE_INT_ARRAY).build(); 45 mDataWithInt = new Data.Builder().putInt(KEY, VALUE_INT).build(); 46 mDataWithLong = new Data.Builder().putLong(KEY, VALUE_LONG).build(); 47 } 48 49 @Test testMerge_singleArgument()50 public void testMerge_singleArgument() { 51 Data output = getOutputFor(mDataWithInt); 52 assertThat(output.size(), is(1)); 53 int[] outputArray = output.getIntArray(KEY); 54 assertThat(outputArray.length, is(1)); 55 assertThat(outputArray[0], is(VALUE_INT)); 56 } 57 58 @Test testMerge_concatenatesNonArrays()59 public void testMerge_concatenatesNonArrays() { 60 Data output = getOutputFor(mDataWithInt, mDataWithInt); 61 assertThat(output.size(), is(1)); 62 int[] outputArray = output.getIntArray(KEY); 63 assertThat(outputArray.length, is(2)); 64 assertThat(outputArray[0], is(VALUE_INT)); 65 assertThat(outputArray[1], is(VALUE_INT)); 66 } 67 68 @Test testMerge_concatenatesArrays()69 public void testMerge_concatenatesArrays() { 70 Data output = getOutputFor(mDataWithIntArray, mDataWithIntArray); 71 assertThat(output.size(), is(1)); 72 int[] outputArray = output.getIntArray(KEY); 73 assertThat(outputArray.length, is(VALUE_INT_ARRAY.length * 2)); 74 for (int i = 0; i < 2; ++i) { 75 for (int j = 0; j < VALUE_INT_ARRAY.length; ++j) { 76 assertThat(outputArray[i * VALUE_INT_ARRAY.length + j], is(VALUE_INT_ARRAY[j])); 77 } 78 } 79 } 80 81 @Test testMerge_concatenatesArrayAndPrimitive()82 public void testMerge_concatenatesArrayAndPrimitive() { 83 Data output = getOutputFor(mDataWithIntArray, mDataWithInt); 84 assertThat(output.size(), is(1)); 85 int[] outputArray = output.getIntArray(KEY); 86 assertThat(outputArray.length, is(VALUE_INT_ARRAY.length + 1)); 87 for (int i = 0; i < VALUE_INT_ARRAY.length; ++i) { 88 assertThat(outputArray[i], is(VALUE_INT_ARRAY[i])); 89 } 90 assertThat(outputArray[VALUE_INT_ARRAY.length], is(VALUE_INT)); 91 } 92 93 @Test testMerge_throwsIllegalArgumentExceptionOnDifferentTypes()94 public void testMerge_throwsIllegalArgumentExceptionOnDifferentTypes() { 95 Throwable throwable = null; 96 try { 97 Data output = getOutputFor(mDataWithInt, mDataWithLong); 98 } catch (Throwable t) { 99 throwable = t; 100 } 101 assertThat(throwable, instanceOf(IllegalArgumentException.class)); 102 } 103 getOutputFor(Data... inputs)104 private Data getOutputFor(Data... inputs) { 105 return mArrayCreatingInputMerger.merge(Arrays.asList(inputs)); 106 } 107 } 108