Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.cfg; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.cfg.ConfigProperty.Type; |
| 21 | |
| 22 | import static org.junit.Assert.*; |
| 23 | import static org.onosproject.cfg.ConfigProperty.Type.*; |
| 24 | import static org.onosproject.cfg.ConfigProperty.defineProperty; |
| 25 | import static org.onosproject.cfg.ConfigProperty.resetProperty; |
| 26 | import static org.onosproject.cfg.ConfigProperty.setProperty; |
| 27 | |
| 28 | /** |
| 29 | * Set of tests of the configuration property class. |
| 30 | */ |
| 31 | public class ConfigPropertyTest { |
| 32 | |
| 33 | @Test |
| 34 | public void basics() { |
| 35 | ConfigProperty p = defineProperty("foo", STRING, "bar", "Foo Prop"); |
| 36 | validate(p, "foo", STRING, "bar", "bar"); |
| 37 | p = setProperty(p, "BAR"); |
| 38 | validate(p, "foo", STRING, "BAR", "bar"); |
| 39 | p = resetProperty(p); |
| 40 | validate(p, "foo", STRING, "bar", "bar"); |
| 41 | } |
| 42 | |
| 43 | @Test |
| 44 | public void equality() { |
| 45 | new EqualsTester() |
| 46 | .addEqualityGroup(defineProperty("foo", STRING, "bar", "Desc"), |
| 47 | defineProperty("foo", STRING, "goo", "Desc")) |
| 48 | .addEqualityGroup(defineProperty("bar", STRING, "bar", "Desc"), |
| 49 | defineProperty("bar", STRING, "goo", "Desc")) |
| 50 | .testEquals(); |
| 51 | } |
| 52 | |
| 53 | private void validate(ConfigProperty p, String name, Type type, String v, String dv) { |
| 54 | assertEquals("incorrect name", name, p.name()); |
| 55 | assertEquals("incorrect type", type, p.type()); |
| 56 | assertEquals("incorrect value", v, p.value()); |
| 57 | assertEquals("incorrect default", dv, p.defaultValue()); |
| 58 | assertEquals("incorrect description", "Foo Prop", p.description()); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void asInteger() { |
| 63 | ConfigProperty p = defineProperty("foo", INTEGER, "123", "Foo Prop"); |
| 64 | validate(p, "foo", INTEGER, "123", "123"); |
| 65 | assertEquals("incorrect value", 123, p.asInteger()); |
| 66 | assertEquals("incorrect value", 123L, p.asLong()); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void asLong() { |
| 71 | ConfigProperty p = defineProperty("foo", LONG, "123", "Foo Prop"); |
| 72 | validate(p, "foo", LONG, "123", "123"); |
| 73 | assertEquals("incorrect value", 123L, p.asLong()); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void asFloat() { |
| 78 | ConfigProperty p = defineProperty("foo", FLOAT, "123.0", "Foo Prop"); |
| 79 | validate(p, "foo", FLOAT, "123.0", "123.0"); |
| 80 | assertEquals("incorrect value", 123.0, p.asFloat(), 0.01); |
| 81 | assertEquals("incorrect value", 123.0, p.asDouble(), 0.01); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void asDouble() { |
| 86 | ConfigProperty p = defineProperty("foo", DOUBLE, "123.0", "Foo Prop"); |
| 87 | validate(p, "foo", DOUBLE, "123.0", "123.0"); |
| 88 | assertEquals("incorrect value", 123.0, p.asDouble(), 0.01); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void asBoolean() { |
| 93 | ConfigProperty p = defineProperty("foo", BOOLEAN, "true", "Foo Prop"); |
| 94 | validate(p, "foo", BOOLEAN, "true", "true"); |
| 95 | assertEquals("incorrect value", true, p.asBoolean()); |
| 96 | } |
| 97 | } |