Getting list of all #Tests (jUnit tests) - java

I need to get list of all the tests in my package... I want to use maven but so far I haven't found appropriate call to do it. I've tried reflection in Java but it did not list all of the #Test methods. Some of un-annotated methods were listed as well..
public static void main(String[] args) {
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(ClasspathHelper.forPackage("com.mypackage"))
.setScanners(new MethodAnnotationsScanner()));
Set<Method> methods = reflections.getMethodsAnnotatedWith(Test.class);
System.out.println(methods);
}
Any help or pointer in right direction would be much appreciated.

i was able to track it by making use of Junit's RunListner
when you run your Junit suites or individual classes make sure you add the listener to it.
// AutomationTestSuites - contains my all Junit test classes
Class cls= AutomationTestSuites.class;
JUnitCore jUnitCore = new JUnitCore();
CustomRunListener customRunListener = new CustomRunListener();
jUnitCore.addListener(customRunListener);
Request request = Request.aClass(cls);
Result result = jUnitCore.run(request);
below is my listener
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import com.dto.TestSuiteDetails;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.concurrent.TimeUnit;
import java.lang.Exception;
public class CustomRunListener extends RunListener{
private Map<String,TestSuiteDetails> map = new LinkedHashMap<>();
private long testSuiteStartTime;
private long testSuiteElapsedTime;
private int ignoredCount;
public void testRunStarted(Description description) throws Exception {
testSuiteStartTime = System.currentTimeMillis();
}
public void testStarted(Description description) throws Exception {
TestSuiteDetails testSuiteDetails = new TestSuiteDetails();
testSuiteDetails.setTestCaseName(description.getMethodName());
String[] arr = description.getTestClass().getName().split("\\.");
String name = arr[arr.length-1];
testSuiteDetails.setTestClassName(name);
String[] arr1 = name.split("_");
String testSuite = arr1[0];
testSuiteDetails.setTestSuiteNmae(testSuite);
testSuiteDetails.setTestStatus("Passed");
testSuiteDetails.setStartTime(System.currentTimeMillis());
map.put(description.getMethodName(),testSuiteDetails);
}
public void testFinished(Description description) throws Exception {
TestSuiteDetails testSuiteDetails= null;
if(map.containsKey(description.getMethodName())){
testSuiteDetails = map.get(description.getMethodName());
testSuiteDetails.setElaspsedTime(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()-testSuiteDetails.getStartTime()));
}
map.put(description.getMethodName(),testSuiteDetails);
}
public void testRunFinished(org.junit.runner.Result result) throws Exception {
testSuiteElapsedTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()-testSuiteStartTime);
}
public void testFailure(Failure failure) throws Exception {
TestSuiteDetails testSuiteDetails= null;
if(map.containsKey(failure.getDescription().getMethodName())){
testSuiteDetails = map.get(failure.getDescription().getMethodName());
}else{
testSuiteDetails = new TestSuiteDetails();
}
testSuiteDetails.setTestCaseName(failure.getDescription().getMethodName());
testSuiteDetails.setTestDescription(failure.getException().toString());
testSuiteDetails.setTestStatus("Failed");
map.put(failure.getDescription().getMethodName(),testSuiteDetails);
}
public void testIgnored(Description description) throws Exception {
TestSuiteDetails testSuiteDetails= null;
if(map.containsKey(description.getMethodName())){
testSuiteDetails = map.get(description.getMethodName());
}else{
testSuiteDetails = new TestSuiteDetails();
testSuiteDetails.setTestCaseName(description.getMethodName());
String[] arr = description.getTestClass().getName().split("\\.");
String name = arr[arr.length-1];
testSuiteDetails.setTestClassName(name);
String[] arr1 = name.split("_");
String testSuite = arr1[0];
testSuiteDetails.setTestSuiteNmae(testSuite);
ignoredCount++;
}
testSuiteDetails.setTestStatus("Ignored");
map.put(description.getMethodName(),testSuiteDetails);
}
public int getIgnoredCount() {
return ignoredCount;
}
public void setIgnoredCount(int ignoredCount) {
this.ignoredCount = ignoredCount;
}
public Map<String, TestSuiteDetails> getMap() {
return map;
}
public long getTestSuiteStartTime() {
return testSuiteStartTime;
}
public long getTestSuiteElapsedTime() {
return testSuiteElapsedTime;
}
}
am using this pojo for holding test suite details
public class TestSuiteDetails {
private String testClassName;
private String testSuiteNmae;
private String testCaseName;
private String testStatus;
private String testDescription;
private long startTime;
private long elaspsedTime;
public String getTestClassName() {
return testClassName;
}
public void setTestClassName(String testClassName) {
this.testClassName = testClassName;
}
public String getTestCaseName() {
return testCaseName;
}
public void setTestCaseName(String testCaseName) {
this.testCaseName = testCaseName;
}
public String getTestStatus() {
return testStatus;
}
public void setTestStatus(String testStatus) {
this.testStatus = testStatus;
}
public String getTestDescription() {
return testDescription;
}
public void setTestDescription(String testDescription) {
this.testDescription = testDescription;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public long getElaspsedTime() {
return elaspsedTime;
}
public void setElaspsedTime(long elaspsedTime) {
this.elaspsedTime = elaspsedTime;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestSuiteDetails that = (TestSuiteDetails) o;
if (testClassName != null ? !testClassName.equals(that.testClassName) : that.testClassName != null)
return false;
return testCaseName != null ? testCaseName.equals(that.testCaseName) : that.testCaseName == null;
}
#Override
public int hashCode() {
int result = testClassName != null ? testClassName.hashCode() : 0;
result = 31 * result + (testCaseName != null ? testCaseName.hashCode() : 0);
return result;
}
public String getTestSuiteNmae() {
return testSuiteNmae;
}
public void setTestSuiteNmae(String testSuiteNmae) {
this.testSuiteNmae = testSuiteNmae;
}
}
now after adding the listener - you can simple utilize it later
HashMap<String, TestSuiteDetails> myTestResultMap = customRunListener.getMap();

Related

Implementing custom prefix remover token filter in lucene producing dirty tokens

i'm trying to implement a lucene filter to remove a prefix from a term in a query.
It seems that sometime after multiple queries, the filter has been reused so the char buffer is dirty.
Code below is simplified, prefix is an external parameter.
public static class PrefixFilter extends TokenFilter {
private final PackedTokenAttributeImpl termAtt = (PackedTokenAttributeImpl) addAttribute(CharTermAttribute.class);
public PrefixFilter(TokenStream in) {
super(in);
}
#Override
public final boolean incrementToken() throws IOException {
if (!input.incrementToken()) {
return false;
}
String value = new String(termAtt.buffer());
value = value.trim();
value = value.toLowerCase();
value = StringUtils.removeStart(value, "prefix_");
if (value.isBlank()) {
termAtt.setEmpty();
} else {
termAtt.copyBuffer(value.toCharArray(), 0, value.length());
termAtt.setLength(value.length());
}
return true;
}
}
So after 10 or twelve queries, the value "prefix_a" became "abcde".
So i'm trying to add termBuffer offset end value in this way:
termAtt.setEmpty();
termAtt.resizeBuffer(value.length());
termAtt.copyBuffer(value.toCharArray(), 0, value.length());
termAtt.setLength(value.length());
termAtt.setOffset(0, value.length());
But i don't know if it's correct. Can anyone help me?
Thanks.
See if this helps you,
/**
* Standard number token filter.
*/
public class StandardnumberTokenFilter extends TokenFilter {
private final LinkedList<PackedTokenAttributeImpl> tokens;
private final StandardnumberService service;
private final Settings settings;
private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
private final PositionIncrementAttribute posIncAtt = addAttribute(PositionIncrementAttribute.class);
private State current;
protected StandardnumberTokenFilter(TokenStream input, StandardnumberService service, Settings settings) {
super(input);
this.tokens = new LinkedList<>();
this.service = service;
this.settings = settings;
}
#Override
public final boolean incrementToken() throws IOException {
if (!tokens.isEmpty()) {
if (current == null) {
throw new IllegalArgumentException("current is null");
}
PackedTokenAttributeImpl token = tokens.removeFirst();
restoreState(current);
termAtt.setEmpty().append(token);
posIncAtt.setPositionIncrement(0);
return true;
}
if (input.incrementToken()) {
detect();
if (!tokens.isEmpty()) {
current = captureState();
}
return true;
} else {
return false;
}
}
private void detect() throws CharacterCodingException {
CharSequence term = new String(termAtt.buffer(), 0, termAtt.length());
Collection<CharSequence> variants = service.lookup(settings, term);
for (CharSequence ch : variants) {
if (ch != null) {
PackedTokenAttributeImpl token = new PackedTokenAttributeImpl();
token.append(ch);
tokens.add(token);
}
}
}
#Override
public void reset() throws IOException {
super.reset();
tokens.clear();
current = null;
}
#Override
public boolean equals(Object object) {
return object instanceof StandardnumberTokenFilter &&
service.equals(((StandardnumberTokenFilter)object).service) &&
settings.equals(((StandardnumberTokenFilter)object).settings);
}
#Override
public int hashCode() {
return service.hashCode() ^ settings.hashCode();
}
}
https://github.com/jprante/elasticsearch-plugin-bundle/blob/f63690f877cc7f50360faffbac827622c9d404ef/src/main/java/org/xbib/elasticsearch/plugin/bundle/index/analysis/standardnumber/StandardnumberTokenFilter.java

error: Not sure how to handle insert method's return type

So I'm trying to use Executor execute the Dao's in my android app, but when I created the Executor, it said it required the method being run to have a Runnable return type?
So I made them all return Runnable, but now Android Studio gives me the error:
/database/TripDao.java:28: error: Not sure how to handle insert method's return type.
Runnable insertTrip(Trip trip);
^
Dao:
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
import java.util.UUID;
#Dao
public interface TripDao {
#Query("DELETE FROM trip")
public void deleteTrips();
#Query("SELECT * FROM trip")
LiveData<List<Trip>> getAll();
#Query("SELECT * FROM trip WHERE uuid=:id")
public LiveData<Trip> getTrip(UUID id);
#Delete
Runnable delete(Trip trip);
#Insert
Runnable insertTrip(Trip trip);
#Update
Runnable updateTrip(Trip trip);
}
Repository:
package com.example.csc202assignment.database;
import android.content.Context;
import androidx.lifecycle.LiveData;
import androidx.room.Room;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class TripRepository {
private static TripRepository INSTANCE = null;
private final String DATABASE_NAME = "trip-database";
private Context context;
TripDatabase database;
static TripDao tripDao;
Executor executor = Executors.newSingleThreadExecutor();
private TripRepository(Context context) {
this.context = context;
this.database = Room.databaseBuilder(
context.getApplicationContext(),
TripDatabase.class,DATABASE_NAME).build();
tripDao = database.tripDao();
}
void deleteTrips(){
tripDao.deleteTrips();
}
void delete(Trip trip){
executor.execute(tripDao.delete(trip));
}
void insertTrip(Trip trip){
executor.execute(tripDao.insertTrip(trip));
}
void updateTrip(Trip trip){
executor.execute(tripDao.updateTrip(trip));
}
public LiveData<List<Trip>> getAll(){ return tripDao.getAll();
}
public static LiveData<Trip> getTrip(UUID id){
return tripDao.getTrip(id);
}
public static void initialise(Context context){
if(INSTANCE==null){
INSTANCE = new TripRepository(context);
}
}
public static TripRepository get() throws IllegalStateException {
try {
return INSTANCE;
} catch (IllegalStateException e) {
System.out.println("TripRepository must be initialised");
}
return INSTANCE;
}
}
Dao_Impl
package com.example.csc202assignment.database;
import android.database.Cursor;
import androidx.lifecycle.LiveData;
import androidx.room.EntityDeletionOrUpdateAdapter;
import androidx.room.EntityInsertionAdapter;
import androidx.room.RoomDatabase;
import androidx.room.RoomSQLiteQuery;
import androidx.room.SharedSQLiteStatement;
import androidx.room.util.CursorUtil;
import androidx.room.util.DBUtil;
import androidx.sqlite.db.SupportSQLiteStatement;
import java.lang.Class;
import java.lang.Exception;
import java.lang.Override;
import java.lang.Runnable;
import java.lang.String;
import java.lang.SuppressWarnings;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
#SuppressWarnings({"unchecked", "deprecation"})
public final class TripDao_Impl implements TripDao {
private final RoomDatabase __db;
private final EntityInsertionAdapter<Trip> __insertionAdapterOfTrip;
private final TripTypeConverter __tripTypeConverter = new TripTypeConverter();
private final EntityDeletionOrUpdateAdapter<Trip> __deletionAdapterOfTrip;
private final EntityDeletionOrUpdateAdapter<Trip> __updateAdapterOfTrip;
private final SharedSQLiteStatement __preparedStmtOfDeleteTrips;
public TripDao_Impl(RoomDatabase __db) {
this.__db = __db;
this.__insertionAdapterOfTrip = new EntityInsertionAdapter<Trip>(__db) {
#Override
public String createQuery() {
return "INSERT OR ABORT INTO `Trip` (`uuid`,`title`,`destination`,`date`,`duration`) VALUES (?,?,?,?,?)";
}
#Override
public void bind(SupportSQLiteStatement stmt, Trip value) {
final String _tmp;
_tmp = __tripTypeConverter.fromUUID(value.uuid);
if (_tmp == null) {
stmt.bindNull(1);
} else {
stmt.bindString(1, _tmp);
}
if (value.title == null) {
stmt.bindNull(2);
} else {
stmt.bindString(2, value.title);
}
if (value.destination == null) {
stmt.bindNull(3);
} else {
stmt.bindString(3, value.destination);
}
if (value.date == null) {
stmt.bindNull(4);
} else {
stmt.bindString(4, value.date);
}
if (value.duration == null) {
stmt.bindNull(5);
} else {
stmt.bindString(5, value.duration);
}
}
};
this.__deletionAdapterOfTrip = new EntityDeletionOrUpdateAdapter<Trip>(__db) {
#Override
public String createQuery() {
return "DELETE FROM `Trip` WHERE `uuid` = ?";
}
#Override
public void bind(SupportSQLiteStatement stmt, Trip value) {
final String _tmp;
_tmp = __tripTypeConverter.fromUUID(value.uuid);
if (_tmp == null) {
stmt.bindNull(1);
} else {
stmt.bindString(1, _tmp);
}
}
};
this.__updateAdapterOfTrip = new EntityDeletionOrUpdateAdapter<Trip>(__db) {
#Override
public String createQuery() {
return "UPDATE OR ABORT `Trip` SET `uuid` = ?,`title` = ?,`destination` = ?,`date` = ?,`duration` = ? WHERE `uuid` = ?";
}
#Override
public void bind(SupportSQLiteStatement stmt, Trip value) {
final String _tmp;
_tmp = __tripTypeConverter.fromUUID(value.uuid);
if (_tmp == null) {
stmt.bindNull(1);
} else {
stmt.bindString(1, _tmp);
}
if (value.title == null) {
stmt.bindNull(2);
} else {
stmt.bindString(2, value.title);
}
if (value.destination == null) {
stmt.bindNull(3);
} else {
stmt.bindString(3, value.destination);
}
if (value.date == null) {
stmt.bindNull(4);
} else {
stmt.bindString(4, value.date);
}
if (value.duration == null) {
stmt.bindNull(5);
} else {
stmt.bindString(5, value.duration);
}
final String _tmp_1;
_tmp_1 = __tripTypeConverter.fromUUID(value.uuid);
if (_tmp_1 == null) {
stmt.bindNull(6);
} else {
stmt.bindString(6, _tmp_1);
}
}
};
this.__preparedStmtOfDeleteTrips = new SharedSQLiteStatement(__db) {
#Override
public String createQuery() {
final String _query = "DELETE FROM trip";
return _query;
}
};
}
#Override
public Runnable insertTrip(final Trip trip) {
__db.assertNotSuspendingTransaction();
}
#Override
public Runnable delete(final Trip trip) {
__db.assertNotSuspendingTransaction();
}
#Override
public Runnable updateTrip(final Trip trip) {
__db.assertNotSuspendingTransaction();
}
#Override
public void deleteTrips() {
__db.assertNotSuspendingTransaction();
final SupportSQLiteStatement _stmt = __preparedStmtOfDeleteTrips.acquire();
__db.beginTransaction();
try {
_stmt.executeUpdateDelete();
__db.setTransactionSuccessful();
} finally {
__db.endTransaction();
__preparedStmtOfDeleteTrips.release(_stmt);
}
}
#Override
public LiveData<List<Trip>> getAll() {
final String _sql = "SELECT * FROM trip";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
return __db.getInvalidationTracker().createLiveData(new String[]{"trip"}, false, new Callable<List<Trip>>() {
#Override
public List<Trip> call() throws Exception {
final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
try {
final int _cursorIndexOfUuid = CursorUtil.getColumnIndexOrThrow(_cursor, "uuid");
final int _cursorIndexOfTitle = CursorUtil.getColumnIndexOrThrow(_cursor, "title");
final int _cursorIndexOfDestination = CursorUtil.getColumnIndexOrThrow(_cursor, "destination");
final int _cursorIndexOfDate = CursorUtil.getColumnIndexOrThrow(_cursor, "date");
final int _cursorIndexOfDuration = CursorUtil.getColumnIndexOrThrow(_cursor, "duration");
final List<Trip> _result = new ArrayList<Trip>(_cursor.getCount());
while(_cursor.moveToNext()) {
final Trip _item;
_item = new Trip();
final String _tmp;
if (_cursor.isNull(_cursorIndexOfUuid)) {
_tmp = null;
} else {
_tmp = _cursor.getString(_cursorIndexOfUuid);
}
_item.uuid = __tripTypeConverter.toUUID(_tmp);
if (_cursor.isNull(_cursorIndexOfTitle)) {
_item.title = null;
} else {
_item.title = _cursor.getString(_cursorIndexOfTitle);
}
if (_cursor.isNull(_cursorIndexOfDestination)) {
_item.destination = null;
} else {
_item.destination = _cursor.getString(_cursorIndexOfDestination);
}
if (_cursor.isNull(_cursorIndexOfDate)) {
_item.date = null;
} else {
_item.date = _cursor.getString(_cursorIndexOfDate);
}
if (_cursor.isNull(_cursorIndexOfDuration)) {
_item.duration = null;
} else {
_item.duration = _cursor.getString(_cursorIndexOfDuration);
}
_result.add(_item);
}
return _result;
} finally {
_cursor.close();
}
}
#Override
protected void finalize() {
_statement.release();
}
});
}
#Override
public LiveData<Trip> getTrip(final UUID id) {
final String _sql = "SELECT * FROM trip WHERE uuid=?";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
int _argIndex = 1;
final String _tmp;
_tmp = __tripTypeConverter.fromUUID(id);
if (_tmp == null) {
_statement.bindNull(_argIndex);
} else {
_statement.bindString(_argIndex, _tmp);
}
return __db.getInvalidationTracker().createLiveData(new String[]{"trip"}, false, new Callable<Trip>() {
#Override
public Trip call() throws Exception {
final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
try {
final int _cursorIndexOfUuid = CursorUtil.getColumnIndexOrThrow(_cursor, "uuid");
final int _cursorIndexOfTitle = CursorUtil.getColumnIndexOrThrow(_cursor, "title");
final int _cursorIndexOfDestination = CursorUtil.getColumnIndexOrThrow(_cursor, "destination");
final int _cursorIndexOfDate = CursorUtil.getColumnIndexOrThrow(_cursor, "date");
final int _cursorIndexOfDuration = CursorUtil.getColumnIndexOrThrow(_cursor, "duration");
final Trip _result;
if(_cursor.moveToFirst()) {
_result = new Trip();
final String _tmp_1;
if (_cursor.isNull(_cursorIndexOfUuid)) {
_tmp_1 = null;
} else {
_tmp_1 = _cursor.getString(_cursorIndexOfUuid);
}
_result.uuid = __tripTypeConverter.toUUID(_tmp_1);
if (_cursor.isNull(_cursorIndexOfTitle)) {
_result.title = null;
} else {
_result.title = _cursor.getString(_cursorIndexOfTitle);
}
if (_cursor.isNull(_cursorIndexOfDestination)) {
_result.destination = null;
} else {
_result.destination = _cursor.getString(_cursorIndexOfDestination);
}
if (_cursor.isNull(_cursorIndexOfDate)) {
_result.date = null;
} else {
_result.date = _cursor.getString(_cursorIndexOfDate);
}
if (_cursor.isNull(_cursorIndexOfDuration)) {
_result.duration = null;
} else {
_result.duration = _cursor.getString(_cursorIndexOfDuration);
}
} else {
_result = null;
}
return _result;
} finally {
_cursor.close();
}
}
#Override
protected void finalize() {
_statement.release();
}
});
}
public static List<Class<?>> getRequiredConverters() {
return Collections.emptyList();
}
}
I've been told I need to use Executor for INSERT, UPDATE and DELETE so they don't get block the main thread, so I'm not sure how to fix this without just removing the executors.
As far as I can figure, there's no actual Runnable object to return, so it doesn't know what to do? If I've got that wrong please correct me.
But yeah, I just can't figure out how to get around the executor wanting a Runnable return type, but there being no Runnable to return. Any help would be appreciated
You are trying to tell room to return a Runnable from the insert. Room knows that an #Insert can only return void or Long/long (the rowid of the inserted row).
if the #Insert inserts multiple rows then it can return void, long[] or Long[].
if you were look at the Build log, then the
You can't have :-
#Insert
Runnable insertTrip(Trip trip);
It must be one of the following:-
#Insert
void insertTrip(Trip trip);
or
#Insert
long insertTrip(Trip trip);
or
#Insert
Long insertTrip(Trip trip);
Similar for #Update and #Delete except they return int (the number or affected rows)
To get/use the Runnable you could use :-
void insertTrip(Trip trip){
executor.execute(new Runnable() {
#Override
public void run() {
tripDao.insertTrip(trip);
}
});
}
obviously likewise for the others.
Working example
Using (note with a simple Trip class but all other classes as per your question bar the changes above) :-
TripRepository.initialise(this);
Trip atrip = new Trip();
atrip.tripName = "My Trip";
TripRepository.get().insertTrip(atrip);
Results in :-

Global combine not producing output Apache Beam

I am trying to write an unbounded ping pipeline that takes output from a ping command and parses it to determine some statistics about the RTT (avg/min/max) and for now, just print the results.
I have already written an unbounded ping source that outputs each line as it comes in. The results are windowed every second for every 5 seconds of pings. The windowed data is fed to a Combine.globally call to statefully process the string outputs. The problem is that the accumulators are never merged and the output is never extracted. This means that the pipeline never continues past this point. What am I doing wrong here?
public class TestPingIPs {
public static void main(String[] args)
{
PipelineOptions options = PipelineOptionsFactory.create();
Pipeline pipeline = Pipeline.create(options);
String destination = "8.8.8.8";
PCollection<PingResult> res =
/*
Run the unbounded ping command. Only the lines where the result of the ping command are returned.
No statistics or first startup lines are returned here.
*/
pipeline.apply("Ping command",
PingCmd.read()
.withPingArguments(PingCmd.PingArguments.create(destination, -1)))
/*
Window the ping command strings into 5 second sliding windows produced every 1 second
*/
.apply("Window strings",
Window.into(SlidingWindows.of(Duration.standardSeconds(5))
.every(Duration.standardSeconds(1))))
/*
Parse and aggregate the strings into a PingResult object using stateful processing.
*/
.apply("Combine the pings",
Combine.globally(new ProcessPings()).withoutDefaults())
/*
Test our output to see what we get here
*/
.apply("Test output",
ParDo.of(new DoFn<PingResult, PingResult>() {
#ProcessElement
public void processElement(ProcessContext c)
{
System.out.println(c.element().getAvgRTT());
System.out.println(c.element().getPacketLoss());
c.output(c.element());
}
}));
pipeline.run().waitUntilFinish();
}
static class ProcessPings extends Combine.CombineFn<String, RttStats, PingResult> {
private long getRTTFromLine(String line){
long rtt = Long.parseLong(line.split("time=")[1].split("ms")[0]);
return rtt;
}
#Override
public RttStats createAccumulator()
{
return new RttStats();
}
#Override
public RttStats addInput(RttStats mutableAccumulator, String input)
{
mutableAccumulator.incTotal();
if (input.contains("unreachable")) {
_unreachableCount.inc();
mutableAccumulator.incPacketLoss();
}
else if (input.contains("General failure")) {
_transmitFailureCount.inc();
mutableAccumulator.incPacketLoss();
}
else if (input.contains("timed out")) {
_timeoutCount.inc();
mutableAccumulator.incPacketLoss();
}
else if (input.contains("could not find")) {
_unknownHostCount.inc();
mutableAccumulator.incPacketLoss();
}
else {
_successfulCount.inc();
mutableAccumulator.add(getRTTFromLine(input));
}
return mutableAccumulator;
}
#Override
public RttStats mergeAccumulators(Iterable<RttStats> accumulators)
{
Iterator<RttStats> iter = accumulators.iterator();
if (!iter.hasNext()){
return createAccumulator();
}
RttStats running = iter.next();
while (iter.hasNext()){
RttStats next = iter.next();
running.addAll(next.getVals());
running.addLostPackets(next.getLostPackets());
}
return running;
}
#Override
public PingResult extractOutput(RttStats stats)
{
stats.calculate();
boolean connected = stats.getPacketLoss() != 1;
return new PingResult(connected, stats.getAvg(), stats.getMin(), stats.getMax(), stats.getPacketLoss());
}
private final Counter _successfulCount = Metrics.counter(ProcessPings.class, "Successful pings");
private final Counter _unknownHostCount = Metrics.counter(ProcessPings.class, "Unknown hosts");
private final Counter _transmitFailureCount = Metrics.counter(ProcessPings.class, "Transmit failures");
private final Counter _timeoutCount = Metrics.counter(ProcessPings.class, "Timeouts");
private final Counter _unreachableCount = Metrics.counter(ProcessPings.class, "Unreachable host");
}
I would guess that there are some issues with the CombineFn that I wrote, but I can't seem to figure out what's going wrong here! I tried following the example here, but there's still something I must be missing.
EDIT: I added the ping command implementation below. This is running on a Direct Runner while I test.
PingCmd.java:
public class PingCmd {
public static Read read(){
if (System.getProperty("os.name").startsWith("Windows")) {
return WindowsPingCmd.read();
}
else{
return null;
}
}
WindowsPingCmd.java:
public class WindowsPingCmd extends PingCmd {
private WindowsPingCmd()
{
}
public static PingCmd.Read read()
{
return new WindowsRead.Builder().build();
}
static class PingCheckpointMark implements UnboundedSource.CheckpointMark, Serializable {
#VisibleForTesting
Instant oldestMessageTimestamp = Instant.now();
#VisibleForTesting
transient List<String> outputs = new ArrayList<>();
public PingCheckpointMark()
{
}
public void add(String message, Instant timestamp)
{
if (timestamp.isBefore(oldestMessageTimestamp)) {
oldestMessageTimestamp = timestamp;
}
outputs.add(message);
}
#Override
public void finalizeCheckpoint()
{
oldestMessageTimestamp = Instant.now();
outputs.clear();
}
// set an empty list to messages when deserialize
private void readObject(java.io.ObjectInputStream stream)
throws IOException, ClassNotFoundException
{
stream.defaultReadObject();
outputs = new ArrayList<>();
}
#Override
public boolean equals(#Nullable Object other)
{
if (other instanceof PingCheckpointMark) {
PingCheckpointMark that = (PingCheckpointMark) other;
return Objects.equals(this.oldestMessageTimestamp, that.oldestMessageTimestamp)
&& Objects.deepEquals(this.outputs, that.outputs);
}
else {
return false;
}
}
}
#VisibleForTesting
static class UnboundedPingSource extends UnboundedSource<String, PingCheckpointMark> {
private final WindowsRead spec;
public UnboundedPingSource(WindowsRead spec)
{
this.spec = spec;
}
#Override
public UnboundedReader<String> createReader(
PipelineOptions options, PingCheckpointMark checkpointMark)
{
return new UnboundedPingReader(this, checkpointMark);
}
#Override
public List<UnboundedPingSource> split(int desiredNumSplits, PipelineOptions options)
{
// Don't really need to ever split the ping source, so we should just have one per destination
return Collections.singletonList(new UnboundedPingSource(spec));
}
#Override
public void populateDisplayData(DisplayData.Builder builder)
{
spec.populateDisplayData(builder);
}
#Override
public Coder<PingCheckpointMark> getCheckpointMarkCoder()
{
return SerializableCoder.of(PingCheckpointMark.class);
}
#Override
public Coder<String> getOutputCoder()
{
return StringUtf8Coder.of();
}
}
#VisibleForTesting
static class UnboundedPingReader extends UnboundedSource.UnboundedReader<String> {
private final UnboundedPingSource source;
private String current;
private Instant currentTimestamp;
private final PingCheckpointMark checkpointMark;
private BufferedReader processOutput;
private Process process;
private boolean finishedPings;
private int maxCount = 5;
private static AtomicInteger currCount = new AtomicInteger(0);
public UnboundedPingReader(UnboundedPingSource source, PingCheckpointMark checkpointMark)
{
this.finishedPings = false;
this.source = source;
this.current = null;
if (checkpointMark != null) {
this.checkpointMark = checkpointMark;
}
else {
this.checkpointMark = new PingCheckpointMark();
}
}
#Override
public boolean start() throws IOException
{
WindowsRead spec = source.spec;
String cmd = createCommand(spec.pingConfiguration().getPingCount(), spec.pingConfiguration().getDestination());
try {
ProcessBuilder builder = new ProcessBuilder(cmd.split(" "));
builder.redirectErrorStream(true);
process = builder.start();
processOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
return advance();
} catch (Exception e) {
throw new IOException(e);
}
}
private String createCommand(int count, String dest){
StringBuilder builder = new StringBuilder("ping");
String countParam = "";
if (count <= 0){
countParam = "-t";
}
else{
countParam += "-n " + count;
}
return builder.append(" ").append(countParam).append(" ").append(dest).toString();
}
#Override
public boolean advance() throws IOException
{
String line = processOutput.readLine();
// Ignore empty/null lines
if (line == null || line.isEmpty()) {
line = processOutput.readLine();
}
// Ignore the 'Pinging <dest> with 32 bytes of data' line
if (line.contains("Pinging " + source.spec.pingConfiguration().getDestination())) {
line = processOutput.readLine();
}
// If the pings have finished, ignore
if (finishedPings) {
return false;
}
// If this is the start of the statistics, the pings are done and we can just exit
if (line.contains("statistics")) {
finishedPings = true;
}
current = line;
currentTimestamp = Instant.now();
checkpointMark.add(current, currentTimestamp);
if (currCount.incrementAndGet() == maxCount){
currCount.set(0);
return false;
}
return true;
}
#Override
public void close() throws IOException
{
if (process != null) {
process.destroy();
if (process.isAlive()) {
process.destroyForcibly();
}
}
}
#Override
public Instant getWatermark()
{
return checkpointMark.oldestMessageTimestamp;
}
#Override
public UnboundedSource.CheckpointMark getCheckpointMark()
{
return checkpointMark;
}
#Override
public String getCurrent()
{
if (current == null) {
throw new NoSuchElementException();
}
return current;
}
#Override
public Instant getCurrentTimestamp()
{
if (current == null) {
throw new NoSuchElementException();
}
return currentTimestamp;
}
#Override
public UnboundedPingSource getCurrentSource()
{
return source;
}
}
public static class WindowsRead extends PingCmd.Read {
private final PingArguments pingConfig;
private WindowsRead(PingArguments pingConfig)
{
this.pingConfig = pingConfig;
}
public Builder builder()
{
return new WindowsRead.Builder(this);
}
PingArguments pingConfiguration()
{
return pingConfig;
}
public WindowsRead withPingArguments(PingArguments configuration)
{
checkArgument(configuration != null, "configuration can not be null");
return builder().setPingArguments(configuration).build();
}
#Override
public PCollection<String> expand(PBegin input)
{
org.apache.beam.sdk.io.Read.Unbounded<String> unbounded =
org.apache.beam.sdk.io.Read.from(new UnboundedPingSource(this));
return input.getPipeline().apply(unbounded);
}
#Override
public void populateDisplayData(DisplayData.Builder builder)
{
super.populateDisplayData(builder);
pingConfiguration().populateDisplayData(builder);
}
static class Builder {
private PingArguments config;
Builder()
{
}
private Builder(WindowsRead source)
{
this.config = source.pingConfiguration();
}
WindowsRead.Builder setPingArguments(PingArguments config)
{
this.config = config;
return this;
}
WindowsRead build()
{
return new WindowsRead(this.config);
}
}
#Override
public int hashCode()
{
return Objects.hash(pingConfig);
}
}
One thing I notice in your code is that advance() always returns True. The watermark only advances on bundle completion, and I think it's runner-dependent whether a runner will ever complete a bundle if advance ever never returns False. You could try returning False after a bounded amount of time/number of pings.
You could also consider re-writing this as an SDF.

JUnitResult - get a list of tests executed

I'm using JUnit in a relatively simple workflow
JUnitCore runner = new JUnitCore();
m_junitResult = runner.run(m_junitRequest);
What I want to achieve is once the execution is over to get the list of all tests executed and their descriptions
i did it with the help of Junit's RunListner
while running your Junit suites or individual classes make sure you add the listener to it.
// AutomationTestSuites - contains my all Junit test classes
Class cls= AutomationTestSuites.class;
JUnitCore jUnitCore = new JUnitCore();
CustomRunListener customRunListener = new CustomRunListener();
jUnitCore.addListener(customRunListener);
Request request = Request.aClass(cls);
Result result = jUnitCore.run(request);
below is my listener
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import com.dto.TestSuiteDetails;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.concurrent.TimeUnit;
import java.lang.Exception;
public class CustomRunListener extends RunListener{
private Map<String,TestSuiteDetails> map = new LinkedHashMap<>();
private long testSuiteStartTime;
private long testSuiteElapsedTime;
private int ignoredCount;
public void testRunStarted(Description description) throws Exception {
testSuiteStartTime = System.currentTimeMillis();
}
public void testStarted(Description description) throws Exception {
TestSuiteDetails testSuiteDetails = new TestSuiteDetails();
testSuiteDetails.setTestCaseName(description.getMethodName());
String[] arr = description.getTestClass().getName().split("\\.");
String name = arr[arr.length-1];
testSuiteDetails.setTestClassName(name);
String[] arr1 = name.split("_");
String testSuite = arr1[0];
testSuiteDetails.setTestSuiteNmae(testSuite);
testSuiteDetails.setTestStatus("Passed");
testSuiteDetails.setStartTime(System.currentTimeMillis());
map.put(description.getMethodName(),testSuiteDetails);
}
public void testFinished(Description description) throws Exception {
TestSuiteDetails testSuiteDetails= null;
if(map.containsKey(description.getMethodName())){
testSuiteDetails = map.get(description.getMethodName());
testSuiteDetails.setElaspsedTime(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()-testSuiteDetails.getStartTime()));
}
map.put(description.getMethodName(),testSuiteDetails);
}
public void testRunFinished(org.junit.runner.Result result) throws Exception {
testSuiteElapsedTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()-testSuiteStartTime);
}
public void testFailure(Failure failure) throws Exception {
TestSuiteDetails testSuiteDetails= null;
if(map.containsKey(failure.getDescription().getMethodName())){
testSuiteDetails = map.get(failure.getDescription().getMethodName());
}else{
testSuiteDetails = new TestSuiteDetails();
}
testSuiteDetails.setTestCaseName(failure.getDescription().getMethodName());
testSuiteDetails.setTestDescription(failure.getException().toString());
testSuiteDetails.setTestStatus("Failed");
map.put(failure.getDescription().getMethodName(),testSuiteDetails);
}
public void testIgnored(Description description) throws Exception {
TestSuiteDetails testSuiteDetails= null;
if(map.containsKey(description.getMethodName())){
testSuiteDetails = map.get(description.getMethodName());
}else{
testSuiteDetails = new TestSuiteDetails();
testSuiteDetails.setTestCaseName(description.getMethodName());
String[] arr = description.getTestClass().getName().split("\\.");
String name = arr[arr.length-1];
testSuiteDetails.setTestClassName(name);
String[] arr1 = name.split("_");
String testSuite = arr1[0];
testSuiteDetails.setTestSuiteNmae(testSuite);
ignoredCount++;
}
testSuiteDetails.setTestStatus("Ignored");
map.put(description.getMethodName(),testSuiteDetails);
}
public int getIgnoredCount() {
return ignoredCount;
}
public void setIgnoredCount(int ignoredCount) {
this.ignoredCount = ignoredCount;
}
public Map<String, TestSuiteDetails> getMap() {
return map;
}
public long getTestSuiteStartTime() {
return testSuiteStartTime;
}
public long getTestSuiteElapsedTime() {
return testSuiteElapsedTime;
}
}
am using this pojo for holding test suite details
public class TestSuiteDetails {
private String testClassName;
private String testSuiteNmae;
private String testCaseName;
private String testStatus;
private String testDescription;
private long startTime;
private long elaspsedTime;
public String getTestClassName() {
return testClassName;
}
public void setTestClassName(String testClassName) {
this.testClassName = testClassName;
}
public String getTestCaseName() {
return testCaseName;
}
public void setTestCaseName(String testCaseName) {
this.testCaseName = testCaseName;
}
public String getTestStatus() {
return testStatus;
}
public void setTestStatus(String testStatus) {
this.testStatus = testStatus;
}
public String getTestDescription() {
return testDescription;
}
public void setTestDescription(String testDescription) {
this.testDescription = testDescription;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public long getElaspsedTime() {
return elaspsedTime;
}
public void setElaspsedTime(long elaspsedTime) {
this.elaspsedTime = elaspsedTime;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestSuiteDetails that = (TestSuiteDetails) o;
if (testClassName != null ? !testClassName.equals(that.testClassName) : that.testClassName != null)
return false;
return testCaseName != null ? testCaseName.equals(that.testCaseName) : that.testCaseName == null;
}
#Override
public int hashCode() {
int result = testClassName != null ? testClassName.hashCode() : 0;
result = 31 * result + (testCaseName != null ? testCaseName.hashCode() : 0);
return result;
}
public String getTestSuiteNmae() {
return testSuiteNmae;
}
public void setTestSuiteNmae(String testSuiteNmae) {
this.testSuiteNmae = testSuiteNmae;
}
}
now after adding the listener - you can simple utilize it later
HashMap<String, TestSuiteDetails> myTestResultMap = customRunListener.getMap();
You can use org.junit.runner.Resultto get the count of successful test runs.
From doc
A Result collects and summarizes information from running multiple tests. Since tests are expected to run correctly, successful tests are only noted in the count of tests that ran.
If there are any test failures then you can get description for failures using org.junit.runner.notification.Failure
From doc
A Failure holds a description of the failed test and the exception that was thrown while running it. In most cases the Description will be of a single test. However, if problems are encountered while constructing the test (for example, if a BeforeClass method is not static), it may describe something other than a single test.
Example code
// From http://www.tutorialspoint.com/
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.getRunCount());
}
}

Java implementing comparator on ArrayList

I'm quite new to Java so this is probably pretty straight forward question.
I want to sort an ArrayList in the class MediaLib based on the natural order of a specified key.
I can't work out how to use my comparator (compareTo(MediaInterface, key)) which is in the Media class. Whats the best way to go about this?
package assign1;
import java.util.*;
public class Media implements MediaInterface {
private Map<String, Object> fields;
private static int compare;
public Media(String title, String format) {
fields = new TreeMap<String, Object>();
fields.put("title", title);
fields.put("format", format);
}
public Object get(String key) {
return fields.get(key);
}
public void put(String key, Object value) {
fields.put(key, value);
}
public boolean hasKeywords(String[] words, boolean combineWithAND) {
Collection<Object> values = (Collection<Object>) fields.values();
int count = 0;
int size = 0;
for (String s: words) {
for (Object o: values) {
String t = o.toString();
if (t.indexOf(s) >= 0) {
count++;
break;
}
}
size++;
}
if ((count == 0 && !combineWithAND) || (combineWithAND && (count != size))) {
return false;
}
return true;
}
public int compareTo(MediaInterface mi, String key) { //<<<<<<<------calling this!!
if (mi == null)
throw new NullPointerException();
Media m = (Media) mi;
Comparable mValue = (Comparable) m.get(key);
Comparable lValue = (Comparable) fields.get(key);
if ((mValue == null) && (lValue == null)){
return 0;
}
if ((lValue == null)){
return 1;
}
if ((mValue == null)){
return -1;
}
return (lValue).compareTo(mValue);
}
#Override
public int compareTo(MediaInterface mi) {
if (mi == null)
throw new NullPointerException();
Media m = (Media) mi;
Set<String> lSet = fields.keySet();
if (compareTo(m, "title") != 0) {
return compareTo(m, "title");
}
if (compareTo(m, "year") != 0) {
return compareTo(m, "year");
}
for (String s: lSet) {
if (compareTo(m, s) != 0) {
return compareTo(m, s);
}
}
return 0;
}
public boolean equals(Object object) {
if (object == null)
return false;
if (!(object instanceof Media))
return false;
Media m = (Media) object;
if (compareTo(m) != 0) {
return false;
}
return true;
}
}
package assign1;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
public class MediaLib implements Searchable {
private ArrayList<MediaInterface> media;
public MediaLib() {
media = new ArrayList<MediaInterface>();
}
#Override
public void add(MediaInterface mi) {
if (media.isEmpty()) {
media.add(mi);
}
else {
for (MediaInterface m: media) {
if (mi.equals(m)) {
return;
}
}
media.add(mi);
}
}
#Override
public boolean contains(MediaInterface mi) {
for (MediaInterface m: media) {
if (mi.equals(m)) {
return true;
}
}
return false;
}
#Override
public Collection<MediaInterface> findByKeyword(String[] words, boolean combineWithAND) {
Collection<MediaInterface> foundList = new ArrayList<MediaInterface>();
for (MediaInterface mi: media) {
if (mi.hasKeywords(words, combineWithAND)) {
foundList.add(mi);
}
}
return foundList;
}
#Override
public Collection<MediaInterface> findByTitle(String str) {
Collection<MediaInterface> foundList = new ArrayList<MediaInterface>();
for (MediaInterface mi: media) {
if ((mi.get("title")).equals(str)) {
foundList.add(mi);
}
}
return foundList;
}
#Override
public Collection<MediaInterface> getAllWithFormat(String formatName) {
Collection<MediaInterface> foundList = new ArrayList<MediaInterface>();
for (MediaInterface mi: media) {
if ((mi.get("format")).equals(formatName)) {
foundList.add(mi);
}
}
return foundList;
}
public Collection<MediaInterface> getAll() {
Collection<MediaInterface> fullList = new ArrayList<MediaInterface>();
for (MediaInterface mi: media) {
fullList.add(mi);
}
return fullList;
}
#Override
public void removeAllWithKeyword(String[] words, boolean combineWithAND) {
Collection<MediaInterface> foundList = findByKeyword(words, combineWithAND);
for (MediaInterface mi: foundList) {
media.remove(mi);
}
}
#Override
public void removeAllWithFormat(String format) {
Collection<MediaInterface> foundList = getAllWithFormat(format);
for (MediaInterface mi: foundList) {
media.remove(mi);
}
}
#Override
public void sort() {
Collections.sort(media);
}
#Override
public void sort(final String fieldName) {
Collections.sort(media, new Media.compareTo(MediaInterface, fieldName)) //<<<<<--------Trying to call compareTo()
}
}
public void parse(java.io.BufferedReader br) throws java.io.IOException {
while(br.readLine()!= null) {
Media mi = new Media(/n br.readLine(), br.readLine());
while
}
}
}
You already implement the Comparable interface in your MediaInterface class, this is a generic interface, so you then implement Comparable<MediaInterface> which will then require you to implement a method with the signature
public int compareTo(final MediaInterface other)
This is why your call to Collections.sort(media); compiles
In order to sort by a specific field name, you need to provide an instance of a Comparator, the easiest way to do this will be to create an inner class in your Media class which you can then pass into Collections.sort. For example
public class Media implements MediaInterface {
public static final class FieldComparator implements Comparator<Media> {
private final String field;
public FieldComparator(final String field) {
this.field = field;
}
public int compare(final Media a, final Media b) {
// implementation to compare a.field to b.field
}
}
}
You can then rewrite your second sort method as
#Override
public void sort(final String fieldName) {
Collections.sort(media, new Media.FieldComparator(fieldName));
}

Categories

Resources