Пример #1
0
 @Test
 public void testReadGenerationChanged() throws IOException {
   BlobId blobId = BlobId.of(BUCKET_NAME, BLOB_NAME);
   reader = new BlobReadChannel(options, blobId, EMPTY_RPC_OPTIONS);
   byte[] firstResult = randomByteArray(DEFAULT_CHUNK_SIZE);
   byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE);
   ByteBuffer firstReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
   ByteBuffer secondReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
   expect(storageRpcMock.read(blobId.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
       .andReturn(StorageRpc.Tuple.of("etag1", firstResult));
   expect(
           storageRpcMock.read(
               blobId.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE, DEFAULT_CHUNK_SIZE))
       .andReturn(StorageRpc.Tuple.of("etag2", secondResult));
   replay(storageRpcMock);
   reader.read(firstReadBuffer);
   try {
     reader.read(secondReadBuffer);
     fail("Expected ReadChannel read to throw StorageException");
   } catch (StorageException ex) {
     StringBuilder messageBuilder = new StringBuilder();
     messageBuilder.append("Blob ").append(blobId).append(" was updated while reading");
     assertEquals(messageBuilder.toString(), ex.getMessage());
   }
 }
  private void executeEscapeTest(String aValue, String aExpectedVal) {
    String name = "AttrName";
    StorageAttribute sa = new StorageAttribute(name, aValue, StorageAttribute.INT_TYPE);

    try {
      assertTrue(name.equals(sa.getName()));
      assertTrue(sa.getEscapedValue().equals(aExpectedVal));
    } catch (StorageException se) {
      assertTrue("Escape fail: " + se.getMessage() + "Val: '" + aValue + "'", false);
    }
  }
  /**
   * Test constructor with name, value and type. Getters are used to verify values.
   *
   * <p>1. Test with Name, Value and Type. 2. Test Name null throws StorageException 3. Test Name ""
   * throws StorageException 4. Test value null. If value is not set null is returned. 5. Test value
   * "". 6. Test one len arguments.
   */
  public void testAttribute() {
    StorageAttribute sa = new StorageAttribute("N/A", "N/A");

    // 1. Test with Name, Value and Type.
    try {
      String name = "AttrName";
      String value = "AttrValue";
      sa.setAttribute(name, value, StorageAttribute.INT_TYPE);

      assertTrue(name.equals(sa.getName()));
      assertTrue(value.equals(sa.getValue()));
      assertTrue(sa.getType() == StorageAttribute.INT_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute set failed: " + se.getMessage(), false);
    }

    // 2. Test null name
    try {
      String name = null;
      String value = "AttrValue";
      sa.setAttribute(name, value, StorageAttribute.INT_TYPE);

      assertTrue("Null name does not throw exception", false);
    } catch (StorageException se) {
      // PASSED
    }

    // 3. Test empty name
    try {
      String name = "";
      String value = "AttrValue";
      sa.setAttribute(name, value, StorageAttribute.INT_TYPE);

      assertTrue("Empty name does not throw exception", false);
    } catch (StorageException se) {
      // PASSED
    }

    // 4. Test null value.
    try {
      String name = "AttrName";
      String value = null;
      sa.setAttribute(name, value, StorageAttribute.NULL_TYPE);

      assertTrue(name.equals(sa.getName()));
      assertTrue(sa.getValue() == null);
      assertTrue(sa.getType() == StorageAttribute.NULL_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute set failed: " + se.getMessage(), false);
    }

    // 5. Test empty value.
    try {
      String name = "AttrName";
      String value = "";
      sa.setAttribute(name, value, StorageAttribute.INT_TYPE);

      assertTrue(name.equals(sa.getName()));
      assertTrue(sa.getValue() == "");
      assertTrue(sa.getType() == StorageAttribute.INT_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute set failed: " + se.getMessage(), false);
    }

    // 6. Test one len arguments
    try {
      String name = "N";
      String value = "V";
      sa.setAttribute(name, value, StorageAttribute.STRING_TYPE);

      assertTrue(name.equals(sa.getName()));
      assertTrue(value.equals(sa.getValue()));
      assertTrue(sa.getType() == StorageAttribute.STRING_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute set failed: " + se.getMessage(), false);
    }
  }
  /**
   * Test constructor with name, value and type. 1. Test with Name, Value and Type. 2. Test Name
   * null throws StorageException 3. Test Name "" throws StorageException 4. Test value null. If
   * value is not set null is returned. 5. Test value "". 6. Test TYPE is set. 7. Test one len
   * arguments. 8. Test not supported type throws StorageException.
   */
  public void testNameValueTypeConstructor() {
    StorageAttribute sa = null;

    // 1. Test with Name, Value and Type.
    try {
      String name = "AttrName";
      String value = "AttrValue";
      sa = new StorageAttribute(name, value, StorageAttribute.INT_TYPE);

      assertTrue(name.equals(sa.getName()));
      assertTrue(value.equals(sa.getValue()));
      assertTrue(sa.getType() == StorageAttribute.INT_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute creation failed: " + se.getMessage(), false);
    }

    // 2. Test null name
    try {
      String name = null;
      String value = "AttrValue";
      sa = new StorageAttribute(name, value, StorageAttribute.INT_TYPE);

      assertTrue("Null name does not throw exception", false);
    } catch (StorageException se) {
      // PASSED
    }

    // 3. Test empty name
    try {
      String name = "";
      String value = "AttrValue";
      sa = new StorageAttribute(name, value, StorageAttribute.INT_TYPE);

      assertTrue("Empty name does not throw exception", false);
    } catch (StorageException se) {
      // PASSED
    }

    // 4. Test null value.
    try {
      String name = "AttrName";
      String value = null;
      sa = new StorageAttribute(name, value, StorageAttribute.NULL_TYPE);

      assertTrue(name.equals(sa.getName()));
      assertTrue(sa.getValue() == null);
      assertTrue(sa.getType() == StorageAttribute.NULL_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute creation failed: " + se.getMessage(), false);
    }

    // 5. Test empty value
    try {
      String name = "AttrName";
      String value = "";
      sa = new StorageAttribute(name, value, StorageAttribute.STRING_TYPE);

      assertTrue(name.equals(sa.getName()));
      assertTrue(sa.getValue() == "");
      assertTrue(sa.getType() == StorageAttribute.STRING_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute creation failed: " + se.getMessage(), false);
    }

    // 6. and 7. Test one len arguments.
    try {
      String name = "A";
      String value = "V";
      sa = new StorageAttribute(name, value, StorageAttribute.STRING_TYPE);

      assertTrue(name.equals(sa.getName()));
      assertTrue(value.equals(sa.getValue()));
      // Default value is set if not defined.
      assertTrue(sa.getType() == StorageAttribute.STRING_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute creation failed: " + se.getMessage(), false);
    }

    // 8. Test not supported type throws StorageException.
    try {
      String name = NAME;
      String value = "AttrValue";
      sa = new StorageAttribute(name, value, 8);

      assertTrue("Not defined type do not throw exp", false);
    } catch (StorageException se) {
      // PASSED
    }
  }
  /**
   * Test constructor with name and value. 2. Test with Name and Value. 3. Test Name null throws
   * StorageException 4. Test Name "" throws StorageException 5. Test value null. It is allowed
   * value. 6. Test value "". 7. Test one len name and value. 8. Test value null, type STRING is
   * allowed.
   */
  public void testNameValueConstructor() {
    StorageAttribute sa = new StorageAttribute("N/A", "N/A");
    // 2. Test with Name and Value
    try {
      String name = "AttrName";
      String value = "AttrValue";
      sa = new StorageAttribute(name, value);

      assertTrue(name.equals(sa.getName()));
      assertTrue(value.equals(sa.getValue()));
      // Default value is set if not defined.
      assertTrue(sa.getType() == StorageAttribute.STRING_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute creation failed: " + se.getMessage(), false);
    }

    // 3. Test null name
    try {
      String name = null;
      String value = "AttrValue";
      sa = new StorageAttribute(name, value);

      assertTrue("Null name does not throw exception", false);
    } catch (StorageException se) {
      // PASSED
    }

    // 4. Test empty name
    try {
      String name = "";
      String value = "AttrValue";
      sa = new StorageAttribute(name, value);

      assertTrue("Empty name does not throw exception", false);
    } catch (StorageException se) {
      // PASSED
    }

    // 5. Test null value.
    try {
      String name = "AttrName";
      String value = null;
      sa = new StorageAttribute(name, value);
    } catch (StorageException se) {
      assertTrue("SE thrown when null value: " + se.toString(), false);
    } catch (Throwable t) {
      assertTrue("Wrong exp thrown: " + t.toString(), false);
    }

    // 6. Test empty value
    try {
      String name = "AttrName";
      String value = "";
      sa = new StorageAttribute(name, value);

      assertTrue(name.equals(sa.getName()));
      assertTrue(sa.getValue() == "");
      assertTrue(sa.getType() == StorageAttribute.STRING_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute creation failed: " + se.getMessage(), false);
    }

    // 7. Test one len arguments
    try {
      String name = "A";
      String value = "V";
      sa = new StorageAttribute(name, value);

      assertTrue(name.equals(sa.getName()));
      assertTrue(value.equals(sa.getValue()));
      // Default value is set if not defined.
      assertTrue(sa.getType() == StorageAttribute.STRING_TYPE);
    } catch (StorageException se) {
      assertTrue("Attribute creation failed: " + se.getMessage(), false);
    }

    // 8. Test value null, type STRING throws StorageException.
    try {
      String name = "A";
      String value = null;
      sa = new StorageAttribute(name, value);
    } catch (StorageException se) {
      assertTrue("SE thrown when null value: " + se.toString(), false);
    } catch (Throwable t) {
      assertTrue("Wrong exception thrown: " + t.toString(), false);
    }
  }