Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add test case for protobuf support.
  • Loading branch information
aij committed Oct 20, 2014
commit a67fac53a8d6106f701c132b10799cb04a8452eb
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.6.0</version>
</dependency>
<!-- Not using this for compile currently
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/googlecode/objectify/annotation/Protobuf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.googlecode.objectify.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.zip.Deflater;

/**
* <p>When placed on an entity field, the field will be written as a single Blob
* property using protobuf serialization. </p>
*
* <ul>
* <li>The field must contain a Google Protocol Buffer object.</li>
* <li>You will not be able to use the field or any child fields in queries.
* (Support could be added in the future.)</li>
* </ul>
*
* @author Ivan Jager <aij+@mrph.org>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Protobuf
{
// TODO: Will we need to add anything here for deserialization to be possible?
}
46 changes: 46 additions & 0 deletions src/test/java/com/googlecode/objectify/test/ProtobufTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.googlecode.objectify.test;

import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Protobuf;
import com.googlecode.objectify.test.util.TestBase;
import org.testng.annotations.Test;

import static com.googlecode.objectify.test.util.TestObjectifyService.fact;
import static com.googlecode.objectify.test.util.TestObjectifyService.ofy;

// For testing, use protobufs that are already baked into the protobuf
// library. This way, we don't need to add a dependency on protoc.
import com.google.protobuf.DescriptorProtos;

/**
* Tests of the {@code @Protobuf} annotation
*/
public class ProtobufTests extends TestBase {
@Entity
@Cache
public static class HasProto {
@Id public Long id;
@Protobuf public DescriptorProtos.FieldDescriptorProto proto;
}

@Test
public void testSimpleProtobuf() throws Exception {
fact().register(HasProto.class);

HasProto hp = new HasProto();
hp.proto = DescriptorProtos.FieldDescriptorProto.newBuilder()
.setName("foo")
.setNumber(57)
.setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING)
.build();

HasProto fetched = ofy().saveClearLoad(hp);
assert fetched.proto.equals(hp.proto);
}

// TODO: Check that the blob being stored is actually a serialized protobuf
// that could be parsed in other languages too. For example,
// DescriptorProtos.FieldDescriptorProto.parseFrom(data) should work.
}