<commit_before>/*
 * Copyright (c) 2010 Mysema Ltd.
 * All rights reserved.
 *
 */
package com.mysema.query;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import org.junit.Test;

import com.mysema.query.QueryFlag.Position;
import com.mysema.query.types.custom.CNumber;
import com.mysema.query.types.expr.ONumber;
import com.mysema.query.types.path.BeanPath;
import com.mysema.query.types.path.PNumber;
import com.mysema.query.types.path.PString;
import com.mysema.util.ReflectionUtils;

public class DefaultQueryMetadataTest {

    private QueryMetadata metadata = new DefaultQueryMetadata();

    private PString str = new PString("str");

    @Test
    public void testSerialization() throws IOException, ClassNotFoundException{
        PString expr = new PString("str");
        metadata.addFlag(new QueryFlag(Position.AFTER_FILTERS, ""));
        metadata.addGroupBy(expr);
        metadata.addHaving(expr.isEmpty());
        metadata.addJoin(JoinType.DEFAULT, expr);
        metadata.getJoins().get(0).addFlag(new JoinFlag(""));
        metadata.addJoinCondition(expr.isEmpty());
        metadata.addOrderBy(expr.asc());
        metadata.addProjection(expr);
        metadata.addWhere(expr.isEmpty());
        
        // serialize metadata
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(baos);
        out.writeObject(metadata);
        out.close();
        
        // deserialize metadata
        ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bain);
        QueryMetadata  metadata2 = (QueryMetadata) in.readObject();
        in.close();
        
        assertEquals(metadata.getFlags(),     metadata2.getFlags());
        assertEquals(metadata.getGroupBy(),   metadata2.getGroupBy());
        assertEquals(metadata.getHaving(),    metadata2.getHaving());
        assertEquals(metadata.getJoins(),     metadata2.getJoins());
        assertEquals(metadata.getModifiers(), metadata2.getModifiers());
        assertEquals(metadata.getOrderBy(),   metadata2.getOrderBy());
        assertEquals(metadata.getParams(),    metadata2.getParams());
        assertEquals(metadata.getProjection(),metadata2.getProjection());
        assertEquals(metadata.getWhere(),     metadata2.getWhere());
    }
    
    @Test
    public void testFullySerizable(){
        Set<Class<?>> checked = new HashSet<Class<?>>();
        checked.addAll(Arrays.<Class<?>>asList(List.class, Set.class, Map.class, Class.class, String.class, Object.class));
        Stack<Class<?>> classes = new Stack<Class<?>>();
        classes.addAll(Arrays.<Class<?>>asList(PNumber.class, ONumber.class, CNumber.class, BeanPath.class, DefaultQueryMetadata.class));
        while (!classes.isEmpty()){            
            Class<?> clazz = classes.pop();
            checked.add(clazz);
            if (!Serializable.class.isAssignableFrom(clazz) && !clazz.isPrimitive()){                
                fail(clazz.getName() + " is not serializable");
            }            
            for (Field field : clazz.getDeclaredFields()){
                Set<Class<?>> types = new HashSet<Class<?>>(3);
                types.add(field.getType());
                if (field.getType().getComponentType() != null){<commit_msg>improved tests<commit_after>/*
 * Copyright (c) 2010 Mysema Ltd.
 * All rights reserved.
 *
 */
package com.mysema.query;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import org.junit.Test;

import com.mysema.query.QueryFlag.Position;
import com.mysema.query.types.custom.CNumber;
import com.mysema.query.types.expr.ONumber;
import com.mysema.query.types.path.BeanPath;
import com.mysema.query.types.path.PNumber;
import com.mysema.query.types.path.PString;
import com.mysema.util.ReflectionUtils;

public class DefaultQueryMetadataTest {

    private QueryMetadata metadata = new DefaultQueryMetadata();

    private PString str = new PString("str");

    @Test
    public void testSerialization() throws IOException, ClassNotFoundException{
        PString expr = new PString("str");
        metadata.addFlag(new QueryFlag(Position.AFTER_FILTERS, ""));
        metadata.addGroupBy(expr);
        metadata.addHaving(expr.isEmpty());
        metadata.addJoin(JoinType.DEFAULT, expr);
        metadata.getJoins().get(0).addFlag(new JoinFlag(""));
        metadata.addJoinCondition(expr.isEmpty());
        metadata.addOrderBy(expr.asc());
        metadata.addProjection(expr);
        metadata.addWhere(expr.isEmpty());
        
        // serialize metadata
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(baos);
        out.writeObject(metadata);
        out.close();
        
        // deserialize metadata
        ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bain);
        QueryMetadata  metadata2 = (QueryMetadata) in.readObject();
        in.close();
        
        assertEquals(metadata.getFlags(),     metadata2.getFlags());
        assertEquals(metadata.getGroupBy(),   metadata2.getGroupBy());
        assertEquals(metadata.getHaving(),    metadata2.getHaving());
        assertEquals(metadata.getJoins(),     metadata2.getJoins());
        assertEquals(metadata.getModifiers(), metadata2.getModifiers());
        assertEquals(metadata.getOrderBy(),   metadata2.getOrderBy());
        assertEquals(metadata.getParams(),    metadata2.getParams());
        assertEquals(metadata.getProjection(),metadata2.getProjection());
        assertEquals(metadata.getWhere(),     metadata2.getWhere());
    }
    
    @Test
    public void testFullySerizable(){
        Set<Class<?>> checked = new HashSet<Class<?>>();
        checked.addAll(Arrays.<Class<?>>asList(List.class, Set.class, Map.class, Object.class, String.class, Class.class));
        Stack<Class<?>> classes = new Stack<Class<?>>();
        classes.addAll(Arrays.<Class<?>>asList(PNumber.class, ONumber.class, CNumber.class, BeanPath.class, DefaultQueryMetadata.class));
        while (!classes.isEmpty()){            
            Class<?> clazz = classes.pop();
            checked.add(clazz);
            if (!Serializable.class.isAssignableFrom(clazz) && !clazz.isPrimitive()){     
                System.out.println(clazz.getName());
                fail(clazz.getName() + " is not serializable");
            }            
            for (Field field : clazz.getDeclaredFields()){
                Set<Class<?>> types = new HashSet<Class<?>>(3);
                types.add(field.getType());
                if (field.getType().getSuperclass() != null){
                    types.add(field.getType().getSuperclass());    
                }                
                if (field.getType().getComponentType() != null){
{'fraction_non_alphanumeric': 0.12882123537551893, 'fraction_numerical': 0.0040256636054849665, 'mean_word_length': 3.1798107255520507, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6442201', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2177, 'n_words': 395}