1 /* 2 * Copyright (C) 2010 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 junit.framework.TestCase; 20 21 /** 22 * Unit tests for {@link OptionUpdateRule} 23 */ 24 public class OptionUpdateRuleTest extends TestCase { 25 private static final String OPTION_NAME = "option-name"; 26 private static final Object CURRENT = "5 current value"; 27 private static final Object UPDATE = "5 update value"; 28 private static final Object SMALL_UPDATE = "0 update value"; 29 private static final Object BIG_UPDATE = "9 update value"; 30 31 public void testFirst_simple() throws Exception { 32 assertTrue(OptionUpdateRule.FIRST.shouldUpdate(OPTION_NAME, null, UPDATE)); 33 assertFalse(OptionUpdateRule.FIRST.shouldUpdate(OPTION_NAME, CURRENT, UPDATE)); 34 } 35 36 public void testLast_simple() throws Exception { 37 assertTrue(OptionUpdateRule.LAST.shouldUpdate(OPTION_NAME, null, UPDATE)); 38 assertTrue(OptionUpdateRule.LAST.shouldUpdate(OPTION_NAME, CURRENT, UPDATE)); 39 } 40 41 public void testGreatest_simple() throws Exception { 42 assertTrue(OptionUpdateRule.GREATEST.shouldUpdate(OPTION_NAME, null, SMALL_UPDATE)); 43 assertFalse(OptionUpdateRule.GREATEST.shouldUpdate(OPTION_NAME, CURRENT, SMALL_UPDATE)); 44 assertTrue(OptionUpdateRule.GREATEST.shouldUpdate(OPTION_NAME, CURRENT, BIG_UPDATE)); 45 } 46 47 public void testLeast_simple() throws Exception { 48 assertTrue(OptionUpdateRule.LEAST.shouldUpdate(OPTION_NAME, null, BIG_UPDATE)); 49 assertTrue(OptionUpdateRule.LEAST.shouldUpdate(OPTION_NAME, CURRENT, SMALL_UPDATE)); 50 assertFalse(OptionUpdateRule.LEAST.shouldUpdate(OPTION_NAME, CURRENT, BIG_UPDATE)); 51 } 52 53 public void testImmutable_simple() throws Exception { 54 assertTrue(OptionUpdateRule.IMMUTABLE.shouldUpdate(OPTION_NAME, null, UPDATE)); 55 try { 56 OptionUpdateRule.IMMUTABLE.shouldUpdate(OPTION_NAME, CURRENT, UPDATE); 57 fail("ConfigurationException not thrown when updating an IMMUTABLE option"); 58 } catch (ConfigurationException e) { 59 // expected 60 } 61 } 62 63 public void testInvalidComparison() throws Exception { 64 try { 65 // Strings aren't comparable with integers 66 OptionUpdateRule.GREATEST.shouldUpdate(OPTION_NAME, 13, UPDATE); 67 fail("ConfigurationException not thrown for invalid comparison."); 68 } catch (ConfigurationException e) { 69 // Expected. Moreover, the exception should be actionable, so make sure we mention the 70 // specific mismatching types. 71 final String msg = e.getMessage(); 72 assertTrue(msg.contains("Integer")); 73 assertTrue(msg.contains("String")); 74 } 75 } 76 77 public void testNotComparable() throws Exception { 78 try { 79 OptionUpdateRule.LEAST.shouldUpdate(OPTION_NAME, new Exception("hi"), UPDATE); 80 } catch (ConfigurationException e) { 81 // expected 82 } 83 } 84 } 85 86