Subversion Repositories bacoAlunos

Rev

Rev 1783 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.domain.test.genson;

import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
import junit.framework.TestCase;
import org.json.JSONException;
import org.json.JSONObject;
import pt.estgp.estgweb.utils.DatesUtils;

import java.util.Date;

/**
 * Created by jorgemachado on 19/06/17.
 */

public class TestFieldsAndMethodsTogether extends TestCase
{


    private 5+0%2Fdocs%2Fapi+Date">Date saveDate;

    public 5+0%2Fdocs%2Fapi+Date">Date getSaveDate() {
        return saveDate;
    }

    public void setSaveDate(5+0%2Fdocs%2Fapi+Date">Date saveDate) {
        this.saveDate = saveDate;
    }

    private int ocorrenciasGetAdulteradoSet;

    public int getOcorrenciasGetAdulteradoSet() {
        return 3000;
    }

    public void setOcorrenciasGetAdulteradoSet(int ocorrenciasGetAdulteradoSet) {
        this.ocorrenciasGetAdulteradoSet = ocorrenciasGetAdulteradoSet;
    }

    private int ocorrenciasGetSetNormal;

    private int ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia;

    private int ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados;

    private int ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet;

    private boolean valorDevolvidoTrue;
    private boolean valorDevolvidoFalse;

    public boolean isValorDevolvidoTrue() {
        return valorDevolvidoTrue;
    }

    public boolean isValorDevolvidoFalse() {
        return valorDevolvidoFalse;
    }

    public int getOcorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet() {
        return ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet;
    }

    public void setOcorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet(int ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet) {
        this.ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet = 100;
    }

    public int getOcorrenciasGetSetNormal()
    {
        return ocorrenciasGetSetNormal;
    }

    public void setOcorrenciasGetSetNormal(int o)
    {
         ocorrenciasGetSetNormal = o;
    }

    public int getOcorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia() {
        return ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia;
    }


    private int campoOcultoSemGetNemSet;

    private int campoOcultoSemGetNemSetNemComOutroNome;

    public int getCampoInvisivel() {
        return campoOcultoSemGetNemSet;
    }

    public void setCampoInvisivel(int campoInvisivel) {
        this.campoOcultoSemGetNemSet = campoInvisivel;
    }

    public void testPriority() throws JSONException {
        Genson genson = new GensonBuilder()
                .include("ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet")
                .include("ocorrenciasGetAdulteradoSet")
                .create();
        TestFieldsAndMethodsTogether t = new TestFieldsAndMethodsTogether();
        t.ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet=1000;

        t.ocorrenciasGetSetNormal = 10;
        t.ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia = 20;
        t.ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados = 30;

        t.valorDevolvidoTrue = true;
        t.valorDevolvidoFalse = false;

        t.ocorrenciasGetAdulteradoSet = 1;//tem o get adulterado para devolver 3000
        1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t);

        JSONObject jsonParsed = new JSONObject(json);
        int valor = jsonParsed.getInt("ocorrenciasGetAdulteradoSet");
        //fica com o valor do set adulterado
        assertEquals(valor,3000);

        TestFieldsAndMethodsTogether t2 = genson.deserialize(json,TestFieldsAndMethodsTogether.class);

        assertTrue(t2.ocorrenciasGetSetNormal == t.ocorrenciasGetSetNormal);
        assertTrue(t2.ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia == 0);
        assertTrue(t2.ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados == 0);

        //METODO TEM PRIORIDADE
        assertTrue(t2.ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet == 100);

        1.5.0/docs/api/java/lang/Boolean.html">Boolean lidoTrue = jsonParsed.getBoolean("valorDevolvidoTrue");
        1.5.0/docs/api/java/lang/Boolean.html">Boolean lidoFalse = jsonParsed.getBoolean("valorDevolvidoFalse");

        assertTrue(lidoTrue);
        assertFalse(lidoFalse);




    }

    public void testFields1() throws JSONException {
        Genson genson = new GensonBuilder()
                .useMethods(false)
                //.exclude(Object.class)  resulta no mesmo porque os campos são privados
                // logo estao automaticamente excluidos e' necessario incluir um a um
                .include("ocorrenciasGetSetNormal", TestFieldsAndMethodsTogether.class)
                .include("ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia",TestFieldsAndMethodsTogether.class)
                .include("ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados",TestFieldsAndMethodsTogether.class)
                .include("valorDevolvidoTrue",TestFieldsAndMethodsTogether.class)
                .include("valorDevolvidoFalse",TestFieldsAndMethodsTogether.class)
                .include("ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet",TestFieldsAndMethodsTogether.class)
                .include("campoInvisivel",TestFieldsAndMethodsTogether.class)
                .create();
        TestFieldsAndMethodsTogether t = new TestFieldsAndMethodsTogether();
        t.ocorrenciasGetSetNormal = 10;
        t.ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia = 20;
        t.ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados = 30;
        t.valorDevolvidoTrue = true;
        t.valorDevolvidoFalse = false;
        t.ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet = 200;
        t.campoOcultoSemGetNemSet = 400;

        1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t);

        JSONObject jsonParsed = new JSONObject(json);

        TestFieldsAndMethodsTogether t2 = genson.deserialize(json,TestFieldsAndMethodsTogether.class);

        assertTrue(t2.ocorrenciasGetSetNormal == t.ocorrenciasGetSetNormal);
        //Neste caso como foi deliberadamente declarado o campo o sistema usa ou o metodo ou o campo para fazer a alteração
        assertTrue(t2.ocorrenciasComGetSemSetUsaMetodoNaoTemSetNaoCopia == 20);
        //Neste caso variaveis explicitamente declaradas o sistema deve copia-las
        assertTrue(t2.ocorrenciasSemGetNemSetUsaFieldNaoCopiaPrivados == 30);
        //NEste caso apesar do Set estar a colocar 100 vai usar o valor do campo privado
        assertTrue(t2.ocorrenciasGetNormalSetUsaValorDiferenteFicaComoDoSet == 200);

        1.5.0/docs/api/java/lang/Boolean.html">Boolean lidoTrue = jsonParsed.getBoolean("valorDevolvidoTrue");
        1.5.0/docs/api/java/lang/Boolean.html">Boolean lidoFalse = jsonParsed.getBoolean("valorDevolvidoFalse");

        //Os campos mesmo que tenham metodos e esses metodos sejam declarados
        //No include do Genson, o include serve apenas para Campos e não para
        //metodos, portanto o campo não é copiado se o use metodos estiver a false
        //cs campos apenas são copiados se publicos ou incluidos especificamente
        assertFalse(t2.campoOcultoSemGetNemSet == 400);

        assertTrue(lidoTrue);
        assertFalse(lidoFalse);




    }

    public void testFields() throws JSONException {
        Genson genson = new GensonBuilder()
                .useMethods(true) //O metodo do campoInvisivel que afeta este campo vai preenche-lo
                .useFields(false)
                .include("campoOcultoSemGetNemSetNemComOutroNome",TestFieldsAndMethodsTogether.class)
                .create();
        TestFieldsAndMethodsTogether t = new TestFieldsAndMethodsTogether();
        t.campoOcultoSemGetNemSet = 400;
        t.campoOcultoSemGetNemSetNemComOutroNome = 100;

        1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t);

        JSONObject jsonParsed = new JSONObject(json);

        TestFieldsAndMethodsTogether t2 = genson.deserialize(json,TestFieldsAndMethodsTogether.class);

        //Tem um metodo que usa o valor e o acerta
        assertTrue(t2.campoOcultoSemGetNemSet == 400);
        //Campos nunca são copiados com o Fields = false logo o valor não passa o assert e' false
        assertTrue(t2.campoOcultoSemGetNemSetNemComOutroNome != 100);




    }


    public void testFields1Herancas() throws JSONException {
        Genson genson = new GensonBuilder()
                .useMethods(false)
                .useClassMetadata(true)
                .useRuntimeType(true)
                        //.exclude(Object.class)  resulta no mesmo porque os campos são privados
                        // logo estao automaticamente excluidos e' necessario incluir um a um
                .include("ocorrenciasGetSetNormal", TestFieldsAndMethodsChild.class)
                .create();
        TestFieldsAndMethodsChild t = new TestFieldsAndMethodsChild();
        t.setOcorrenciasGetSetNormal(10);

        1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t);

        JSONObject jsonParsed = new JSONObject(json);

        TestFieldsAndMethodsTogether t2 = genson.deserialize(json,TestFieldsAndMethodsTogether.class);

        assertTrue(t2.getOcorrenciasGetSetNormal() == t.getOcorrenciasGetSetNormal());



        genson = new GensonBuilder()
                .useMethods(false)
                .include("ocorrenciasGetSetNormal", TestFieldsAndMethodsTogether.class)
                .create();
        t = new TestFieldsAndMethodsChild();
        t.setOcorrenciasGetSetNormal(10);

        json = genson.serialize(t);

        jsonParsed = new JSONObject(json);

        t2 = genson.deserialize(json,TestFieldsAndMethodsTogether.class);

        assertTrue(t2.getOcorrenciasGetSetNormal() == t.getOcorrenciasGetSetNormal());

    }

    public void testFieldsDates() throws JSONException {
        Genson genson = new GensonBuilder()
                .useFields(true)
                .useMethods(false)
                .exclude("saveDate",TestFieldsAndMethodsTogether.class)
                .include("dateStr", TestFieldsAndMethodsTogether.class)
                .create();
        TestFieldsAndMethodsChild t = new TestFieldsAndMethodsChild();
        t.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date());

        1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t);

        TestFieldsAndMethodsChild t2 = genson.deserialize(json,TestFieldsAndMethodsChild.class);

        //Tem um metodo que usa o valor e o acerta
        assertEquals(t2.getDateStr(),t.getDateStr());


    }



    public void testMetodosIncludesEdates() throws JSONException {
        Genson genson = new GensonBuilder()
                .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
                .useFields(false)
                .useMethods(true)
                .exclude("ocorrenciasGetSetNormal",TestFieldsAndMethodsTogether.class)
                //VAR inexistente apenas existe o metodo
                .include("dateStr2",TestFieldsAndMethodsChild.class)
                .create();
        TestFieldsAndMethodsChild t = new TestFieldsAndMethodsChild();
        t.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date());

        1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(t);

        TestFieldsAndMethodsChild t2 = genson.deserialize(json,TestFieldsAndMethodsChild.class);

        //Um metodo pode ser excluido, nao pode é ser incluido
        assertTrue(t2.getOcorrenciasGetSetNormal() == 0);
        //Tem um metodo que usa o valor e o acerta
        assertEquals(t2.getDateStr2(),t.getDateStr2());

        assertEquals(DatesUtils.getYear(t2.getSaveDate()),DatesUtils.getYear(t.getSaveDate()));
        assertEquals(DatesUtils.getMonth(t2.getSaveDate()),DatesUtils.getMonth(t.getSaveDate()));
        assertEquals(DatesUtils.getDay(t2.getSaveDate()),DatesUtils.getDay(t.getSaveDate()));


    }
}