1 /* 2 * Copyright (C) 2012 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 com.android.tradefed.config; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 import static org.junit.Assert.fail; 22 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 27 import java.util.ArrayList; 28 import java.util.Collection; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 /** Unit tests for {@link OptionCopier}. */ 33 @RunWith(JUnit4.class) 34 public class OptionCopierTest { 35 36 private static enum DefaultEnumClass { 37 VAL1, VAL3, VAL2; 38 } 39 40 private static class OptionSource { 41 @Option(name = "string", shortName = 's') 42 private String mMyOption = "value"; 43 44 @Option(name = "int") 45 private int mMyIntOption = 42; 46 47 @SuppressWarnings("unused") 48 @Option(name = "notInDest") 49 private String mMyNotInDestOption = "foo"; 50 51 @Option(name = "string_collection") 52 private Collection<String> mStringCollection = new ArrayList<String>(); 53 54 @SuppressWarnings("unused") 55 @Option(name = "enum") 56 private DefaultEnumClass mEnum = DefaultEnumClass.VAL1; 57 58 @Option(name = "enum_map") 59 private Map<DefaultEnumClass, DefaultEnumClass> mEnumMap = 60 new HashMap<DefaultEnumClass, DefaultEnumClass>(); 61 62 @Option(name = "enum_collection") 63 private Collection<DefaultEnumClass> mEnumCollection = 64 new ArrayList<DefaultEnumClass>(); 65 } 66 67 /** 68 * Option source with an option with same name as OptionSource, but a different type. 69 */ 70 private static class OptionWrongTypeDest { 71 @Option(name = "string", shortName = 's') 72 private int mMyOption; 73 } 74 75 /** 76 * Option source with an option with same name as OptionSource, but only the primitive type of 77 * the collection. 78 */ 79 private static class OptionCollectionWrongTypeDest { 80 @Option(name = "string_collection", shortName = 's') 81 private String mString = null; 82 } 83 84 /** 85 * Option source with an option with same name as OptionSource, and with a different primitive 86 * type of the collection. 87 */ 88 private static class OptionCollectionWrongPrimitiveTypeDest { 89 @Option(name = "string_collection", shortName = 's') 90 private int mmyOption; 91 } 92 93 /** Option source with a non-initialized collection. */ 94 private static class OptionCollectionNull { 95 @Option(name = "string_collection", shortName = 's') 96 private Collection<String> mStringCollection = null; 97 } 98 99 /** 100 * Option source with an option with same name as OptionSource, but a different type. 101 */ 102 @SuppressWarnings("unused") 103 private static class OptionWrongGenericTypeDest { 104 105 @Option(name = "string_collection") 106 private Collection<Integer> mIntCollection = new ArrayList<Integer>(); 107 108 } 109 110 private static class OptionDest { 111 112 @Option(name = "string", shortName = 's') 113 private String mDestOption; 114 115 @Option(name = "int") 116 private int mDestIntOption; 117 118 @Option(name = "string_collection") 119 private Collection<String> mStringDestCollection = new ArrayList<String>(); 120 121 @Option(name = "enum") 122 private DefaultEnumClass mEnum = null; 123 124 @Option(name = "enum_map") 125 private Map<DefaultEnumClass, DefaultEnumClass> mEnumMap = 126 new HashMap<DefaultEnumClass, DefaultEnumClass>(); 127 128 @Option(name = "enum_collection") 129 private Collection<DefaultEnumClass> mEnumCollection = 130 new ArrayList<DefaultEnumClass>(); 131 } 132 133 /** Test success case for {@link OptionCopier} using String fields */ 134 @Test 135 public void testCopyOptions_string() throws ConfigurationException { 136 OptionSource source = new OptionSource(); 137 OptionDest dest = new OptionDest(); 138 OptionCopier.copyOptions(source, dest); 139 assertEquals(source.mMyOption, dest.mDestOption); 140 } 141 142 /** Test success case for {@link OptionCopier} for an int field */ 143 @Test 144 public void testCopyOptions_int() throws ConfigurationException { 145 OptionSource source = new OptionSource(); 146 OptionDest dest = new OptionDest(); 147 OptionCopier.copyOptions(source, dest); 148 assertEquals(source.mMyIntOption, dest.mDestIntOption); 149 } 150 151 /** Test success case for {@link OptionCopier} for a {@link Collection}. */ 152 @Test 153 public void testCopyOptions_collection() throws ConfigurationException { 154 OptionSource source = new OptionSource(); 155 source.mStringCollection.add("foo"); 156 OptionDest dest = new OptionDest(); 157 dest.mStringDestCollection.add("bar"); 158 OptionCopier.copyOptions(source, dest); 159 assertEquals(2, dest.mStringDestCollection.size()); 160 assertTrue(dest.mStringDestCollection.contains("foo")); 161 assertTrue(dest.mStringDestCollection.contains("bar")); 162 } 163 164 /** Test success case for {@link OptionCopier} for an enum field */ 165 @Test 166 public void testCopyOptions_enum() throws ConfigurationException { 167 OptionSource source = new OptionSource(); 168 OptionDest dest = new OptionDest(); 169 OptionCopier.copyOptions(source, dest); 170 assertEquals(DefaultEnumClass.VAL1, dest.mEnum); 171 } 172 173 /** Test success case for {@link OptionCopier} for an enum {@link Collection}. */ 174 @Test 175 public void testCopyOptions_enumCollection() throws ConfigurationException { 176 OptionSource source = new OptionSource(); 177 source.mEnumCollection.add(DefaultEnumClass.VAL2); 178 OptionDest dest = new OptionDest(); 179 source.mEnumCollection.add(DefaultEnumClass.VAL3); 180 OptionCopier.copyOptions(source, dest); 181 assertEquals(2, dest.mEnumCollection.size()); 182 assertTrue(dest.mEnumCollection.contains(DefaultEnumClass.VAL2)); 183 assertTrue(dest.mEnumCollection.contains(DefaultEnumClass.VAL3)); 184 } 185 186 /** Test success case for {@link OptionCopier} for an enum {@link Map}. */ 187 @Test 188 public void testCopyOptions_enumMap() throws ConfigurationException { 189 OptionSource source = new OptionSource(); 190 source.mEnumMap.put(DefaultEnumClass.VAL1, DefaultEnumClass.VAL2); 191 OptionDest dest = new OptionDest(); 192 dest.mEnumMap.put(DefaultEnumClass.VAL2, DefaultEnumClass.VAL3); 193 OptionCopier.copyOptions(source, dest); 194 assertEquals(2, dest.mEnumMap.size()); 195 assertEquals(DefaultEnumClass.VAL2, dest.mEnumMap.get(DefaultEnumClass.VAL1)); 196 } 197 198 /** Test {@link OptionCopier} when field's to be copied have different types */ 199 @Test 200 public void testCopyOptions_wrongType() { 201 OptionSource source = new OptionSource(); 202 OptionWrongTypeDest dest = new OptionWrongTypeDest(); 203 try { 204 OptionCopier.copyOptions(source, dest); 205 fail("ConfigurationException not thrown"); 206 } catch (ConfigurationException e) { 207 // expected 208 } 209 } 210 211 /** Copying a collection to primitive type will fail. */ 212 @Test 213 public void testCopyOptions_collectionToPrimitive() { 214 OptionSource source = new OptionSource(); 215 OptionCollectionWrongTypeDest dest = new OptionCollectionWrongTypeDest(); 216 try { 217 OptionCopier.copyOptions(source, dest); 218 fail("ConfigurationException not thrown"); 219 } catch (ConfigurationException e) { 220 // expected 221 assertEquals("internal error when setting option 'string_collection'", e.getMessage()); 222 } 223 } 224 225 /** Copying a null initialized primitive value to collection will fail. */ 226 @Test 227 public void testCopyOptions_primitiveToCollection_null() { 228 OptionCollectionWrongTypeDest source = new OptionCollectionWrongTypeDest(); 229 OptionSource dest = new OptionSource(); 230 try { 231 OptionCopier.copyOptions(source, dest); 232 fail("ConfigurationException not thrown"); 233 } catch (ConfigurationException e) { 234 // expected 235 assertEquals( 236 "Value 'null' is not of type 'class java.lang.String' like the Collection.", 237 e.getMessage()); 238 } 239 } 240 241 /** 242 * Copying an initialized primitive value to collection will succeed since the type is 243 * compatible. 244 */ 245 @Test 246 public void testCopyOptions_primitiveToCollection() throws Exception { 247 OptionCollectionWrongTypeDest source = new OptionCollectionWrongTypeDest(); 248 OptionSetter setter = new OptionSetter(source); 249 setter.setOptionValue("string_collection", "not_null"); 250 OptionSource dest = new OptionSource(); 251 OptionCopier.copyOptions(source, dest); 252 assertEquals(1, dest.mStringCollection.size()); 253 assertEquals("not_null", dest.mStringCollection.iterator().next()); 254 } 255 256 /** 257 * Copying a primitive value different from the collection type will fail since we do not want 258 * to mix types in a collection. 259 */ 260 @Test 261 public void testCopyOptions_primitiveToCollection_differentPrimitive() throws Exception { 262 OptionCollectionWrongPrimitiveTypeDest source = 263 new OptionCollectionWrongPrimitiveTypeDest(); 264 OptionSetter setter = new OptionSetter(source); 265 setter.setOptionValue("string_collection", "5"); 266 OptionSource dest = new OptionSource(); 267 try { 268 OptionCopier.copyOptions(source, dest); 269 fail("ConfigurationException not thrown"); 270 } catch (ConfigurationException e) { 271 // expected 272 assertEquals( 273 "Value '5' is not of type 'class java.lang.String' like the Collection.", 274 e.getMessage()); 275 } 276 } 277 278 /** Copying a non-initialized collection (null) to a collection results in an exception. */ 279 @Test 280 public void testCopyOptions_nullCollection() throws Exception { 281 OptionCollectionNull source = new OptionCollectionNull(); 282 OptionSource dest = new OptionSource(); 283 try { 284 OptionCopier.copyOptions(source, dest); 285 fail("ConfigurationException not thrown"); 286 } catch (ConfigurationException e) { 287 // expected 288 assertEquals( 289 "Value 'null' is not of type 'class java.lang.String' like the Collection.", 290 e.getMessage()); 291 } 292 } 293 } 294