From d9e465b177eaa66fc58e925ea9b67fea86060f23 Mon Sep 17 00:00:00 2001 From: ashitsalesforce Date: Thu, 18 Jan 2024 09:27:38 -0800 Subject: [PATCH] code refactoring of ProgressAdapter classes - eliminated TestProgressMonitor by switching to NihilistProgressAdapter for relevant tests. - made SWTProgressAdapter a subclass of NihilistProgressAdapter to leverage common member fields and methods. --- .../progress/NihilistProgressAdapter.java | 73 +++++----- .../action/progress/SWTProgressAdapter.java | 61 ++------ .../dataloader/process/ProcessRunner.java | 2 +- .../dataloader/TestProgressMontitor.java | 130 ------------------ .../dataloader/process/ProcessTestBase.java | 3 +- 5 files changed, 58 insertions(+), 211 deletions(-) delete mode 100644 src/test/java/com/salesforce/dataloader/TestProgressMontitor.java diff --git a/src/main/java/com/salesforce/dataloader/action/progress/NihilistProgressAdapter.java b/src/main/java/com/salesforce/dataloader/action/progress/NihilistProgressAdapter.java index 3ae71b62..72e2ceab 100644 --- a/src/main/java/com/salesforce/dataloader/action/progress/NihilistProgressAdapter.java +++ b/src/main/java/com/salesforce/dataloader/action/progress/NihilistProgressAdapter.java @@ -26,9 +26,6 @@ package com.salesforce.dataloader.action.progress; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.LogManager; - /** * This class implements the ILoaderProgress but does nothing with * the callbacks. @@ -38,88 +35,102 @@ * @author Lexi Viripaeff * @since 6.0 */ -public enum NihilistProgressAdapter implements ILoaderProgress { - INSTANCE; - public static NihilistProgressAdapter get() { - return INSTANCE; - } +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; - //logger - private final Logger logger = LogManager.getLogger(getClass()); - private String message = ""; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class NihilistProgressAdapter implements ILoaderProgress { + private String message; + private boolean success = false; private int numRowsWithError = 0; + private int numberBatchesTotal = 0; + private final Logger logger = LogManager.getLogger(getClass()); + private String taskName; + private int workDone; + private int totalWork; + private final List subTasksInOrder = new ArrayList(); - @Override - public void beginTask(String name, int totalWork) { + public NihilistProgressAdapter() { + // no op } - boolean success = false; - @Override + public void beginTask(String name, int totalWork) { + this.taskName = name; + this.totalWork = totalWork; + } + public void doneError(String msg) { success = false; message = msg; logger.error(msg); } - @Override public void doneSuccess(String msg) { success = true; message = msg; logger.info(msg); - } - @Override public void worked(int worked) { - + this.workDone += worked; } public void setTaskName(String name) { - + this.taskName = name; } - @Override public void setSubTask(String name) { + this.subTasksInOrder.add(name); logger.info(name); } + + public String getTaskName() { + return this.taskName; + } + + public int getTotalWork() { + return this.totalWork; + } + + public int getNumWorked() { + return this.workDone; + } + + public List getSubTasks() { + return Collections.unmodifiableList(this.subTasksInOrder); + } - @Override public boolean isCanceled() { return false; } - private int numberBatchesTotal = 0; - @Override public void setNumberBatchesTotal(int numberBatchesTotal) { this.numberBatchesTotal = numberBatchesTotal; } - @Override public boolean isSuccess() { return this.success; } - @Override public String getMessage() { return this.message; } - @Override public int getNumberBatchesTotal() { return this.numberBatchesTotal; } - @Override public void setNumberRowsWithError(int rowsWithError) { this.numRowsWithError = rowsWithError; } - @Override public int getNumberRowsWithError() { return this.numRowsWithError; } - -} +} \ No newline at end of file diff --git a/src/main/java/com/salesforce/dataloader/action/progress/SWTProgressAdapter.java b/src/main/java/com/salesforce/dataloader/action/progress/SWTProgressAdapter.java index 0f1b6642..c736c8cd 100644 --- a/src/main/java/com/salesforce/dataloader/action/progress/SWTProgressAdapter.java +++ b/src/main/java/com/salesforce/dataloader/action/progress/SWTProgressAdapter.java @@ -37,15 +37,13 @@ /** * @author Lexi Viripaeff */ -public class SWTProgressAdapter implements ILoaderProgress { +public class SWTProgressAdapter extends NihilistProgressAdapter { private IProgressMonitor monitor = null; - private String dispMessage; private final Controller controller; - private boolean success = false; - private int numRowsWithError = 0; public SWTProgressAdapter(IProgressMonitor monitor_, Controller controller) { + super(); monitor = monitor_; this.controller = controller; } @@ -55,13 +53,10 @@ public SWTProgressAdapter(IProgressMonitor monitor_, Controller controller) { */ @Override public void beginTask(String name, int totalWork) { + super.beginTask(name, totalWork); monitor.beginTask(name, totalWork); } - - public void done() { - monitor.done(); - } - + /* * (non-Javadoc) * @@ -69,10 +64,9 @@ public void done() { */ @Override public void doneSuccess(String message) { - success = true; + super.doneSuccess(message); monitor.done(); controller.setLastOperationSuccessful(true); - dispMessage = message; Display.getDefault().syncExec(new Thread() { @Override public void run() { @@ -80,11 +74,11 @@ public void run() { //if extraction pop open an extraction finished dialog if (controller.getConfig().getOperationInfo().isExtraction()) { ExtractionFinishDialog dlg = new ExtractionFinishDialog(LoaderWindow.getApp().getShell(), controller); - dlg.setMessage(dispMessage); + dlg.setMessage(getMessage()); dlg.open(); } else { LoadFinishDialog dlg = new LoadFinishDialog(LoaderWindow.getApp().getShell(), controller); - dlg.setMessage(dispMessage); + dlg.setMessage(getMessage()); dlg.open(); } } @@ -94,26 +88,17 @@ public void run() { @Override public void doneError(String message) { - success = false; + super.doneError(message); monitor.done(); controller.setLastOperationSuccessful(false); - dispMessage = message; Display.getDefault().syncExec(new Thread() { @Override public void run() { - UIUtils.errorMessageBox(LoaderWindow.getApp().getShell(), dispMessage); + UIUtils.errorMessageBox(LoaderWindow.getApp().getShell(), getMessage()); } }); } - - public boolean isSuccess() { - return this.success; - } - - public String getMessage() { - return this.dispMessage; - } - + /* * (non-Javadoc) * @@ -121,6 +106,7 @@ public String getMessage() { */ @Override public void worked(int worked) { + super.worked(worked); monitor.worked(worked); } @@ -130,6 +116,7 @@ public void worked(int worked) { * @see com.sfdc.action.progress.ILoaderProgress#setTaskName(java.lang.String) */ public void setTaskName(String name) { + super.setTaskName(name); monitor.setTaskName(name); } @@ -140,6 +127,7 @@ public void setTaskName(String name) { */ @Override public void setSubTask(String name) { + super.setSubTask(name); monitor.subTask(name); } @@ -152,27 +140,4 @@ public void setSubTask(String name) { public boolean isCanceled() { return monitor.isCanceled(); } - - private int numberBatchesTotal = 0; - @Override - public void setNumberBatchesTotal(int numberBatchesTotal) { - this.numberBatchesTotal = numberBatchesTotal; - } - - @Override - public int getNumberBatchesTotal() { - return this.numberBatchesTotal; - } - - @Override - public void setNumberRowsWithError(int rowsWithError) { - this.numRowsWithError = rowsWithError; - - } - - @Override - public int getNumberRowsWithError() { - return this.numRowsWithError; - } - } diff --git a/src/main/java/com/salesforce/dataloader/process/ProcessRunner.java b/src/main/java/com/salesforce/dataloader/process/ProcessRunner.java index d01a8b16..8ede84c3 100644 --- a/src/main/java/com/salesforce/dataloader/process/ProcessRunner.java +++ b/src/main/java/com/salesforce/dataloader/process/ProcessRunner.java @@ -116,7 +116,7 @@ protected ProcessRunner() { public synchronized void run(ILoaderProgress monitor) throws Exception { if (monitor == null) { - monitor = NihilistProgressAdapter.get(); + monitor = new NihilistProgressAdapter(); } this.monitor = monitor; final String oldName = Thread.currentThread().getName(); diff --git a/src/test/java/com/salesforce/dataloader/TestProgressMontitor.java b/src/test/java/com/salesforce/dataloader/TestProgressMontitor.java deleted file mode 100644 index ef8c620b..00000000 --- a/src/test/java/com/salesforce/dataloader/TestProgressMontitor.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (c) 2015, salesforce.com, inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided - * that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list of conditions and the - * following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or other materials provided with the distribution. - * - * Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or - * promote products derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package com.salesforce.dataloader; - -import com.salesforce.dataloader.action.progress.ILoaderProgress; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * Progress monitor for use by tests. - * - * @author Colin Jarvis - * @since 20.0 - */ -public class TestProgressMontitor implements ILoaderProgress { - - private String taskName; - private int workDone; - private int totalWork; - private boolean success; - private String message; - private final List subTasksInOrder = new ArrayList(); - private int numberBatchesTotal = 0; - private int numRowsWithError = 0; - - @Override - public void beginTask(String name, int totalWork) { - this.taskName = name; - this.totalWork = totalWork; - } - - @Override - public void doneError(String message) { - this.success = false; - this.message = message; - } - - @Override - public void doneSuccess(String message) { - this.success = true; - this.message = message; - } - - @Override - public void worked(int worked) { - this.workDone += worked; - } - - @Override - public void setSubTask(String name) { - this.subTasksInOrder.add(name); - } - - @Override - public boolean isCanceled() { - return false; - } - - @Override - public void setNumberBatchesTotal(int numberBatchesTotal) { - this.numberBatchesTotal = numberBatchesTotal; - } - - public int getNumberBatchesTotal() { - return numberBatchesTotal; - } - - // /////////// getters for test verification ////////////////// - - public String getTaskName() { - return this.taskName; - } - - public int getTotalWork() { - return this.totalWork; - } - - public boolean isSuccess() { - return this.success; - } - - public String getMessage() { - return this.message; - } - - public int getNumWorked() { - return this.workDone; - } - - public List getSubTasks() { - return Collections.unmodifiableList(this.subTasksInOrder); - } - - @Override - public void setNumberRowsWithError(int rowsWithError) { - this.numRowsWithError = rowsWithError; - - } - - @Override - public int getNumberRowsWithError() { - return this.numRowsWithError; - } - -} diff --git a/src/test/java/com/salesforce/dataloader/process/ProcessTestBase.java b/src/test/java/com/salesforce/dataloader/process/ProcessTestBase.java index 6754fe6f..fd20ad4a 100644 --- a/src/test/java/com/salesforce/dataloader/process/ProcessTestBase.java +++ b/src/test/java/com/salesforce/dataloader/process/ProcessTestBase.java @@ -49,6 +49,7 @@ import com.salesforce.dataloader.model.Row; import com.salesforce.dataloader.util.AppUtil; import com.salesforce.dataloader.util.Base64; +import com.salesforce.dataloader.action.progress.NihilistProgressAdapter; import com.sforce.soap.partner.*; import com.sforce.soap.partner.fault.ApiFault; import com.sforce.soap.partner.sobject.SObject; @@ -712,7 +713,7 @@ protected IProcess runBatchProcess(Map argMap) { args[i++] = entry.getKey() + "=" + entry.getValue(); } this.getBinding(); // establish the test connection if not done so already - final TestProgressMontitor monitor = new TestProgressMontitor(); + final NihilistProgressAdapter monitor = new NihilistProgressAdapter(); return DataLoaderRunner.runApp(args, monitor); }