Committed by
Ray Milkey
Allow null as parameter
Change-Id: I9601bca4af0dadf706a2e0ca2502595d10e7ee74
Showing
2 changed files
with
62 additions
and
1 deletions
| ... | @@ -41,7 +41,7 @@ public final class PositionalParameterStringFormatter { | ... | @@ -41,7 +41,7 @@ public final class PositionalParameterStringFormatter { |
| 41 | if (!current.contains("{}")) { | 41 | if (!current.contains("{}")) { |
| 42 | return current; | 42 | return current; |
| 43 | } | 43 | } |
| 44 | - current = current.replaceFirst("\\{\\}", parameter.toString()); | 44 | + current = current.replaceFirst("\\{\\}", String.valueOf(parameter)); |
| 45 | } | 45 | } |
| 46 | return current; | 46 | return current; |
| 47 | } | 47 | } | ... | ... |
| 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 | + | ||
| 17 | +package org.onlab.util; | ||
| 18 | + | ||
| 19 | +import static org.junit.Assert.*; | ||
| 20 | +import static org.onlab.util.PositionalParameterStringFormatter.format; | ||
| 21 | + | ||
| 22 | +import org.junit.Test; | ||
| 23 | + | ||
| 24 | +public class PositionalParameterStringFormatterTest { | ||
| 25 | + | ||
| 26 | + @Test | ||
| 27 | + public void testFormat0() { | ||
| 28 | + String fmt = "Some string 1 2 3"; | ||
| 29 | + assertEquals("Some string 1 2 3", format(fmt)); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + @Test | ||
| 33 | + public void testFormat1() { | ||
| 34 | + String fmt = "Some string {} 2 3"; | ||
| 35 | + assertEquals("Some string 1 2 3", format(fmt, 1)); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + @Test | ||
| 39 | + public void testFormat2() { | ||
| 40 | + String fmt = "Some string {} 2 {}"; | ||
| 41 | + assertEquals("Some string 1 2 3", format(fmt, 1, "3")); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + @Test | ||
| 45 | + public void testFormatNull() { | ||
| 46 | + String fmt = "Some string {} 2 {}"; | ||
| 47 | + assertEquals("Some string 1 2 null", format(fmt, 1, null)); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + @Test | ||
| 51 | + public void testFormatExtraBracket() { | ||
| 52 | + String fmt = "Some string {} 2 {}"; | ||
| 53 | + assertEquals("Some string 1 2 {}", format(fmt, 1)); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + @Test | ||
| 57 | + public void testFormatMissingBracket() { | ||
| 58 | + String fmt = "Some string 1 2 3"; | ||
| 59 | + assertEquals("Some string 1 2 3", format(fmt, 7)); | ||
| 60 | + } | ||
| 61 | +} |
-
Please register or login to post a comment