<commit_before>/**
 * Copyright (c) 2012-2015 DataTorrent, Inc.
 * All rights reserved.
 */
package com.datatorrent.stram.webapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;

import org.codehaus.jackson.annotate.JsonTypeInfo.As;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;

import com.datatorrent.api.BaseOperator;
import com.datatorrent.stram.webapp.TypeDiscoverer.UI_TYPE;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

public class OperatorDiscoveryTest
{
  @Test
  public void testPropertyDiscovery() throws Exception
  {
    OperatorDiscoverer od = new OperatorDiscoverer();
    Assert.assertNotNull(od.getOperatorClass(BaseOperator.class.getName()));

    JSONObject desc = od.describeClass(TestOperator.class);
    System.out.println("\ntype info for " + TestOperator.class + ":\n" + desc.toString(2));

    JSONArray props = desc.getJSONArray("properties");
    Assert.assertNotNull("properties", props);
    Assert.assertEquals("properties " + props, 13, props.length());
    JSONObject mapProperty = props.getJSONObject(6);
    Assert.assertEquals("name " + mapProperty, "map", mapProperty.get("name"));
    Assert.assertEquals("canGet " + mapProperty, true, mapProperty.get("canGet"));
    Assert.assertEquals("canSet " + mapProperty, true, mapProperty.get("canSet"));
    Assert.assertEquals("type " + mapProperty, java.util.Map.class.getName(), mapProperty.get("type"));

    JSONArray typeArgs = mapProperty.getJSONArray("typeArgs");
    Assert.assertNotNull("typeArgs", typeArgs);
    Assert.assertEquals("typeArgs " + typeArgs, 2, typeArgs.length());
    Assert.assertEquals("", String.class.getName(), typeArgs.getJSONObject(0).get("type"));
    Assert.assertEquals("", Structured.class.getName(), typeArgs.getJSONObject(1).get("type"));

    JSONObject enumDesc = od.describeClass(Color.class);
    JSONArray enumNames = enumDesc.getJSONArray("enum");
    Assert.assertNotNull("enumNames", enumNames);
    Assert.assertEquals("", Color.BLUE.name(), enumNames.get(0));
    JSONArray enumProps = enumDesc.getJSONArray("properties");
    Assert.assertNotNull("properties", enumProps);
    Assert.assertEquals("props " + enumProps, 0, enumProps.length());

    JSONObject structuredProperty = props.getJSONObject(8);
    Assert.assertEquals("name " + structuredProperty, "nested", structuredProperty.get("name"));
    Assert.assertEquals("type " + structuredProperty, Structured.class.getName(), structuredProperty.get("type"));

    JSONObject genericArray = props.getJSONObject(3);
    Assert.assertEquals("name " + genericArray, "genericArray", genericArray.get("name"));
    Assert.assertEquals("type " + genericArray, Object[].class.getName(), genericArray.get("type"));

    JSONObject propProperty = props.getJSONObject(9);
    Assert.assertEquals("uitype " + propProperty, UI_TYPE.MAP.getName(), propProperty.get("uiType"));

    desc = od.describeClass(ExtendedOperator.class);
    props = desc.getJSONArray("properties");
    genericArray = props.getJSONObject(3);
    Assert.assertEquals("name " + genericArray, "genericArray", genericArray.get("name"));
    Assert.assertEquals("type " + genericArray, String[].class.getName(), genericArray.get("type"));

    // type is not a primitive type
    // fetch property meta data to find out how to render it
    desc = od.describeClass(Structured.class);
    System.out.println("\ntype info for " + Structured.class + ":\n" + desc.toString(2));

    desc = od.describeClass(Color.class);
    System.out.println("\ntype info for " + Color.class + ":\n" + desc.toString(2));
    
    desc = od.describeClass(Properties.class);
    System.out.println("\ntype info for " + Properties.class + ":\n" + desc.toString(2));
    
    desc = od.describeClass(HashMap.class);
    System.out.println("\ntype info for " + HashMap.class + ":\n" + desc.toString(2));

  }

