jueves, 25 de junio de 2015

Trazas en el log de webservices


El siguiente código te permitirá imprimir en el log las trazas de entrada y salida de un servicio web:

Si usas JDK 7 en adelante:

1
2
3
4
5
6
static {
       System.setProperty("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump", "true");
       System.setProperty("com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump", "true");
       System.setProperty("com.sun.xml.ws.transport.http.HttpAdapter.dump", "true");
       System.setProperty("com.sun.xml.internal.ws.transport.http.HttpAdapter.dump", "true");
   }

Si es anterior, puedes usar un Handler:

LogMessageSOAPHandler.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.ws.handler;
 
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
 
import javax.xml.namespace.QName;
import javax.xml.soap.MimeHeader;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.ws.util.XMLFormatUtil;
 
public class LogMessageSOAPHandler implements SOAPHandler {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(LogMessageSOAPHandler.class);
 
    private String getMessageEncoding(SOAPMessage msg) {
        String encoding = "UTF-8";
        try {
            if (msg.getProperty(SOAPMessage.CHARACTER_SET_ENCODING) != null) {
                encoding = msg.getProperty(SOAPMessage.CHARACTER_SET_ENCODING).toString();
            }
        } catch (SOAPException e) {
            LOGGER.warn("Error when trying to obtain encoding character from SoapMessage, set {} by default", encoding, e);
        }
        return encoding;
    }
 
    private void logSoapMessage(SOAPMessageContext context) {
        String headerTrace = "[SOAP MESSAGE]:";
        boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outbound) {
            LOGGER.debug("{} Direction=outbound", headerTrace);
        } else {
            LOGGER.debug("{} Direction=inbound", headerTrace);
        }
 
        SOAPMessage msg = context.getMessage();
        if (msg == null) {
            LOGGER.warn("{} Message is null", headerTrace);
            return;
        }
 
        LOGGER.debug("{} Mime Headers: ", headerTrace);
        // infer generytype
        @SuppressWarnings("unchecked")
        Iterator allHeaders = msg.getMimeHeaders().getAllHeaders();
        while (allHeaders.hasNext()) {
            MimeHeader mh = allHeaders.next();
            String mime = new StringBuffer().append("name=").append(mh.getName()).append(", value=").append(mh.getValue()).toString();
            LOGGER.debug(mime);
        }
 
        try {
            LOGGER.debug("{} \n {}", headerTrace, XMLFormatUtil.prettyFormat(msg, 2, getMessageEncoding(msg)));
        } catch (Exception e) {
            LOGGER.warn("Error trying to trace soap message", e);
        }
 
    }
 
    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        logSoapMessage(context);
        return true;
    }
 
    @Override
    public boolean handleFault(SOAPMessageContext context) {
        logSoapMessage(context);
        return true;
    }
 
    @Override
    public void close(MessageContext context) {
        // Do nothing because this handler use to print inboud or outbound
        // message
    }
 
    @Override
    public Set getHeaders() {
        return Collections.emptySet();
    }
}
(PARA CLIENTE)
LogHandlerResolver.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.ws.handler;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;
 
 
public class LogHandlerResolver implements HandlerResolver {
 
 
    @SuppressWarnings("rawtypes")
    @Override
    public List getHandlerChain(PortInfo portInfo) {
        List handlerChain = new ArrayList();
        // Add log web service
        handlerChain.add(new LogMessageSOAPHandler());
        return handlerChain;
    }
 
}

(PARA SERVIDOR)
handler-chain.xml
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <javaee:handler-chain>
  <javaee:handler>
   <!-- log soap message on publisher service -->
   <javaee:handler-class>com.inetpsa.ped.manager.ws.handler.LogMessageSOAPHandler</javaee:handler-class>
  </javaee:handler>
 </javaee:handler-chain>
</javaee:handler-chains>


Codigo Servidor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.ws.ressources;
 
import javax.inject.Inject;
import javax.jws.HandlerChain;
import javax.jws.WebService;
import javax.xml.ws.soap.Addressing;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
@Addressing(enabled = true, required = false)
@WebService(targetNamespace = "http://xml.ws.com/ReferentielFabrication", endpointInterface = "com.gen.resources.DemandeAlerteServicePortType", serviceName = "DemandeAlerteService", portName = "DemandeAlerteServiceHttpPort")
@HandlerChain(file = "handler-chain.xml")
public class DemandeAlerteWebServiceImpl implements
DemandeAlerteServicePortType {
 
private static final Logger LOGGER = LoggerFactory.getLogger(DemandeAlerteWebServiceImpl.class);
 
@Inject
public AlerteService alerteService;
 
@Override
public ReponseAlerteType alerte(DemandeAlerteType demandeale)
throws ServiceException_Exception {
 
}
 
}
Codigo Cliente:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public void callWebService() throws Exception {
 
// call webservice
WebServiceClient webServiceClient = AbonnementCalendrierService2.class.getAnnotation(WebServiceClient.class);
URL wsdlURL = AbonnementCalendrierService2.class.getClassLoader()
.getResource(WSDL_FILE_PATH);
AbonnementCalendrierService2 client = new AbonnementCalendrierService2(
wsdlURL, new QName(webServiceClient.targetNamespace(),
webServiceClient.name()));
 
// Add log xml ws message
client.setHandlerResolver(new PedLogHandlerResolver());
 
// Add WS-Addressing
AddressingFeature addressingfeature = new AddressingFeature();
 
AbonnementCalendrierService2PortType port = client.getAbonnementCalendrierService2HttpPort(addressingfeature);
 
if (IS_HTTP_TRANSPORT) {
 
((BindingProvider) port).getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY, uriHttpService);
 
LOGGER.debug("Sending soap message to {}", uriHttpService);
} else {
 
((BindingProvider) port).getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY, uriJmsService);
 
// Add a port to the Service
client.addPort(new QName(webServiceClient.targetNamespace(),
webServiceClient.name()),
SOAP_JMS_SPECIFICATION_TRANSPORTID, uriJmsService);
 
LOGGER.debug("Sending soap message to {}", uriJmsService);
}
 
