Lars J. Nilsson avatar Lars J. Nilsson committed a00a565

Issue #4: Supporting null enums in structs in java packets

Comments (0)

Files changed (7)

core/src/main/java/com/cubeia/firebase/io/PacketInputStream.java

     public PacketInputStream(ByteBuffer inBuffer) {
         this.inBuffer = inBuffer;
     }
+    
+    public int peek() throws IOException {
+    	int pos = inBuffer.position();
+    	return (pos + 1 > inBuffer.limit() ? -1 : inBuffer.get(pos));
+    }
 
     public byte loadByte() throws IOException {
         return inBuffer.get();

core/src/test/java/com/cubeia/firebase/protocol/PacketInputStreamTest.java

+package com.cubeia.firebase.protocol;
+
+import java.nio.ByteBuffer;
+
+import junit.framework.TestCase;
+
+import com.cubeia.firebase.io.PacketInputStream;
+
+public class PacketInputStreamTest extends TestCase {
+
+	public void testPeek() throws Exception {
+		ByteBuffer b = ByteBuffer.allocate(4);
+		PacketInputStream in = new PacketInputStream(b);
+		b.put(0, (byte) 1);
+		b.put(1, (byte) 2);
+		b.put(2, (byte) 3);
+		b.put(3, (byte) 4);
+		assertEquals(1, in.loadByte()); // read first
+		assertEquals(2, in.peek()); // peek second
+		assertEquals(1, b.position()); // check position isn't advanced
+		assertEquals(2, in.loadByte()); // read second
+		assertEquals(2, b.position()); // check position
+		assertEquals(3, in.loadByte());
+		assertEquals(4, in.loadByte());
+		assertEquals(-1, in.peek()); // end of stream	
+	}
+}

maven/protocol-generator-plugin/src/main/scripts/java_generator.rb

   def load_enum(enum)
     name = enum.type.capitalize
     name = camelcase(name)
-    wi("#{camelcase(enum.name)} = Enums.make#{name}(ps.loadUnsignedByte());")
+    wi("if (ps.peek() == -1) {");
+    @indent_level += 1
+    wi("this.#{camelcase(enum.name)} = null;")
+    @indent_level -= 1
+    wi("} else {")
+    @indent_level += 1
+    wi("this.#{camelcase(enum.name)} = Enums.make#{name}(ps.loadUnsignedByte());")
+    @indent_level -= 1
+    wi("}")
   end
   
   def save_enum(enum)
     name = camelcase(enum.name)
-    wi("ps.saveUnsignedByte(#{name}.ordinal());")
+    wi("if (this.#{name} == null) {");
+    @indent_level += 1
+    wi("ps.saveUnsignedByte(-1);")
+    @indent_level -= 1
+    wi("} else {")
+    @indent_level += 1
+    wi("ps.saveUnsignedByte(this.#{name}.ordinal());")
+    @indent_level -= 1
+    wi("}")
   end
   
   def load_scalar(scalar)

maven/protocol-tests/java-test/pom.xml

+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<artifactId>styx-java-test</artifactId>
+	<name>Styx Java Test</name>
+	<packaging>jar</packaging>
+
+	<parent>
+		<groupId>com.cubeia.styx</groupId>
+		<artifactId>styx-tests-root</artifactId>
+		<version>1.11-SNAPSHOT</version>
+	</parent>
+
+	<dependencies>
+		<dependency>
+			<groupId>com.cubeia.styx</groupId>
+			<artifactId>styx-core</artifactId>
+			<version>${project.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.10</version>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>com.cubeia.styx</groupId>
+				<artifactId>protocol-generator-plugin</artifactId>
+				<version>${project.version}</version>
+				<configuration>
+					<protocol_file>src/test/resources/protocol.xml</protocol_file>
+					<language>java</language>
+					<package_name>com.cubeia.styx.test.protocol</package_name>
+					<generate_visitors>false</generate_visitors>
+					<fail_on_bad_packet_order>true</fail_on_bad_packet_order>
+				</configuration>
+				<executions>
+					<execution>
+						<phase>generate-sources</phase>
+						<goals>
+							<goal>generate</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+</project>

maven/protocol-tests/java-test/src/test/java/com/cubeia/styx/test/ProtocolTest.java

+package com.cubeia.styx.test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+
+import org.junit.Test;
+
+import com.cubeia.firebase.io.PacketOutputStream;
+import com.cubeia.styx.test.protocol.EnumTestPacket;
+
+public class ProtocolTest {
+
+	@Test
+	public void nullEnumOnSave() throws Exception {
+		EnumTestPacket e = new EnumTestPacket();
+		e.type = null;
+		ByteArrayOutputStream ba = new ByteArrayOutputStream();
+		PacketOutputStream out = new PacketOutputStream(new DataOutputStream(ba));
+		e.save(out);
+	}
+}

maven/protocol-tests/java-test/src/test/resources/protocol.xml

+<protocol version="$Revision: 6329 $">
+
+    <enum name="enum_type">
+      <value>STRING</value>
+      <value>INT</value>
+      <value>DATE</value>
+    </enum>
+
+    <!-- ==========  General Structs  ========== -->
+    
+    <struct id="1" name="enum_test_packet">
+        <var name="type" type="enum_type"/>
+    </struct>
+    
+    <struct id="2" name="type_test_packet">
+        <var name="i8" type="int8"/>
+        <var name="ui8" type="uint8"/>
+        <var name="i16" type="int16"/>
+        <var name="i32" type="int32"/>
+        <var name="b" type="bool"/>
+        <var name="s" type="string"/>
+    </struct>
+    
+    <struct ud="3" name="list_test_packet">
+        <list name="l" type="uint8"/>
+    </struct>
+    
+    <struct ud="4" name="dep_test_packet">
+        <var name="t" type="type_test_packet"/>
+    </struct>
+    
+    <struct id="5" name="list_of_enums">
+    	<list name="l" type="enum_type" />
+    </struct>
+    
+    <struct id="6" name="list_of_ints">
+    	<list name="l" type="int32" />
+    </struct>
+  
+</protocol>

maven/protocol-tests/pom.xml

-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-  <artifactId>styx-tests-root</artifactId>
-  <name>Styx Tests Root</name>
-  <packaging>pom</packaging>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<artifactId>styx-tests-root</artifactId>
+	<name>Styx Tests Root</name>
+	<packaging>pom</packaging>
 
-  <parent>
-	<artifactId>styx-maven</artifactId>
-	<groupId>com.cubeia.styx</groupId>
-	<version>1.11-SNAPSHOT</version>
-</parent>
+	<parent>
+		<artifactId>styx-maven</artifactId>
+		<groupId>com.cubeia.styx</groupId>
+		<version>1.11-SNAPSHOT</version>
+	</parent>
 
-<modules>
-  <module>js-test</module>
-</modules>
+	<modules>
+		<module>js-test</module>
+		<module>java-test</module>
+	</modules>
+
+	<build>
+		<pluginManagement>
+			<plugins>
+				<!--This plugin's configuration is used to store Eclipse m2e settings 
+					only. It has no influence on the Maven build itself. -->
+				<plugin>
+					<groupId>org.eclipse.m2e</groupId>
+					<artifactId>lifecycle-mapping</artifactId>
+					<version>1.0.0</version>
+					<configuration>
+						<lifecycleMappingMetadata>
+							<pluginExecutions>
+								<pluginExecution>
+									<pluginExecutionFilter>
+										<groupId>com.cubeia.styx</groupId>
+										<artifactId>
+											protocol-generator-plugin
+										</artifactId>
+										<versionRange>[1.7.0,)</versionRange>
+										<goals>
+											<goal>generate</goal>
+										</goals>
+									</pluginExecutionFilter>
+									<action>
+										<ignore />
+									</action>
+								</pluginExecution>
+							</pluginExecutions>
+						</lifecycleMappingMetadata>
+					</configuration>
+				</plugin>
+			</plugins>
+		</pluginManagement>
+
+	</build>
 
 </project>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.