<commit_before>/*
 * Copyright © EMC Corporation. All rights reserved.
 */
package radl.java.generation.spring;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;

import radl.common.io.IO;
import radl.common.xml.Xml;
import radl.core.code.SourceFile;
import radl.core.scm.SourceCodeManagementSystem;
import radl.java.code.JavaCode;
import radl.test.RadlBuilder;
import radl.test.RandomData;
import radl.test.TestUtil;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;


public class RadlToSpringServerTest {

  private static final RandomData RANDOM = new RandomData();

  private final RadlToSpringServer generator = new RadlToSpringServer();
  private final File radlFile = new File(TestUtil.randomDir(RadlToSpringServerTest.class), RANDOM.string() + ".radl");
  private final File baseDir = TestUtil.randomDir(RadlToSpringServerTest.class);

  @Before
  public void init() {
    IO.delete(baseDir);
    radlFile.getParentFile().mkdirs();
  }

  @After
  public void done() {
    IO.delete(radlFile);
    IO.delete(baseDir);
  }

  @Test
  public void generatesSpringServerSourceFilesFromRadl() throws IOException {
    String lower = "f";
    String upper = lower.toUpperCase(Locale.getDefault());
    String name = RANDOM.string();
    Document radlDocument = RadlBuilder.aRadlDocument()
        .withResource()
            .named(lower + name)
        .end()
    .build();
    PrintWriter writer = new PrintWriter(radlFile, "UTF8");
    try {
      writer.print(Xml.toString(radlDocument));
    } finally {
      writer.close();
    }
    String packagePrefix = somePackage();
    String generatedSourceSetDir = someSourceSetDir();
    String mainSourceSetDir = someSourceSetDir();
    SourceCodeManagementSystem scm = mock(SourceCodeManagementSystem.class);
    String header = RANDOM.string();

    generator.generate(radlFile, baseDir, packagePrefix, generatedSourceSetDir, mainSourceSetDir, scm, header);

    Collection<File> files = generatedFiles();
    File controller = find(files, upper + name + "Controller.java");
    assertNotNull("Missing controller: " + files, controller);
    String expectedPath = expectedFilePath(generatedSourceSetDir, packagePrefix, lower + name, controller);
    assertEquals("Path: " + expectedPath + " vs. " + controller.getPath(), expectedPath, controller.getPath());
    JavaCode javaCode = toJava(controller);
    TestUtil.assertCollectionEquals("Header for " + controller.getName(), Arrays.asList(header),
        javaCode.fileComments());

    File controllerHelper = find(files, upper + name + "ControllerHelper.java");
    assertNotNull("Missing controller helper: " + files, controllerHelper);
    expectedPath = expectedFilePath(mainSourceSetDir, packagePrefix, lower + name, controllerHelper);
    assertEquals("Path: " + expectedPath + " vs. " + controllerHelper.getPath(), expectedPath, controllerHelper.getPath());
  }

  private JavaCode toJava(File file) {
    return (JavaCode)new SourceFile(file.getPath()).code();
  }

  private String somePackage() {
    return "com." + RANDOM.string(3);
  }

  private String someSourceSetDir() {
    return RANDOM.string(8) + File.separator + RANDOM.string(8) + File.separator + RANDOM.string(8);
  }

  private File find(Collection<File> files, String name) {
    for (File file : files) {
      if (file.getName().equals(name)) {
        return file;
      }
    }
    return null;
  }

  private Collection<File> generatedFiles() {
    Collection<File> result = new ArrayList<File>();
    addFilesIn(baseDir, result);
    return result;
  }

  private void addFilesIn(File file, Collection<File> files) {
    if (file.isDirectory()) {
      for (File child : file.listFiles()) {
        addFilesIn(child, files);
      }
    } else if (file.isFile()) {
      files.add(file);
    }
  }

  private String expectedFilePath(String sourceSetDir, String packagePrefix, String packageName, File type) {
    return baseDir.getPath() + File.separator + sourceSetDir + File.separator
        + packagePrefix.replaceAll("\\.", '\\' + File.separator) + File.separator + packageName + File.separator
        + type.getName();
  }

}<commit_msg>Added debug info to trace mysterious failure in Travis CI<commit_after>/*
 * Copyright © EMC Corporation. All rights reserved.
 */
package radl.java.generation.spring;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;