NotificationAbonnementType2 notificationAbonnement2 = new NotificationAbonnementType2();
notificationAbonnement2.setDateMAJ("12/01/2001");
port.abonnement(notificationAbonnement2);
 
}

miércoles, 24 de junio de 2015

Consultas SQL para la base de datos Oracle

Consultas SQL útiles para obtener información sobre Oracle Database 


Vista que muestra el estado de la base de datos:
1
select * from v$instance

Consulta que muestra si la base de datos está abierta:
1
select status from v$instance

Vista que muestra los parámetros generales de Oracle:
1
select * from v$system_parameter

Versión de Oracle:
1
2
3
select value
from v$system_parameter
where name = 'compatible'

Ubicación y nombre del fichero spfile:
1
2
3
select value
from v$system_parameter
where name = 'spfile'

Ubicación y número de ficheros de control:
1
2
3
select value
from v$system_parameter
where name = 'control_files'

Nombre de la base de datos
1
2
3
select value
from v$system_parameter
where name = 'db_name'

Vista que muestra las conexiones actuales a Oracle:
1
2
3
select osuser, username, machine, program
  from v$session
  order by osuser

Vista que muestra el número de conexiones actuales a Oracle agrupado por aplicación que realiza la conexión
1
2
3
4
select program Aplicacion, count(program) Numero_Sesiones
from v$session
group by program
order by Numero_Sesiones desc

Vista que muestra los usuarios de Oracle conectados y el número de sesiones por usuario

1
2
3
4
select username Usuario_Oracle, count(username) Numero_Sesiones
from v$session
group by username
order by Numero_Sesiones desc

Propietarios de objetos y número de objetos por propietario
1
2
3
4
select owner, count(owner) Numero
  from dba_objects
  group by owner
  order by Numero desc

Diccionario de datos (incluye todas las vistas y tablas de la Base de Datos):
1
2
3
select * from dictionary
 
select table_name from dictionary

Muestra los datos de una tabla especificada (en este caso todas las tablas que lleven la cadena "EMPLO"):
1
2
3
select *
from ALL_ALL_TABLES
where upper(table_name) like '%EMPLO%'

Muestra los disparadores (triggers) de la base de datos Oracle Database:
1
2
select *
from ALL_TRIGGERS

Tablas propiedad del usuario actual:
1
select * from user_tables

Todos los objetos propiedad del usuario conectado a Oracle:
1
select * from user_catalog


Consulta SQL para el DBA de Oracle que muestra los tablespaces, el espacio utilizado, el espacio libre y los ficheros de datos de los mismos:
1
2
3
4
5
6
7
8
9
10
11
12
Select t.tablespace_name  "Tablespace",  t.status "Estado"
    ROUND(MAX(d.bytes)/1024/1024,2) "MB Tamaño",
    ROUND((MAX(d.bytes)/1024/1024) -
    (SUM(decode(f.bytes, NULL,0, f.bytes))/1024/1024),2) "MB Usados",  
    ROUND(SUM(decode(f.bytes, NULL,0, f.bytes))/1024/1024,2) "MB Libres",
    t.pct_increase "% incremento",
    SUBSTR(d.file_name,1,80) "Fichero de datos" 
FROM DBA_FREE_SPACE f, DBA_DATA_FILES d,  DBA_TABLESPACES t 
WHERE t.tablespace_name = d.tablespace_name  AND
    f.tablespace_name(+) = d.tablespace_name   
    AND f.file_id(+) = d.file_id GROUP BY t.tablespace_name,  
    d.file_name,   t.pct_increase, t.status ORDER BY 1,3 DESC

Productos Oracle instalados y la versión:
1
select * from product_component_version

Roles y privilegios por roles:
1
select * from role_sys_privs

Reglas de integridad y columna a la que afectan:
1
2
select constraint_name, column_name
from sys.all_cons_columns

Tablas de las que es propietario un usuario, en este caso "HR":
1
2
3
SELECT table_owner, table_name
from sys.all_synonyms
where table_owner like 'HR'

Otra forma más efectiva (tablas de las que es propietario un usuario):
1
2
3
4
5
6
7
8
9
10
11
12
SELECT DISTINCT TABLE_NAME
FROM ALL_ALL_TABLES
WHERE OWNER LIKE 'HR'
Parámetros de Oracle, valor actual y su descripción:
SELECT v.name, v.value value, decode(ISSYS_MODIFIABLE, 'DEFERRED',
     'TRUE', 'FALSE') ISSYS_MODIFIABLE,  decode(v.isDefault, 'TRUE', 'YES',
     'FALSE', 'NO') "DEFAULT",  DECODE(ISSES_MODIFIABLE,  'IMMEDIATE'
     'YES','FALSE''NO''DEFERRED', 'NO', 'YES') SES_MODIFIABLE,  
     DECODE(ISSYS_MODIFIABLE, 'IMMEDIATE', 'YES''FALSE', 'NO'
     'DEFERRED', 'YES','YES') SYS_MODIFIABLE ,  v.description 
FROM V$PARAMETER v
WHERE name not like 'nls%'   ORDER BY 1

Usuarios de Oracle y todos sus datos (fecha de creación, estado, id, nombre, tablespace temporal,...):
1
2
3
4
5
6
7
8
Select  * FROM dba_users
Tablespaces y propietarios de los mismos:
select owner, decode(partition_name, null, segment_name,
   segment_name || ':' || partition_name) name,
   segment_type, tablespace_name,bytes,initial_extent,
   next_extent, PCT_INCREASE, extents, max_extents
from dba_segments
Where 1=1 And extents > 1 order by 9 desc, 3

Últimas consultas SQL ejecutadas en Oracle y usuario que las ejecutó:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
select distinct vs.sql_text, vs.sharable_mem,
  vs.persistent_mem, vs.runtime_mem,  vs.sorts,
  vs.executions, vs.parse_calls, vs.module, 
  vs.buffer_gets, vs.disk_reads, vs.version_count,
  vs.users_opening, vs.loads, 
  to_char(to_date(vs.first_load_time,
  'YYYY-MM-DD/HH24:MI:SS'),'MM/DD  HH24:MI:SS') first_load_time, 
  rawtohex(vs.address) address, vs.hash_value hash_value ,
  rows_processed  , vs.command_type, vs.parsing_user_id  ,
  OPTIMIZER_MODE  , au.USERNAME parseuser 
