<commit_before>package classycle;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;

import junit.framework.TestCase;
import classycle.classfile.ConstantPoolPrinter;
import classycle.graph.AtomicVertex;
import classycle.graph.Vertex;

import com.sun.tools.javac.Main;

/**
 * 
 * 
 * @author <NAME>
 */
public class ParserTest extends TestCase {
  private static final Main JAVAC = new Main();
  private static final String TMP = "temporaryDirectory" + File.separator;
  private static final String CLASS_NAME = "Test";
  private static final String JAVA_FILE = TMP + CLASS_NAME + ".java";
  private static final String CLASS_FILE = TMP + CLASS_NAME + ".class";
  
  public static void main(String[] args) throws IOException {
    String file = "Test.java";
    Writer writer = new FileWriter(file);
    writer.write(args[0]);
    writer.close();
    if (compile(file) == 0) {
      ConstantPoolPrinter.main(new String[] {"Test.class"});
    }
  }
  
  private static int compile(String file)
  {
    return JAVAC.compile(new String[] {file, "-target", "1.1"});
  }

  private static AtomicVertex createVertex(String code) throws IOException {
    Writer writer = new FileWriter(JAVA_FILE);
    writer.write(code);
    writer.close();
    assertEquals("Exit code", 0, compile(JAVA_FILE));
    //System.out.println("======\n" + code);
    //ConstantPoolPrinter.main(new String[] {CLASS_FILE});
    return Parser.readClassFiles(new String[] {CLASS_FILE})[0];
  }

  public ParserTest(String name) {
    super(name);
  }
  
  protected void setUp() throws Exception {
    new File(TMP).mkdir();
  }

  protected void tearDown() throws Exception {
    new File(JAVA_FILE).delete();
    new File(CLASS_FILE).delete();
    new File(TMP).delete();
  }

  private void check(String[] expectedClasses, String javaCode) 
                                                          throws IOException {
    AtomicVertex vertex = createVertex(javaCode);
    HashSet classSet = new HashSet();
    for (int i = 0; i < expectedClasses.length; i++) {
      classSet.add(expectedClasses[i]);
    }
    for (int i = 0, n = vertex.getNumberOfOutgoingArcs(); i < n; i++) {
      Vertex v = vertex.getHeadVertex(i);
      String name = ((ClassAttributes) v.getAttributes()).getName();
      assertTrue(name + " not expected", classSet.contains(name));
      classSet.remove(name); 
    }
    assertEquals("number of classes (missing: " + classSet + ")", 
                 expectedClasses.length, 
                 vertex.getNumberOfOutgoingArcs());
  }

  public void testParseFieldDescriptor() throws IOException {
    check(new String[] {"java.lang.Object", "java.awt.color.ICC_ColorSpace", 
                        "java.lang.String", "java.awt.LayoutManager2",
                        "java.lang.StringBuffer"},
          "class Test { String[][] a; java.awt.LayoutManager2 b; int i;"
          + "StringBuffer[] sb;"
          + "double[] d; boolean[][] z; java.awt.color.ICC_ColorSpace cs;}");
  }

  public void testInvalidFieldDescriptors() throws IOException {
    check(new String[] {"java.lang.Object", "java.lang.String"},
          "class Test { String[] a = {\"La;a\", \"[Lb;?\", \"L;\", \"L ;\","
          + "\"La.;\", \"L.a;\", \"La..b;\", \"L1;\", };}");
  }
  
  public void testParseMethodDescriptor() throws IOException {
    check(new String[] {"java.lang.String", "java.lang.Integer",
                        "java.lang.Double", "java.lang.Boolean",
                        "java.lang.Byte", "java.lang.Object", 
                        "java.lang.Long", "java.lang.Short",
                        "java.lang.Exception", "java.lang.Class"},
          "interface Test { void a(String a); int b(); Integer c();"
          + "Exception d(double d, Double d2); int e(Boolean[][] z);"
          + "short f(Byte b, int[][] i); Short g(Long l, Class c);}");
  }
  
  public void testInvalidMethodDescriptors() throws IOException {
    check(new String[] {"java.lang.Object", "java.lang.String"},
          "class Test { String[] a = {\"(La;\", \"(La1;??\", \"(Lb;)\","
          + "\"(Lc;)?\", \"(Ld;)v\", \"(Le;)V?\", \"(Lf;)D?\", \"(Lg;)L1;\","
          + "\"(Lh;)Li;?\", \"([)Lj;\", \"(L2;)Lk;\", \"( )Ll;\", \"(d)Lm;\","
          + "\"(Ln;[)Lo;\", \"(Lp;L)V\"};}");
  }
  <commit_msg>add  - testReflection()  - testNoReflection()<commit_after>package classycle;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;

import junit.framework.TestCase;
import classycle.classfile.ConstantPoolPrinter;
import classycle.graph.AtomicVertex;
import classycle.graph.Vertex;
import classycle.util.StringPattern;
import classycle.util.TrueStringPattern;
import classycle.util.WildCardPattern;

import com.sun.tools.javac.Main;

/**
 * 
 * 
 * @author <NAME>
 */
public class ParserTest extends TestCase {
  private static final String REFLECTION_EXAMPLE 
      = "class Test { "
      + "  String[] a = {\"java.util.Vector\", \"hello\", \"www.w3c.org\"};"
      + "  Class c = Thread.class;"
      + "}";
  private static final Main JAVAC = new Main();
  private static final String TMP = "temporaryDirectory" + File.separator;
  private static final String CLASS_NAME = "Test";
  private static final String JAVA_FILE = TMP + CLASS_NAME + ".java";
  private static final String CLASS_FILE = TMP + CLASS_NAME + ".class";
  