import radl.common.io.IO;
import radl.common.xml.Xml;
import radl.core.code.SourceFile;
import radl.core.scm.SourceCodeManagementSystem;
import radl.java.code.JavaCode;
import radl.test.RadlBuilder;
import radl.test.RandomData;
import radl.test.TestUtil;


public class RadlToSpringServerTest {

  private static final RandomData RANDOM = new RandomData();

  private final RadlToSpringServer generator = new RadlToSpringServer();
  private final File radlFile = new File(TestUtil.randomDir(RadlToSpringServerTest.class), RANDOM.string() + ".radl");
  private final File baseDir = TestUtil.randomDir(RadlToSpringServerTest.class);

  @Before
  public void init() {
    IO.delete(baseDir);
    radlFile.getParentFile().mkdirs();
  }

  @After
  public void done() {
    IO.delete(radlFile);
    IO.delete(baseDir);
  }

  @Test
  public void generatesSpringServerSourceFilesFromRadl() throws IOException {
    String lower = "f";
    String upper = lower.toUpperCase(Locale.getDefault());
    String name = RANDOM.string();
    Document radlDocument = RadlBuilder.aRadlDocument()
        .withResource()
            .named(lower + name)
        .end()
    .build();
    PrintWriter writer = new PrintWriter(radlFile, "UTF8");
    try {
      writer.print(Xml.toString(radlDocument));
    } finally {
      writer.close();
    }
    String packagePrefix = somePackage();
    String generatedSourceSetDir = someSourceSetDir();
    String mainSourceSetDir = someSourceSetDir();
    SourceCodeManagementSystem scm = mock(SourceCodeManagementSystem.class);
    String header = RANDOM.string();

    generator.generate(radlFile, baseDir, packagePrefix, generatedSourceSetDir, mainSourceSetDir, scm, header);

    Collection<File> files = generatedFiles();
    File controller = find(files, upper + name + "Controller.java");
    assertNotNull("Missing controller: " + files, controller);
    String expectedPath = expectedFilePath(generatedSourceSetDir, packagePrefix, lower + name, controller);
    assertEquals("Controller path", expectedPath, controller.getAbsolutePath());
    JavaCode javaCode = toJava(controller);
    TestUtil.assertCollectionEquals("Header for " + controller.getName(), Arrays.asList(header),
        javaCode.fileComments());

    File controllerHelper = find(files, upper + name + "ControllerHelper.java");
    assertNotNull("Missing controller helper: " + files, controllerHelper);
    expectedPath = expectedFilePath(mainSourceSetDir, packagePrefix, lower + name, controllerHelper);
    assertEquals("Controller helper path", expectedPath, controllerHelper.getAbsolutePath());
  }

  private JavaCode toJava(File file) {
    return (JavaCode)new SourceFile(file.getPath()).code();
  }

  private String somePackage() {
    return "com." + RANDOM.string(3);
  }

  private String someSourceSetDir() {
    return RANDOM.string(8) + File.separator + RANDOM.string(8) + File.separator + RANDOM.string(8);
  }

  private File find(Collection<File> files, String name) {
    for (File file : files) {
      if (file.getName().equals(name)) {
        return file;
      }
    }
    return null;
  }

  private Collection<File> generatedFiles() {
    Collection<File> result = new ArrayList<File>();
    addFilesIn(baseDir, result);
    return result;
  }

  private void addFilesIn(File file, Collection<File> files) {
    if (file.isDirectory()) {
      for (File child : file.listFiles()) {
        addFilesIn(child, files);
      }
    } else if (file.isFile()) {
      files.add(file);
    }
  }

  private String expectedFilePath(String sourceSetDir, String packagePrefix, String packageName, File type) {
    return baseDir.getAbsolutePath() + File.separator + sourceSetDir + File.separator
        + packagePrefix.replaceAll("\\.", '\\' + File.separator) + File.separator + packageName + File.separator
        + type.getName();
  }

  private void assertEquals(String message, String expected, String actual) {
    try {
      Assert.assertEquals(message, expected, actual);
    } catch (AssertionError e) {
      System.err.println(e.getMessage()); // NOPMD
      throw e;
    }
  }
  
}
{'fraction_non_alphanumeric': 0.11759553268367459, 'fraction_numerical': 0.00131391656629804, 'mean_word_length': 4.0076754385964914, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3406070', 'n_tokens_mistral': 2676, 'n_tokens_neox': 2624, 'n_words': 618}