/** * Validate a request. * * @param message number (zero-based) * @param first message sent * @param previous previous request, or {@code null} if this is the first request ever sent * @param request request message */ private void validateRequest( final int idx, final Node first, final Node previous, final Node request) { try { assertValidXML(request); assertSingleBodyElement(request); assertNoComments(request); assertNoProcessingInstructions(request); assertRequestIDSequential(request, previous); if (previous == null) { validateRequestHeaders(idx, request, previous); assertSessionCreationRequestID(request); assertSessionCreationRequestIDRange(request); validateSessionCreationAck(idx, request); validateSessionCreationSID(idx, request); validateSessionCreationHold(idx, request); } else { validateRequestHeaders(idx, request, previous); validateSubsequentRequestSID(idx, request); validateSubsequestRequestAck(idx, first, request); validateSubsequentPause(idx, request, previous); } } catch (AssertionError err) { LOG.info( "Assertion failed for request #" + idx + ": " + err.getMessage() + "\n" + request.getBody().toXML()); throw (err); } }
@Test public void unmetExpectationUsingHamcrestMatcherAsPredicate() { try { expect("foo").to(match(startsWith("d"))); } catch (AssertionError expected) { assertEquals( "[foo] did not satisfy [" + StringDescription.toString(startsWith("d")) + ']', expected.getMessage()); return; } fail(); }
@org.junit.Test public void parseTest() throws IOException { if (htmlExpectFile != null) assertNotNull("Missing expect file: " + htmlExpectFile.getAbsolutePath(), htmlFile); Object retVal = null; try { CreoleParser parser = new CreoleParser(); parser.setPrivileges(EnumSet.allOf(JCreolePrivilege.class)); /* Replace the statement above with something like this to test * privileges: parser.setPrivileges(EnumSet.of( JCreolePrivilege.ENUMFORMATS, JCreolePrivilege.TOC, JCreolePrivilege.RAWHTML, JCreolePrivilege.STYLESHEET, JCreolePrivilege.JCXBLOCK, JCreolePrivilege.JCXSPAN, JCreolePrivilege.STYLER )); */ parser.setInterWikiMapper( new InterWikiMapper() { // Use wiki name of "nil" to force lookup failure for path. // Use wiki page of "nil" to force lookup failure for label. public String toPath(String wikiName, String wikiPage) { if (wikiName != null && wikiName.equals("nil")) return null; return "{WIKI-LINK to: " + wikiName + '|' + wikiPage + '}'; } public String toLabel(String wikiName, String wikiPage) { if (wikiPage == null) throw new RuntimeException("Null page name sent to InterWikiMapper"); if (wikiPage.equals("nil")) return null; return "{LABEL for: " + wikiName + '|' + wikiPage + '}'; } }); retVal = parser.parse(CreoleScanner.newCreoleScanner(creoleFile, false, null)); } catch (Exception e) { if (!shouldSucceed) return; // A ok. No output file to write. AssertionError ae = new AssertionError("Failed to parse '" + creoleFile + "'"); ae.initCause(e); throw ae; } FileUtils.writeStringToFile( htmlFile, ((retVal == null) ? "" : (((WashedSymbol) retVal).toString())), "UTF-8"); if (!shouldSucceed) fail("Should have failed, but generated '" + htmlFile + "'"); assertTrue( "From '" + creoleFile + "': '" + htmlFile + "' != '" + htmlExpectFile + "'", FileUtils.contentEquals(htmlExpectFile, htmlFile)); htmlFile.delete(); }
@Test public void testReadAggregate_WrongIdentifier() { fixture.registerAggregateFactory(mockAggregateFactory); fixture.registerAnnotatedCommandHandler( new MyCommandHandler(fixture.getRepository(), fixture.getEventBus())); TestExecutor exec = fixture.given(new MyEvent("AggregateId", 1)); try { exec.when(new TestCommand("OtherIdentifier")); fail("Expected an AssertionError"); } catch (AssertionError e) { assertTrue( "Wrong message. Was: " + e.getMessage(), e.getMessage().contains("OtherIdentifier")); assertTrue("Wrong message. Was: " + e.getMessage(), e.getMessage().contains("AggregateId")); } }
@Test public void testMultipleErrors3() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); ts.onError(new TestException()); ts.onError(new TestException()); try { ts.assertError(new TestException()); } catch (AssertionError ex) { if (!(ex.getCause() instanceof CompositeException)) { fail("Multiple Error present but the reported error doesn't have a composite cause!"); } // expected return; } fail("Multiple Error present but no assertion error!"); }
@Test public void testFixtureDetectsStateChangeOutsideOfHandler_AggregateDeleted() { TestExecutor exec = fixture .registerAnnotatedCommandHandler( new MyCommandHandler(fixture.getRepository(), fixture.getEventBus())) .given(new MyEvent("aggregateId", 5)); try { exec.when(new DeleteCommand("aggregateId", true)); fail("Fixture should have failed"); } catch (AssertionError error) { assertTrue( "Wrong message: " + error.getMessage(), error.getMessage().contains("considered deleted")); } }
@Test public void testNoTerminalEventBut2Errors() { TestSubscriber<Integer> ts = TestSubscriber.create(); ts.onError(new TestException()); ts.onError(new TestException()); try { ts.assertNoTerminalEvent(); fail("Failed to report there were terminal event(s)!"); } catch (AssertionError ex) { // expected if (!(ex.getCause() instanceof CompositeException)) { fail("Did not report a composite exception cause: " + ex.getCause()); } } }
@Test public void testFixtureDetectsStateChangeOutsideOfHandler_NullValue() { List<?> givenEvents = Arrays.asList( new MyEvent("aggregateId", 1), new MyEvent("aggregateId", 2), new MyEvent("aggregateId", 3)); try { fixture .registerAnnotatedCommandHandler( new MyCommandHandler(fixture.getRepository(), fixture.getEventBus())) .given(givenEvents) .when(new IllegalStateChangeCommand("aggregateId", null)); fail("Expected AssertionError"); } catch (AssertionError e) { assertTrue("Wrong message: " + e.getMessage(), e.getMessage().contains(".lastNumber\"")); assertTrue("Wrong message: " + e.getMessage(), e.getMessage().contains("<null>")); assertTrue("Wrong message: " + e.getMessage(), e.getMessage().contains("<4>")); } }
public void geoSpacialTest(final ODataPubFormat format, final String contentType, final String prefer, final int id) { try { final ODataEntity entity = ODataFactory.newEntity("Microsoft.Test.OData.Services.AstoriaDefaultService.AllSpatialTypes"); entity.addProperty(ODataFactory.newPrimitiveProperty("Id", new ODataPrimitiveValue.Builder().setText(String.valueOf(id)).setType(EdmSimpleType.Int32).build())); final Point point1 = new Point(Geospatial.Dimension.GEOGRAPHY); point1.setX(6.2); point1.setY(1.1); final Point point2 = new Point(Geospatial.Dimension.GEOGRAPHY); point2.setX(33.33); point2.setY(-2.5); // create a point entity.addProperty(ODataFactory.newPrimitiveProperty("GeogPoint", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyPoint). setValue(point1).build())); // create multiple point final List<Point> points = new ArrayList<Point>(); points.add(point1); points.add(point2); final MultiPoint multipoint = new MultiPoint(Geospatial.Dimension.GEOGRAPHY, points); entity.addProperty(ODataFactory.newPrimitiveProperty("GeogMultiPoint", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyMultiPoint). setValue(multipoint).build())); // create a line final List<Point> linePoints = new ArrayList<Point>(); linePoints.add(point1); linePoints.add(point2); final LineString lineString = new LineString(Geospatial.Dimension.GEOGRAPHY, linePoints); entity.addProperty(ODataFactory.newPrimitiveProperty("GeogLine", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyLineString). setValue(lineString).build())); // create a polygon linePoints.set(1, point2); linePoints.add(point2); linePoints.add(point1); final Polygon polygon = new Polygon(Geospatial.Dimension.GEOGRAPHY, linePoints, linePoints); entity.addProperty(ODataFactory.newPrimitiveProperty("GeogPolygon", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyPolygon). setValue(polygon).build())); // create a multi line string final List<LineString> multipleLines = new ArrayList<LineString>(); multipleLines.add(lineString); multipleLines.add(lineString); final MultiLineString multiLine = new MultiLineString(Geospatial.Dimension.GEOGRAPHY, multipleLines); entity.addProperty(ODataFactory.newPrimitiveProperty("GeogMultiLine", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyMultiLineString). setValue(multiLine).build())); // create a multi polygon final List<Polygon> polygons = new ArrayList<Polygon>(); polygons.add(polygon); polygons.add(polygon); final MultiPolygon multiPolygon = new MultiPolygon(Geospatial.Dimension.GEOGRAPHY, polygons); entity.addProperty(ODataFactory.newPrimitiveProperty("GeogMultiPolygon", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyMultiPolygon). setValue(multiPolygon).build())); // create acolletion of various shapes final List<Geospatial> geospatialCollection = new ArrayList<Geospatial>(); geospatialCollection.add(point1); geospatialCollection.add(lineString); geospatialCollection.add(polygon); final GeospatialCollection collection = new GeospatialCollection(Geospatial.Dimension.GEOGRAPHY, geospatialCollection); entity.addProperty(ODataFactory.newPrimitiveProperty("GeogCollection", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyCollection). setValue(collection).build())); // with geometry test final Point goemPoint1 = new Point(Geospatial.Dimension.GEOMETRY); goemPoint1.setX(6.2); goemPoint1.setY(1.1); final Point goemPoint2 = new Point(Geospatial.Dimension.GEOMETRY); goemPoint2.setX(33.33); goemPoint2.setY(-2.5); // create a point entity.addProperty(ODataFactory.newPrimitiveProperty("GeomPoint", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryPoint). setValue(goemPoint2).build())); // create multiple point final List<Point> geomPoints = new ArrayList<Point>(); geomPoints.add(point1); geomPoints.add(point2); final MultiPoint geomMultipoint = new MultiPoint(Geospatial.Dimension.GEOMETRY, geomPoints); entity.addProperty(ODataFactory.newPrimitiveProperty("GeomMultiPoint", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryMultiPoint). setValue(geomMultipoint).build())); // create a line final List<Point> geomLinePoints = new ArrayList<Point>(); geomLinePoints.add(goemPoint1); geomLinePoints.add(goemPoint2); final LineString geomLineString = new LineString(Geospatial.Dimension.GEOMETRY, geomLinePoints); entity.addProperty(ODataFactory.newPrimitiveProperty("GeomLine", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryLineString). setValue(geomLineString).build())); // create a polygon geomLinePoints.set(1, goemPoint2); geomLinePoints.add(goemPoint2); geomLinePoints.add(goemPoint1); final Polygon geomPolygon = new Polygon(Geospatial.Dimension.GEOMETRY, geomLinePoints, geomLinePoints); entity.addProperty(ODataFactory.newPrimitiveProperty("GeomPolygon", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryPolygon). setValue(geomPolygon).build())); // create a multi line string final List<LineString> geomMultipleLines = new ArrayList<LineString>(); geomMultipleLines.add(geomLineString); geomMultipleLines.add(geomLineString); final MultiLineString geomMultiLine = new MultiLineString(Geospatial.Dimension.GEOMETRY, geomMultipleLines); entity.addProperty(ODataFactory.newPrimitiveProperty("GeomMultiLine", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryMultiLineString). setValue(geomMultiLine).build())); // create a multi polygon final List<Polygon> geomPolygons = new ArrayList<Polygon>(); geomPolygons.add(geomPolygon); geomPolygons.add(geomPolygon); final MultiPolygon geomMultiPolygon = new MultiPolygon(Geospatial.Dimension.GEOMETRY, geomPolygons); entity.addProperty(ODataFactory.newPrimitiveProperty("GeomMultiPolygon", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyMultiPolygon). setValue(geomMultiPolygon).build())); // create a collection of various shapes final List<Geospatial> geomspatialCollection = new ArrayList<Geospatial>(); geomspatialCollection.add(goemPoint1); geomspatialCollection.add(geomLineString); final GeospatialCollection geomCollection = new GeospatialCollection(Geospatial.Dimension.GEOMETRY, geomspatialCollection); entity.addProperty(ODataFactory.newPrimitiveProperty("GeomCollection", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeometryCollection). setValue(geomCollection).build())); // create request final ODataEntityCreateRequest createReq = ODataCUDRequestFactory. getEntityCreateRequest(new ODataURIBuilder(testDefaultServiceRootURL). appendEntityTypeSegment("AllGeoTypesSet").build(), entity); createReq.setFormat(format); createReq.setContentType(contentType); createReq.setPrefer(prefer); final ODataEntityCreateResponse createRes = createReq.execute(); final ODataEntity entityAfterCreate = createRes.getBody(); final ODataProperty geogCollection = entityAfterCreate.getProperty("GeogCollection"); if (format.equals(ODataPubFormat.JSON) || format.equals(ODataPubFormat.JSON_NO_METADATA)) { assertTrue(geogCollection.hasComplexValue()); } else { assertEquals(EdmSimpleType.GeographyCollection.toString(), geogCollection.getPrimitiveValue(). getTypeName()); final ODataProperty geometryCollection = entityAfterCreate.getProperty("GeomCollection"); assertEquals(EdmSimpleType.GeographyCollection.toString(), geogCollection.getPrimitiveValue().getTypeName()); int count = 0; for (Geospatial g : geogCollection.getPrimitiveValue().<GeospatialCollection>toCastValue()) { assertNotNull(g); count++; } assertEquals(3, count); count = 0; for (Geospatial g : geometryCollection.getPrimitiveValue().<GeospatialCollection>toCastValue()) { assertNotNull(g); count++; } assertEquals(2, count); } // update geog points final Point updatePoint1 = new Point(Geospatial.Dimension.GEOGRAPHY); updatePoint1.setX(21.2); updatePoint1.setY(31.1); final Point updatePoint2 = new Point(Geospatial.Dimension.GEOGRAPHY); updatePoint2.setX(99.99); updatePoint2.setY(-3.24); ODataProperty property = entityAfterCreate.getProperty("GeogPoint"); entityAfterCreate.removeProperty(property); entityAfterCreate.addProperty(ODataFactory.newPrimitiveProperty("GeogPoint", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyPoint). setValue(updatePoint1).build())); updateGeog(format, contentType, prefer, entityAfterCreate, UpdateType.REPLACE, entityAfterCreate.getETag()); // update geography line final List<Point> updateLinePoints = new ArrayList<Point>(); updateLinePoints.add(updatePoint1); updateLinePoints.add(updatePoint2); final LineString updateLineString = new LineString(Geospatial.Dimension.GEOGRAPHY, updateLinePoints); ODataProperty lineProperty = entityAfterCreate.getProperty("GeogLine"); entityAfterCreate.removeProperty(lineProperty); entityAfterCreate.addProperty(ODataFactory.newPrimitiveProperty("GeogLine", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyLineString). setValue(updateLineString).build())); //updateGeog(format,contentType, prefer, entityAfterCreate, UpdateType.REPLACE,entityAfterCreate.getETag()); // update a geography polygon updateLinePoints.set(1, updatePoint2); updateLinePoints.add(updatePoint2); updateLinePoints.add(updatePoint1); final Polygon updatePolygon = new Polygon(Geospatial.Dimension.GEOGRAPHY, updateLinePoints, updateLinePoints); ODataProperty polygonProperty = entityAfterCreate.getProperty("GeogPolygon"); entityAfterCreate.removeProperty(polygonProperty); entityAfterCreate.addProperty(ODataFactory.newPrimitiveProperty("GeogPolygon", new ODataGeospatialValue.Builder().setType(EdmSimpleType.GeographyPolygon). setValue(updatePolygon).build())); //updateGeog(format,contentType, prefer, entityAfterCreate, UpdateType.REPLACE,entityAfterCreate.getETag()); // delete the entity ODataURIBuilder deleteUriBuilder = new ODataURIBuilder(testDefaultServiceRootURL). appendEntityTypeSegment("AllGeoTypesSet(" + id + ")"); ODataDeleteRequest deleteReq = ODataCUDRequestFactory.getDeleteRequest(deleteUriBuilder.build()); deleteReq.setFormat(format); deleteReq.setContentType(contentType); assertEquals(204, deleteReq.execute().getStatusCode()); } catch (Exception e) { LOG.error("", e); if (format.equals(ODataPubFormat.JSON) || format.equals(ODataPubFormat.JSON_NO_METADATA)) { assertTrue(true); } else { fail(e.getMessage()); } } catch (AssertionError e) { LOG.error("", e); fail(e.getMessage()); } }