Skip to content

Commit dd22568

Browse files
committed
new unit test for ApiResponseHelper
- unit test for getDateStringInternal - StringBuffer replaced with StringBuilder, string concatenation replaced with append Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
1 parent 2c4ac5e commit dd22568

2 files changed

Lines changed: 89 additions & 24 deletions

File tree

‎server/src/com/cloud/api/ApiResponseHelper.java‎

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,45 +3389,40 @@ public String getDateStringInternal(Date inputDate) {
33893389
Calendar cal = Calendar.getInstance(tz);
33903390
cal.setTime(inputDate);
33913391

3392-
StringBuffer sb = new StringBuffer();
3393-
sb.append(cal.get(Calendar.YEAR) + "-");
3392+
StringBuilder sb = new StringBuilder(32);
3393+
sb.append(cal.get(Calendar.YEAR)).append('-');
33943394

33953395
int month = cal.get(Calendar.MONTH) + 1;
33963396
if (month < 10) {
3397-
sb.append("0" + month + "-");
3398-
} else {
3399-
sb.append(month + "-");
3397+
sb.append('0');
34003398
}
3399+
sb.append(month).append('-');
34013400

34023401
int day = cal.get(Calendar.DAY_OF_MONTH);
34033402
if (day < 10) {
3404-
sb.append("0" + day);
3405-
} else {
3406-
sb.append("" + day);
3403+
sb.append('0');
34073404
}
3405+
sb.append(day);
34083406

34093407
sb.append("'T'");
34103408

34113409
int hour = cal.get(Calendar.HOUR_OF_DAY);
34123410
if (hour < 10) {
3413-
sb.append("0" + hour + ":");
3414-
} else {
3415-
sb.append(hour + ":");
3411+
sb.append('0');
34163412
}
3413+
sb.append(hour).append(':');
34173414

34183415
int minute = cal.get(Calendar.MINUTE);
34193416
if (minute < 10) {
3420-
sb.append("0" + minute + ":");
3421-
} else {
3422-
sb.append(minute + ":");
3417+
sb.append('0');
34233418
}
3419+
sb.append(minute).append(':');
34243420

34253421
int seconds = cal.get(Calendar.SECOND);
34263422
if (seconds < 10) {
3427-
sb.append("0" + seconds);
3428-
} else {
3429-
sb.append("" + seconds);
3423+
sb.append('0');
34303424
}
3425+
sb.append(seconds);
34313426

34323427
double offset = cal.get(Calendar.ZONE_OFFSET);
34333428
if (tz.inDaylightTime(inputDate)) {
@@ -3443,26 +3438,28 @@ public String getDateStringInternal(Date inputDate) {
34433438

34443439
if (hourOffset < 0) {
34453440
if (hourOffset > -10) {
3446-
sb.append("-0" + Math.abs(hourOffset));
3441+
sb.append("-0");
34473442
} else {
3448-
sb.append("-" + Math.abs(hourOffset));
3443+
sb.append('-');
34493444
}
3445+
sb.append(Math.abs(hourOffset));
34503446
} else {
34513447
if (hourOffset < 10) {
3452-
sb.append("+0" + hourOffset);
3448+
sb.append("+0");
34533449
} else {
3454-
sb.append("+" + hourOffset);
3450+
sb.append("+");
34553451
}
3452+
sb.append(hourOffset);
34563453
}
34573454

3458-
sb.append(":");
3455+
sb.append(':');
34593456

34603457
if (minuteOffset == 0) {
34613458
sb.append("00");
34623459
} else if (minuteOffset < 10) {
3463-
sb.append("0" + minuteOffset);
3460+
sb.append('0').append(minuteOffset);
34643461
} else {
3465-
sb.append("" + minuteOffset);
3462+
sb.append(minuteOffset);
34663463
}
34673464

34683465
return sb.toString();
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.api;
18+
19+
import java.lang.reflect.Field;
20+
import java.text.ParseException;
21+
import java.text.SimpleDateFormat;
22+
import java.util.TimeZone;
23+
24+
import org.apache.cloudstack.usage.UsageService;
25+
import org.junit.Assert;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.mockito.Mock;
30+
import org.mockito.Mockito;
31+
import org.mockito.runners.MockitoJUnitRunner;
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class ApiResponseHelperTest {
35+
36+
@Mock
37+
UsageService usageService;
38+
39+
ApiResponseHelper helper;
40+
41+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ZZZ");
42+
43+
@Before
44+
public void injectMocks() throws SecurityException, NoSuchFieldException,
45+
IllegalArgumentException, IllegalAccessException {
46+
Field usageSvcField = ApiResponseHelper.class
47+
.getDeclaredField("_usageSvc");
48+
usageSvcField.setAccessible(true);
49+
helper = new ApiResponseHelper();
50+
usageSvcField.set(helper, usageService);
51+
}
52+
53+
@Test
54+
public void getDateStringInternal() throws ParseException {
55+
Mockito.when(usageService.getUsageTimezone()).thenReturn(
56+
TimeZone.getTimeZone("UTC"));
57+
Assert.assertEquals("2014-06-29'T'23:45:00+00:00", helper
58+
.getDateStringInternal(dateFormat.parse("2014-06-29 23:45:00 UTC")));
59+
Assert.assertEquals("2014-06-29'T'23:45:01+00:00", helper
60+
.getDateStringInternal(dateFormat.parse("2014-06-29 23:45:01 UTC")));
61+
Assert.assertEquals("2014-06-29'T'23:45:11+00:00", helper
62+
.getDateStringInternal(dateFormat.parse("2014-06-29 23:45:11 UTC")));
63+
Assert.assertEquals("2014-06-29'T'23:05:11+00:00", helper
64+
.getDateStringInternal(dateFormat.parse("2014-06-29 23:05:11 UTC")));
65+
Assert.assertEquals("2014-05-29'T'08:45:11+00:00", helper
66+
.getDateStringInternal(dateFormat.parse("2014-05-29 08:45:11 UTC")));
67+
}
68+
}

0 commit comments

Comments
 (0)