martes, 17 de noviembre de 2015

Eliminar varias lineas del log o de un fichero de texto para mejorar la lectura

Si tienes un fichero Log y deseas eliminar varias lineas para leer mejor el fichero, te enseñare un truco para trabajarlo con Notepad++

  1. Ir al Menú: Buscar / Buscar, pestaña Mark.
    • Activar el checkbox Marcar Línea. Si lo necesitamos, usamos expresiones regulares o pedimos que diferencia las mayúsculas de las minúsculas.
  2. Pulsar el botón Buscar todo
Notepad Mark


Una vez marcadas las lineas podemos eliminar las marcas:
  1. Menú: Buscar / Marcador / Borrar lineas marcas
O podemos invertir la selección y borrar las marcadas. Todo a gusto del consumidor!
  1. Menú: Buscar Marcador Invertir marcadores
  2. Menú: Buscar Marcador Borrar lineas marcas


viernes, 23 de octubre de 2015

Estimaciones de Software

El error más común de los analistas y programadores de software es estimar de menos. Sabemos que esto ocurre en otras ingenierías (quién no ha oído algún amigo al que le costó el doble de lo previsto la construcción de una casa inicialmente, quién no ha leído de proyectos de autopistas, puentes, grandes represas, que han terminado costando varias veces más de lo presupuestado…), pero parece ser que el caso del software es más sencillo.

¿Sabes conducir un coche o una moto? Alguna vez te has planteado: ¿Cuánto tiempo te lleva ir de Madrid a Moscú (ida y vuelta)? ¿Cuánto gastarás en combustible? Si tu jefe te dice que estés esta tarde en Moscú para una reunión: ¿Llegarás a tiempo? Estoy completamente seguro que te preguntarás lo mismo que nos preguntamos todos: Tipo de Transporte, Medios de Transporte, Costes, Guías, Mapas… Y qué sucede si además tu jefe te dice que solamente dispones de una moto para ahorrar costes! y adicionalmente sin GPS, sin mapas... Y a la final recibes una moto de bajo cilindraje (una vespa).

Estoy seguro que proyectos de informática en los que trabajaras, y que se plantean este tipo de situaciones, encontrarán la misma respuesta en el momento de estimar la construcción y puesta en marcha de un software. AL AZAR Y QUE DIOS NOS PILLE CONFESADOS!

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:

 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

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


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
<?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:
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:
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:
select * from v$instance

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

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

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

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

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

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

Vista que muestra las conexiones actuales a Oracle:
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
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

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
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):
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"):
select * 
from ALL_ALL_TABLES 
where upper(table_name) like '%EMPLO%'

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

Tablas propiedad del usuario actual:
select * from user_tables

Todos los objetos propiedad del usuario conectado a Oracle:
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:
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:
select * from product_component_version

Roles y privilegios por roles:
select * from role_sys_privs

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

Tablas de las que es propietario un usuario, en este caso "HR":
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):
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,...):
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ó:
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:
select * from V$DATAFILE

Ficheros temporales:
select * from V$TEMPFILE

Tablespaces:
select * from V$TABLESPACE

Otras vistas muy interesantes:
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:
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:
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):
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:
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):
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
select sum(BYTES)/1024/1024 MB 
from DBA_EXTENTS  

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

Tamaño ocupado por una tabla concreta sin incluir los índices de la misma
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
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:
select sum(vsize('NOMBRECOLUMNA'))/1024/1024 MB 
from NOMBRETABLA

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

Espacio ocupado por los diferentes segmentos (tablas, índices, undo, rollback, cluster, ...):
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:
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, ...:
SELECT distinct object_name 
FROM all_arguments 
WHERE package_name = 'STANDARD'
order by object_name

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

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

Obtener la IP del servidor de la base de datos Oracle Database:
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):
select username, action_name, priv_used, returncode
from dba_audit_trail

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

