The StringDefinition should not expose its internal data structures.
The StringBuffer is a temporary buffer used by read. Nothing outside
the 'read' method should ever play with this object.
Change-Id: I2edda57f9208f4d1a56a3aa619545f803ea4278d
Signed-off-by: Etienne Bergeron <etienne.bergeron@gmail.com>
Reviewed-on: https://git.eclipse.org/r/19235
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
IP-Clean: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Tested-by: Hudson CI
public void testParseField_simple3() {
StringDefinition fieldDef = new StringDefinition(
new StringDeclaration(), null, fieldName);
- fieldDef.setString(new StringBuilder("Hello World"));
+ fieldDef.setValue("Hello World");
String other = "\"Hello World\"";
assertNotNull(fieldDef);
String content = "test" + i;
defs[i] = new StringDefinition(
new StringDeclaration(Encoding.UTF8), null, content);
- defs[i].setString(new StringBuilder(content));
+ defs[i].setValue(content);
}
return defs;
}
package org.eclipse.linuxtools.ctf.core.tests.types;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
*/
@Test
public void testGetDeclaration() {
- fixture.setString(new StringBuilder());
StringDeclaration result = fixture.getDeclaration();
assertNotNull(result);
}
/**
- * Run the StringBuilder getString() method test.
+ * Run the String getValue() method test.
*/
@Test
- public void testGetString() {
- fixture.setString(new StringBuilder());
- StringBuilder result = fixture.getString();
+ public void testGetValue() {
+ String result = fixture.getValue();
assertNotNull(result);
}
/**
- * Run the String getValue() method test.
+ * Run the String setValue() method test.
*/
@Test
- public void testGetValue() {
- fixture.setString(new StringBuilder());
+ public void testSetValue() {
+ fixture.setValue("dummy");
String result = fixture.getValue();
assertNotNull(result);
+ assertEquals("dummy", result);
}
/**
*/
@Test
public void testRead() throws CTFReaderException {
- fixture.setString(new StringBuilder());
BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
fixture.read(input);
}
*/
@Test
public void testSetDeclaration() {
- fixture.setString(new StringBuilder());
StringDeclaration declaration = new StringDeclaration();
fixture.setDeclaration(declaration);
}
- /**
- * Run the void setString(StringBuilder) method test.
- */
- @Test
- public void testSetString() {
- fixture.setString(new StringBuilder());
- StringBuilder string = new StringBuilder();
- fixture.setString(string);
- }
-
/**
* Run the String toString() method test.
*/
@Test
public void testToString() {
- fixture.setString(new StringBuilder());
String result = fixture.toString();
assertNotNull(result);
}
// Attributes
// ------------------------------------------------------------------------
- private StringDeclaration declaration;
+ private StringDeclaration fDeclaration;
- private StringBuilder string;
+ private String fString;
// ------------------------------------------------------------------------
// Constructors
IDefinitionScope definitionScope, String fieldName) {
super(definitionScope, fieldName);
- this.declaration = declaration;
+ fDeclaration = declaration;
- string = new StringBuilder();
+ fString = ""; //$NON-NLS-1$
}
// ------------------------------------------------------------------------
@Override
public StringDeclaration getDeclaration() {
- return declaration;
+ return fDeclaration;
}
/**
* the declaration
*/
public void setDeclaration(StringDeclaration declaration) {
- this.declaration = declaration;
+ fDeclaration = declaration;
}
/**
- * Gets the string
- *
- * @return the stringbuilder
- */
- public StringBuilder getString() {
- return string;
- }
-
- /**
- * Sets a stringbuilder for the definition
+ * Gets the string (value)
*
- * @param string
- * the stringbuilder
+ * @return the string
*/
- public void setString(StringBuilder string) {
- this.string = string;
+ public String getValue() {
+ return fString;
}
/**
- * Gets the string (value)
+ * Sets the string (value)
*
- * @return the string
+ * @param str the string
*/
- public String getValue() {
- return string.toString();
+ public void setValue(String str) {
+ fString = str;
}
// ------------------------------------------------------------------------
@Override
public void read(BitBuffer input) throws CTFReaderException {
/* Offset the buffer position wrt the current alignment */
- alignRead(input, this.declaration);
- string.setLength(0);
+ alignRead(input, fDeclaration);
+
+ StringBuilder sb = new StringBuilder();
char c = (char) input.get(8, false);
while (c != 0) {
- string.append(c);
+ sb.append(c);
c = (char) input.get(8, false);
}
+ fString = sb.toString();
}
@Override