summaryrefslogtreecommitdiff
path: root/tests/src/com/android/loganalysis/util/config/OptionUpdateRuleTest.java
blob: 3dc187fc38e20242b309fce6b161cd2b230c0de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.loganalysis.util.config;

import junit.framework.TestCase;

/**
 * Unit tests for {@link OptionUpdateRule}
 */
public class OptionUpdateRuleTest extends TestCase {
    private static final String mOptionName = "option-name";
    private static final Object mCurrent = "5 current value";
    private static final Object mUpdate = "5 update value";
    private static final Object mSmallUpdate = "0 update value";
    private static final Object mBigUpdate = "9 update value";

    public void testFirst_simple() throws Exception {
        assertEquals(mUpdate, OptionUpdateRule.FIRST.update(mOptionName, null, mUpdate));
        assertEquals(mCurrent, OptionUpdateRule.FIRST.update(mOptionName, mCurrent, mUpdate));
    }

    public void testLast_simple() throws Exception {
        assertEquals(mUpdate, OptionUpdateRule.LAST.update(mOptionName, null, mUpdate));
        assertEquals(mUpdate, OptionUpdateRule.LAST.update(mOptionName, mCurrent, mUpdate));
    }

    public void testGreatest_simple() throws Exception {
        assertEquals(mSmallUpdate,
                OptionUpdateRule.GREATEST.update(mOptionName, null, mSmallUpdate));
        assertEquals(mCurrent,
                OptionUpdateRule.GREATEST.update(mOptionName, mCurrent, mSmallUpdate));
        assertEquals(mBigUpdate,
                OptionUpdateRule.GREATEST.update(mOptionName, mCurrent, mBigUpdate));
    }

    public void testLeast_simple() throws Exception {
        assertEquals(mBigUpdate,
                OptionUpdateRule.LEAST.update(mOptionName, null, mBigUpdate));
        assertEquals(mSmallUpdate,
                OptionUpdateRule.LEAST.update(mOptionName, mCurrent, mSmallUpdate));
        assertEquals(mCurrent,
                OptionUpdateRule.LEAST.update(mOptionName, mCurrent, mBigUpdate));
    }

    public void testImmutable_simple() throws Exception {
        assertEquals(mUpdate, OptionUpdateRule.IMMUTABLE.update(mOptionName, null, mUpdate));
        try {
            OptionUpdateRule.IMMUTABLE.update(mOptionName, mCurrent, mUpdate);
            fail("ConfigurationException not thrown when updating an IMMUTABLE option");
        } catch (ConfigurationException e) {
            // expected
        }
    }

    public void testInvalidComparison() throws Exception {
        try {
            // Strings aren't comparable with integers
            OptionUpdateRule.GREATEST.update(mOptionName, 13, mUpdate);
            fail("ConfigurationException not thrown for invalid comparison.");
        } catch (ConfigurationException e) {
            // Expected.  Moreover, the exception should be actionable, so make sure we mention the
            // specific mismatching types.
            final String msg = e.getMessage();
            assertTrue(msg.contains("Integer"));
            assertTrue(msg.contains("String"));
        }
    }

    public void testNotComparable() throws Exception {
        try {
            OptionUpdateRule.LEAST.update(mOptionName, new Exception("hi"), mUpdate);
        } catch (ConfigurationException e) {
            // expected
        }
    }
}