Search in sources :

Example 1 with FixedTemplate

use of com.linkedin.data.template.FixedTemplate in project rest.li by linkedin.

the class TestFixed method testFixed.

private <T extends FixedTemplate> void testFixed(Class<T> fixedClass) {
    try {
        // check for ByteString constructor
        Constructor<T> byteStringConstructor = fixedClass.getConstructor(ByteString.class);
        // check for Object constructor
        Constructor<T> objectConstructor = fixedClass.getConstructor(Object.class);
        // has embedded FixedDataSchema
        FixedDataSchema schema = (FixedDataSchema) DataTemplateUtil.getSchema(fixedClass);
        // get size of fixed
        int size = schema.getSize();
        // create input value
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append((char) ('a' + i % 26));
        }
        String stringValue = sb.toString();
        ByteString byteStringValue = ByteString.copy(stringValue.getBytes(Data.UTF_8_CHARSET));
        // Object ctor, value is String
        T fixed = objectConstructor.newInstance(stringValue);
        assertEquals(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // Object ctor, value is ByteString
        fixed = objectConstructor.newInstance(byteStringValue);
        assertSame(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // ByteString ctor
        fixed = byteStringConstructor.newInstance(byteStringValue);
        assertSame(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // schema()
        assertSame(fixed.schema(), schema);
        // toString()
        assertEquals(fixed.toString(), byteStringValue.toString());
        // check for clone and copy override with correct return type
        TestDataTemplateUtil.assertCloneAndCopyReturnType(fixedClass);
        // test clone
        FixedTemplate fixedClone = fixed.clone();
        assertSame(fixedClone.getClass(), fixed.getClass());
        assertSame(fixedClone.bytes(), fixed.bytes());
        // test copy
        FixedTemplate fixedCopy = fixed.clone();
        assertSame(fixedCopy.getClass(), fixed.getClass());
        assertSame(fixedCopy.bytes(), fixed.bytes());
    } catch (Exception exc) {
        fail("Unexpected exception", exc);
    }
}
Also used : FixedTemplate(com.linkedin.data.template.FixedTemplate) ByteString(com.linkedin.data.ByteString) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) ByteString(com.linkedin.data.ByteString)

Example 2 with FixedTemplate

use of com.linkedin.data.template.FixedTemplate in project rest.li by linkedin.

the class TestFixed method testFixed.

private <T extends FixedTemplate> void testFixed(Class<T> fixedClass) {
    try {
        // check for ByteString constructor
        Constructor<T> byteStringConstructor = fixedClass.getConstructor(ByteString.class);
        // check for Object constructor
        Constructor<T> objectConstructor = fixedClass.getConstructor(Object.class);
        // has embedded FixedDataSchema
        FixedDataSchema schema = (FixedDataSchema) DataTemplateUtil.getSchema(fixedClass);
        // get size of fixed
        int size = schema.getSize();
        // create input value
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append((char) ('a' + i % 26));
        }
        String stringValue = sb.toString();
        ByteString byteStringValue = ByteString.copy(stringValue.getBytes(Data.UTF_8_CHARSET));
        // Object ctor, value is String
        T fixed = objectConstructor.newInstance(stringValue);
        assertEquals(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // Object ctor, value is ByteString
        fixed = objectConstructor.newInstance(byteStringValue);
        assertSame(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // ByteString ctor
        fixed = byteStringConstructor.newInstance(byteStringValue);
        assertSame(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // schema()
        assertSame(fixed.schema(), schema);
        // toString()
        assertEquals(fixed.toString(), byteStringValue.toString());
        // check for clone and copy override with correct return type
        TestDataTemplateUtil.assertCloneAndCopyReturnType(fixedClass);
        // test clone
        FixedTemplate fixedClone = fixed.clone();
        assertSame(fixedClone.getClass(), fixed.getClass());
        assertSame(fixedClone.bytes(), fixed.bytes());
        // test copy
        FixedTemplate fixedCopy = fixed.clone();
        assertSame(fixedCopy.getClass(), fixed.getClass());
        assertSame(fixedCopy.bytes(), fixed.bytes());
    } catch (Exception exc) {
        fail("Unexpected exception", exc);
    }
}
Also used : FixedTemplate(com.linkedin.data.template.FixedTemplate) ByteString(com.linkedin.data.ByteString) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) ByteString(com.linkedin.data.ByteString)

Aggregations

ByteString (com.linkedin.data.ByteString)2 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)2 FixedTemplate (com.linkedin.data.template.FixedTemplate)2