下面列出了怎么用org.testng.util.Strings的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean checkWiresharkLocation() {
Preferences preferences = PreferencesManager.getInstance().getPreferences();
if (preferences != null && !Strings.isNullOrEmpty(preferences.getWireSharkLocation())) {
String executablePath = preferences.getWireSharkLocation();
return !Strings.isNullOrEmpty(executablePath) && new File(executablePath).exists();
}
boolean wsinstalled = locateWireshark();
if (!wsinstalled) {
TrexAlertBuilder.build()
.setType(Alert.AlertType.ERROR)
.setTitle("Wireshark error")
.setHeader("Unable to start monitor")
.setContent("Could not find Wireshark in default installation path.\n" +
"Please install it first to proceed with this action.\n" +
"You can download it from https://www.wireshark.org\n" +
"Or you can specify location manually in preferences")
.getAlert()
.showAndWait();
}
return wsinstalled;
}
/**
* Set table data
*
* @param packetInfoList
*/
private void setTableData(List<PacketInfo> packetInfoList) {
index = 1;
tableDataList.clear();
for (PacketInfo packetInfo : packetInfoList) {
ImportPcapTableData tableData = new ImportPcapTableData();
StringBuilder name = new StringBuilder();
if (!Strings.isNullOrEmpty(propertiesBinder.getPrefix())) {
name.append(propertiesBinder.getPrefix()).append("_");
}
name.append("packet_").append(index);
tableData.setName(name.toString());
tableData.setIndex(index);
tableData.setLength(packetInfo.getPacket().length());
tableData.setMacSrc(packetInfo.getSrcMac());
tableData.setMacDst(packetInfo.getDestMac());
tableData.setIpSrc(packetInfo.getSrcIpv4());
tableData.setIpDst(packetInfo.getDestIpv4());
tableData.setPacketType(trafficProfile.getPacketTypeText(packetInfo.getPacket()).getType());
tableData.setPacket(packetInfo.getPacket());
tableData.setHasVlan(packetInfo.hasVlan());
tableData.setTimeStamp(packetInfo.getTimeStamp());
tableDataList.add(tableData);
index++;
}
importedStreamTable.setItems(tableDataList);
}
private void checkPortAttrs() {
Stream.of(
"index",
"rxFilterMode",
"owner",
"status",
"numaMode",
"pciAddress",
"rxQueueing",
"gratArp")
.forEach(attrId -> {
Label attr = lookup("#"+ attrId).query();
Assert.assertFalse(Strings.isNullOrEmpty(attr.getText()));
});
}
@Test
public void testSerializeApiErrorFromThrowable() throws IOException {
Exception e = new Exception("error");
e.setStackTrace(Thread.currentThread().getStackTrace());
ApiError error = ApiError.builderFromThrowable(e).build();
ApiError deserialised = fromJson(asJson(error), ApiError.class);
assertFalse(Strings.isNullOrEmpty(deserialised.getDetails()), "Expected details to contain exception stack trace");
assertEquals(deserialised, error);
}
/**
* loads the given file into a string, ignoring the comments, but considering "#!/bin/bash"
* @param file file to read
* @return file content as a string
* @throws IOException
*/
private String loadFile(String file) throws IOException {
StringBuilder sb = new StringBuilder();
List<String> lines = IOUtils
.readLines(new InputStreamReader(GobblinAWSClusterLauncherTest.class.getClassLoader().getResourceAsStream(file), "UTF-8"));
for (String line : lines) {
if (line.equals(CloudInitScriptBuilder.BASH) || (!line.startsWith("#") && !Strings.isNullOrEmpty(line))) {
sb.append(line).append("\n");
}
}
return sb.toString();
}
public static PortModel createModelFrom(Port port) {
PortModel model = new PortModel();
model.index.bindBidirectional(port.indexProperty());
model.portDriver.bindBidirectional(port.driverProper());
model.rxFilterMode.bindBidirectional(port.rxFilterModeProperty());
model.multicast.bindBidirectional(port.multicastProperty());
model.promiscuousMode.bindBidirectional(port.promiscuousProperty());
model.owner.bindBidirectional(port.ownerProperty());
port.ownerProerty.addListener((observable, oldVal, newVal) -> {
String currentUser = ConnectionManager.getInstance().getClientName();
model.setIsOwned(currentUser.equalsIgnoreCase(newVal));
});
model.portSpeed.bind(port.getAttr().speedProperty().asString());
model.portStatus.addListener((_o, o, n) -> model.isTransmitProperty.set(n.equalsIgnoreCase("tx") || n.equalsIgnoreCase("pause")));
model.portStatus.bindBidirectional(port.statusProerty());
model.capturingMode.bind(port.captureStatusProperty());
model.linkStatus.bindBidirectional(port.linkProperty());
model.ledControl.bindBidirectional(port.ledProperty());
model.numaMode.bind(port.numaProerty().asString());
model.pciAddress.bind(port.pciAddrProperty());
model.rxQueueing.bind(port.rxQueueProperty());
PortStatus.PortStatusResult.PortStatusResultRxInfo.PortStatusResultRxInfoGratArp grat_arp = port.getRx_info().getGrat_arp();
model.gratARP.bind(grat_arp.stateProperty());
model.flowControl.setValue(port.getFlowControl());
model.serviceModeProperty.bindBidirectional(port.serviceModeProerty());
model.initHandlers(port);
PortStatus.PortStatusResult.PortStatusResultAttr.PortStatusResultAttrLayerCfg layerConfiguration = port.getAttr().getLayer_cfg();
PortStatus.PortStatusResult.PortStatusResultAttr.PortStatusResultAttrLayerCfg.PortStatusResultAttrLayerCfgEther l2 = layerConfiguration.getEther();
model.l2Configuration = new PortLayerConfigurationModel(ConfigurationMode.L2, l2.getSrc(), l2.getDst(), l2.getState());
model.l2Configuration.dstProperty().bindBidirectional(port.getAttr().getLayer_cfg().getEther().dstProperty());
PortStatus.PortStatusResult.PortStatusResultAttr.PortStatusResultAttrLayerCfg.PortStatusResultAttrLayerCfgIPv4 l3 = layerConfiguration.getIpv4();
model.l3Configuration = new PortLayerConfigurationModel(ConfigurationMode.L3, l3.getSrc(), l3.getDst(), l3.getState());
model.l3Configuration.srcProperty().bindBidirectional(port.getAttr().getLayer_cfg().getIpv4().srcProperty());
model.l3Configuration.dstProperty().bindBidirectional(port.getAttr().getLayer_cfg().getIpv4().dstProperty());
if (Strings.isNullOrEmpty(l3.getSrc()) && Strings.isNullOrEmpty(l3.getDst())) {
model.layerConfigurationType.setValue(ConfigurationMode.L2);
} else {
model.layerConfigurationType.setValue(ConfigurationMode.L3);
}
model.linkControlSupportProperty().set(port.is_link_supported);
model.ledControlProperty().set(port.is_led_supported);
model.flowControlSupportProperty().set(port.is_fc_supported);
return model;
}
@Test
public void test() throws IOException {
State state = new State();
state.setProp(ConfigurationKeys.WRITER_PARTITIONER_CLASS, TestPartitioner.class.getCanonicalName());
TestPartitionAwareWriterBuilder builder = new TestPartitionAwareWriterBuilder();
PartitionedDataWriter writer = new PartitionedDataWriter<String, String>(builder, state);
Assert.assertEquals(builder.actions.size(), 0);
String record1 = "abc";
writer.writeEnvelope(new RecordEnvelope(record1));
Assert.assertEquals(builder.actions.size(), 2);
TestPartitionAwareWriterBuilder.Action action = builder.actions.poll();
Assert.assertEquals(action.getPartition(), "a");
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.BUILD);
action = builder.actions.poll();
Assert.assertEquals(action.getPartition(), "a");
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.WRITE);
Assert.assertEquals(action.getTarget(), record1);
Assert.assertTrue(writer.isSpeculativeAttemptSafe());
String record2 = "123";
writer.writeEnvelope(new RecordEnvelope(record2));
Assert.assertEquals(builder.actions.size(), 2);
action = builder.actions.poll();
Assert.assertEquals(action.getPartition(), "1");
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.BUILD);
Assert.assertFalse(writer.isSpeculativeAttemptSafe());
action = builder.actions.poll();
Assert.assertEquals(action.getPartition(), "1");
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.WRITE);
Assert.assertEquals(action.getTarget(), record2);
writer.writeEnvelope(new RecordEnvelope(record1));
Assert.assertEquals(builder.actions.size(), 1);
action = builder.actions.poll();
Assert.assertEquals(action.getPartition(), "a");
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.WRITE);
Assert.assertEquals(action.getTarget(), record1);
Assert.assertEquals(writer.recordsWritten(), 3);
Assert.assertEquals(writer.bytesWritten(), 3);
Assert.assertFalse(writer.isSpeculativeAttemptSafe());
writer.cleanup();
Assert.assertEquals(builder.actions.size(), 2);
action = builder.actions.poll();
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.CLEANUP);
action = builder.actions.poll();
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.CLEANUP);
// Before close, partitions info is not serialized
String partitionsKey = "writer.0.partitions";
Assert.assertTrue(state.getProp(partitionsKey) == null);
writer.close();
Assert.assertEquals(builder.actions.size(), 2);
action = builder.actions.poll();
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.CLOSE);
action = builder.actions.poll();
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.CLOSE);
// After close, partitions info is available
Assert.assertFalse(Strings.isNullOrEmpty(state.getProp(partitionsKey)));
List<PartitionDescriptor> partitions = PartitionedDataWriter.getPartitionInfoAndClean(state, 0);
Assert.assertTrue(state.getProp(partitionsKey) == null);
Assert.assertEquals(partitions.size(), 2);
DatasetDescriptor dataset = new DatasetDescriptor("testPlatform", "testDataset");
Assert.assertEquals(partitions.get(0), new PartitionDescriptor("a", dataset));
Assert.assertEquals(partitions.get(1), new PartitionDescriptor("1", dataset));
writer.commit();
Assert.assertEquals(builder.actions.size(), 2);
action = builder.actions.poll();
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.COMMIT);
action = builder.actions.poll();
Assert.assertEquals(action.getType(), TestPartitionAwareWriterBuilder.Actions.COMMIT);
}