vongosling commented on a change in pull request #1249: Add unit tests for 
org.apache.rocketmq.common.UtilAll
URL: https://github.com/apache/rocketmq/pull/1249#discussion_r293649117
 
 

 ##########
 File path: common/src/test/java/org/apache/rocketmq/common/UtilAllTests.java
 ##########
 @@ -0,0 +1,332 @@
+/*
+ * 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 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 org.apache.rocketmq.common;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.lang.management.RuntimeMXBean;
+import java.net.InetAddress;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+public class UtilAllTests {
+
+    @PrepareForTest({RuntimeMXBean.class,
+            UtilAll.class, ManagementFactory.class})
+    @Test
+    public void testGetPid1() {
+        PowerMockito.mockStatic(ManagementFactory.class);
+        RuntimeMXBean runtimeMXBean = PowerMockito.mock(RuntimeMXBean.class);
+        PowerMockito.when(runtimeMXBean.getName()).thenReturn("[email protected]");
+        PowerMockito.doReturn(runtimeMXBean).when(ManagementFactory.class);
+        ManagementFactory.getRuntimeMXBean();
+
+        Assert.assertEquals(40, UtilAll.getPid());
+    }
+
+    @PrepareForTest({RuntimeMXBean.class,
+            UtilAll.class, ManagementFactory.class})
+    @Test
+    public void testGetPid2() {
+        PowerMockito.mockStatic(ManagementFactory.class);
+        RuntimeMXBean runtimeMXBean = PowerMockito.mock(RuntimeMXBean.class);
+        PowerMockito.when(runtimeMXBean.getName()).thenReturn(null);
+        PowerMockito.doReturn(runtimeMXBean).when(ManagementFactory.class);
+        ManagementFactory.getRuntimeMXBean();
+
+        Assert.assertEquals(-1, UtilAll.getPid());
+    }
+
+    @PrepareForTest({UtilAll.class})
+    @Test
+    public void testCurrentStackTrace() {
+        PowerMockito.mockStatic(Thread.class);
+        StackTraceElement stackTraceElement =
+                new StackTraceElement("Bar", "foo", "baz", 101);
+        PowerMockito.when(Thread.currentThread().getStackTrace())
+                .thenReturn(new StackTraceElement[]{stackTraceElement});
+
+        Assert.assertEquals("\n\tBar.foo(baz:101)",
+                UtilAll.currentStackTrace());
+    }
+
+    @Test
+    public void testOffset2FileName() {
+        Assert.assertEquals("00000000000000000002",
+                UtilAll.offset2FileName(2l));
+        Assert.assertEquals("00000001558787014000",
+                UtilAll.offset2FileName(1558787014000l));
+    }
+
+    @PrepareForTest({UtilAll.class, System.class})
+    @Test
+    public void testComputeEclipseTimeMilliseconds() {
+        PowerMockito.mockStatic(System.class);
+        PowerMockito.when(System.currentTimeMillis())
+                .thenReturn(1558787014000l);
+
+        Assert.assertEquals(2592000000l,
+                UtilAll.computeEclipseTimeMilliseconds(1556195014000l));
+    }
+
+    @PrepareForTest({Calendar.class, UtilAll.class})
+    @Test
+    public void testIsItTimeToDo() {
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeInMillis(1558787014000l);
+        PowerMockito.mockStatic(Calendar.class);
+        PowerMockito.when(Calendar.getInstance()).thenReturn(calendar);
+
+        Assert.assertTrue(UtilAll.isItTimeToDo("13;12;14"));
+
+        Assert.assertFalse(UtilAll.isItTimeToDo("00;00;00;"));
+    }
+
+    @PrepareForTest({UtilAll.class, System.class})
+    @Test
+    public void testTimeMillisToHumanString() {
+        PowerMockito.mockStatic(System.class);
+        PowerMockito.when(System.currentTimeMillis())
+                .thenReturn(1558787014000l);
+
+        Assert.assertEquals("20190525132334000",
+                UtilAll.timeMillisToHumanString());
+    }
+
+    @Test
+    public void testTimeMillisToHumanStringLong() {
+        Assert.assertEquals("20190525132334000",
+                UtilAll.timeMillisToHumanString(1558787014000l));
+    }
+
+    @PrepareForTest({System.class, UtilAll.class})
+    @Test
+    public void testComputNextMorningTimeMillis() {
+        PowerMockito.mockStatic(System.class);
+        PowerMockito.when(System.currentTimeMillis())
+                .thenReturn(1515585600000L);
+
+        Assert.assertEquals(1515628800000l,
+                UtilAll.computNextMorningTimeMillis());
+    }
+
+    @PrepareForTest({System.class, UtilAll.class})
+    @Test
+    public void testComputNextMinutesTimeMillis() {
+        PowerMockito.mockStatic(System.class);
+        PowerMockito.when(System.currentTimeMillis())
+                .thenReturn(1515585600000L);
+
+        Assert.assertEquals(1515585660000l,
+                UtilAll.computNextMinutesTimeMillis());
+    }
+
+    @PrepareForTest({System.class, UtilAll.class})
+    @Test
+    public void testComputNextHourTimeMillis() {
+        PowerMockito.mockStatic(System.class);
+        PowerMockito.when(System.currentTimeMillis())
+                .thenReturn(1515585600000L);
+
+        Assert.assertEquals(1515589200000l,
+                UtilAll.computNextHourTimeMillis());
+    }
+
+    @PrepareForTest({UtilAll.class, System.class})
+    @Test
+    public void testComputNextHalfHourTimeMillis() {
+        PowerMockito.mockStatic(System.class);
+        PowerMockito.when(System.currentTimeMillis())
+                .thenReturn(1515585600000L);
+
+        Assert.assertEquals(1515591000000l,
+                UtilAll.computNextHalfHourTimeMillis());
+    }
+
+    @Test
+    public void testTimeMillisToHumanString2() {
+        Assert.assertEquals("2019-04-25 13:23:34,000",
+                UtilAll.timeMillisToHumanString2(1556195014000l));
+    }
+
+    @Test
+    public void testTimeMillisToHumanString3() {
+        Assert.assertEquals("20190425132334",
+                UtilAll.timeMillisToHumanString3(1556195014000l));
+    }
+
+    @Test
+    public void testGetDiskPartitionSpaceUsedPercent() {
+        Assert.assertEquals(-1,
+                UtilAll.getDiskPartitionSpaceUsedPercent(""), 0);
+        Assert.assertEquals(-1,
+                UtilAll.getDiskPartitionSpaceUsedPercent(null), 0);
+        Assert.assertEquals(-1,
+                UtilAll.getDiskPartitionSpaceUsedPercent("foo"), 0);
+    }
+
+    @Rule
+    private TemporaryFolder folder = new TemporaryFolder();
+
+    @Test
+    public void testGetDiskPartitionSpaceUsedPercent1() throws IOException {
+        File file = folder.newFile("foo.txt");
 
 Review comment:
   Do you consider the data clearance when test stateful function?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to