TestItem.java
Go to the documentation of this file.
00001 package edu.wpi.rail.jinteractiveworld.ros.msgs.interactiveworldmsgs;
00002 
00003 import static org.junit.Assert.*;
00004 
00005 import javax.json.Json;
00006 import javax.json.JsonObject;
00007 
00008 import org.junit.Before;
00009 import org.junit.Test;
00010 
00011 import edu.wpi.rail.jrosbridge.messages.Message;
00012 
00013 public class TestItem {
00014 
00015         private Item empty, o1;
00016 
00017         @Before
00018         public void setUp() {
00019                 empty = new Item();
00020                 o1 = new Item("name", 1.5, 3.0);
00021         }
00022 
00023         @Test
00024         public void testConstructor() {
00025                 assertEquals("", empty.getName());
00026                 assertEquals(0.0, empty.getWidth(), 0);
00027                 assertEquals(0.0, empty.getHeight(), 0);
00028 
00029                 assertEquals("{\"name\":\"\",\"width\":0.0,\"height\":0.0}",
00030                                 empty.toString());
00031 
00032                 assertEquals(3, empty.toJsonObject().size());
00033                 assertEquals("", empty.toJsonObject().getString(Item.FIELD_NAME));
00034                 assertEquals(0.0, empty.toJsonObject().getJsonNumber(Item.FIELD_WIDTH)
00035                                 .doubleValue(), 0);
00036                 assertEquals(0.0, empty.toJsonObject().getJsonNumber(Item.FIELD_HEIGHT)
00037                                 .doubleValue(), 0);
00038 
00039                 assertEquals(Item.TYPE, empty.getMessageType());
00040         }
00041 
00042         @Test
00043         public void testStringDoubleAndDoubleConstructor() {
00044                 assertEquals("name", o1.getName());
00045                 assertEquals(1.5, o1.getWidth(), 0);
00046                 assertEquals(3.0, o1.getHeight(), 0);
00047 
00048                 assertEquals("{\"name\":\"name\",\"width\":1.5,\"height\":3.0}",
00049                                 o1.toString());
00050 
00051                 assertEquals(3, o1.toJsonObject().size());
00052                 assertEquals("name", o1.toJsonObject().getString(Item.FIELD_NAME));
00053                 assertEquals(1.5, o1.toJsonObject().getJsonNumber(Item.FIELD_WIDTH)
00054                                 .doubleValue(), 0);
00055                 assertEquals(3.0, o1.toJsonObject().getJsonNumber(Item.FIELD_HEIGHT)
00056                                 .doubleValue(), 0);
00057 
00058                 assertEquals(Item.TYPE, o1.getMessageType());
00059         }
00060 
00061         @Test
00062         public void testSetMessageType() {
00063                 empty.setMessageType("test");
00064                 assertEquals("test", empty.getMessageType());
00065         }
00066 
00067         @Test
00068         public void testHashCode() {
00069                 assertEquals(empty.toString().hashCode(), empty.hashCode());
00070                 assertEquals(o1.toString().hashCode(), o1.hashCode());
00071         }
00072 
00073         @Test
00074         public void testEquals() {
00075                 assertFalse(empty.equals(o1));
00076                 assertFalse(o1.equals(empty));
00077 
00078                 assertTrue(o1.equals(o1));
00079                 assertTrue(empty.equals(empty));
00080         }
00081 
00082         @Test
00083         public void testEqualsWrongObject() {
00084                 assertFalse(o1.equals(new String(o1.toString())));
00085         }
00086 
00087         @Test
00088         public void testClone() {
00089                 Item clone = o1.clone();
00090                 assertEquals(o1.toString(), clone.toString());
00091                 assertEquals(o1.toJsonObject(), clone.toJsonObject());
00092                 assertEquals(o1.getMessageType(), clone.getMessageType());
00093                 assertEquals(o1.getName(), clone.getName());
00094                 assertEquals(o1.getWidth(), clone.getWidth(), 0);
00095                 assertEquals(o1.getHeight(), clone.getHeight(), 0);
00096                 assertNotSame(o1, clone);
00097                 assertNotSame(o1.toString(), clone.toString());
00098                 assertNotSame(o1.toJsonObject(), clone.toJsonObject());
00099         }
00100 
00101         @Test
00102         public void testFromJsonString() {
00103                 Item p = Item.fromJsonString(o1.toString());
00104                 assertEquals(o1.toString(), p.toString());
00105                 assertEquals(o1.toJsonObject(), p.toJsonObject());
00106                 assertEquals(o1.getMessageType(), p.getMessageType());
00107                 assertEquals(o1.getName(), p.getName());
00108                 assertEquals(o1.getWidth(), p.getWidth(), 0);
00109                 assertEquals(o1.getHeight(), p.getHeight(), 0);
00110                 assertNotSame(o1, p);
00111                 assertNotSame(o1.toString(), p.toString());
00112                 assertNotSame(o1.toJsonObject(), p.toJsonObject());
00113         }
00114 
00115         @Test
00116         public void testFromMessage() {
00117                 Message m = new Message(o1.toString());
00118                 Item p = Item.fromMessage(m);
00119                 assertEquals(o1.toString(), p.toString());
00120                 assertEquals(o1.toJsonObject(), p.toJsonObject());
00121                 assertEquals(o1.getMessageType(), p.getMessageType());
00122                 assertEquals(o1.getName(), p.getName());
00123                 assertEquals(o1.getWidth(), p.getWidth(), 0);
00124                 assertEquals(o1.getHeight(), p.getHeight(), 0);
00125                 assertNotSame(o1, p);
00126                 assertNotSame(o1.toString(), p.toString());
00127                 assertNotSame(o1.toJsonObject(), p.toJsonObject());
00128         }
00129 
00130         @Test
00131         public void testFromJsonObject() {
00132                 JsonObject jsonObject = Json.createObjectBuilder()
00133                                 .add(Item.FIELD_NAME, o1.getName())
00134                                 .add(Item.FIELD_WIDTH, o1.getWidth())
00135                                 .add(Item.FIELD_HEIGHT, o1.getHeight()).build();
00136                 Item p = Item.fromJsonObject(jsonObject);
00137                 assertEquals(o1.toString(), p.toString());
00138                 assertEquals(o1.toJsonObject(), p.toJsonObject());
00139                 assertEquals(o1.getMessageType(), p.getMessageType());
00140                 assertEquals(o1.getName(), p.getName());
00141                 assertEquals(o1.getWidth(), p.getWidth(), 0);
00142                 assertEquals(o1.getHeight(), p.getHeight(), 0);
00143                 assertNotSame(o1, p);
00144                 assertNotSame(o1.toString(), p.toString());
00145                 assertNotSame(o1.toJsonObject(), p.toJsonObject());
00146         }
00147 
00148         @Test
00149         public void testFromJsonObjectNoName() {
00150                 JsonObject jsonObject = Json.createObjectBuilder()
00151                                 .add(Item.FIELD_WIDTH, o1.getWidth())
00152                                 .add(Item.FIELD_HEIGHT, o1.getHeight()).build();
00153                 Item p = Item.fromJsonObject(jsonObject);
00154                 assertEquals("", p.getName());
00155                 assertEquals(o1.getWidth(), p.getWidth(), 0);
00156                 assertEquals(o1.getHeight(), p.getHeight(), 0);
00157         }
00158 
00159         @Test
00160         public void testFromJsonObjectNoWidth() {
00161                 JsonObject jsonObject = Json.createObjectBuilder()
00162                                 .add(Item.FIELD_NAME, o1.getName())
00163                                 .add(Item.FIELD_HEIGHT, o1.getHeight()).build();
00164                 Item p = Item.fromJsonObject(jsonObject);
00165                 assertEquals(o1.getName(), p.getName());
00166                 assertEquals(0.0, p.getWidth(), 0);
00167                 assertEquals(o1.getHeight(), p.getHeight(), 0);
00168         }
00169 
00170         @Test
00171         public void testFromJsonObjectNoHeight() {
00172                 JsonObject jsonObject = Json.createObjectBuilder()
00173                                 .add(Item.FIELD_NAME, o1.getName())
00174                                 .add(Item.FIELD_WIDTH, o1.getWidth()).build();
00175                 Item p = Item.fromJsonObject(jsonObject);
00176                 assertEquals(o1.getName(), p.getName());
00177                 assertEquals(o1.getWidth(), p.getWidth(), 0);
00178                 assertEquals(0.0, p.getHeight(), 0);
00179         }
00180 }


jinteractiveworld
Author(s): Russell Toris
autogenerated on Sun Dec 14 2014 11:27:03