from v$sqlarea vs , all_users au  
where (parsing_user_id != 0)  AND
(au.user_id(+)=vs.parsing_user_id) 
and (executions >= 1) order by   buffer_gets/executions desc

Todos los ficheros de datos y su ubicación:
1
select * from V$DATAFILE

Ficheros temporales:
1
select * from V$TEMPFILE

Tablespaces:
1
select * from V$TABLESPACE

Otras vistas muy interesantes:
1
2
3
4
5
6
7
8
9
10
11
12
13
select * from V$BACKUP
 
select * from V$ARCHIVE  
 
select * from V$LOG  
 
select * from V$LOGFILE   
 
select * from V$LOGHIST         
 
select * from V$ARCHIVED_LOG   
 
select * from V$DATABASE


Memoria Share_Pool libre y usada:
1
2
3
4
5
select name,to_number(value) bytes
from v$parameter where name ='shared_pool_size'
union all
select name,bytes
from v$sgastat where pool = 'shared pool' and name = 'free memory'

Cursores abiertos por usuario:
1
2
3
4
5
6
7
8
9
10
select b.sid, a.username, b.value Cursores_Abiertos
      from v$session a,
           v$sesstat b,
           v$statname c
      where c.name in ('opened cursors current')
      and   b.statistic# = c.statistic#
      and   a.sid = b.sid
      and   a.username is not null
      and   b.value >0
      order by 3

Aciertos de la caché (no debe superar el 1 por ciento):
1
2
3
4
select sum(pins) Ejecuciones, sum(reloads) Fallos_cache,
  trunc(sum(reloads)/sum(pins)*100,2) Porcentaje_aciertos
from v$librarycache
where namespace in ('TABLE/PROCEDURE','SQL AREA','BODY','TRIGGER');

Sentencias SQL completas ejecutadas con un texto determinado en el SQL:
1
2
3
4
5
SELECT c.sid, d.piece, c.serial#, c.username, d.sql_text
FROM v$session c, v$sqltext d
WHERE  c.sql_hash_value = d.hash_value
  and upper(d.sql_text) like '%WHERE CAMPO LIKE%'
ORDER BY c.sid, d.piece

Una sentencia SQL concreta (filtrado por sid):
1
2
3
4
SELECT c.sid, d.piece, c.serial#, c.username, d.sql_text
FROM v$session c, v$sqltext d
WHERE  c.sql_hash_value = d.hash_value and sid = 105
ORDER BY c.sid, d.piece

Tamaño ocupado por la base de datos
1
2
select sum(BYTES)/1024/1024 MB
from DBA_EXTENTS 

Tamaño de los ficheros de datos de la base de datos:
1
2
select sum(bytes)/1024/1024 MB
from dba_data_files

Tamaño ocupado por una tabla concreta sin incluir los índices de la misma
1
2
3
select sum(bytes)/1024/1024 MB
from user_segments
where segment_type='TABLE' and segment_name='NOMBRETABLA'

Tamaño ocupado por una tabla concreta incluyendo los índices de la misma
1
2
3
4
5
6
7
select sum(bytes)/1024/1024 Table_Allocation_MB
from user_segments
where segment_type in ('TABLE','INDEX') and
  (segment_name='NOMBRETABLA' or segment_name in
    (select index_name
     from user_indexes
     where table_name='NOMBRETABLA'))

Tamaño ocupado por una columna de una tabla:
1
2
select sum(vsize('NOMBRECOLUMNA'))/1024/1024 MB
from NOMBRETABLA

Espacio ocupado por usuario:
1
2
3
SELECT owner, SUM(BYTES)/1024/1024
FROM DBA_EXTENTS MB
GROUP BY owner

Espacio ocupado por los diferentes segmentos (tablas, índices, undo, rollback, cluster, ...):
1
2
3
SELECT SEGMENT_TYPE, SUM(BYTES)/1024/1024
FROM DBA_EXTENTS MB
GROUP BY SEGMENT_TYPE

Espacio ocupado por todos los objetos de la base de datos, muestra los objetos que más ocupan primero:
1
2
3
4
SELECT SEGMENT_NAME, SUM(BYTES)/1024/1024
FROM DBA_EXTENTS MB
GROUP BY SEGMENT_NAME
ORDER BY 2 DESC

Obtener todas las funciones de Oracle: NVL, ABS, LTRIM, ...:
1
2
3
4
SELECT distinct object_name
FROM all_arguments
WHERE package_name = 'STANDARD'
order by object_name

Obtener los roles existentes en Oracle Database:
1
select * from DBA_ROLES

Obtener los privilegios otorgados a un rol de Oracle:
1
2
3
select privilege
from dba_sys_privs
where grantee = 'NOMBRE_ROL'

Obtener la IP del servidor de la base de datos Oracle Database:
1
2
select utl_inaddr.get_host_address IP
from dual

Mostrar datos de auditoría de la base de datos Oracle (inicio y desconexión de sesiones):
1
2
select username, action_name, priv_used, returncode
from dba_audit_trail

Comprobar si la auditoría de la base de datos Oracle está activada:
1
2
3
select name, value
from v$parameter
where name like 'audit_trail'

Desbloquear un usuario:
1
alter user username account UNLOCK;

lunes, 15 de junio de 2015

Como agregar un wsee-security y basic-authentication aun webservice con JAX-WS

Después de investigar un problema relacionado con la duplicidad de encabezados en los Handlers al intentar manipular el mensaje SOAP que enviamos mediante un WebService utilizando la libreria JAX-WS, he encontrado al fin una solución que les aliviará los dolores de cabeza con estos errores. 

Seguramente te habrás encontrado la duplicidad de targetnamepsace:

  • S="http://schemas.xmlsoap.org/soap/envelope/" 
  • SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/". 


El posible origen del problema es que estas usando JAX-WS para generar el servicio web (bajo JAX-B) y posteriormente usas SoapHandler (bajo SAAJ) para manipular el mensaje SOAP.

¿Cómo lo sé? La respuesta es que el targetnamepsace S lo genera JAXB y el targetnamepsace SOAP-ENV lo genera SAAJ.

