Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC (http://ursaj.com) 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 com.ursaj.hfs.gc;
 
 
 import java.io.*;
 import java.util.*;
 
 import static com.ursaj.hfs.HfsTestUtils.*;
 import static com.ursaj.hfs.gc.HfsGcResponseWriter.HEADER;
 import static org.testng.Assert.*;

 
 public class HfsGcResponseReaderTest {
    
Test input validations.

Throws:
java.io.IOException In case of any IO exception.
 
     @Test
     public void testValidations() throws IOException {
         for (int i = 0; i < 20; i++) {
             // Validate invalid constructor arguments.
             assertIllegalArgument("Underlying input stream cannot be null.",
                 HfsGcResponseReader.class"newInstance", (InputStreamnull);
 
             // Validate underlying stream is too short.
             byte[] data = new byte[.nextInt(.)];
             assertTrue(data.length < .);
 
             .nextBytes(data);
 
             assertThrows(IOException.class"Underlying input stream is not a GC response stream.",
                 HfsGcResponseReader.class"newInstance"new ByteArrayInputStream(data));
 
             // Validate invalid header.
             data = new byte[. + .nextInt(5)];
             assertTrue(data.length >= .);
 
             do {
                 .nextBytes(data);
             }
             while (Arrays.equals(Arrays.copyOf(data.), ));
 
             assertThrows(IOException.class"Underlying input stream is not a GC response stream.",
                 HfsGcResponseReader.class"newInstance"new ByteArrayInputStream(data));
         }
     }

    
Test input validations.

Throws:
java.io.IOException In case of any IO exception.
 
     @Test
     public void testWrittenData() throws IOException {
         final long p = .nextLong() & .;
         final long q = .nextLong() & .;
 
         for (int i = 0; i < 100; i++) {
             // Validate correctly written IDs.
             Collection<UUIDids = new ArrayList<UUID>();
 
             for (int j = 0; j < ij++)
                 ids.add(new UUID(p + iq + j)); // Ascending order.
 
             boolean corrupted = (i & 1) == 0;
             byte[] data = generateData(idscorrupted);
             HfsGcResponseReader in = new HfsGcResponseReader(new ByteArrayInputStream(data));
 
             try {
                 for (UUID id : ids)
                     assertEquals(in.retrieve(), id);
 
                 if (corrupted)
                     assertThrows(EOFException.class"Underlying input stream ends unexpectedly."in"retrieve");
                 else
                     assertNull(in.retrieve());
             }
             finally {
                 in.close();
             }
 
            if (i < 2)
                continue;
            // Validate invalid integrity code.
            for (int j = 0; j < ij++) {
                int pos = . + j * 18 + .nextInt(18);
                data[pos] ^= 0xFF; // Break integrity validation.
                in = new HfsGcResponseReader(new ByteArrayInputStream(data));
                for (int k = 0; k < jk++)
                    in.retrieve(); // Read correctly.
                assertThrows(IOException.class"Failed to validate stream integrity (stop reading).",
                    in"retrieve");
                data[pos] ^= 0xFF; // Recover integrity validation.
            }
            // Validate invalid order.
            for (int j = 0; j < i - 1; j++) {
                int pos = . + j * 18;
                byte[] backup = Arrays.copyOfRange(datapospos + 18);
                System.arraycopy(datapos + 18, datapos, 18); // Switch two records.
                System.arraycopy(backup, 0, datapos + 18, 18);
                in = new HfsGcResponseReader(new ByteArrayInputStream(data));
                for (int k = 0; k <= jk++)
                    in.retrieve(); // Read correctly.
                assertThrows(IOException.class"Failed to validate streamed data order (stop reading).",
                    in"retrieve");
                System.arraycopy(dataposdatapos + 18, 18); // Recover records order.
                System.arraycopy(backup, 0, datapos, 18);
            }
        }
    }

    
Test compatibility between GC response output and input.

Throws:
java.lang.Exception In case of any exception.
    @Test
    public void testCompatibility() throws Exception {
        for (int i = 0; i < 10; i++) {
            int size = .nextInt(.);
            int byteSize = size * 18 + .;
            List<UUIDids = new ArrayList<UUID>(size);
            while (ids.size() < size)
                ids.add(UUID.randomUUID());
            Collections.sort(ids.);
            ByteArrayOutputStream baos = new ByteArrayOutputStream(byteSize);
            HfsGcResponseWriter out = new HfsGcResponseWriter(baos);
            for (UUID id : ids)
                assertTrue(out.offer(id));
            assertEquals(baos.size(), byteSize);
            HfsGcResponseReader in = new HfsGcResponseReader(new ByteArrayInputStream(baos.toByteArray()));
            for (UUID id : ids)
                assertEquals(in.retrieve(), id);
            for (int j = 0; j < 20; j++)
                assertNull(in.retrieve());
        }
    }

    
Generate well-formed input data from collection of IDs.

Parameters:
ids File IDs to form input data with.
corrupted Generate corrupted data stream.
Returns:
Well-formed input data.
    private byte[] generateData(Collection<UUIDidsboolean corrupted) {
        byte[] data = new byte[. + ids.size() * 18 + (corrupted ? 1 + .nextInt(17) : 0)];
        ByteBuffer buf = ByteBuffer.wrap(data);
        buf.put();
        for (UUID id : ids) {
            HfsGcResponseWriter.writeUUID(bufid);
            int hashCode = id.hashCode();
            buf.putShort((short) ((hashCode >> 16) ^ hashCode));
        }
        return data;
    }
New to GrepCode? Check out our FAQ X