• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.loganalysis.util.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 
testFirst_simple()31     public void testFirst_simple() throws Exception {
32         assertEquals(UPDATE, OptionUpdateRule.FIRST.update(OPTION_NAME, null, UPDATE));
33         assertEquals(CURRENT, OptionUpdateRule.FIRST.update(OPTION_NAME, CURRENT, UPDATE));
34     }
35 
testLast_simple()36     public void testLast_simple() throws Exception {
37         assertEquals(UPDATE, OptionUpdateRule.LAST.update(OPTION_NAME, null, UPDATE));
38         assertEquals(UPDATE, OptionUpdateRule.LAST.update(OPTION_NAME, CURRENT, UPDATE));
39     }
40 
testGreatest_simple()41     public void testGreatest_simple() throws Exception {
42         assertEquals(
43                 SMALL_UPDATE, OptionUpdateRule.GREATEST.update(OPTION_NAME, null, SMALL_UPDATE));
44         assertEquals(CURRENT, OptionUpdateRule.GREATEST.update(OPTION_NAME, CURRENT, SMALL_UPDATE));
45         assertEquals(
46                 BIG_UPDATE, OptionUpdateRule.GREATEST.update(OPTION_NAME, CURRENT, BIG_UPDATE));
47     }
48 
testLeast_simple()49     public void testLeast_simple() throws Exception {
50         assertEquals(BIG_UPDATE, OptionUpdateRule.LEAST.update(OPTION_NAME, null, BIG_UPDATE));
51         assertEquals(
52                 SMALL_UPDATE, OptionUpdateRule.LEAST.update(OPTION_NAME, CURRENT, SMALL_UPDATE));
53         assertEquals(CURRENT, OptionUpdateRule.LEAST.update(OPTION_NAME, CURRENT, BIG_UPDATE));
54     }
55 
testImmutable_simple()56     public void testImmutable_simple() throws Exception {
57         assertEquals(UPDATE, OptionUpdateRule.IMMUTABLE.update(OPTION_NAME, null, UPDATE));
58         try {
59             OptionUpdateRule.IMMUTABLE.update(OPTION_NAME, CURRENT, UPDATE);
60             fail("ConfigurationException not thrown when updating an IMMUTABLE option");
61         } catch (ConfigurationException e) {
62             // expected
63         }
64     }
65 
testInvalidComparison()66     public void testInvalidComparison() throws Exception {
67         try {
68             // Strings aren't comparable with integers
69             OptionUpdateRule.GREATEST.update(OPTION_NAME, 13, UPDATE);
70             fail("ConfigurationException not thrown for invalid comparison.");
71         } catch (ConfigurationException e) {
72             // Expected.  Moreover, the exception should be actionable, so make sure we mention the
73             // specific mismatching types.
74             final String msg = e.getMessage();
75             assertTrue(msg.contains("Integer"));
76             assertTrue(msg.contains("String"));
77         }
78     }
79 
testNotComparable()80     public void testNotComparable() throws Exception {
81         try {
82             OptionUpdateRule.LEAST.update(OPTION_NAME, new Exception("hi"), UPDATE);
83         } catch (ConfigurationException e) {
84             // expected
85         }
86     }
87 }
88 
89