Al usar el SOAPHandler posiblemente estés obteniendo el mensaje de esta forma:
    SOAPMessage msg = SOAPMessageContext.getMessage();
Al obtener el mensaje usando  SOAPMessageContext.getMessage() automaticamente SAAJ intenta crear una cabecera por lo que agrega un header vacío. y en su defecto declara el nuevo targetnamespace SOAP-ENV.

Para resolver el problema, diseñe una clase Helper que te facilitará el trabajo de agregar en JAX-WS la cabecera sin manipular el mensaje. Además verás otras utilidades interesantes que se pueden incluir en el helper.

clase JAXWSHelper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.hek.ws.handler;
 
import java.util.Map;
 
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.ws.BindingProvider;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
 
import com.inetpsa.ped.core.PEDTechnicalException;
import com.sun.xml.ws.api.message.Header;
import com.sun.xml.ws.developer.WSBindingProvider;
import com.sun.xml.ws.message.DOMHeader;
 
/**
 * Helper class only to use with JAX-WS and useful to obtain a reference to the binding provider and to append different WebServices operations:
 * <p>
 * <ul>
 * <li>basic authentication</li>
 * <li>security headers on paylaod</li>
 * <li>add published url service</li>
 * <li>among others</li>
 * </ul>
 * </p>
 * You can downcast to WSBindingProvider which defines a few more methods provided only by the JAX-WS RI.
 * <p />
 * If you dont use JAX-WS, not use this helper class. You can use javax.xml.ws.handler.soap.SoapHandler to adding security headers
 *
 * @author E460165
 * @param <S> the BindingProvider generic type
 */
public class JAXWSHelper<S extends BindingProvider> {
 
    /** The Constant WS_SECEXT. */
 
    /** The Constant WS_USER_TOKEN_PROFILE. */
    public final static String WS_USER_TOKEN_PROFILE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText";
 
    /** The provider. */
    private S provider;
 
    /**
     * Instantiates a new wss ws security helper.
     *
     * @param provider the provider
     * @param username the username
     * @param password the password
     */
    public JAXWSHelper(S provider) {
        this.provider = provider;
    }
 
    /**
     * Adds the http basic authorization.
     */
    public void addHttpBasicAuthorization(String username, String password) {
        Map<String, Object> requestContext = provider.getRequestContext();
        requestContext.put(BindingProvider.USERNAME_PROPERTY, username);
        requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
    }
 
    /**
     * Adds the wsse security relates to header and asigned username and password.
     */
    public void addWsseSecurityHeader(String username, String password) {
        try {
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Element root = document.createElementNS(WS_SECEXT, "wsse:Security");
            document.appendChild(root);
 
            Element usernameTokenEl = document.createElementNS(WS_SECEXT, "wsse:UsernameToken");
            root.appendChild(usernameTokenEl);
 
            Element usernameEl = document.createElementNS(WS_SECEXT, "wsse:Username");
            usernameTokenEl.appendChild(usernameEl);
            usernameEl.setTextContent(username);
 
            Element passwordEl = document.createElementNS(WS_SECEXT, "wsse:Password");
            usernameTokenEl.appendChild(passwordEl);
            passwordEl.setTextContent(password);
            passwordEl.setAttribute("Type", WS_USER_TOKEN_PROFILE);
 
            Header securityHeader = new DOMHeader<Element>(document.getDocumentElement());
 
            if (provider instanceof WSBindingProvider) {
                ((WSBindingProvider) provider).setOutboundHeaders(securityHeader);
            }
        } catch (ParserConfigurationException e) {
            throw new PEDTechnicalException("Error adding ws-security header to the webservice", e);
        }
    }
 
    /**
     * Adds the http endpoint.
     *
     * @param url the url
     */
    public void addHttpEndpoint(String url) {
        Map<String, Object> requestContext = provider.getRequestContext();
        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
    }
}
Ejemplo de como utilizar la clase JAXWSHelper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    /** The url service bps. */
    private String URL_SERVICE_BPS;
 
    /** The wsdl resource. */
    private String WSDL_RESOURCE;
 
    /** The username. */
    private String USERNAME;
 
    /** The password. */
    private String PASSWORD;
 
    private AccesBPSPortType createClientBps() {
        URL wsdlURL = this.getClass().getClassLoader().getResource(WSDL_RESOURCE);
 
        // call webservice
        WebServiceClient webServiceClient = AccesBPS.class.getAnnotation(WebServiceClient.class);
        AccesBPS accesBps = new AccesBPS(wsdlURL, new QName(webServiceClient.targetNamespace(), webServiceClient.name()));
 
        AccesBPSPortType client = accesBps.getPort(AccesBPSPortType.class);
 
        JAXWSHelper<bindingprovider> jaxWSHelper = new JAXWSHelper<bindingprovider>(((BindingProvider) client));
 
        // adding http basic authorization
        jaxWSHelper.addHttpBasicAuthorization(USERNAME, PASSWORD);
        // adding wsse security headers
        jaxWSHelper.addWsseSecurityHeader(USERNAME, PASSWORD);
        // adding httpEndpoint
        jaxWSHelper.addHttpEndpoint(URL_SERVICE_BPS);
 
        return client;
 
    }
 
</bindingprovider></bindingprovider>

viernes, 5 de junio de 2015

Clases utiliarias para automatizar los Test Unitarios

Aquí tienen otra clase utilitaria para automatizar los Test Unitarios.

También pueden echar un vistazo a testUtils, es una libreria interesante que les ayudara a realizar los test unitarios de una manera más elegante y sin escribir tanta redundancia de código:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package com.hector;
 
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.persistence.Embeddable;
import javax.persistence.Entity;
 
import org.junit.Assert;
import org.junit.Test;
import org.powermock.reflect.Whitebox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * Test automatique des getter/setter. A voir si on s'en sert.
 */
public class BeansAutomatedTest {
 /** logger */
 private static final Logger LOGGER = LoggerFactory
 .getLogger(BeansAutomatedTest.class);
 
 /** prefixes possibles pour les noms des getters */
 public static final String[] POSSIBLE_GETTER_PREFIXES = { "get", "is",
 "has" };
 
