Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC ( 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 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 com.ursaj.hfs.service;
 import java.util.*;
 import static com.ursaj.hfs.HfsTestUtils.assertThrows;
 import static org.testng.Assert.*;

HfsFileService common test cases for all implementations.

<T> File information type your application works with.
 public abstract class HfsFileServiceTestAdapter<T extends HfsFileInfo> {
Zero UUID.
     private static final UUID ZERO = new UUID(0, 0);

Gets file service instance to test.

File service instance to test.
     protected abstract HfsFileService<T> getService();

Test file service behavior.

java.lang.Exception In case of any exception.
     public void testFileService() throws Exception {
         final Random rnd = .;
         final HfsFileService<T> service = getService();
         final List<UUIDconsumed = new ArrayList<UUID>();
         final SortedMap<UUID, T> stored = new TreeMap<UUID, T>(.);
         assertTrue(service.collectAliveFiles(new HfsConsumer<UUIDIOException>(consumed)));
         assertEquals(consumed, Collections.emptyList(), "Expects no data initially stored in the file service.");
         for (int i = 0; i < 20; i++) {
             Map<UUID, T> fileInfos = newFileInfo(rnd.nextInt(5));
             // Store file info.
             for (T fileInfo : fileInfos.values())
                 assertNull(stored.put(fileInfo.getFileId(), fileInfo), "Expects no such entry in the storage.");
             // Expects duplicate storing fails.
             if (!fileInfos.isEmpty())
             for (T fileInfo : fileInfos.values())
                 assertThrows(IOException.classnullservice"storeFiles", Collections.singleton(fileInfo));
             // Iterate over all stored file information entries.
             for (UUID storedId : stored.keySet()) {
                 assertTrue(service.collectAliveFiles(storedIdnew HfsConsumer<UUIDIOException>(consumed)));
                     "Validate stored IDs are collected in correct order.");
                 assertEquals(service.getFiles(Collections.singleton(storedId)).get(storedId), stored.get(storedId),
                     "Expects retrieved file info equals the stored one.");
             assertEquals(service.getFiles(fileInfos.keySet()), fileInfos);
         assertEquals(service.getFiles(stored.keySet()), stored);
         // Collect ALL file IDs.
         assertTrue(service.collectAliveFiles(new HfsConsumer<UUIDIOException>(consumed)));
         // Validate discarding consumer is called only once.
        for (UUID fileId : stored.keySet()) {
            DiscardCounterConsumer<UUIDconsumer = new DiscardCounterConsumer<UUID>();
            assertEquals(consumer.counter.get(), 1L);

Constructs new collection of file information entries to store in the file service.

count Number of new file information entries to create.
Collection of new file information entries.
    protected Map<UUID, T> newFileInfo(int count) {
        Map<UUID, T> result = new HashMap<UUID, T>(count, 1.0f);
        while (count-- > 0) {
            UUID fileId = UUID.randomUUID();
        return result;

Constructs new file information entry to store in the file service.

fileId File ID of new file information entry to create.
New file information entry to store in the file service.
    protected abstract T newFileEntry(UUID fileId);

Consumer discarding all offered data.

<E> Collected items type.
    private static final class DiscardCounterConsumer<E> extends ArrayList<E> implements Consumer<E, IOException> {
Consumer calls counter.
        private final AtomicLong counter = new AtomicLong();
        public boolean offer(E itemthrows IOException {
            return false;
New to GrepCode? Check out our FAQ X