summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authork.kedron <k.kedron@partner.samsung.com>2019-08-12 10:55:01 +0200
committerOfir Sonsino <ofir.sonsino@intl.att.com>2019-08-14 13:17:24 +0000
commit5a1a0612b2df2dda86189a5d7dfc793c7d8a9df6 (patch)
tree6c6aae08fec8576cc9427042f5c81cbe5a5995db
parent643fd67cf618f518b702322fba9ae0e37a7878ba (diff)
Added new tests for Report class
Issue-ID: SDC-2327 Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com> Change-Id: Ic937dca9d831e38c1b9980997c9afe7e2eb24fb7
-rw-r--r--dcaedt_tools/src/test/java/ReportTest.java117
1 files changed, 117 insertions, 0 deletions
diff --git a/dcaedt_tools/src/test/java/ReportTest.java b/dcaedt_tools/src/test/java/ReportTest.java
new file mode 100644
index 0000000..cca1421
--- /dev/null
+++ b/dcaedt_tools/src/test/java/ReportTest.java
@@ -0,0 +1,117 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Samsung. All rights reserved.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+
+import static org.junit.Assert.assertEquals;
+
+import java.security.Permission;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.BiConsumer;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import utilities.Report;
+
+public class ReportTest {
+
+ private static final String TEST_CREATED_MESSAGE = "testCreateMessage";
+ private static final String TEST_UPDATED_MESSAGE = "testUpdateMessage";
+ private static final String TEST_NOT_UPDATED_MESSAGE = "testNotUpdateMessage";
+ private static final String TEST_ERROR_MESSAGE = "testErrorMessage";
+
+ private static final String CREATED_KEY = "Created:";
+ private static final String UPDATED_KEY = "Updated:";
+ private static final String NOT_UPDATED_KEY = "Not updated:";
+ private static final String ERROR_KEY = "Error:";
+
+ private Report testObject;
+
+ @Before
+ public void setup() {
+ testObject = new Report();
+ }
+
+ @Test
+ public void createReportTest() {
+ testObject.addCreatedMessage(TEST_CREATED_MESSAGE);
+ testObject.addUpdatedMessage(TEST_UPDATED_MESSAGE);
+ testObject.addNotUpdatedMessage(TEST_NOT_UPDATED_MESSAGE);
+ testObject.addErrorMessage(TEST_ERROR_MESSAGE);
+
+ Map result = toHashMap(testObject.toString().split(System.lineSeparator()));
+
+ assertEquals(TEST_CREATED_MESSAGE, result.get(CREATED_KEY));
+ assertEquals(TEST_UPDATED_MESSAGE, result.get(UPDATED_KEY));
+ assertEquals(TEST_NOT_UPDATED_MESSAGE, result.get(NOT_UPDATED_KEY));
+ assertEquals(TEST_ERROR_MESSAGE, result.get(ERROR_KEY));
+ }
+
+ @Test
+ public void statusCodeTest() {
+ checkSystemExitStatus(0);
+ checkSystemExitStatus(2);
+ }
+
+ private Map<String, String> toHashMap(String[] result) {
+ Map<String, String> hashMap = new HashMap<>();
+ fill(result, hashMap::put);
+ return hashMap;
+ }
+
+ private void fill(String[] collection, BiConsumer<String, String> consumer) {
+ for (int i = 0; i < collection.length; i += 2) {
+ consumer.accept(collection[i], collection[i + 1]);
+ }
+ }
+
+ private void checkSystemExitStatus(int statusCode) {
+
+ class NoExitException extends RuntimeException {
+ }
+
+ SecurityManager securityManager = System.getSecurityManager();
+ System.setSecurityManager(new SecurityManager() {
+
+ @Override
+ public void checkPermission(Permission permission) {
+ }
+
+ @Override
+ public void checkPermission(Permission permission, Object o) {
+ }
+
+ @Override
+ public void checkExit(int status) {
+ assertEquals(statusCode, status);
+ super.checkExit(status);
+ throw new NoExitException();
+ }
+ });
+
+ try {
+ testObject.setStatusCode(statusCode);
+ testObject.reportAndExit();
+ } catch (Exception ignore) {
+ }
+
+ System.setSecurityManager(securityManager);
+ }
+}