 /** prefixes possibles pour les noms des setters */
 private static final String[] POSSIBLE_SETTER_PREFIXES = { "set" };
 
 /**
 * map des implémentations à utiliser pour instancier des interfaces. Ex:
 * List --> ArrayList
 */
 private static final Map <Class <?>, Class <?>> IMPLEMENTATIONS_TO_USE = new HashMap <Class <?>, Class <?>>();
 
 /**
 * map des wrapper de types primitifs à utiliser pour tester les
 * getter/setter sur des Integer, Boolean...
 */
 private static final Set <Class <?>> BOXED_PRIMITIVES = new HashSet <Class <?>>();
 
 /**
 * map des primitifs à utiliser pour tester les getter/setter sur des types
 * primitifs int, boolean...
 */
 private static final Map <Class <?>, Object> PRIMITIVES_TO_USE = new HashMap <Class <?>, Object>();
 
 /** map des tableaux de champs à ignorer par classe */
 private static final Map <String, List <String>> IGNORED_FIELDS = new HashMap <String, List <String>>();
 
 /** list des classes ignorées */
 private static final List <String> IGNORED_BEANS = new ArrayList <String>();
 
 private static enum TypeTest {
 Entities, Representations, Dto, Vo
 }
 
 static {
 IMPLEMENTATIONS_TO_USE.put(List.class, ArrayList.class);
 IMPLEMENTATIONS_TO_USE.put(Set.class, HashSet.class);
 IMPLEMENTATIONS_TO_USE.put(Collection.class, ArrayList.class);
 IMPLEMENTATIONS_TO_USE.put(Map.class, HashMap.class);
 
 BOXED_PRIMITIVES.add(Integer.class);
 BOXED_PRIMITIVES.add(Long.class);
 BOXED_PRIMITIVES.add(Character.class);
 BOXED_PRIMITIVES.add(Double.class);
 BOXED_PRIMITIVES.add(Float.class);
 BOXED_PRIMITIVES.add(Boolean.class);
 BOXED_PRIMITIVES.add(BigDecimal.class);
 
 PRIMITIVES_TO_USE.put(int.class, 0);
 PRIMITIVES_TO_USE.put(Integer.class, 0);
 PRIMITIVES_TO_USE.put(long.class, 0L);
 PRIMITIVES_TO_USE.put(Long.class, 0L);
 PRIMITIVES_TO_USE.put(char.class, '\0');
 PRIMITIVES_TO_USE.put(Character.class, '\0');
 PRIMITIVES_TO_USE.put(boolean.class, true);
 PRIMITIVES_TO_USE.put(Boolean.class, Boolean.TRUE);
 PRIMITIVES_TO_USE.put(float.class, 0f);
 PRIMITIVES_TO_USE.put(Float.class, 0f);
 PRIMITIVES_TO_USE.put(double.class, 0d);
 PRIMITIVES_TO_USE.put(Double.class, 0d);
 PRIMITIVES_TO_USE.put(BigDecimal.class, BigDecimal.ZERO);
 
 // Les champs ci-dessous sont modifiés à leur set ou à leur get
 // TODO tester ces getters/setters unitairement
 IGNORED_FIELDS
 .put("com.hector.initialization.portefeuilledecommandes.PortefeuilleDeCommandesDto",
 Arrays.asList(new String[] { "paysGamme",
 "paysCommerce", "paysProgramme" }));
 IGNORED_FIELDS
 .put("com.hector.batch.referentiels.produit.bcv.BcvRequestHeaderDto",
 Arrays.asList(new String[] { "header" }));
 IGNORED_FIELDS.put(
 "com.hector.domain.centredemontage.JourDeProduction",
 Arrays.asList(new String[] { "dateJourProd" }));
 IGNORED_FIELDS
 .put("com.hector.rest.commande.RechercheCommandePlanifieRepresentation",
 Arrays.asList(new String[] { "familles" }));
 IGNORED_FIELDS
 .put("com.hector.cycleprogramme.AppariementRepresentation",
 Arrays.asList(new String[] { "alerte" }));
 IGNORED_FIELDS
 .put("com.hector.rest.cycleprogramme.CycleProgrammeRepresentation",
 Arrays.asList(new String[] { "numSeqAnnee" }));
 IGNORED_FIELDS
 .put("com.hector.rest.seuilsAlerteventes.SeuilsAlerteVentesRepresentation",
 Arrays.asList(new String[] { "codePcom" }));
 IGNORED_FIELDS
 .put("com.hector.rest.traitement.TraitementDeDefilementRepresentation",
 Arrays.asList(new String[] { "annee", "Numordre" }));
 IGNORED_FIELDS
 .put("com.hector.rest.centredemontage.DifferentielcalendrierRepresentation",
 Arrays.asList(new String[] { "dateJourProdstring" }));
 
 // Beans ignorés
 // TODO tester ces beans unitairement
 IGNORED_BEANS
 .add("com.hector.batch.diffusion.evn.EVNRequestFooterDto");
 IGNORED_BEANS
 .add("com.hector.batch.diffusion.evn.EVNRequestHeaderDto");
 IGNORED_BEANS
 .add("com.hector.batch.diffusion.ftr.FDPFtrRequestBodyDto");
 IGNORED_BEANS
 .add("com.hector.batch.diffusion.sisif.SisifDiffOfEnvoyeUsineRequestBodyDto");
 IGNORED_BEANS
 .add("com.hector.batch.luoreactualisee.PostEusiKeyDto");
 IGNORED_BEANS
 .add("com.hector.batch.replanification.officialisation.IodaDto");
 IGNORED_BEANS.add("com.hector.rest.PEDBaseRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.chart.XDateYIntSizeValuesRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.chart.XDateYIntValuesRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.chart.XDateYIntZIntValuesRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.pfab.PossibiliteFabricationRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.replanification.AttributionCessionModeRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.replanification.AttributionCessionSemaineRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.replanification.AvanceRetardVolumeCafRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.replanification.CommandesEspaceAttenteRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.replanification.PopulationSFPRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.replanification.ResultatRessourceRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.replanification.SuiviQualitatifReplanifRepresentation");
 IGNORED_BEANS
 .add("com.hector.rest.ressource.AleaRessourceRepresentation");
 IGNORED_BEANS.add("com.hector.rest.slot.SlotVoRepresentation");
 IGNORED_BEANS.add("com.hector.rest.slot.TimeLineRepresentation");
 }
 
