From 0ea2ebc4bb16fbf1e206ca1467a42e2d180f2972 Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Wed, 10 Oct 2012 17:54:58 +0000 Subject: [PATCH] Remove unused deprecated classes. SVN: 27137 --- .../compression/file/CompressionWorker.java | 146 -------- .../common/compression/file/Compressor.java | 137 ------- .../compression/file/FailureRecord.java | 78 ---- .../compression/file/ICompressionMethod.java | 44 --- .../file/InPlaceCompressionMethod.java | 212 ----------- .../file/CompressionWorkerTest.java | 336 ------------------ 6 files changed, 953 deletions(-) delete mode 100644 common/source/java/ch/systemsx/cisd/common/compression/file/CompressionWorker.java delete mode 100644 common/source/java/ch/systemsx/cisd/common/compression/file/Compressor.java delete mode 100644 common/source/java/ch/systemsx/cisd/common/compression/file/FailureRecord.java delete mode 100644 common/source/java/ch/systemsx/cisd/common/compression/file/ICompressionMethod.java delete mode 100644 common/source/java/ch/systemsx/cisd/common/compression/file/InPlaceCompressionMethod.java delete mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/compression/file/CompressionWorkerTest.java diff --git a/common/source/java/ch/systemsx/cisd/common/compression/file/CompressionWorker.java b/common/source/java/ch/systemsx/cisd/common/compression/file/CompressionWorker.java deleted file mode 100644 index 5b7ff60c270..00000000000 --- a/common/source/java/ch/systemsx/cisd/common/compression/file/CompressionWorker.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright 2008 ETH Zuerich, CISD - * - * 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. - */ - -package ch.systemsx.cisd.common.compression.file; - -import java.io.File; -import java.util.Collection; -import java.util.Queue; -import java.util.concurrent.atomic.AtomicInteger; - -import org.apache.log4j.Logger; - -import ch.rinn.restrictions.Private; -import ch.systemsx.cisd.common.exception.Status; -import ch.systemsx.cisd.common.exception.StatusFlag; -import ch.systemsx.cisd.common.logging.LogCategory; -import ch.systemsx.cisd.common.logging.LogFactory; - -/** - * A worker {@link Runnable} for (image) compression. - * - * @author Bernd Rinn - */ -@Deprecated -class CompressionWorker implements Runnable -{ - - @Private - static final int MAX_RETRY_OF_FAILED_COMPRESSIONS = 3; - - @Private - static final String COMPRESSING_MSG_TEMPLATE = "Compressing '%s'."; - - @Private - static final String EXCEPTION_COMPRESSING_MSG_TEMPLATE = - "Exceptional condition when trying to compress '%s'."; - - @Private - static final String INTERRPTED_MSG = "Thread has been interrupted - exiting worker."; - - @Private - static final String EXITING_MSG = "No more files to compress - exiting worker."; - - @Private - final static Logger operationLog = - LogFactory.getLogger(LogCategory.OPERATION, CompressionWorker.class); - - private final Queue<File> workerQueue; - - private final Collection<FailureRecord> failures; - - private final ICompressionMethod compressor; - - private final AtomicInteger activeWorkers; - - CompressionWorker(final Queue<File> incommingQueue, final Collection<FailureRecord> failures, - final ICompressionMethod compressor, final AtomicInteger activeWorkers) - { - assert incommingQueue != null; - assert failures != null; - assert compressor != null; - assert activeWorkers != null; - assert activeWorkers.get() > 0; - - this.workerQueue = incommingQueue; - this.failures = failures; - this.compressor = compressor; - this.activeWorkers = activeWorkers; - } - - @Override - public void run() - { - try - { - do - { - if (Thread.interrupted()) - { - if (operationLog.isInfoEnabled()) - { - operationLog.info(INTERRPTED_MSG); - } - return; - } - final File fileToCompressOrNull = workerQueue.poll(); - if (fileToCompressOrNull == null) - { - operationLog.info(EXITING_MSG); - return; - } - if (operationLog.isDebugEnabled()) - { - operationLog.debug(String - .format(COMPRESSING_MSG_TEMPLATE, fileToCompressOrNull)); - } - Status status = null; - int count = 0; - do - { - try - { - status = compressor.compress(fileToCompressOrNull); - } catch (final Throwable th) - { - operationLog.error(String.format(EXCEPTION_COMPRESSING_MSG_TEMPLATE, - fileToCompressOrNull), th); - failures.add(new FailureRecord(fileToCompressOrNull, th)); - status = null; - break; - } - } while (StatusFlag.RETRIABLE_ERROR.equals(status.getFlag()) - && ++count < MAX_RETRY_OF_FAILED_COMPRESSIONS); - if (status != null && Status.OK.equals(status) == false) - { - failures.add(new FailureRecord(fileToCompressOrNull, status)); - } - } while (true); - } finally - { - // if there are no remaining threads working notify main compressor thread that - // is waiting for all failures (see Compressor) - if (0 == activeWorkers.decrementAndGet()) - { - synchronized (failures) - { - failures.notify(); - } - } - } - } - -} diff --git a/common/source/java/ch/systemsx/cisd/common/compression/file/Compressor.java b/common/source/java/ch/systemsx/cisd/common/compression/file/Compressor.java deleted file mode 100644 index ab452df9eb8..00000000000 --- a/common/source/java/ch/systemsx/cisd/common/compression/file/Compressor.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2008 ETH Zuerich, CISD - * - * 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. - */ - -package ch.systemsx.cisd.common.compression.file; - -import java.io.File; -import java.io.FileFilter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Queue; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.atomic.AtomicInteger; - -import org.apache.log4j.Logger; - -import ch.systemsx.cisd.common.exception.EnvironmentFailureException; -import ch.systemsx.cisd.common.fileconverter.FileConverter; -import ch.systemsx.cisd.common.filesystem.FileUtilities; -import ch.systemsx.cisd.common.logging.Log4jSimpleLogger; -import ch.systemsx.cisd.common.logging.LogCategory; -import ch.systemsx.cisd.common.logging.LogFactory; -import ch.systemsx.cisd.common.logging.LogInitializer; -import ch.systemsx.cisd.common.utilities.ISelfTestable; - -/** - * The base class for file compression. - * - * @deprecated Use {@link FileConverter} instead. - * @author Bernd Rinn - */ -@Deprecated -public class Compressor -{ - - static - { - LogInitializer.init(); - } - - private static final Logger machineLog = - LogFactory.getLogger(LogCategory.MACHINE, Compressor.class); - - private static final Logger operationLog = - LogFactory.getLogger(LogCategory.OPERATION, Compressor.class); - - private static final int NUMBER_OF_PROCESSORS = Runtime.getRuntime().availableProcessors(); - - private static Queue<File> tryFillWorkerQueue(File directory, final FileFilter filter) - throws EnvironmentFailureException - { - final File[] filesToCompressOrNull = - FileUtilities.tryListFiles(directory, filter, new Log4jSimpleLogger(machineLog)); - if (filesToCompressOrNull == null) - { - String errorMsg = String.format("Path '%s' is not a directory.", directory.getPath()); - machineLog.error(errorMsg); - throw new EnvironmentFailureException(errorMsg); - } - if (operationLog.isInfoEnabled()) - { - operationLog.info(String.format("Found %d files to compress.", - filesToCompressOrNull.length)); - } - if (filesToCompressOrNull.length == 0) - { - return null; - } - return new ArrayBlockingQueue<File>(filesToCompressOrNull.length, false, Arrays - .asList(filesToCompressOrNull)); - } - - private static int getInitialNumberOfWorkers(int threadsPerProcessor) - { - assert threadsPerProcessor > 0; - return NUMBER_OF_PROCESSORS * threadsPerProcessor; - } - - private static void startUpWorkerThreads(AtomicInteger workersCounter, Queue<File> workerQueue, - Collection<FailureRecord> failed, ICompressionMethod compressor) - { - int counter = workersCounter.get(); - for (int i = 0; i < counter; ++i) - { - new Thread(new CompressionWorker(workerQueue, failed, compressor, workersCounter), - "Compressor " + i).start(); - } - if (operationLog.isInfoEnabled()) - { - operationLog.info(String.format("Started up %d worker threads.", counter)); - } - } - - public static Collection<FailureRecord> start(String directoryName, - ICompressionMethod compressionMethod, int threadsPerProcessor) - throws InterruptedException, EnvironmentFailureException - { - if (compressionMethod instanceof ISelfTestable) - { - ((ISelfTestable) compressionMethod).check(); - } - final Queue<File> workerQueue = - tryFillWorkerQueue(new File(directoryName), compressionMethod); - final Collection<FailureRecord> failed = - Collections.synchronizedCollection(new ArrayList<FailureRecord>()); - if (workerQueue == null || workerQueue.size() == 0) - { - System.out.println("No files to compress."); - return failed; - } - AtomicInteger workersCounter = - new AtomicInteger(getInitialNumberOfWorkers(threadsPerProcessor)); - startUpWorkerThreads(workersCounter, workerQueue, failed, compressionMethod); - synchronized (failed) - { - while (workersCounter.get() > 0) - { - failed.wait(); - } - } - return failed; - } -} diff --git a/common/source/java/ch/systemsx/cisd/common/compression/file/FailureRecord.java b/common/source/java/ch/systemsx/cisd/common/compression/file/FailureRecord.java deleted file mode 100644 index 160c881d3ac..00000000000 --- a/common/source/java/ch/systemsx/cisd/common/compression/file/FailureRecord.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2008 ETH Zuerich, CISD - * - * 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. - */ - -package ch.systemsx.cisd.common.compression.file; - -import java.io.File; - -import ch.systemsx.cisd.common.exception.Status; -import ch.systemsx.cisd.common.exception.StatusFlag; - -/** - * A class that holds the information about a compression failure. - * - * @author Bernd Rinn - */ -@Deprecated -public class FailureRecord -{ - private final File failedFile; - - private final Status failureStatus; - - private final Throwable throwableOrNull; - - FailureRecord(File failedFile, Status failureStatus) - { - this.failedFile = failedFile; - this.failureStatus = failureStatus; - this.throwableOrNull = null; - } - - FailureRecord(File failedFile, Throwable throwableOrNull) - { - this.failedFile = failedFile; - this.failureStatus = - Status.createError("Exceptional condition: " - + throwableOrNull.getClass().getSimpleName()); - this.throwableOrNull = throwableOrNull; - } - - /** - * Returns the file that caused the failure. - */ - public final File getFailedFile() - { - return failedFile; - } - - /** - * Returns the {@link Status} of the failure. Can have a {@link StatusFlag} of - * {@link StatusFlag#RETRIABLE_ERROR} if retrying the operation did not help. - */ - public final Status getFailureStatus() - { - return failureStatus; - } - - /** - * Returns the {@link Throwable}, if any has occurred in the compression method. - */ - public final Throwable tryGetThrowable() - { - return throwableOrNull; - } -} \ No newline at end of file diff --git a/common/source/java/ch/systemsx/cisd/common/compression/file/ICompressionMethod.java b/common/source/java/ch/systemsx/cisd/common/compression/file/ICompressionMethod.java deleted file mode 100644 index bc87f67acdc..00000000000 --- a/common/source/java/ch/systemsx/cisd/common/compression/file/ICompressionMethod.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2008 ETH Zuerich, CISD - * - * 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. - */ - -package ch.systemsx.cisd.common.compression.file; - -import java.io.File; -import java.io.FileFilter; - -import ch.systemsx.cisd.common.exception.Status; -import ch.systemsx.cisd.common.fileconverter.IFileConversionMethod; - -/** - * A role that compresses a file. A compression method may only be suitable for some files, thus it - * is also a {@link FileFilter}. - * - * @deprecated Use {@link IFileConversionMethod} instead. - * @author Bernd Rinn - */ -@Deprecated -public interface ICompressionMethod extends FileFilter -{ - - /** - * Compress the <var>fileToCompress</var> - * - * @return {@link Status#OK} if the operation was successful, a status indicating the kind of - * problem otherwise. - */ - public Status compress(File fileToCompress); - -} diff --git a/common/source/java/ch/systemsx/cisd/common/compression/file/InPlaceCompressionMethod.java b/common/source/java/ch/systemsx/cisd/common/compression/file/InPlaceCompressionMethod.java deleted file mode 100644 index 062cf669882..00000000000 --- a/common/source/java/ch/systemsx/cisd/common/compression/file/InPlaceCompressionMethod.java +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Copyright 2008 ETH Zuerich, CISD - * - * 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. - */ - -package ch.systemsx.cisd.common.compression.file; - -import java.io.File; -import java.util.List; - -import org.apache.log4j.Logger; - -import ch.systemsx.cisd.common.exception.ConfigurationFailureException; -import ch.systemsx.cisd.common.exception.EnvironmentFailureException; -import ch.systemsx.cisd.common.exception.Status; -import ch.systemsx.cisd.common.fileconverter.IFileConversionMethod; -import ch.systemsx.cisd.common.logging.LogCategory; -import ch.systemsx.cisd.common.logging.LogFactory; -import ch.systemsx.cisd.common.process.ProcessExecutionHelper; -import ch.systemsx.cisd.common.utilities.ISelfTestable; - -/** - * An {@link ICompressionMethod} that performs in-place compression of a bulk of files by means of - * calling an external compression program and running it per file in an external process. - * - * @deprecated Use {@link IFileConversionMethod} instead. - * @author Bernd Rinn - */ -@Deprecated -public abstract class InPlaceCompressionMethod implements ICompressionMethod, ISelfTestable -{ - - private static final String INPROGRESS_MARKER = ".COMPRESSION_IN_PROGRESS_"; - - private static final String COMPRESSED_MARKER = ".COMPRESSED_"; - - protected static final Logger machineLog = - LogFactory.getLogger(LogCategory.MACHINE, InPlaceCompressionMethod.class); - - protected static final Logger operationLog = - LogFactory.getLogger(LogCategory.OPERATION, InPlaceCompressionMethod.class); - - private File prefixInProgress(File file) - { - assert file != null; - - return new File(file.getParent(), INPROGRESS_MARKER + file.getName()); - } - - private File prefixCompressed(File file) - { - assert file != null; - - return new File(file.getParent(), COMPRESSED_MARKER + file.getName()); - } - - private File tryRemovePrefix(File file) - { - assert file != null; - - final String name = file.getName(); - if (name.startsWith(INPROGRESS_MARKER)) - { - return new File(file.getParent(), name.substring(INPROGRESS_MARKER.length())); - } else if (name.startsWith(COMPRESSED_MARKER)) - { - return new File(file.getParent(), name.substring(COMPRESSED_MARKER.length())); - } else - { - return null; - } - } - - private boolean isCompressedFile(File fileToCompress) - { - return fileToCompress.getName().startsWith(COMPRESSED_MARKER); - } - - private boolean isInProgressFile(File fileToCompress) - { - return fileToCompress.getName().startsWith(INPROGRESS_MARKER); - } - - private Status createStatusAndLog(String msgTemplate, Object... params) - { - final String msg = String.format(msgTemplate, params); - operationLog.error(msg); - return Status.createError(msg); - } - - /** - * Creates the command line of the external program to call in order to perform the compression. - */ - protected abstract List<String> createCommandLine(File fileToCompress, File inProgressFile); - - /** - * Returns the file extensions of files that this compression method can compress. - * <p> - * All extensions need to be returned in lower case. - */ - protected abstract List<String> getAcceptedExtensions(); - - /** - * Perform any check necessary to see whether the external program that has been found is - * suitable for the compression task (e.g. program version). - */ - @Override - public abstract void check() throws EnvironmentFailureException, ConfigurationFailureException; - - @Override - public boolean accept(File pathname) - { - if (pathname.isFile() == false) - { - return false; - } - final String name = pathname.getName().toLowerCase(); - for (String extension : getAcceptedExtensions()) - { - if (name.endsWith(extension)) - { - return true; - } - } - return false; - } - - @Override - public Status compress(File fileToCompress) - { - assert fileToCompress != null; - - // Clean up - if (isInProgressFile(fileToCompress)) - { - final boolean ok = fileToCompress.delete(); - if (ok) - { - operationLog.warn(String.format("Clean up: deleting left-over file '%s'", - fileToCompress.getAbsolutePath())); - return Status.OK; - } else - { - return createStatusAndLog("Clean up: Unable to delete left-over file '%s'", - fileToCompress.getAbsolutePath()); - } - } - if (isCompressedFile(fileToCompress)) - { - final File originalFile = tryRemovePrefix(fileToCompress); - assert originalFile != null; - if (originalFile.exists()) - { - final boolean ok = originalFile.delete(); - if (ok == false) - { - return createStatusAndLog("Clean up: Unable to delete uncompressed file '%s'", - originalFile); - } - } - if (fileToCompress.renameTo(originalFile)) - { - return Status.OK; - } else - { - return createStatusAndLog( - "Renaming compressed file '%s' to original name '%s' failed.", - fileToCompress, originalFile); - } - } - final File inProgressFile = prefixInProgress(fileToCompress); - final File compressionFinishedFile = prefixCompressed(fileToCompress); - final boolean runOK = - ProcessExecutionHelper.runAndLog(createCommandLine(fileToCompress, inProgressFile), - operationLog, machineLog); - if (runOK == false) - { - return createStatusAndLog("Unable to compress '%s'.", fileToCompress.getAbsolutePath()); - } - final boolean firstRenameOK = inProgressFile.renameTo(compressionFinishedFile); - if (firstRenameOK == false) - { - return createStatusAndLog("Unable to rename '%s' to '%s'.", inProgressFile - .getAbsolutePath(), compressionFinishedFile.getAbsolutePath()); - } - final boolean removalOfOriginalOK = fileToCompress.delete(); - if (removalOfOriginalOK == false) - { - return createStatusAndLog("Unable to delete original file '%s'", fileToCompress - .getAbsolutePath()); - } - final boolean secondRenameOK = compressionFinishedFile.renameTo(fileToCompress); - if (secondRenameOK == false) - { - return createStatusAndLog("Unable to rename '%s' to '%s'.", compressionFinishedFile - .getAbsolutePath(), fileToCompress.getAbsolutePath()); - } - return Status.OK; - } - -} \ No newline at end of file diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/compression/file/CompressionWorkerTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/compression/file/CompressionWorkerTest.java deleted file mode 100644 index 42f44402045..00000000000 --- a/common/sourceTest/java/ch/systemsx/cisd/common/compression/file/CompressionWorkerTest.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright 2008 ETH Zuerich, CISD - * - * 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. - */ - -package ch.systemsx.cisd.common.compression.file; - -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertNull; -import static org.testng.AssertJUnit.assertTrue; - -import java.io.File; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Queue; -import java.util.concurrent.atomic.AtomicInteger; - -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.api.Invocation; -import org.jmock.lib.action.CustomAction; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import ch.rinn.restrictions.Friend; -import ch.systemsx.cisd.common.exception.Status; -import ch.systemsx.cisd.common.exception.StatusFlag; -import ch.systemsx.cisd.common.logging.BufferedAppender; -import ch.systemsx.cisd.common.logging.LogInitializer; - -/** - * Test cases for the CompressionWorker. - * - * @author Bernd Rinn - */ -@SuppressWarnings("deprecation") -@Friend(toClasses = CompressionWorker.class) -public final class CompressionWorkerTest -{ - private Mockery context; - - private Queue<File> queue; - - private Collection<FailureRecord> failed; - - private ICompressionMethod compressionMethod; - - private CompressionWorker worker; - - private BufferedAppender logRecorder; - - private Level previousLevel; - - @SuppressWarnings("unchecked") - private Queue<File> createFileQueue() - { - return context.mock(Queue.class); - } - - @BeforeMethod - public void setUp() - { - LogInitializer.init(); - context = new Mockery(); - queue = createFileQueue(); - failed = new ArrayList<FailureRecord>(); - compressionMethod = context.mock(ICompressionMethod.class); - logRecorder = new BufferedAppender("%-5p %c - %m%n", Level.DEBUG); - final Logger operationLogger = CompressionWorker.operationLog; - previousLevel = operationLogger.getLevel(); - assertNull(previousLevel); - operationLogger.setLevel(Level.DEBUG); - worker = new CompressionWorker(queue, failed, compressionMethod, new AtomicInteger(1)); - } - - @AfterMethod - public void tearDown() - { - logRecorder.reset(); - CompressionWorker.operationLog.setLevel(previousLevel); - context.assertIsSatisfied(); - } - - @Test - public void testCompressionWorkerImmediateExit() - { - context.checking(new Expectations() - { - { - one(queue).poll(); - will(returnValue(null)); - } - }); - worker.run(); - assertTrue(logRecorder.getLogContent().indexOf(CompressionWorker.EXITING_MSG) >= 0); - assertEquals(0, failed.size()); - } - - @Test - public void testCompressionWorkerHappyCase() - { - final File[] files = new File[] - { new File("a"), new File("b"), new File("c") }; - context.checking(new Expectations() - { - { - for (File f : files) - { - one(queue).poll(); - will(returnValue(f)); - one(compressionMethod).compress(f); - will(returnValue(Status.OK)); - } - one(queue).poll(); - will(returnValue(null)); - } - }); - worker.run(); - assertTrue(logRecorder.getLogContent().indexOf(CompressionWorker.EXITING_MSG) >= 0); - assertEquals(0, failed.size()); - } - - @Test - public void testCompressionWorkerWithRetriableFailure() - { - final String faultyFile = "b"; - final Status faultyStatus = Status.createRetriableError("some problem"); - final File[] files = new File[] - { new File("a"), new File(faultyFile), new File("c") }; - context.checking(new Expectations() - { - { - for (File f : files) - { - one(queue).poll(); - will(returnValue(f)); - one(compressionMethod).compress(f); - if (faultyFile.equals(f.getName())) - { - will(returnValue(faultyStatus)); - one(compressionMethod).compress(f); - will(returnValue(Status.OK)); - } else - { - will(returnValue(Status.OK)); - } - } - one(queue).poll(); - will(returnValue(null)); - } - }); - worker.run(); - assertEquals(0, failed.size()); - assertTrue(logRecorder.getLogContent().indexOf(CompressionWorker.EXITING_MSG) >= 0); - } - - @Test - public void testCompressionWorkerWithRetriableFailureFinallyFailed() - { - final String faultyFile = "b"; - final Status faultyStatus = Status.createRetriableError("some problem"); - final File[] files = new File[] - { new File("a"), new File(faultyFile), new File("c") }; - context.checking(new Expectations() - { - { - for (File f : files) - { - one(queue).poll(); - will(returnValue(f)); - if (faultyFile.equals(f.getName())) - { - for (int i = 0; i < CompressionWorker.MAX_RETRY_OF_FAILED_COMPRESSIONS; ++i) - { - one(compressionMethod).compress(f); - will(returnValue(faultyStatus)); - } - } else - { - one(compressionMethod).compress(f); - will(returnValue(Status.OK)); - } - } - one(queue).poll(); - will(returnValue(null)); - } - }); - worker.run(); - assertEquals(1, failed.size()); - final FailureRecord record = failed.iterator().next(); - assertEquals(faultyFile, record.getFailedFile().getName()); - assertEquals(StatusFlag.RETRIABLE_ERROR, record.getFailureStatus().getFlag()); - assertEquals(faultyStatus.tryGetErrorMessage(), record.getFailureStatus() - .tryGetErrorMessage()); - assertTrue(logRecorder.getLogContent().indexOf(CompressionWorker.EXITING_MSG) >= 0); - } - - @Test - public void testCompressionWorkerWithFatalFailure() - { - final String faultyFile = "b"; - final Status fatalStatus = Status.createError("some problem"); - final File[] files = new File[] - { new File("a"), new File(faultyFile), new File("c") }; - context.checking(new Expectations() - { - { - for (File f : files) - { - one(queue).poll(); - will(returnValue(f)); - one(compressionMethod).compress(f); - if (faultyFile.equals(f.getName())) - { - will(returnValue(fatalStatus)); - } else - { - will(returnValue(Status.OK)); - } - } - one(queue).poll(); - will(returnValue(null)); - } - }); - worker.run(); - assertEquals(1, failed.size()); - final FailureRecord record = failed.iterator().next(); - assertEquals(faultyFile, record.getFailedFile().getName()); - assertEquals(StatusFlag.ERROR, record.getFailureStatus().getFlag()); - assertEquals(fatalStatus.tryGetErrorMessage(), record.getFailureStatus() - .tryGetErrorMessage()); - assertTrue(logRecorder.getLogContent().indexOf(CompressionWorker.EXITING_MSG) >= 0); - } - - @Test - public void testCompressionWorkerInterrupted() - { - final File[] files = new File[] - { new File("a"), new File("b"), new File("c") }; - context.checking(new Expectations() - { - { - for (File f : files) - { - one(queue).poll(); - will(returnValue(f)); - one(compressionMethod).compress(f); - if (f.getName().equals("c")) - { - will(new CustomAction("Interrupt Thread") - { - @Override - public Object invoke(Invocation invocation) throws Throwable - { - Thread.currentThread().interrupt(); - return Status.OK; - } - }); - } else - { - will(returnValue(Status.OK)); - } - } - } - }); - worker.run(); - assertTrue(logRecorder.getLogContent().indexOf(CompressionWorker.INTERRPTED_MSG) >= 0); - } - - private final class FakedException extends RuntimeException - { - - private static final long serialVersionUID = 1L; - } - - @Test - public void testCompressionWorkerCompressorThrowsException() - { - final String faultyFile = "b"; - final File[] files = new File[] - { new File("a"), new File(faultyFile), new File("c") }; - final FakedException ex = new FakedException(); - context.checking(new Expectations() - { - { - for (File f : files) - { - one(queue).poll(); - will(returnValue(f)); - one(compressionMethod).compress(f); - if (f.getName().equals(faultyFile)) - { - will(new CustomAction("Throws Exception") - { - @Override - public Object invoke(Invocation invocation) throws Throwable - { - throw ex; - } - }); - } else - { - will(returnValue(Status.OK)); - } - } - one(queue).poll(); - will(returnValue(null)); - } - }); - worker.run(); - assertEquals(1, failed.size()); - final FailureRecord record = failed.iterator().next(); - assertEquals(faultyFile, record.getFailedFile().getName()); - assertEquals(StatusFlag.ERROR, record.getFailureStatus().getFlag()); - assertEquals("Exceptional condition: " + FakedException.class.getSimpleName(), record - .getFailureStatus().tryGetErrorMessage()); - assertEquals(ex, record.tryGetThrowable()); - assertTrue(logRecorder.getLogContent().indexOf( - String.format(CompressionWorker.EXCEPTION_COMPRESSING_MSG_TEMPLATE, faultyFile)) >= 0); - } - -} -- GitLab