Search in sources :

Example 11 with XMLStartElement

use of io.georocket.util.XMLStartElement in project georocket by georocket.

the class MultiMergerTest method xmlSimple.

/**
 * Test if simple XML chunks can be merged
 * @param context the Vert.x test context
 */
@Test
public void xmlSimple(TestContext context) {
    Buffer chunk1 = Buffer.buffer(XMLHEADER + "<root><test chunk=\"1\"></test></root>");
    Buffer chunk2 = Buffer.buffer(XMLHEADER + "<root><test chunk=\"2\"></test></root>");
    XMLChunkMeta cm = new XMLChunkMeta(Arrays.asList(new XMLStartElement("root")), XMLHEADER.length() + 6, chunk1.length() - 7);
    doMerge(context, Observable.just(chunk1, chunk2), Observable.just(cm, cm), XMLHEADER + "<root><test chunk=\"1\"></test><test chunk=\"2\"></test></root>");
}
Also used : Buffer(io.vertx.core.buffer.Buffer) XMLStartElement(io.georocket.util.XMLStartElement) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) Test(org.junit.Test)

Example 12 with XMLStartElement

use of io.georocket.util.XMLStartElement in project georocket by georocket.

the class MultiMergerTest method mixedInit.

/**
 * Test if the merger fails if chunks with a different type should be merged
 * @param context the Vert.x test context
 */
@Test
public void mixedInit(TestContext context) {
    String strChunk1 = "{\"type\":\"Feature\"}";
    String strChunk2 = XMLHEADER + "<root><test chunk=\"2\"></test></root>";
    Buffer chunk1 = Buffer.buffer(strChunk1);
    Buffer chunk2 = Buffer.buffer(strChunk2);
    GeoJsonChunkMeta cm1 = new GeoJsonChunkMeta("Feature", "features", 0, chunk1.length());
    XMLChunkMeta cm2 = new XMLChunkMeta(Arrays.asList(new XMLStartElement("root")), XMLHEADER.length() + 6, chunk2.length() - 7);
    MultiMerger m = new MultiMerger();
    Async async = context.async();
    m.init(cm1).flatMap(v -> m.init(cm2)).subscribe(v -> {
        context.fail();
    }, err -> {
        context.assertTrue(err instanceof IllegalStateException);
        async.complete();
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) GeoJsonChunkMeta(io.georocket.storage.GeoJsonChunkMeta) RunWith(org.junit.runner.RunWith) XMLStartElement(io.georocket.util.XMLStartElement) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) ChunkMeta(io.georocket.storage.ChunkMeta) Observable(rx.Observable) Rule(org.junit.Rule) DelegateChunkReadStream(io.georocket.util.io.DelegateChunkReadStream) Pair(org.apache.commons.lang3.tuple.Pair) Buffer(io.vertx.core.buffer.Buffer) BufferWriteStream(io.georocket.util.io.BufferWriteStream) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) ChunkReadStream(io.georocket.storage.ChunkReadStream) XMLStartElement(io.georocket.util.XMLStartElement) Async(io.vertx.ext.unit.Async) GeoJsonChunkMeta(io.georocket.storage.GeoJsonChunkMeta) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) Test(org.junit.Test)

Example 13 with XMLStartElement

use of io.georocket.util.XMLStartElement in project georocket by georocket.

the class AllSameStrategyTest method canMerge.

/**
 * Test if canMerge works correctly
 * @param context the test context
 */
@Test
public void canMerge(TestContext context) {
    XMLChunkMeta cm2 = new XMLChunkMeta(Arrays.asList(new XMLStartElement("other")), 10, 20);
    XMLChunkMeta cm3 = new XMLChunkMeta(Arrays.asList(new XMLStartElement("pre", "root")), 10, 20);
    XMLChunkMeta cm4 = new XMLChunkMeta(Arrays.asList(new XMLStartElement(null, "root", new String[] { "" }, new String[] { "uri" })), 10, 20);
    Async async = context.async();
    MergeStrategy strategy = new AllSameStrategy();
    strategy.canMerge(cm).doOnNext(c -> context.assertTrue(c)).flatMap(v -> strategy.init(cm)).flatMap(v -> strategy.canMerge(cm)).doOnNext(c -> context.assertTrue(c)).flatMap(v -> strategy.canMerge(cm2)).doOnNext(c -> context.assertFalse(c)).flatMap(v -> strategy.canMerge(cm3)).doOnNext(c -> context.assertFalse(c)).flatMap(v -> strategy.canMerge(cm4)).doOnNext(c -> context.assertFalse(c)).subscribe(v -> {
        async.complete();
    }, context::fail);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) DelegateChunkReadStream(io.georocket.util.io.DelegateChunkReadStream) Buffer(io.vertx.core.buffer.Buffer) BufferWriteStream(io.georocket.util.io.BufferWriteStream) RunWith(org.junit.runner.RunWith) XMLStartElement(io.georocket.util.XMLStartElement) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) XMLStartElement(io.georocket.util.XMLStartElement) Async(io.vertx.ext.unit.Async) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) Test(org.junit.Test)