  @Test
  public void testFindDescendants() throws Exception
  {
    OperatorDiscoverer od = new OperatorDiscoverer();
    od.includeJRE();

    System.out.println("The descendants list of java type java.util.Map: \n" + od.getDescendants("java.util.Map"));

    System.out.println("The descendants list of java type java.util.List: \n" + od.getDescendants("java.util.List"));

    System.out.println("The descendants list of concrete public type java.util.Map: \n" + od.getPublicConcreteDescendants("java.util.Map", Integer.MAX_VALUE));

    System.out.println("The descendants list of concrete public type java.util.List: \n" + od.getPublicConcreteDescendants("java.util.List", Integer.MAX_VALUE));
    
    Set<String> actualQueueClass = Sets.newHashSet();
    String[] jdkQueue = new String[] {ArrayBlockingQueue.class.getName(), DelayQueue.class.getName(), LinkedBlockingDeque.class.getName(), 
        LinkedBlockingQueue.class.getName(), LinkedTransferQueue.class.getName(), 
        PriorityBlockingQueue.class.getName(), SynchronousQueue.class.getName()};
    JSONArray queueJsonArray = od.getPublicConcreteDescendants("java.util.concurrent.BlockingQueue", Integer.MAX_VALUE);
    // at lease include all the classes in jdk
    Assert.assertTrue("All the queue class in jdk are expected in result ", queueJsonArray.length() >= jdkQueue.length);
    for (int i = 0; i < queueJsonArray.length(); i++) {
      actualQueueClass.add(queueJsonArray.getString(i));<commit_msg>Fix compile error on jdk1.6<commit_after>/**
 * Copyright (c) 2012-2015 DataTorrent, Inc.
 * All rights reserved.
 */
package com.datatorrent.stram.webapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;

import org.codehaus.jackson.annotate.JsonTypeInfo.As;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;

import com.datatorrent.api.BaseOperator;
import com.datatorrent.stram.webapp.TypeDiscoverer.UI_TYPE;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

public class OperatorDiscoveryTest
{
  @Test
  public void testPropertyDiscovery() throws Exception
  {
    OperatorDiscoverer od = new OperatorDiscoverer();
    Assert.assertNotNull(od.getOperatorClass(BaseOperator.class.getName()));

    JSONObject desc = od.describeClass(TestOperator.class);
    System.out.println("\ntype info for " + TestOperator.class + ":\n" + desc.toString(2));

    JSONArray props = desc.getJSONArray("properties");
    Assert.assertNotNull("properties", props);
    Assert.assertEquals("properties " + props, 13, props.length());
    JSONObject mapProperty = props.getJSONObject(6);
    Assert.assertEquals("name " + mapProperty, "map", mapProperty.get("name"));
    Assert.assertEquals("canGet " + mapProperty, true, mapProperty.get("canGet"));
    Assert.assertEquals("canSet " + mapProperty, true, mapProperty.get("canSet"));
    Assert.assertEquals("type " + mapProperty, java.util.Map.class.getName(), mapProperty.get("type"));

    JSONArray typeArgs = mapProperty.getJSONArray("typeArgs");
    Assert.assertNotNull("typeArgs", typeArgs);
    Assert.assertEquals("typeArgs " + typeArgs, 2, typeArgs.length());
    Assert.assertEquals("", String.class.getName(), typeArgs.getJSONObject(0).get("type"));
    Assert.assertEquals("", Structured.class.getName(), typeArgs.getJSONObject(1).get("type"));

    JSONObject enumDesc = od.describeClass(Color.class);
    JSONArray enumNames = enumDesc.getJSONArray("enum");
    Assert.assertNotNull("enumNames", enumNames);
    Assert.assertEquals("", Color.BLUE.name(), enumNames.get(0));
    JSONArray enumProps = enumDesc.getJSONArray("properties");
    Assert.assertNotNull("properties", enumProps);
    Assert.assertEquals("props " + enumProps, 0, enumProps.length());

    JSONObject structuredProperty = props.getJSONObject(8);
    Assert.assertEquals("name " + structuredProperty, "nested", structuredProperty.get("name"));
    Assert.assertEquals("type " + structuredProperty, Structured.class.getName(), structuredProperty.get("type"));

    JSONObject genericArray = props.getJSONObject(3);
    Assert.assertEquals("name " + genericArray, "genericArray", genericArray.get("name"));
    Assert.assertEquals("type " + genericArray, Object[].class.getName(), genericArray.get("type"));

    JSONObject propProperty = props.getJSONObject(9);
    Assert.assertEquals("uitype " + propProperty, UI_TYPE.MAP.getName(), propProperty.get("uiType"));

    desc = od.describeClass(ExtendedOperator.class);
    props = desc.getJSONArray("properties");
    genericArray = props.getJSONObject(3);
    Assert.assertEquals("name " + genericArray, "genericArray", genericArray.get("name"));
    Assert.assertEquals("type " + genericArray, String[].class.getName(), genericArray.get("type"));

    // type is not a primitive type
    // fetch property meta data to find out how to render it
    desc = od.describeClass(Structured.class);
    System.out.println("\ntype info for " + Structured.class + ":\n" + desc.toString(2));

    desc = od.describeClass(Color.class);
    System.out.println("\ntype info for " + Color.class + ":\n" + desc.toString(2));
    
    desc = od.describeClass(Properties.class);
    System.out.println("\ntype info for " + Properties.class + ":\n" + desc.toString(2));
    
    desc = od.describeClass(HashMap.class);
    System.out.println("\ntype info for " + HashMap.class + ":\n" + desc.toString(2));

  }

  @Test
  public void testFindDescendants() throws Exception
  {
    OperatorDiscoverer od = new OperatorDiscoverer();
    od.includeJRE();

    System.out.println("The descendants list of java type java.util.Map: \n" + od.getDescendants("java.util.Map"));

    System.out.println("The descendants list of java type java.util.List: \n" + od.getDescendants("java.util.List"));

    System.out.println("The descendants list of concrete public type java.util.Map: \n" + od.getPublicConcreteDescendants("java.util.Map", Integer.MAX_VALUE));

    System.out.println("The descendants list of concrete public type java.util.List: \n" + od.getPublicConcreteDescendants("java.util.List", Integer.MAX_VALUE));
    
    Set<String> actualQueueClass = Sets.newHashSet();
    String[] jdkQueue = new String[] {ArrayBlockingQueue.class.getName(), DelayQueue.class.getName(), LinkedBlockingDeque.class.getName(), 
        LinkedBlockingQueue.class.getName(), PriorityBlockingQueue.class.getName(), SynchronousQueue.class.getName()};
    JSONArray queueJsonArray = od.getPublicConcreteDescendants("java.util.concurrent.BlockingQueue", Integer.MAX_VALUE);
    // at lease include all the classes in jdk
    Assert.assertTrue("All the queue class in jdk are expected in result ", queueJsonArray.length() >= jdkQueue.length);
    for (int i = 0; i < queueJsonArray.length(); i++) {
      actualQueueClass.add(queueJsonArray.getString(i));
{'fraction_non_alphanumeric': 0.14036580178647384, 'fraction_numerical': 0.004593789876648235, 'mean_word_length': 4.952405063291139, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 7, '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': '6818808', 'n_tokens_mistral': 3454, 'n_tokens_neox': 3344, 'n_words': 664}