  public static void main(String[] args) throws IOException {
    String file = "Test.java";
    Writer writer = new FileWriter(file);
    writer.write(args[0]);
    writer.close();
    if (compile(file) == 0) {
      ConstantPoolPrinter.main(new String[] {"Test.class"});
    }
  }
  
  private static int compile(String file)
  {
    return JAVAC.compile(new String[] {file, "-target", "1.1"});
  }

  private static AtomicVertex createVertex(String code) throws IOException 
  {
    return createVertex(code, new TrueStringPattern());
  }
  
  private static AtomicVertex createVertex(String code, 
                                           StringPattern reflectionPattern) 
                 throws IOException 
  {
    Writer writer = new FileWriter(JAVA_FILE);
    writer.write(code);
    writer.close();
    assertEquals("Exit code", 0, compile(JAVA_FILE));
    //System.out.println("======\n" + code);
    //ConstantPoolPrinter.main(new String[] {CLASS_FILE});
    return Parser.readClassFiles(new String[] {CLASS_FILE}, 
                                 new TrueStringPattern(), 
                                 reflectionPattern)[0];
  }

  public ParserTest(String name) {
    super(name);
  }
  
  protected void setUp() throws Exception {
    new File(TMP).mkdir();
  }

  protected void tearDown() throws Exception {
    new File(JAVA_FILE).delete();
    new File(CLASS_FILE).delete();
    new File(TMP).delete();
  }

  private void check(String[] expectedClasses, String javaCode) 
               throws IOException 
  {
     check(expectedClasses, javaCode, null); 
  }

  private void check(String[] expectedClasses, String javaCode, 
                     StringPattern reflectionPattern) 
               throws IOException {
    AtomicVertex vertex = createVertex(javaCode, reflectionPattern);
    HashSet classSet = new HashSet();
    for (int i = 0; i < expectedClasses.length; i++) {
      classSet.add(expectedClasses[i]);
    }
    for (int i = 0, n = vertex.getNumberOfOutgoingArcs(); i < n; i++) {
      Vertex v = vertex.getHeadVertex(i);
      String name = ((ClassAttributes) v.getAttributes()).getName();
      assertTrue(name + " not expected", classSet.contains(name));
      classSet.remove(name); 
    }
    assertEquals("number of classes (missing: " + classSet + ")", 
                 expectedClasses.length, 
                 vertex.getNumberOfOutgoingArcs());
  }

  public void testParseFieldDescriptor() throws IOException {
    check(new String[] {"java.lang.Object", "java.awt.color.ICC_ColorSpace", 
                        "java.lang.String", "java.awt.LayoutManager2",
                        "java.lang.StringBuffer"},
          "class Test { String[][] a; java.awt.LayoutManager2 b; int i;"
          + "StringBuffer[] sb;"
          + "double[] d; boolean[][] z; java.awt.color.ICC_ColorSpace cs;}");
  }
  
  public void testNoReflection() throws IOException
  {
    check(new String[] {"java.lang.Object", "java.lang.String", 
                        "java.lang.Class", "java.lang.NoClassDefFoundError",
                        "java.lang.ClassNotFoundException", 
                        "java.lang.Throwable"},
          REFLECTION_EXAMPLE);
  }

  public void testReflection() throws IOException
  {
    check(new String[] {"java.lang.Object", "java.lang.String", 
            "java.lang.Class", "java.lang.NoClassDefFoundError",
            "java.lang.ClassNotFoundException", 
            "java.lang.Throwable", 
            "java.util.Vector", "java.lang.Thread", 
            "hello", "www.w3c.org"},
            REFLECTION_EXAMPLE,
            new TrueStringPattern());
    check(new String[] {"java.lang.Object", "java.lang.String", 
            "java.lang.Class", "java.lang.NoClassDefFoundError",
            "java.lang.ClassNotFoundException", 
            "java.lang.Throwable", 
            "java.util.Vector", "java.lang.Thread"},
            REFLECTION_EXAMPLE,
            new WildCardPattern("java.*"));
  }

  public void testInvalidFieldDescriptors() throws IOException {
    check(new String[] {"java.lang.Object", "java.lang.String"},
          "class Test { String[] a = {\"La;a\", \"[Lb;?\", \"L;\", \"L ;\","
          + "\"La.;\", \"L.a;\", \"La..b;\", \"L1;\", };}");
  }
  
  public void testParseMethodDescriptor() throws IOException {
    check(new String[] {"java.lang.String", "java.lang.Integer",
                        "java.lang.Double", "java.lang.Boolean",
                        "java.lang.Byte", "java.lang.Object", 
                        "java.lang.Long", "java.lang.Short",
                        "java.lang.Exception", "java.lang.Class"},
          "interface Test { void a(String a); int b(); Integer c();"
          + "Exception d(double d, Double d2); int e(Boolean[][] z);"
          + "short f(Byte b, int[][] i); Short g(Long l, Class c);}");
  }
  
  public void testInvalidMethodDescriptors() throws IOException {
    check(new String[] {"java.lang.Object", "java.lang.String"},
          "class Test { String[] a = {\"(La;\", \"(La1;??\", \"(Lb;)\","
          + "\"(Lc;)?\", \"(Ld;)v\", \"(Le;)V?\", \"(Lf;)D?\", \"(Lg;)L1;\","
          + "\"(Lh;)Li;?\", \"([)Lj;\", \"(L2;)Lk;\", \"( )Ll;\", \"(d)Lm;\","
          + "\"(Ln;[)Lo;\", \"(Lp;L)V\"};}");
  }
  
{'fraction_non_alphanumeric': 0.16581228446267127, 'fraction_numerical': 0.0029825706030384936, 'mean_word_length': 2.612794612794613, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7547147', 'n_tokens_mistral': 3336, 'n_tokens_neox': 3217, 'n_words': 736}