下面列出了怎么用com.google.protobuf.TextFormat.ParseException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void statistics() throws ParseException {
when(statisticsManager.getPublishInformationByTopic())
.thenReturn(
ImmutableMap.of(
TestHelpers.PROJECT1_TOPIC1,
givenStatisticsInformation(1, 10L, 55L, 500L, 59L, 19L, 10L, 11L, 1L, 91L, 5L),
TestHelpers.PROJECT1_TOPIC2,
givenStatisticsInformation(0, 90L, 90L),
TestHelpers.PROJECT2_TOPIC1,
givenStatisticsInformation(1, 10L, 55L, 500L, 59L, 19L, 10L, 11L, 1L, 91L, 5L),
TestHelpers.PROJECT2_TOPIC2,
givenStatisticsInformation(0, 90L, 90L)));
when(statisticsManager.getSubscriberInformationByTopic())
.thenReturn(
new ImmutableMap.Builder<String, StatisticsInformation>()
.put(TestHelpers.PROJECT1_TOPIC1, givenStatisticsInformation(0, 200L, 50L, 100L))
.put(TestHelpers.PROJECT1_TOPIC2, new StatisticsInformation())
.put(TestHelpers.PROJECT2_TOPIC1, givenStatisticsInformation(0, 200L, 50L, 100L))
.put(TestHelpers.PROJECT2_TOPIC2, new StatisticsInformation())
.build());
StatisticsResponse statisticsResponse =
blockingStub.statistics(StatisticsRequest.newBuilder().build());
assertThat(statisticsResponse, Matchers.equalTo(getExpectedResponse()));
}
protected Message parse(String textProto) {
try {
Message.Builder builder = defaultInstance.toBuilder();
PARSER.merge(textProto, builder);
return builder.build();
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
protected final Message parsePartial(String textProto) {
try {
Message.Builder builder = defaultInstance.toBuilder();
PARSER.merge(textProto, builder);
return builder.buildPartial();
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/** Parses the config file, in proto text format, and returns it. */
public Service getApiProtoConfig(String configFileName) throws ParseException {
String content = readTestData(configFileName);
Service.Builder builder = Service.newBuilder();
TextFormat.merge(content, builder);
return builder.build();
}
public boolean primitive(Object value) throws ParseException, IOException
{
if (getMatchKeyList().contains(key)) {
found = true;
this.value = value;
keyValMap.put(key, value);
key = null;
keyCount++;
return false;
}
return true;
}
public void writeMultipleEvents() throws Exception, ParseException {
final int TOTAL_WRITES = 100000;
final long startTime = System.currentTimeMillis();
successes = 0;
fails = 0;
for (int z = 0; z < TOTAL_WRITES; z++) {
es.appendToStream(STREAM_NAME, new ResponseReceiver() {
@Override
public void onResponseReturn(Message msg) {
// log.debug("Response returned="+i);
successes++;
if ((successes + fails) == (TOTAL_WRITES)) {
processing.release();
}
}
@Override
public void onErrorReturn(Exception ex) {
fails++;
if ((successes + fails) == TOTAL_WRITES) {
processing.release();
}
}
}, generateEvents());
}
processing.acquire();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
log.debug("Writing finished. Number of writes={} ({} successful, {} failed), duration={}",
TOTAL_WRITES, successes, fails, duration);
}
@Test
public void writeMultipleEvents() throws Exception, ParseException {
final int TOTAL_WRITES = 100000;
final long startTime = System.currentTimeMillis();
successes = 0;
fails = 0;
for (int z = 0; z < TOTAL_WRITES; z++) {
es.appendToStream(STREAM_NAME, new ResponseReceiver() {
@Override
public void onResponseReturn(Message msg) {
// log.debug("Response returned="+i);
successes++;
if ((successes + fails) == (TOTAL_WRITES)) {
processing.release();
}
}
@Override
public void onErrorReturn(Exception ex) {
fails++;
if ((successes + fails) == TOTAL_WRITES) {
processing.release();
}
}
}, generateEvents());
}
processing.acquire();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
log.debug("Writing finished. Number of writes={} ({} successful, {} failed), duration={}",
TOTAL_WRITES, successes, fails, duration);
}
public void startJSON() throws ParseException, IOException
{
found = false;
end = false;
}
public void endJSON() throws ParseException, IOException
{
end = true;
}
public boolean startArray() throws ParseException, IOException
{
return true;
}
public boolean startObject() throws ParseException, IOException
{
return true;
}
public boolean startObjectEntry(String key) throws ParseException, IOException
{
this.key = key;
return true;
}
public boolean endArray() throws ParseException, IOException
{
return false;
}
public boolean endObject() throws ParseException, IOException
{
return true;
}
public boolean endObjectEntry() throws ParseException, IOException
{
return true;
}
private static CToolchain.Feature getFeature(String protoText) throws ParseException {
CToolchain.Feature.Builder feature = CToolchain.Feature.newBuilder();
TextFormat.merge(protoText, feature);
return feature.build();
}
private static CToolchain.ActionConfig getActionConfig(String protoText) throws ParseException {
CToolchain.ActionConfig.Builder actionConfig = CToolchain.ActionConfig.newBuilder();
TextFormat.merge(protoText, actionConfig);
return actionConfig.build();
}
@Nullable
public static Platform getPlatformProto(Spawn spawn, @Nullable RemoteOptions remoteOptions)
throws UserExecException {
SortedMap<String, String> defaultExecProperties =
remoteOptions != null
? remoteOptions.getRemoteDefaultExecProperties()
: ImmutableSortedMap.of();
if (spawn.getExecutionPlatform() == null
&& spawn.getCombinedExecProperties().isEmpty()
&& defaultExecProperties.isEmpty()) {
return null;
}
Platform.Builder platformBuilder = Platform.newBuilder();
if (!spawn.getCombinedExecProperties().isEmpty()) {
for (Map.Entry<String, String> entry : spawn.getCombinedExecProperties().entrySet()) {
platformBuilder.addPropertiesBuilder().setName(entry.getKey()).setValue(entry.getValue());
}
} else if (spawn.getExecutionPlatform() != null
&& !Strings.isNullOrEmpty(spawn.getExecutionPlatform().remoteExecutionProperties())) {
// Try and get the platform info from the execution properties.
try {
TextFormat.getParser()
.merge(spawn.getExecutionPlatform().remoteExecutionProperties(), platformBuilder);
} catch (ParseException e) {
String message =
String.format(
"Failed to parse remote_execution_properties from platform %s",
spawn.getExecutionPlatform().label());
throw new UserExecException(
e, createFailureDetail(message, Code.INVALID_REMOTE_EXECUTION_PROPERTIES));
}
} else {
for (Map.Entry<String, String> property : defaultExecProperties.entrySet()) {
platformBuilder.addProperties(
Property.newBuilder().setName(property.getKey()).setValue(property.getValue()).build());
}
}
sortPlatformProperties(platformBuilder);
return platformBuilder.build();
}