 @Test
 public void testPEDAppDtos() {
 new PEDBeansAutomatedTest().test("com.hector.app", TypeTest.Dto);
 }
 
 @Test
 public void testPEDBatchDtos() {
 new PEDBeansAutomatedTest().test("com.hector.batch", TypeTest.Dto);
 }
 
 @Test
 public void testPEDDomainEntities() {
 new PEDBeansAutomatedTest().test("com.hector.domain",
 TypeTest.Entities);
 }
 
 @Test
 public void testPEDDomainVos() {
 new PEDBeansAutomatedTest().test("com.hector.domain", TypeTest.Vo);
 }
 
 @Test
 public void testPEDWebRepresentations() {
 new PEDBeansAutomatedTest().test("com.hector.rest",
 TypeTest.Representations);
 }
 
 /**
 * Méthode principale de test. 
 
 * 1. Collecte les entités.
 
 * 2. Teste les entités.
 
 * 3. Affiche les résultats.
 
 *
 * @param domainPkg
 * package contenant les entités à tester. Seul un niveau
 * d'arborescence est supporté pour l'instant (domain/container,
 * domain/magasin....)
 */
 public void test(String domainPkg, TypeTest typeTest) {
 
 List <Class <?>> entitiesToTest = collectAllEntities(domainPkg, typeTest);
 TestResult allTestsResults = new TestResult();
 for (Class <?> entity : entitiesToTest) {
 if (IGNORED_BEANS.contains(entity.getName())) {
 LOGGER.debug("Bean {} ignored", entity.getName());
 allTestsResults.addIgnoredBean();
 } else {
 allTestsResults.merge(testEntity(entity));
 }
 }
 LOGGER.info("{} beans of type {} have been tested in {}",
 entitiesToTest.size(), typeTest, domainPkg);
 LOGGER.info(" --> {} beans ignored",
 allTestsResults.getNbBeansIgnored());
 LOGGER.info(" --> {} fields tested OK",
 allTestsResults.getNbTestedSucceeded());
 LOGGER.info(" --> {} fields tested KO",
 allTestsResults.getNbTestedFailed());
 LOGGER.info(
 " --> {} fields could not be tested due to primitive/implementation issues",
 allTestsResults.getNbNotTestedDueToPrimitiveOrImp());
 LOGGER.info(" --> {} fields could not be tested due to other issues",
 allTestsResults.getNbNotTestedOther());
 LOGGER.info(" --> {} fields ignored", allTestsResults.getNbIgnored());
 
 Assert.assertEquals(0, allTestsResults.getNbTestedFailed());
 Assert.assertEquals(0,
 allTestsResults.getNbNotTestedDueToPrimitiveOrImp());
 Assert.assertEquals(0, allTestsResults.getNbNotTestedOther());
 }
 
 /**
 * try to test the getter / setter of the entity. Return true if it was
 * tested
 *
 * @param entityClass
 * entity to test
 * @return number of tested getter/setter pairs
 */
 private TestResult testEntity(Class <?> entityClass) {
 LOGGER.debug("Trying to test {}", entityClass.getName());
 List <String> ignoredFields = (IGNORED_FIELDS.containsKey(entityClass
 .getName())) ? IGNORED_FIELDS.get(entityClass.getName())
 : new ArrayList <String>();
 TestResult testResultClass = new TestResult();
 try {
 Object entityInstance = tryToInstantiateClass(entityClass);
 
 if (entityInstance == null) {
 LOGGER.warn("Bean {} could not be instantiated.",
 entityClass.getName());
 testResultClass.merge(TestResultType.NOT_TESTED_DUE_TO_OTHER
 .getTestResult());
 } else {
 for (Field field : entityClass.getDeclaredFields()) {
 if (ignoredFields.contains(field.getName())) {
 LOGGER.debug("Field {} of {} ignored.",
 field.getName(), entityClass.getName());
 testResultClass.merge(TestResultType.IGNORED
 .getTestResult());
 } else {
 Method getter = this.findGetter(entityClass, field);
 Method setter = this.findSetter(entityClass, field);
 if (getter != null && setter != null) {
 LOGGER.debug(
 "Getter and setter found for field {}.",
 field.getName());
 TestResultType oneTest = setThenGet(entityInstance,
 field, setter, getter);
 testResultClass.merge(oneTest.getTestResult());
 }
 }
 }
 }
 
 } catch (SecurityException e) {
 LOGGER.warn(e.getMessage(), e);
 } catch (IllegalArgumentException e) {
 LOGGER.warn(e.getMessage(), e);
 }
 
 return testResultClass;
 
 }
 
 /**
 * Set puis get un objet. Vérifie que l'objet setté est bien l'objet getté.
 *
 * @param entityInstance
 * instance de l'entité testée
 * @param field
 * attribut de l'instance
 * @param setter
 * setter de l'attribut
 * @param getter
 * getter de l'attribut
 * @return résultat du test
 */
 private TestResultType setThenGet(Object entityInstance, Field field,
 Method setter, Method getter) {
 TestResultType result = TestResultType.NOT_TESTED_DUE_TO_OTHER;
 try {
 Class <?> type = field.getType();
 Object objectToSet = createInstanceOfType(type);
 if (objectToSet == null) {
 LOGGER.warn("Class {} : could not create {}", entityInstance
 .getClass().getName(), field.getName());
 result = TestResultType.NOT_TESTED_DUE_TO_PRIMITIVE_OR_IMPLEMENTATION;
 } else {
 // on fait un set puis un get. On vérifie ensuite que l'objet
 // qu'on a set est bien celui qu'on get.
 setter.invoke(entityInstance, objectToSet);
 Object resultOfGet = getter.invoke(entityInstance);
 // String msg = "Class " + entityInstance.getClass().getName() +
 // ": one of " + getter.getName() + "()/"
 // +
 // setter.getName()
 // + "() is wrong.\n" +
 // "The getter do not return what the setter set.";
 // if (!type.isPrimitive()) {
 // Assert.assertTrue(msg, resultOfGet == objectToSet);
 // } else {
 // Assert.assertEquals(msg, objectToSet, resultOfGet);
 // }
 if ((!type.isPrimitive() && resultOfGet != objectToSet)
 || (type.isPrimitive() && !resultOfGet
 .equals(objectToSet))) {
 LOGGER.warn("Class " + entityInstance.getClass().getName()
 + ": one of " + getter.getName() + "()/"
 + setter.getName() + "() is wrong.\n"
 + "The getter do not return what the setter set.");
 // AssertEquals(monObjetGet, monObjetSet);
 result = TestResultType.TESTED_AND_FAILED;
 
 } else {
 LOGGER.debug("!!! successfully tested {}", field.getName());
 result = TestResultType.TESTED_AND_SUCCEEDED;
 }
 }
 } catch (IllegalAccessException e) {
 LOGGER.warn(e.getMessage(), e);
 } catch (IllegalArgumentException e) {
 LOGGER.warn(e.getMessage(), e);
 } catch (InvocationTargetException e) {
 LOGGER.warn(e.getMessage(), e);
 } catch (SecurityException e) {
 LOGGER.warn(e.getMessage(), e);
 }
 return result;
 }
 