Example 14 with XMLStartElement

use of io.georocket.util.XMLStartElement in project georocket by georocket.

the class AllSameStrategyTest method mergeFail.

/**
 * Test if the merge method fails if it is called with an unexpected chunk
 * @param context the test context
 */
@Test
public void mergeFail(TestContext context) {
    XMLChunkMeta cm2 = new XMLChunkMeta(Arrays.asList(new XMLStartElement("other")), 10, 20);
    Async async = context.async();
    MergeStrategy strategy = new AllSameStrategy();
    BufferWriteStream bws = new BufferWriteStream();
    strategy.init(cm).flatMap(v -> strategy.merge(new DelegateChunkReadStream(chunk2), cm2, bws)).subscribe(v -> context.fail(), err -> {
        context.assertTrue(err instanceof IllegalArgumentException);
        async.complete();
    });
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) DelegateChunkReadStream(io.georocket.util.io.DelegateChunkReadStream) Buffer(io.vertx.core.buffer.Buffer) BufferWriteStream(io.georocket.util.io.BufferWriteStream) RunWith(org.junit.runner.RunWith) XMLStartElement(io.georocket.util.XMLStartElement) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) DelegateChunkReadStream(io.georocket.util.io.DelegateChunkReadStream) XMLStartElement(io.georocket.util.XMLStartElement) Async(io.vertx.ext.unit.Async) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) BufferWriteStream(io.georocket.util.io.BufferWriteStream) Test(org.junit.Test)

Example 15 with XMLStartElement

use of io.georocket.util.XMLStartElement in project georocket by georocket.

the class XMLSplitter method makeResult.

/**
 * Create a new chunk starting from the marked position and ending on the
 * given position. Reset the mark afterwards and advance the window to the
 * end position. Return a {@link io.georocket.input.Splitter.Result} object
 * with the new chunk and its metadata.
 * @param pos the end position
 * @return the {@link io.georocket.input.Splitter.Result} object
 */
protected Result<XMLChunkMeta> makeResult(int pos) {
    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
    // append the full stack of start elements (backwards)
    List<XMLStartElement> chunkParents = new ArrayList<>();
    startElements.descendingIterator().forEachRemaining(e -> {
        chunkParents.add(e);
        sb.append(e + "\n");
    });
    // get chunk start in bytes
    int chunkStart = sb.toString().getBytes(StandardCharsets.UTF_8).length;
    // append current element
    byte[] bytes = window.getBytes(mark, pos);
    sb.append(new String(bytes, StandardCharsets.UTF_8));
    window.advanceTo(pos);
    mark = -1;
    // get chunk end in bytes
    int chunkEnd = chunkStart + bytes.length;
    // append the full stack of end elements
    startElements.iterator().forEachRemaining(e -> sb.append("\n</" + e.getName() + ">"));
    XMLChunkMeta meta = new XMLChunkMeta(chunkParents, chunkStart, chunkEnd);
    return new Result<XMLChunkMeta>(sb.toString(), meta);
}
Also used : XMLStartElement(io.georocket.util.XMLStartElement) ArrayList(java.util.ArrayList) XMLChunkMeta(io.georocket.storage.XMLChunkMeta)

Aggregations

XMLStartElement (io.georocket.util.XMLStartElement)17 XMLChunkMeta (io.georocket.storage.XMLChunkMeta)14 Test (org.junit.Test)13 Buffer (io.vertx.core.buffer.Buffer)7 Result (io.georocket.input.Splitter.Result)6 BufferWriteStream (io.georocket.util.io.BufferWriteStream)4 DelegateChunkReadStream (io.georocket.util.io.DelegateChunkReadStream)4 Async (io.vertx.ext.unit.Async)4 TestContext (io.vertx.ext.unit.TestContext)4 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)4 Arrays (java.util.Arrays)4 Pair (org.apache.commons.lang3.tuple.Pair)4 RunWith (org.junit.runner.RunWith)4 ChunkMeta (io.georocket.storage.ChunkMeta)2 ChunkReadStream (io.georocket.storage.ChunkReadStream)2 GeoJsonChunkMeta (io.georocket.storage.GeoJsonChunkMeta)2 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)2 ArrayList (java.util.ArrayList)2 Rule (org.junit.Rule)2 Observable (rx.Observable)2