use of com.sun.identity.log.LogQuery in project OpenAM by OpenRock.
the class AMLogTest method readLogQuery.
private void readLogQuery(String fileName, SSOToken ssot, int recsWritten) throws AMLogException {
/*
* can have:
* LogQuery(), which sets:
* maxRecord = LogQuery.MOST_RECENT_MAX_RECORDS
* globalOperand = LogQuery.MATCH_ANY_CONDITION
* queries = null
* sortBy = null
* LogQuery(int max_record), which sets:
* maxRecord = max_record
* globalOperand = LogQuery.MATCH_ANY_CONDITION
* queries = null
* sortBy = null
* LogQuery(int max_record,
* int matchCriteria,
* String sortingBy), which sets:
* maxRecord = max_record
* globalOperand = matchCriteria
* sortBy = sortingBy
* queries = null
*
* use lq.addQuery(QueryElement qryElement) to
* add query elements to the LoqQuery's list of queries
* QueryElement(String fld, String val, int rel)
* fieldName = fld
* fieldValue = val
* relation = rel
* QueryElement()
* fieldName = new String()
* fieldValue = new String()
* relation = QueryElement.EQ
* use:
* QueryElement.setFieldName(String field)
* QueryElement.setFieldValue(String value)
* QueryElement.setRelation(int value)
*/
int totalRecsRead = 0;
/*
* test 1:
* all records, any condition, no sorting
* should be same as read all.
*/
LogQuery lq = new LogQuery(LogQuery.ALL_RECORDS, LogQuery.MATCH_ANY_CONDITION, null);
int numRecs = 0;
String[][] result = new String[1][1];
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != (recsWritten + 2)) {
throw new AMLogException("Number of records read test 1 (" + numRecs + ") doesn't match expected (" + (recsWritten + 1) + ")");
}
/*
* test 2:
* only read 2 most recent records
*/
lq = new LogQuery(2, LogQuery.MATCH_ANY_CONDITION, null);
result = new String[1][1];
numRecs = 0;
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != 3) {
throw new AMLogException("Number of records read test 2 (" + (numRecs - 1) + ") doesn't match expected (2)");
}
// two most recent should be "...data #1" and "...data #0"
String temp = result[1][1];
if (!temp.contains(msgDataPrefixData + "1")) {
throw new AMLogException("Read test 2: second most recent = " + temp + ", not " + msgDataPrefixData + "1");
}
temp = result[2][1];
if (!temp.contains(msgDataPrefixData + "0")) {
throw new AMLogException("Read test 2: most recent = " + temp + ", not " + msgDataPrefixData + "0");
}
/*
* test 3:
* get records that end with "XX2" in the MessageID field.
* should only be one.
*/
lq = new LogQuery(LogQuery.ALL_RECORDS, LogQuery.MATCH_ALL_CONDITIONS, null);
QueryElement qe = new QueryElement("MessageID", "XX2", QueryElement.EW);
lq.addQuery(qe);
result = new String[1][1];
numRecs = 0;
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != 2) {
throw new AMLogException("Number of records read test 3 (" + (numRecs - 1) + ") doesn't match expected (1)");
}
// assume MessageID is in column 8 (starting from 0)
temp = result[1][8];
if (!temp.contains("XX2")) {
throw new AMLogException("Read test 3: record = " + temp + ", not XX2");
}
/*
* test 4:
* get records that contain "data #4", "data #0" or "data #2"
* in the Data field. specify them in that order, with
* sort by Data field.
* msgDataPrefixData = "data #"
*/
lq = new LogQuery(LogQuery.ALL_RECORDS, LogQuery.MATCH_ANY_CONDITION, "Data");
qe = new QueryElement("Data", msgDataPrefixData + "4", QueryElement.CN);
lq.addQuery(qe);
qe = new QueryElement("Data", msgDataPrefixData + "0", QueryElement.CN);
lq.addQuery(qe);
qe = new QueryElement("Data", msgDataPrefixData + "2", QueryElement.CN);
lq.addQuery(qe);
result = new String[1][1];
numRecs = 0;
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != 4) {
throw new AMLogException("Number of records read test 4 (" + (numRecs - 1) + ") doesn't match expected (3)");
}
/*
* gonna expect Data column is in result[x][1]. also
* that with sorting, result[1][1] contains "...data #0",
* result[2][1] contains "...data #2", and result[3][1]
* contains "...data #4".
*/
temp = result[1][1];
if (!temp.contains(msgDataPrefixData + "0")) {
throw new AMLogException("Read test 4: first sorted record = " + temp + ", not " + msgDataPrefixData + "0");
}
temp = result[2][1];
if (!temp.contains(msgDataPrefixData + "2")) {
throw new AMLogException("Read test 4: second sorted record = " + temp + ", not " + msgDataPrefixData + "2");
}
temp = result[3][1];
if (!temp.contains(msgDataPrefixData + "4")) {
throw new AMLogException("Read test 4: third sorted record = " + temp + ", not " + msgDataPrefixData + "4");
}
}
Aggregations