From 8b6dcf8b6e85b7164dbc5a3cc33b3ddc7b4da17d Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Sat, 21 Sep 2013 15:32:52 +0800 Subject: [PATCH] Clean out a few warnings on potential resource leakage --- utils/src/com/cloud/utils/NumbersUtil.java | 10 ++++-- .../test/com/cloud/utils/NumbersUtilTest.java | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 utils/test/com/cloud/utils/NumbersUtilTest.java diff --git a/utils/src/com/cloud/utils/NumbersUtil.java b/utils/src/com/cloud/utils/NumbersUtil.java index d9b06b7758f..e7e1f7645e5 100755 --- a/utils/src/com/cloud/utils/NumbersUtil.java +++ b/utils/src/com/cloud/utils/NumbersUtil.java @@ -199,25 +199,29 @@ public class NumbersUtil { public static String toReadableSize(long bytes) { if (bytes <= KB && bytes >= 0) { return Long.toString(bytes) + " bytes"; - } else if (bytes <= MB) { + } else if (bytes < MB) { StringBuilder builder = new StringBuilder(); Formatter format = new Formatter(builder); format.format("%.2f KB", (float)bytes / (float)KB); + format.close(); return builder.toString(); - } else if (bytes <= GB) { + } else if (bytes < GB) { StringBuilder builder = new StringBuilder(); Formatter format = new Formatter(builder); format.format("%.2f MB", (float)bytes / (float)MB); + format.close(); return builder.toString(); - } else if (bytes <= TB) { + } else if (bytes < TB) { StringBuilder builder = new StringBuilder(); Formatter format = new Formatter(builder); format.format("%.2f GB", (float)bytes / (float)GB); + format.close(); return builder.toString(); } else { StringBuilder builder = new StringBuilder(); Formatter format = new Formatter(builder); format.format("%.4f TB", (float)bytes / (float)TB); + format.close(); return builder.toString(); } } diff --git a/utils/test/com/cloud/utils/NumbersUtilTest.java b/utils/test/com/cloud/utils/NumbersUtilTest.java new file mode 100644 index 00000000000..711cc225089 --- /dev/null +++ b/utils/test/com/cloud/utils/NumbersUtilTest.java @@ -0,0 +1,31 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// 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.cloud.utils; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class NumbersUtilTest { + + @Test + public void formattingCheck() { + long size = 1024*1024*1024; + String formatted = NumbersUtil.toReadableSize(size); + assertEquals("1.00 GB", formatted); + } +}