Skip to content

Commit

Permalink
start work on target hardware def test - complete DAO test [23650083]
Browse files Browse the repository at this point in the history
  • Loading branch information
rdn committed Mar 27, 2012
1 parent 9584777 commit 3a7f629
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.arinc.arinc838.ThwDefinition;

import edu.cmu.sv.arinc838.binary.BdfFile;
import edu.cmu.sv.arinc838.validation.DataValidator;

public class TargetHardwareDefinitionDao {

Expand All @@ -35,11 +34,12 @@ public TargetHardwareDefinitionDao(ThwDefinition jaxbDef) {
public TargetHardwareDefinitionDao() {
}

public TargetHardwareDefinitionDao(BdfFile bdfFile) throws IOException {
public TargetHardwareDefinitionDao(BdfFile bdfFile) throws IOException {
setThwId(bdfFile.readStr64k());
long positionsLength = bdfFile.readUint32();
for(int i=0; i<positionsLength; i++) {
bdfFile.readUint32(); // Read out the pointer to the next thw-position. We don't use it.
for (int i = 0; i < positionsLength; i++) {
bdfFile.readUint32(); // Read out the pointer to the next
// thw-position. We don't use it.
getPositions().add(bdfFile.readStr64k());
}
}
Expand All @@ -49,42 +49,48 @@ public String getThwId() {
}

public void setThwId(String value) {
this.id = DataValidator.validateStr64kXml(value);
this.id = value;
}

public List<String> getPositions() {
return positions;
}

public void addPosition(String position) {
if (positions == null) {
positions = new ArrayList<String> ();
}
positions.add(position);
}

public boolean isLast() {
return isLast;
}

public void setIsLast(boolean value) {
isLast = value;
}

@Override
public int hashCode() {
if(this.getThwId() != null){
if (this.getThwId() != null) {
return this.getThwId().hashCode();
}

return 0;
}

// @Override
// public boolean equals(Object obj) {
// return obj != null &&
// this == obj ||
// (obj instancTargetHardwareDefinitionBuildernDao &&
// equaTargetHardwareDefinitionBuildernDao)obj));
// }
//
// public boolean equTargetHardwareDefinitionBuildernDao obj){
// return obj != null &&
// this == obj ||
// (this.getThwId().equals(obj.getThwId()) &&
// this.getPositions().equals(obj.getPositions()));
// }

@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof TargetHardwareDefinitionDao))
return false;

TargetHardwareDefinitionDao other = (TargetHardwareDefinitionDao) obj;

boolean ret = (this.getThwId() == null ? other.getThwId() == null : this.getThwId().equals(other.getThwId()));
if (!ret) return false;

ret = this.getPositions() == null ? other.getPositions() == null : getPositions().equals (other.getPositions());
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,71 +35,6 @@ public void setup() {
second.getPositions().add("two");
}

@Test
public void getId() {
com.arinc.arinc838.ThwDefinition jaxbDef = new com.arinc.arinc838.ThwDefinition();
jaxbDef.setThwId("test");
TargetHardwareDefinitionBuilder xmlDef = new TargetHardwareDefinitionBuilder(
jaxbDef);

assertEquals(xmlDef.getThwId(), jaxbDef.getThwId());
}

@Test
public void setId() {
com.arinc.arinc838.ThwDefinition jaxbDef = new com.arinc.arinc838.ThwDefinition();
jaxbDef.setThwId("test");
TargetHardwareDefinitionBuilder xmlDef = new TargetHardwareDefinitionBuilder(
jaxbDef);

String value = "new test";

xmlDef.setThwId(value);

assertEquals(xmlDef.getThwId(), value);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testSetIdInvalid()
{
// invalid str64k
first.setThwId("1 > 2");
}

@Test
public void getPositions() {
com.arinc.arinc838.ThwDefinition jaxbDef = new ThwDefinition();
jaxbDef.setThwId("id");
jaxbDef.getThwPosition().add("one");
jaxbDef.getThwPosition().add("two");
TargetHardwareDefinitionBuilder xmlDef = new TargetHardwareDefinitionBuilder(
jaxbDef);

assertEquals(xmlDef.getPositions().size(), 2);
assertEquals(xmlDef.getPositions().get(0), "one");
assertEquals(xmlDef.getPositions().get(1), "two");
}

@Test
public void getPositionsWithNoPositions() {
com.arinc.arinc838.ThwDefinition jaxbDef = new ThwDefinition();
jaxbDef.setThwId("id");
TargetHardwareDefinitionBuilder xmlDef = new TargetHardwareDefinitionBuilder(
jaxbDef);

assertEquals(xmlDef.getPositions().size(), 0);
}


@Test
public void equalsFailsIfPositionsAreNotSame() {
second.getPositions().clear();
second.getPositions().add("two");
second.getPositions().add("one");

assertNotEquals(first, second);
}

@Test
public void testBuildReturnsProperJaxbObject() {
ThwDefinition def = first.buildXml();
Expand All @@ -110,19 +45,4 @@ public void testBuildReturnsProperJaxbObject() {
assertEquals(def.getThwPosition().get(i), first.getPositions().get(i));
}
}

@Test
public void testHashCode(){
assertEquals(first.hashCode(), first.getThwId().hashCode());
}

@Test
public void testHashCodeWithNoId(){
assertEquals(new TargetHardwareDefinitionBuilder().hashCode(), 0);
}

@Test
public void testEquals(){
assertEquals(first, second);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2012 Chris Ellison, Mike Deats, Liron Yahdav, Ryan Neal,
* Brandon Sutherlin, Scott Griffin
*
* This software is released under the MIT license
* (http://www.opensource.org/licenses/mit-license.php)
*
* Created on Feb 7, 2012
*/
package edu.cmu.sv.arinc838.dao;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;

import com.arinc.arinc838.ThwDefinition;

public class TargetHardwareDefinitionDaoTest {
private TargetHardwareDefinitionDao first;
private TargetHardwareDefinitionDao second;

@Test
public void testIdAccessors () {
ThwDefinition jaxbDef = new com.arinc.arinc838.ThwDefinition();
jaxbDef.setThwId("test");

TargetHardwareDefinitionDao thDao = new TargetHardwareDefinitionDao();
thDao.setThwId(jaxbDef.getThwId());

assertEquals(thDao.getThwId(), jaxbDef.getThwId());

thDao.setThwId("Not that value");
assertNotEquals(thDao, jaxbDef.getThwId());
}

@Test
public void testPositionsAccessors () {
ThwDefinition jaxbDef = new ThwDefinition();

jaxbDef.setThwId("id");
jaxbDef.getThwPosition().add("one");
jaxbDef.getThwPosition().add("two");

TargetHardwareDefinitionDao thDao = new TargetHardwareDefinitionDao();

assertEquals(thDao.getPositions().size(), 0);

for (String pos : jaxbDef.getThwPosition()) {
thDao.addPosition(pos);
}

assertEquals(thDao.getPositions().size(), jaxbDef.getThwPosition().size());
assertEquals(thDao.getPositions().get(0), "one");
assertEquals(thDao.getPositions().get(1), "two");
}

@Test
public void equalsFailsIfPositionsAreNotSame() {
first = new TargetHardwareDefinitionDao();
first.setThwId("first");
first.getPositions().add("one");
first.getPositions().add("two");

second = new TargetHardwareDefinitionDao();
second.setThwId(first.getThwId());
second.addPosition(first.getPositions().get(1));
second.addPosition(first.getPositions().get(0));

assertNotEquals(first, second);
}

@Test
public void testHashCode() {
assertEquals(first.hashCode(), first.getThwId().hashCode());
}

@Test
public void testHashCodeWithNoId() {
assertEquals(new TargetHardwareDefinitionDao().hashCode(), 0);
}

@Test
public void testEquals() {
first = new TargetHardwareDefinitionDao();
first.setThwId("first");
first.getPositions().add("one");
first.getPositions().add("two");

second = new TargetHardwareDefinitionDao();
second.setThwId(first.getThwId());
second.addPosition(first.getPositions().get(0));
second.addPosition(first.getPositions().get(1));

assertTrue (first.equals(second));
assertTrue (second.equals(first));
assertFalse (first.equals(null));
assertFalse (first.equals(new Object()));
}
}

0 comments on commit 3a7f629

Please sign in to comment.