Skip to content

Commit

Permalink
Added test to confirm empty bdf file on build binary
Browse files Browse the repository at this point in the history
  • Loading branch information
hibrandon committed Apr 11, 2012
1 parent dfd1d26 commit 3de522b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Expand Up @@ -179,7 +179,12 @@ public SdfFile buildXml() {
}

@Override
public int buildBinary(BdfFile file) throws IOException {
public int buildBinary(BdfFile file) throws IOException, IllegalArgumentException{

if (file.length() != 0){
throw new IllegalArgumentException();
}

file.seek(0);
// write the header
file.writePlaceholder(); // file size
Expand Down Expand Up @@ -210,7 +215,9 @@ public int buildBinary(BdfFile file) throws IOException {
for (int i = 0; i < size; i++) {
this.getFileDefinitions().get(i).buildBinary(file);
}


//calculate CRC up to this point

// write the SDF integrity def
file.writeSdfIntegrityDefinitionPointer();
this.getSdfIntegrityDefinition().setIntegrityValue(
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/edu/cmu/sv/arinc838/writer/BdfWriter.java
Expand Up @@ -21,10 +21,15 @@ public class BdfWriter implements SdfWriter {
public String write(String path, SoftwareDefinitionFileBuilder builder)
throws Exception {


File fileOnDisk =new File(path+builder.getBinaryFileName());


BdfFile file = new BdfFile(fileOnDisk);

//This file must be empty
file.setLength(0);

write(file, builder);

return fileOnDisk.getAbsolutePath();
Expand Down
Expand Up @@ -324,7 +324,7 @@ public void testBuildBinaryWritesSoftwareDefinition() throws IOException {
InOrder order = inOrder(file, swDescription, thdBuilder,
thdBuilderLast, fdBuilder, fdBuilderLast, sdfInteg, lspInteg);

when(file.length()).thenReturn(14L);
when(file.length()).thenReturn(0L, 14L);
int bytesWritten = swDefFileBuilder.buildBinary(file);
assertEquals(bytesWritten, 14L);

Expand All @@ -348,8 +348,9 @@ public void testBuildBinaryWritesSoftwareDefinition() throws IOException {
order.verify(fdBuilder, times(2)).buildBinary(file);
order.verify(fdBuilderLast).buildBinary(file);

order.verify(file)
order.verify(file).writeSdfIntegrityDefinitionPointer();
// TODO actually calculate the CRC

order.verify(sdfInteg).setIntegrityValue(
Converter.hexToBytes("0000000A"));
order.verify(sdfInteg).buildBinary(file);
Expand Down Expand Up @@ -504,4 +505,17 @@ public void testInitializeXmlClearsTargetHardwareDefinitions(){

assertEquals(builder.getTargetHardwareDefinitions().size(), swDefFile.getThwDefinitions().size());
}


@Test (expectedExceptions = IllegalArgumentException.class)
public void testBinaryFileIsEmptyPriorToBuild() throws FileNotFoundException, IOException{
BdfFile bdf = new BdfFile(File.createTempFile("tmp", "BDF"));
bdf.write(3);

SoftwareDefinitionFileBuilder builder = new SoftwareDefinitionFileBuilder();

builder.buildBinary(bdf);

}

}

0 comments on commit 3de522b

Please sign in to comment.