 /**
 * Crée une instance de l'objet de type type ou fournis un objet par défaut
 * si c'est une primitive, une enum, etc...
 *
 * @param type
 * le type
 * @return une instance/une primitive/une enum
 */
 protected Object createInstanceOfType(Class <?> type) {
 Object objectToSet = null;
 try {
 // si c'est une enum, on retourne la 1ere constante de l'enum
 if (type.isEnum()) {
 List <?> enumConstants = Arrays.asList(type.getEnumConstants());
 objectToSet = enumConstants.get(0);
 }
 // si c'est une primitive ou une boxed primitive, on utilise un
 // objet par défaut
 else if (type.isPrimitive() || typeIsBoxedPrimitive(type)) {
 objectToSet = PRIMITIVES_TO_USE.get(type);
 if (objectToSet == null) {
 LOGGER.warn("No primitive to use for {}", type);
 }
 }
 // si c'est une interface, on utilise une implémentation par défaut
 else if (type.isInterface()) {
 Class <?> typeImp = IMPLEMENTATIONS_TO_USE.get(type);
 if (typeImp == null) {
 LOGGER.warn("No implementation defined for {}", type);
 }
 objectToSet = typeImp.newInstance();
 }
 // sinon, on invoke le constructeur par défaut
 else {
 objectToSet = tryToInstantiateClass(type);
 }
 // pire cas de figure : whitebox
 if (objectToSet == null) {
 objectToSet = Whitebox.newInstance(type);
 }
 } catch (IllegalArgumentException e) {
 LOGGER.warn(e.getMessage(), e);
 } catch (InstantiationException e) {
 LOGGER.warn(e.getMessage(), e);
 } catch (IllegalAccessException e) {
 LOGGER.warn(e.getMessage(), e);
 }
 return objectToSet;
 }
 
 private Object tryToInstantiateClass(Class <?> type) {
 Object instance = null;
 Constructor <?>[] constructors = type.getDeclaredConstructors();
 
 // Test every constructor
 for (Constructor <?> constructor : constructors) {
 
 constructor.setAccessible(true);
 Class <?>[] paramTypes = constructor.getParameterTypes();
 
 try {
 // Constructor w/ params
 List <Object> params = new ArrayList <Object>();
 for (Class <?> paramType : paramTypes) {
 params.add(paramType.getDeclaredConstructor().newInstance());
 }
 instance = constructor.newInstance(params.toArray());
 } catch (NoSuchMethodException | InstantiationException
 | IllegalAccessException | IllegalArgumentException
 | InvocationTargetException | SecurityException e) {
 LOGGER.debug("Could not instantiate {} with {} ",
 type.getName(), constructor.getName());
 }
 }
 return instance;
 }
 
 /**
 * Type est un type primitif
 *
 * @param type
 * le type
 * @return Type est un type primitif
 */
 private boolean typeIsBoxedPrimitive(Class <?> type) {
 return BOXED_PRIMITIVES.contains(type);
 }
 
 /**
 * Trouve le setter pour un attribut d'une entitée
 *
 * @param entity
 * la classe de l'entité
 * @param field
 * l'attribut
 * @return le setter ou null si non trouvé
 */
 private Method findSetter(Class <?> entity, Field field) {
 for (String setterPrefix : POSSIBLE_SETTER_PREFIXES) {
 try {
 String setterToTry = setterPrefix
 + capitalizeFirstLetter(field.getName());
 LOGGER.debug("Trying setter {}", setterToTry);
 Method setter = entity.getMethod(setterToTry, field.getType());
 return setter;
 } catch (SecurityException e) {
 LOGGER.warn("Security exception for class {}", entity.getName());
 } catch (NoSuchMethodException e) {
 LOGGER.debug("No setter found");
 }
 }
 return null;
 }
 
 /**
 * Renvoie la string avec la première lettre en majuscule
 *
 * @param str
 * la string
 * @return string avec la première lettre en majuscule
 */
 private String capitalizeFirstLetter(String str) {
 
 if (str.length() == 0) {
 return str;
 } else if (str.length() == 1) {
 return String.valueOf(Character.toUpperCase(str.charAt(0)));
 }
 return Character.toUpperCase(str.charAt(0)) + str.substring(1);
 }
 
 /**
 * Trouve le getter pour un attribut d'une entitée
 *
 * @param entity
 * la classe de l'entité
 * @param field
 * l'attribut
 * @return le getter ou null si non trouvé
 */
 private Method findGetter(Class <?> entity, Field field) {
 for (String getterPrefix : POSSIBLE_GETTER_PREFIXES) {
 try {
 String getterToTry = getterPrefix
 + capitalizeFirstLetter(field.getName());
 LOGGER.debug("Trying getter {}", getterToTry);
 Method getter = entity.getMethod(getterToTry);
 return getter;
 } catch (SecurityException e) {
 LOGGER.warn("Security exception for class {}", entity.getName());
 } catch (NoSuchMethodException e) {
 LOGGER.debug("No setter found");
 }
 }
 return null;
 }
 
