IDEMPIERE-645 Fitnesse Recorder

This commit is contained in:
Juliana Corredor 2013-03-11 16:39:58 -05:00
parent ed47ed7118
commit 722537e0f5
1 changed files with 184 additions and 125 deletions

View File

@ -16,9 +16,12 @@
*****************************************************************************/ *****************************************************************************/
package org.idempiere.fitrecorder; package org.idempiere.fitrecorder;
import groovy.swing.factory.WidgetFactory;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -28,14 +31,18 @@ import java.sql.Timestamp;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.logging.Level; import java.util.logging.Level;
import org.codehaus.groovy.control.messages.Message;
import org.compiere.model.MClient; import org.compiere.model.MClient;
import org.compiere.model.MColumn; import org.compiere.model.MColumn;
import org.compiere.model.MPInstance; import org.compiere.model.MPInstance;
import org.compiere.model.MPInstancePara; import org.compiere.model.MPInstancePara;
import org.compiere.model.MProcess; import org.compiere.model.MProcess;
import org.compiere.model.MProcessPara; import org.compiere.model.MProcessPara;
import org.compiere.model.MSearchDefinition;
import org.compiere.model.MSession;
import org.compiere.model.MSysConfig; import org.compiere.model.MSysConfig;
import org.compiere.model.MTable; import org.compiere.model.MTable;
import org.compiere.model.MUser; import org.compiere.model.MUser;
@ -47,11 +54,13 @@ import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
import org.compiere.util.DisplayType; import org.compiere.util.DisplayType;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Ini;
public class FitRecorder implements ModelValidator { public class FitRecorder implements ModelValidator {
private static FileOutputStream tempFile = null; private static FileOutputStream FileOr = null;
private static Writer writer; private static Writer writer;
private HashMap<Integer, Writer> files;
/** Logger */ /** Logger */
private static CLogger log = CLogger.getCLogger(FitRecorder.class); private static CLogger log = CLogger.getCLogger(FitRecorder.class);
@ -103,13 +112,9 @@ public class FitRecorder implements ModelValidator {
@Override @Override
public void initialize(ModelValidationEngine engine, MClient client) { public void initialize(ModelValidationEngine engine, MClient client) {
SimpleDateFormat format = new SimpleDateFormat ("yyyyMMddHHmmss"); files=new HashMap<Integer, Writer>();
String dateTimeText = format.format(new Timestamp(System.currentTimeMillis()));
try { try {
File fileNameOr = File.createTempFile("fit_test_" + dateTimeText + "_", ".txt");
tempFile = new FileOutputStream(fileNameOr, true);
writer = new BufferedWriter(new OutputStreamWriter(tempFile, "UTF8"));
String sql = "SELECT ta.TableName" String sql = "SELECT ta.TableName"
+" FROM AD_Table ta" +" FROM AD_Table ta"
@ -164,53 +169,6 @@ public class FitRecorder implements ModelValidator {
@Override @Override
public String login(int AD_Org_ID, int AD_Role_ID, int AD_User_ID) { public String login(int AD_Org_ID, int AD_Role_ID, int AD_User_ID) {
try {
writer.append("\n");
writer.append("\n");
writer.append("LOGIN");
writer.append("\n");
writer.append("!");
writer.append("|Login|");
writer.append("\n");
MUser user = MUser.get(Env.getCtx(), AD_User_ID);
writer.append("|User|");
if (MSysConfig.getBooleanValue(MSysConfig.USE_EMAIL_FOR_LOGIN, false))
writer.append(user.getEMail() + "|");
else
if (user.getLDAPUser() != null)
writer.append(user.getLDAPUser() + "|");
else
writer.append(user.getName() + "|");
writer.append("\n");
writer.append("|Password|");
writer.append(user.getPassword() + "|");
writer.append("\n");
writer.append("|AD_Client_ID|");
MClient client = MClient.get(Env.getCtx(), Env.getContextAsInt(Env.getCtx(), "#AD_Client_ID"));
writer.append("@Ref=AD_Client[Name='" + client.getName() + "'].AD_Client_ID|");
writer.append("\n");
writer.append("|AD_Org_ID|");
String orgName = DB.getSQLValueString(null, "SELECT Name FROM AD_Org WHERE AD_Org_ID=?", AD_Org_ID);
writer.append("@Ref=AD_Org[Name='" + orgName + "'].AD_Org_ID|");
writer.append("\n");
writer.append("|AD_Role_ID|");
writer.append("@Ref=AD_Role[Name='" + Env.getContext(Env.getCtx(), "#AD_Role_Name") + "'].AD_Role_ID|");
writer.append("\n");
int warehouseid = Env.getContextAsInt(Env.getCtx(), Env.M_WAREHOUSE_ID);
if (warehouseid > 0) {
MWarehouse warehouse = MWarehouse.get(Env.getCtx(), warehouseid);
writer.append("|M_Warehouse_ID|");
writer.append("@Ref=M_Warehouse[Name='" + warehouse.getName() + "'].M_Warehouse_ID|");
writer.append("\n");
}
writer.append("|*Login*|");
writer.append("\n");
writer.flush();
} catch (Exception e) {
return e.getLocalizedMessage();
}
return null; return null;
} }
@ -219,6 +177,10 @@ public class FitRecorder implements ModelValidator {
try { try {
if (type == TYPE_AFTER_NEW ) { if (type == TYPE_AFTER_NEW ) {
if(po instanceof MSession){
loginFixture(po);
}
if (dontLogTables.contains(po.get_TableName().toUpperCase())) if (dontLogTables.contains(po.get_TableName().toUpperCase()))
return null; return null;
@ -230,15 +192,15 @@ public class FitRecorder implements ModelValidator {
if (po.get_TrxName().startsWith("WFP_")) if (po.get_TrxName().startsWith("WFP_"))
return null; return null;
writer.append("\n"); writeFile("\n");
writer.append("\n"); writeFile("\n");
writer.append("CREATE RECORD"); writeFile("CREATE RECORD");
writer.append("\n"); writeFile("\n");
writer.append("!"); writeFile("!");
writer.append("|Create Record||"); writeFile("|Create Record||");
writer.append("\n"); writeFile("\n");
writer.append("|*Table*|"); writeFile("|*Table*|");
writer.append(po.get_TableName() + "|"); writeFile(po.get_TableName() + "|");
MTable table = MTable.get(Env.getCtx(),po.get_Table_ID()); MTable table = MTable.get(Env.getCtx(),po.get_Table_ID());
for(MColumn column : table.getColumns(false)) { for(MColumn column : table.getColumns(false)) {
@ -260,19 +222,18 @@ public class FitRecorder implements ModelValidator {
String value = po.get_ValueAsString(colName); String value = po.get_ValueAsString(colName);
if (value != null && value.length() > 0) { if (value != null && value.length() > 0) {
writer.append("\n"); writeFile("\n");
writer.append("|" + colName + "|"); writeFile("|" + colName + "|");
if (DisplayType.isLookup(column.getAD_Reference_ID()) && DisplayType.List != column.getAD_Reference_ID()) { if (DisplayType.isLookup(column.getAD_Reference_ID()) && DisplayType.List != column.getAD_Reference_ID()) {
writer.append(resolveValue(po, table, column) + "|"); writeFile(resolveValue(po, table, column) + "|");
} else { } else {
writer.append(value +"|"); writeFile(value +"|");
} }
} }
} }
writer.append("\n"); writeFile("\n");
writer.append("|*Save*|"); writeFile("|*Save*|");
writer.append("\n"); writeFile("\n");
writer.flush();
} }
@ -281,15 +242,15 @@ public class FitRecorder implements ModelValidator {
MProcess pro = MProcess.get(Env.getCtx(), po.get_ValueAsInt("AD_Process_ID")); MProcess pro = MProcess.get(Env.getCtx(), po.get_ValueAsInt("AD_Process_ID"));
MPInstance pint = (MPInstance)po; MPInstance pint = (MPInstance)po;
writer.append("\n"); writeFile("\n");
writer.append("\n"); writeFile("\n");
writer.append("RUN PROCESS"); writeFile("RUN PROCESS");
writer.append("\n"); writeFile("\n");
writer.append("!"); writeFile("!");
writer.append("|Run Process|"); writeFile("|Run Process|");
writer.append("\n"); writeFile("\n");
writer.append("|*ProcessValue*|"); writeFile("|*ProcessValue*|");
writer.append(pro.getValue() + "|"); writeFile(pro.getValue() + "|");
MPInstancePara[] iparas = pint.getParameters(); MPInstancePara[] iparas = pint.getParameters();
for (MProcessPara para : pro.getParameters()) { for (MProcessPara para : pro.getParameters()) {
MPInstancePara ipara = null; MPInstancePara ipara = null;
@ -327,23 +288,34 @@ public class FitRecorder implements ModelValidator {
valueTo = ipara.getP_String_To(); valueTo = ipara.getP_String_To();
} }
if (value != null) { if (value != null) {
writer.append("\n"); writeFile("\n");
writer.append("|" + para.getColumnName() + "|"); writeFile("|" + para.getColumnName() + "|");
writer.append(value + "|"); writeFile(value + "|");
} }
if (para.isRange() && valueTo != null) { if (para.isRange() && valueTo != null) {
writer.append("\n"); writeFile("\n");
writer.append("|" + para.getColumnName() + "_2|"); writeFile("|" + para.getColumnName() + "_2|");
writer.append(valueTo + "|"); writeFile(valueTo + "|");
} }
} }
writer.append("\n"); writeFile("\n");
writer.append("|*Run*|"); writeFile("|*Run*|");
writer.append("\n"); writeFile("\n");
writer.flush();
} }
} }
if (po instanceof MSession){
if (type == TYPE_AFTER_CHANGE) {
MSession session =(MSession) po;
if(session.isProcessed()){
String msg=closefile();
if (msg != null){
return msg;
}
}
}
}
}catch (Exception e) { }catch (Exception e) {
return e.getLocalizedMessage(); return e.getLocalizedMessage();
} }
@ -435,47 +407,46 @@ public class FitRecorder implements ModelValidator {
if (timing == TIMING_BEFORE_POST) if (timing == TIMING_BEFORE_POST)
action = "PO"; action = "PO";
writer.append("\n"); writeFile("\n");
writer.append("\n"); writeFile("\n");
if (action.equals("CO")) { if (action.equals("CO")) {
// run process // run process
String processValue = DB.getSQLValueString(po.get_TrxName(), String processValue = DB.getSQLValueString(po.get_TrxName(),
"SELECT p.Value FROM AD_Process p JOIN AD_Workflow w ON (p.AD_Workflow_ID=w.AD_Workflow_ID) WHERE w.AD_Table_ID=?", po.get_Table_ID()); "SELECT p.Value FROM AD_Process p JOIN AD_Workflow w ON (p.AD_Workflow_ID=w.AD_Workflow_ID) WHERE w.AD_Table_ID=?", po.get_Table_ID());
writer.append("RUN PROCESS"); writeFile("RUN PROCESS");
writer.append("\n"); writeFile("\n");
writer.append("!"); writeFile("!");
writer.append("|Run Process|"); writeFile("|Run Process|");
writer.append("\n"); writeFile("\n");
writer.append("|*ProcessValue*|"); writeFile("|*ProcessValue*|");
writer.append(processValue + "|"); writeFile(processValue + "|");
writer.append("\n"); writeFile("\n");
writer.append("|*RecordID*|"); writeFile("|*RecordID*|");
writer.append(resolveValueDoc(po.get_ID(), po.get_TableName()) + "|"); writeFile(resolveValueDoc(po.get_ID(), po.get_TableName()) + "|");
writer.append("\n"); writeFile("\n");
writer.append("|*DocAction*|"); writeFile("|*DocAction*|");
writer.append(action + "|"); writeFile(action + "|");
writer.append("\n"); writeFile("\n");
writer.append("|*Run*|"); writeFile("|*Run*|");
} else { } else {
// set doc action // set doc action
writer.append("SET DOC ACTION"); writeFile("SET DOC ACTION");
writer.append("\n"); writeFile("\n");
writer.append("!"); writeFile("!");
writer.append("|Set DocAction|"); writeFile("|Set DocAction|");
writer.append("\n"); writeFile("\n");
writer.append("|*Table*|"); writeFile("|*Table*|");
writer.append(po.get_TableName() + "|"); writeFile(po.get_TableName() + "|");
writer.append("\n"); writeFile("\n");
writer.append("|" + po.get_TableName() + "_ID|"); writeFile("|" + po.get_TableName() + "_ID|");
writer.append(resolveValueDoc(po.get_ID(), po.get_TableName()) + "|"); writeFile(resolveValueDoc(po.get_ID(), po.get_TableName()) + "|");
writer.append("\n"); writeFile("\n");
writer.append("|DocAction|"); writeFile("|DocAction|");
writer.append(action + "|"); writeFile(action + "|");
writer.append("\n"); writeFile("\n");
writer.append("|*Save*|"); writeFile("|*Save*|");
} }
writer.append("\n"); writeFile("\n");
writer.flush();
} }
} catch (Exception e) { } catch (Exception e) {
if (log.isLoggable(Level.INFO)) log.info(e.getLocalizedMessage()); if (log.isLoggable(Level.INFO)) log.info(e.getLocalizedMessage());
@ -514,4 +485,92 @@ public class FitRecorder implements ModelValidator {
return value; return value;
} }
public String loginFixture(PO po){
String msg=null;
String preference = Env.getPreference(Env.getCtx(), 0, "FitRecorder", false);
if (preference.equals("Y")) {
int Session_ID = po.get_ID();
int User_ID = Env.getContextAsInt(Env.getCtx(), "#AD_User_ID");
int AD_Org_ID = Env.getContextAsInt(Env.getCtx(), "#AD_Org_ID");
MUser user = new MUser(Env.getCtx(), User_ID, null);
String name = Ini.getAdempiereHome() + "/log/fit_test_" + Session_ID+ user.getName() + ".txt";
File fileNameOr;
try {
fileNameOr = new File(name);
FileOr = new FileOutputStream(fileNameOr, true);
writer = new BufferedWriter(new OutputStreamWriter(FileOr,
"UTF8"));
files.put(Session_ID, writer);
writer.append("\n");
writer.append("\n");
writer.append("LOGIN");
writer.append("\n");
writer.append("!");
writer.append("|Login|");
writer.append("\n");
writer.append("|User|");
if (MSysConfig.getBooleanValue(MSysConfig.USE_EMAIL_FOR_LOGIN,
false))
writer.append(user.getEMail() + "|");
else if (user.getLDAPUser() != null)
writer.append(user.getLDAPUser() + "|");
else
writer.append(user.getName() + "|");
writer.append("\n");
writer.append("|Password|");
writer.append(" |");
writer.append("\n");
writer.append("|AD_Client_ID|");
MClient client = MClient.get(Env.getCtx(),Env.getContextAsInt(Env.getCtx(), "#AD_Client_ID"));
writer.append("@Ref=AD_Client[Name='" + client.getName() + "'].AD_Client_ID|");
writer.append("\n");
writer.append("|AD_Org_ID|");
String orgName = DB.getSQLValueString(null,"SELECT Name FROM AD_Org WHERE AD_Org_ID=?", AD_Org_ID);
writer.append("@Ref=AD_Org[Name='" + orgName + "'].AD_Org_ID|");
writer.append("\n");
writer.append("|AD_Role_ID|");
writer.append("@Ref=AD_Role[Name='"+ Env.getContext(Env.getCtx(), "#AD_Role_Name")+ "'].AD_Role_ID|");
writer.append("\n");
int warehouseid = Env.getContextAsInt(Env.getCtx(),Env.M_WAREHOUSE_ID);
if (warehouseid > 0) {
MWarehouse warehouse = MWarehouse.get(Env.getCtx(),warehouseid);
writer.append("|M_Warehouse_ID|");
writer.append("@Ref=M_Warehouse[Name='" + warehouse.getName()+ "'].M_Warehouse_ID|");
writer.append("\n");
}
writer.append("|*Login*|");
writer.append("\n");
files.put(Session_ID, writer);
} catch (Exception e) {
return e.getLocalizedMessage();
}
}
return msg;
}
public void writeFile(String msg){
int Session_ID=Env.getContextAsInt(Env.getCtx(), "#AD_Session_ID");
Writer writerOr=(Writer) files.get(Session_ID);
try{
writerOr.append(msg);
writerOr.flush();
}catch (Exception e) {
}
}
public String closefile(){
String msg=null;
int Session_ID=Env.getContextAsInt(Env.getCtx(), "#AD_Session_ID");
Writer writerOr=(Writer) files.get(Session_ID);
try {
writerOr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
return e.getLocalizedMessage();
}
return msg;
}
} }