/** tests cat. This one has IDREFs. */ public void testCat() throws Exception { Cat cat = new Cat(); Circle face = new Circle(); face.setRadius(20); cat.setFace(face); Triangle ear = new Triangle(); ear.setBase(5); ear.setHeight(10); ear.setId("earId"); cat.setEars(Arrays.asList(ear, ear)); // The eyes are the same as the ears, but so it needs to be for this test. cat.setEyes(new Triangle[] {ear, ear}); Line noseLine = new Line(); noseLine.setId("noseId"); Line mouthLine = new Line(); mouthLine.setId("mouthLine"); cat.setNose(noseLine); cat.setMouth(mouthLine); cat.setWhiskers(Arrays.asList(noseLine, mouthLine)); JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(); ObjectMapper catMapper = provider.locateMapper(Cat.class, MediaType.APPLICATION_JSON_TYPE); ObjectMapper clientMapper = new ObjectMapper(); ByteArrayOutputStream out = new ByteArrayOutputStream(); catMapper.writeValue(out, cat); shapes.json.animals.Cat clientCat = clientMapper.readValue( new ByteArrayInputStream(out.toByteArray()), shapes.json.animals.Cat.class); shapes.json.Circle clientFace = clientCat.getFace(); assertEquals(20, clientFace.getRadius()); assertEquals(2, clientCat.getEars().size()); shapes.json.Triangle[] clientEars = (shapes.json.Triangle[]) clientCat.getEars().toArray(new shapes.json.Triangle[2]); assertNotSame( "referential integrity should NOT have been preserved since Jackson doesn't support it yet", clientEars[0], clientEars[1]); assertEquals(5, clientEars[0].getBase()); assertEquals(10, clientEars[0].getHeight()); assertEquals("earId", clientEars[0].getId()); assertEquals(5, clientEars[1].getBase()); assertEquals(10, clientEars[1].getHeight()); assertEquals("earId", clientEars[1].getId()); shapes.json.Triangle[] clientEyes = clientCat.getEyes(); assertEquals(2, clientEyes.length); assertNotSame(clientEyes[0], clientEyes[1]); assertEquals(5, clientEyes[0].getBase()); assertEquals(10, clientEyes[0].getHeight()); assertEquals("earId", clientEyes[0].getId()); assertEquals(5, clientEyes[1].getBase()); assertEquals(10, clientEyes[1].getHeight()); assertEquals("earId", clientEyes[1].getId()); assertFalse( "The ears should NOT be the same object as one of the eyes since Jackson doesn't support referential integrity.", clientEars[0] == clientEyes[0] || clientEars[0] == clientEyes[1]); shapes.json.Line clientNose = clientCat.getNose(); assertEquals("noseId", clientNose.getId()); shapes.json.Line clientMouth = clientCat.getMouth(); assertEquals("mouthLine", clientMouth.getId()); assertFalse( "The nose line should NOT also be one of the whiskers since Jackson doesn't support referential integrity.", clientCat.getWhiskers().contains(clientNose)); assertFalse( "The mouth line should NOT also be one of the whiskers since Jackson doesn't support referential integrity.", clientCat.getWhiskers().contains(clientMouth)); out = new ByteArrayOutputStream(); clientMapper.writeValue(out, clientCat); cat = catMapper.readValue(new ByteArrayInputStream(out.toByteArray()), Cat.class); face = cat.getFace(); assertEquals(20, face.getRadius()); assertEquals(2, cat.getEars().size()); Triangle[] ears = cat.getEars().toArray(new Triangle[2]); assertNotSame( "referential integrity should NOT have been preserved since Jackson doesn't support referential integrity.", ears[0], ears[1]); assertEquals(5, ears[0].getBase()); assertEquals(10, ears[0].getHeight()); assertEquals("earId", ears[0].getId()); Triangle[] eyes = cat.getEyes(); assertEquals(2, eyes.length); assertNotSame(eyes[0], eyes[1]); assertEquals(5, eyes[0].getBase()); assertEquals(10, eyes[0].getHeight()); assertEquals("earId", eyes[0].getId()); assertEquals(5, eyes[1].getBase()); assertEquals(10, eyes[1].getHeight()); assertEquals("earId", eyes[1].getId()); assertFalse( "The ears should NOT be the same object as one of the eyes since Jackson doesn't support referential integrity.", ears[0] == eyes[0] || ears[0] == eyes[1]); Line nose = cat.getNose(); assertEquals("noseId", nose.getId()); Line mouth = cat.getMouth(); assertEquals("mouthLine", mouth.getId()); assertFalse( "The nose line should also be one of the whiskers since Jackson doesn't support referential integrity.", cat.getWhiskers().contains(nose)); assertFalse( "The mouth line should also be one of the whiskers since Jackson doesn't support referential integrity.", cat.getWhiskers().contains(mouth)); }