 /**
 * Collecte toutes les entités du package
 *
 * @param pkg
 * le pkg de domain
 * @return les entités collectées
 */
 private List <Class <?>> collectAllEntities(String pkg, TypeTest typeTest) {
 List <Class <?>> entitiesToTest = new ArrayList <Class <?>>();
 
 try {
 List <Class <?>> classes = getClasses(pkg);
 
 for (Class <?> potentialEntity : classes) {
 LOGGER.debug("Potential entity : " + potentialEntity.getName());
 if (TypeTest.Entities.equals(typeTest)
 && potentialEntity.getAnnotation(Entity.class) != null) {
 LOGGER.debug(" ->>>> this is an Entity");
 entitiesToTest.add(potentialEntity);
 } else if (TypeTest.Representations.equals(typeTest)
 && potentialEntity.getSimpleName().endsWith(
 "Representation")) {
 LOGGER.debug(" ->>>> this is a Representation");
 entitiesToTest.add(potentialEntity);
 } else if (TypeTest.Dto.equals(typeTest)
 && potentialEntity.getSimpleName().endsWith("Dto")) {
 LOGGER.debug(" ->>>> this is a Dto");
 entitiesToTest.add(potentialEntity);
 } else if (TypeTest.Vo.equals(typeTest)
 && potentialEntity.getAnnotation(Embeddable.class) != null) {
 LOGGER.debug(" ->>>> this is a Vo");
 entitiesToTest.add(potentialEntity);
 }
 }
 } catch (ClassNotFoundException | IOException e) {
 Assert.fail("Unable to scan package " + pkg);
 LOGGER.error("Unable to scan package.", e);
 }
 return entitiesToTest;
 }
 
 private static List <Class <?>> getClasses(String packageName)
 throws ClassNotFoundException, IOException {
 ClassLoader classLoader = Thread.currentThread()
 .getContextClassLoader();
 Assert.assertNotNull(classLoader);
 String path = packageName.replace('.', '/');
 Enumeration <URL> resources = classLoader.getResources(path);
 List <File> dirs = new ArrayList <File>();
 while (resources.hasMoreElements()) {
 URL resource = resources.nextElement();
 dirs.add(new File(resource.getFile()));
 }
 ArrayList <Class <?>> classes = new ArrayList <Class <?>>();
 for (File directory : dirs) {
 classes.addAll(findClasses(directory, packageName));
 }
 return classes;
 }
 
 /**
 * Recursive method used to find all classes in a given directory and
 * subdirs.
 *
 * @param directory
 * The base directory
 * @param packageName
 * The package name for classes found inside the base directory
 * @return The classes
 * @throws ClassNotFoundException
 */
 private static List <Class <?>> findClasses(File directory, String packageName)
 throws ClassNotFoundException {
 List <Class <?>> classes = new ArrayList <Class <?>>();
 if (!directory.exists()) {
 return classes;
 }
 File[] files = directory.listFiles();
 for (File file : files) {
 if (file.isDirectory()) {
 Assert.assertFalse(file.getName().contains("."));
 classes.addAll(findClasses(file,
 packageName + "." + file.getName()));
 } else if (file.getName().endsWith(".class")) {
 try {
 classes.add(Class.forName(packageName
 + '.'
 + file.getName().substring(0,
 file.getName().length() - 6)));
 } catch (Throwable e) {
 LOGGER.warn("Could not initialize {}", file.getName());
 }
 }
 }
 return classes;
 }
}

jueves, 4 de junio de 2015

Clase Utilitaria para Test Unitarios

Siempre es interesante contar con una clase utilitaria que nos permita ahorrar lineas de código, incluso en los test unitarios. Por esta razón, facilito esta clase que permite hacer validaciones de una forma fácil y sencilla
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.hector;
 
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
 
import org.junit.Assert;
 
import com.hector.TechnicalException;
 
public abstract class ManagerTest {
    public void superficialEnumCodeCoverageundefinedfinal Class<? extends Enum<?>> enumClass) {
        try {
            for undefinedObject o : undefinedObject[]) enumClass.getMethodundefined"values").invokeundefinednull)) {
                enumClass.getMethodundefined"valueOf", String.class).invokeundefinednull, o.toStringundefined));
            }
        } catch undefinedThrowable e) {
            throw new PEDTechnicalExceptionundefinede);
        }
    }
 
    public void shouldValidateThrowsExceptionWithMessageundefinedfinal Exception e, final String message) {
        Assert.assertNotNullundefinede);
        Assert.assertTrueundefinede.getMessageundefined).startsWithundefinedmessage));
    }
 
    public void callPrivateConstructorsForCodeCoverageundefinedfinal Class<?> class1) throws SecurityException, NoSuchMethodException,
            IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<?>[] classesToConstruct = { class1 };
        for undefinedClass<?> clazz : classesToConstruct) {
            Constructor<?> constructor = clazz.getDeclaredConstructorundefined);
            constructor.setAccessibleundefinedtrue);
            Assert.assertNotNullundefinedconstructor.newInstanceundefined));
        }
    }
 
    public void assertUtilityClassWellDefinedundefinedfinal Class<?> clazz) throws NoSuchMethodException, InvocationTargetException, InstantiationException,
            IllegalAccessException {
        Assert.assertTrueundefined"class must be final", Modifier.isFinalundefinedclazz.getModifiersundefined)));
        Assert.assertEqualsundefined"There must be only one constructor", 1, clazz.getDeclaredConstructorsundefined).length);
        final Constructor<?> constructor = clazz.getDeclaredConstructorundefined);
        if undefinedconstructor.isAccessibleundefined) || !Modifier.isPrivateundefinedconstructor.getModifiersundefined))) {
            Assert.failundefined"constructor is not private");
        }
        constructor.setAccessibleundefinedtrue);
        constructor.newInstanceundefined);
        constructor.setAccessibleundefinedfalse);
        for undefinedfinal Method method : clazz.getMethodsundefined)) {
            if undefined!Modifier.isStaticundefinedmethod.getModifiersundefined)) && method.getDeclaringClassundefined).equalsundefinedclazz)) {
                Assert.failundefined"there exists a non-static method:" + method);
            }
        }
    }
}