|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2013-2021 Real Logic Limited. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + * https://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | +package uk.co.real_logic.sbe.generation.java; | 
|  | 17 | + | 
|  | 18 | +import baseline.CarDecoder; | 
|  | 19 | +import baseline.EngineDecoder; | 
|  | 20 | +import baseline.MessageHeaderDecoder; | 
|  | 21 | +import baseline.OptionalExtrasDecoder; | 
|  | 22 | +import org.agrona.concurrent.UnsafeBuffer; | 
|  | 23 | +import org.junit.jupiter.api.Test; | 
|  | 24 | +import uk.co.real_logic.sbe.EncodedCarTestBase; | 
|  | 25 | + | 
|  | 26 | +import java.nio.ByteBuffer; | 
|  | 27 | +import java.util.ArrayList; | 
|  | 28 | + | 
|  | 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; | 
|  | 30 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; | 
|  | 31 | + | 
|  | 32 | +public class RewindTest extends EncodedCarTestBase | 
|  | 33 | +{ | 
|  | 34 | + private static final int MSG_BUFFER_CAPACITY = 4 * 1024; | 
|  | 35 | + | 
|  | 36 | + @Test | 
|  | 37 | + void shouldRewindAfterReadingFullMessage() | 
|  | 38 | + { | 
|  | 39 | + final ByteBuffer encodedMsgBuffer = ByteBuffer.allocate(MSG_BUFFER_CAPACITY); | 
|  | 40 | + encodeTestMessage(encodedMsgBuffer); | 
|  | 41 | + | 
|  | 42 | + final MessageHeaderDecoder header = new MessageHeaderDecoder(); | 
|  | 43 | + final CarDecoder carDecoder = new CarDecoder(); | 
|  | 44 | + carDecoder.wrapAndApplyHeader(new UnsafeBuffer(encodedMsgBuffer), 0, header); | 
|  | 45 | + | 
|  | 46 | + final ArrayList<Object> passOne = getValues(carDecoder); | 
|  | 47 | + carDecoder.sbeRewind(); | 
|  | 48 | + final ArrayList<Object> passTwo = getValues(carDecoder); | 
|  | 49 | + assertEquals(passOne, passTwo); | 
|  | 50 | + | 
|  | 51 | + carDecoder.sbeRewind(); | 
|  | 52 | + final ArrayList<Object> partialPassOne = getPartialValues(carDecoder); | 
|  | 53 | + carDecoder.sbeRewind(); | 
|  | 54 | + final ArrayList<Object> partialPassTwo = getPartialValues(carDecoder); | 
|  | 55 | + assertNotEquals(passOne, partialPassOne); | 
|  | 56 | + assertEquals(partialPassOne, partialPassTwo); | 
|  | 57 | + | 
|  | 58 | + carDecoder.sbeRewind(); | 
|  | 59 | + final ArrayList<Object> passThree = getValues(carDecoder); | 
|  | 60 | + assertEquals(passOne, passThree); | 
|  | 61 | + } | 
|  | 62 | + | 
|  | 63 | + private ArrayList<Object> getValues(final CarDecoder carDecoder) | 
|  | 64 | + { | 
|  | 65 | + final ArrayList<Object> values = new ArrayList<>(); | 
|  | 66 | + | 
|  | 67 | + values.add(carDecoder.serialNumber()); | 
|  | 68 | + values.add(carDecoder.modelYear()); | 
|  | 69 | + values.add(carDecoder.available()); | 
|  | 70 | + values.add(carDecoder.code()); | 
|  | 71 | + values.add(CarDecoder.someNumbersLength()); | 
|  | 72 | + for (int i = 0, n = CarDecoder.someNumbersLength(); i < n; i++) | 
|  | 73 | + { | 
|  | 74 | + values.add(carDecoder.someNumbers(i)); | 
|  | 75 | + } | 
|  | 76 | + values.add(carDecoder.vehicleCode()); | 
|  | 77 | + final OptionalExtrasDecoder extras = carDecoder.extras(); | 
|  | 78 | + values.add(extras.sunRoof()); | 
|  | 79 | + values.add(extras.sportsPack()); | 
|  | 80 | + values.add(extras.cruiseControl()); | 
|  | 81 | + final EngineDecoder engine = carDecoder.engine(); | 
|  | 82 | + values.add(engine.capacity()); | 
|  | 83 | + values.add(engine.numCylinders()); | 
|  | 84 | + values.add(engine.maxRpm()); | 
|  | 85 | + values.add(engine.manufacturerCode()); | 
|  | 86 | + values.add(engine.fuel()); | 
|  | 87 | + final CarDecoder.FuelFiguresDecoder fuelFigures = carDecoder.fuelFigures(); | 
|  | 88 | + while (fuelFigures.hasNext()) | 
|  | 89 | + { | 
|  | 90 | + fuelFigures.next(); | 
|  | 91 | + values.add(fuelFigures.speed()); | 
|  | 92 | + values.add(fuelFigures.mpg()); | 
|  | 93 | + } | 
|  | 94 | + final CarDecoder.PerformanceFiguresDecoder performanceFigures = carDecoder.performanceFigures(); | 
|  | 95 | + while (performanceFigures.hasNext()) | 
|  | 96 | + { | 
|  | 97 | + performanceFigures.next(); | 
|  | 98 | + values.add(performanceFigures.octaneRating()); | 
|  | 99 | + final CarDecoder.PerformanceFiguresDecoder.AccelerationDecoder acceleration = | 
|  | 100 | + performanceFigures.acceleration(); | 
|  | 101 | + while (acceleration.hasNext()) | 
|  | 102 | + { | 
|  | 103 | + acceleration.next(); | 
|  | 104 | + values.add(acceleration.mph()); | 
|  | 105 | + values.add(acceleration.seconds()); | 
|  | 106 | + } | 
|  | 107 | + } | 
|  | 108 | + values.add(carDecoder.manufacturer()); | 
|  | 109 | + values.add(carDecoder.model()); | 
|  | 110 | + return values; | 
|  | 111 | + } | 
|  | 112 | + | 
|  | 113 | + private ArrayList<Object> getPartialValues(final CarDecoder carDecoder) | 
|  | 114 | + { | 
|  | 115 | + final ArrayList<Object> values = new ArrayList<>(); | 
|  | 116 | + | 
|  | 117 | + values.add(carDecoder.serialNumber()); | 
|  | 118 | + values.add(carDecoder.modelYear()); | 
|  | 119 | + values.add(carDecoder.available()); | 
|  | 120 | + values.add(carDecoder.code()); | 
|  | 121 | + values.add(CarDecoder.someNumbersLength()); | 
|  | 122 | + for (int i = 0, n = CarDecoder.someNumbersLength(); i < n; i++) | 
|  | 123 | + { | 
|  | 124 | + values.add(carDecoder.someNumbers(i)); | 
|  | 125 | + } | 
|  | 126 | + values.add(carDecoder.vehicleCode()); | 
|  | 127 | + final OptionalExtrasDecoder extras = carDecoder.extras(); | 
|  | 128 | + values.add(extras.sunRoof()); | 
|  | 129 | + values.add(extras.sportsPack()); | 
|  | 130 | + values.add(extras.cruiseControl()); | 
|  | 131 | + final EngineDecoder engine = carDecoder.engine(); | 
|  | 132 | + values.add(engine.capacity()); | 
|  | 133 | + values.add(engine.numCylinders()); | 
|  | 134 | + values.add(engine.maxRpm()); | 
|  | 135 | + values.add(engine.manufacturerCode()); | 
|  | 136 | + values.add(engine.fuel()); | 
|  | 137 | + final CarDecoder.FuelFiguresDecoder fuelFigures = carDecoder.fuelFigures(); | 
|  | 138 | + while (fuelFigures.hasNext()) | 
|  | 139 | + { | 
|  | 140 | + fuelFigures.next(); | 
|  | 141 | + values.add(fuelFigures.speed()); | 
|  | 142 | + values.add(fuelFigures.mpg()); | 
|  | 143 | + } | 
|  | 144 | + final CarDecoder.PerformanceFiguresDecoder performanceFigures = carDecoder.performanceFigures(); | 
|  | 145 | + | 
|  | 146 | + // Stop decoding part way through the message. | 
|  | 147 | + if (performanceFigures.hasNext()) | 
|  | 148 | + { | 
|  | 149 | + performanceFigures.next(); | 
|  | 150 | + values.add(performanceFigures.octaneRating()); | 
|  | 151 | + final CarDecoder.PerformanceFiguresDecoder.AccelerationDecoder acceleration = | 
|  | 152 | + performanceFigures.acceleration(); | 
|  | 153 | + if (acceleration.hasNext()) | 
|  | 154 | + { | 
|  | 155 | + acceleration.next(); | 
|  | 156 | + values.add(acceleration.mph()); | 
|  | 157 | + } | 
|  | 158 | + } | 
|  | 159 | + | 
|  | 160 | + return values; | 
|  | 161 | + } | 
|  | 162 | +} | 
0 commit comments