Desbloquear un usuario:
 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
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. */
    public final static String WS_SECEXT = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";

    /** 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
    /** 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 jaxWSHelper = new JAXWSHelper(((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;

    }

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:


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.  <br>
 * 1. Collecte les entités. <br>
 * 2. Teste les entités. <br>
 * 3. Affiche les résultats. <br>
 * 
 * @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
 


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);
            }
        }
    }
}

lunes, 18 de mayo de 2015

Error: TNS:listener: all appropriate instances are blocking new connections

Solventando el error: TNS:listener: all appropriate instances are blocking new connections

En cuanto a ORACLE, seria importante hacerse las siguiente preguntas:

¿Cómo saber si una base de datos Oracle, esta activa?

SELECT open_mode 
FROM   v$database; 

Si el valor es:

  • 'MOUNTED', la base de datos esta montada 
  • 'READ WRITE', entonces asumimos que la base de datos esta activa 
  • 'READ ONLY' , entonces podría ejecutar consultas en modo de sólo lectura, pero no esta activa.
SELECT controlfile_type 
FROM   v$database; 


Si el valor es:

  • 'CURRENT', la base de datos esta activa
  • 'STANDBY', la base de datos esta en espera.


La siguiente consulta muestra el estado de la instancia, su nombre y su estado.

SELECT instance_name,database_status,status,instance_role,active_state 
FROM   v$instance; 

Si el valor STATUS es: 
  • STARTED, indica que la base de datos esta activa pero no montada
  • MOUNTED, indica que la base de datos esta montada.
  • OPEN, indica que el esta abierta y activa
  • OPEN-MIGRATE, indica que esta en actualización

Si el valor DATABASE_STATUS es: 
  • ACTIVE
Si el valor ACTIVE_STATE es: 
  • 'NORMAL' indica la base de datos está en un estado normal.
  • 'QUIESCING' indica que la sentencia ALTER SYSTEM QUIESCE RESTRICTED ha sido ejecutada: no hay nuevas transacciones de usuario, consultas o  PL/SQL son procesados en este caso. 
  • 'QUIESCED', es Inmovilizado. indica que la sentencia RESTRINGIDO ALTER SISTEMA QUIESCE se ha ejecutado: Transacciones de usuario, consultas o declaraciones PL / SQL emitidas después de la instrucción no se procesan.
¿Verificamos las sessiones? Posiblemente existan mas que la licencia permitida

SELECT Nvl(s.username, '(oracle)') AS username,s.osuser,s.sid,s.serial#,p.spid, 
       s.lockwait,s.status,s.MODULE,s.machine,s.program, 
       To_char(s.logon_time, 'DD-MON-YYYY HH24:MI:SS') AS logon_time 
FROM   v$session s,v$process p 
WHERE  s.paddr = p.addr 
       AND s.username = 'PED_CDD' -- Change the username
ORDER  BY s.username,s.osuser; 


¿Cuantos cursores están permitidos y cuantos se abrieron?

SELECT Max(a.value) AS highest_open_cur,p.value AS max_open_cur 
FROM   v$sesstat a,v$statname b,v$parameter p 
WHERE  a.statistic# = b.statistic# 
       AND b.name = 'opened cursors current' 
       AND p.name = 'open_cursors' 
GROUP  BY p.value; 

¿Cuantos procesos, sessiones y que ultima sql ejecutaron ?

SELECT sess.process,sess.status,sess.username,sess.schemaname,SQL.sql_text 
FROM   v$session sess,v$sql SQL 
WHERE  SQL.sql_id(+) = sess.sql_id; 

SELECT sess.process,sess.status,sess.username,sess.schemaname,SQL.sql_text 
FROM   v$session sess,v$sql SQL 
WHERE  SQL.sql_id(+) = sess.sql_id 
       AND sess.TYPE = 'USER' 

SELECT S.username,s.sid,s.osuser,t.sql_id,sql_text 
FROM   v$sqltext_with_newlines t,v$session s 
WHERE  t.address = s.sql_address 
       AND t.hash_value = s.sql_hash_value 
       AND s.status = 'ACTIVE' 
       AND s.username <> 'SYSTEM' 
ORDER  BY s.sid,t.piece 

Solucion final:

Shutdown Inmediate and Start mount.  LSNRCTL for a STOP and START


lunes, 6 de abril de 2015

Error al ejecutar Maven Update Project - "Java annotation model update".

Si al ejecutar el Maven -> Update Project recibes errores de Maven Build Model y al ver los detalles o al abrir el fichero .log del eclipse, te encuentras el siguiente mensaje: !MESSAGE An internal error occurred during: "Java annotation model update". !STACK 0 java.lang.NullPointerException Este mensaje suele ocurrir cuando algunos ficheros del plugin m2e están corruptos. Para corregir el error sigue los siguientes pasos: 1. Cierra el eclipse 2. Ve a la carpeta .metadata/plugins localizada debajo de tu workspace de trabajo. 3. Copia o haz un respaldo de las carpetas org.eclipse.m2e.* 4. borra las carpetas org.eclipse.m2e.* 5. Reinicia el eclipse Vuelve a ejecutar el maven -> update project.

lunes, 9 de marzo de 2015

Base de datos HSQLDB

HSQL es un motor de bases de datos SQL (OpenSource) implementado completamente en Java.

En estos apuntes usare la variable nombre_bd como nombre de la base de datos. Sustituye esta variable por el nombre correcto de tu proyecto.

Ejecutar la base de datos desde un servicio

Si sigues las siguientes instrucciones podrás ejecutar HSQL como servicio de windows 

net stop HSQLService
hsql.exe -uninstall HSQLService
set HSQL_BIN=c:/tools/hsqldb/lib
set DATABASE=c:/tools/hsqldb/data/nombre_bd
set CURRENTDIR=c:/tools/hsqldb/data
hsql.exe -install HSQLService "%JAVA_HOME%\jre\bin\server\jvm.dll" -jvm_option -Xms128m -Xmx128m -Djava.class.path=".;%HSQL_BIN%\hsqldb.jar" -jvm_option -Dsystem.drive="c:" -start org.hsqldb.Server -params -database.0 %DATABASE% -dbname.0 nombre_bd -current "% CURRENTDIR%" -out "%CURRENTDIR%/service.out" -err "%CURRENTDIR%/service.err"
net start HSQLService

Ejecutar la base de datos desde un .bat 

Otra opción es crear simplemente un script (es decir, Start_nombre_bd.bat) en el directorio del proyecto donde vas a utilizar la base de datos HSQLDB.

java -cp ../lib/hsqldb.jar org.hsqldb.Server -database.0 c:/temp/nombre_bd -dbname.0 nombre_bd