Ejemplo n.º 1
0
  public void execute()
      throws IOException, DocumentException, ParseException, InterruptedException,
          SubmissionException {
    FileReader r = new FileReader(problemJson);
    //		ProblemWithTestCases problemWithTestCases;
    //		try {
    //			problemWithTestCases = new ProblemReader().read(r);
    //		} finally {
    //			r.close();
    //		}

    ProblemAndTestCaseList problemWithTestCases = new ProblemAndTestCaseList();

    try {
      JSONConversion.readProblemAndTestCaseData(
          problemWithTestCases,
          ReflectionFactory.forClass(Problem.class),
          ReflectionFactory.forClass(TestCase.class),
          r);
    } finally {
      r.close();
    }

    // Set fake problem id and course id
    problemWithTestCases.getProblem().setProblemId(1);
    problemWithTestCases.getProblem().setCourseId(1);

    // Start a server thread for communicating with the builder
    ServerSocket serverSocket = new ServerSocket(OutOfProcessSubmitService.DEFAULT_PORT);
    this.serverTask = new ServerTask(serverSocket);
    Thread serverThread = new Thread(serverTask);
    serverThread.start();

    try {
      testAll(fileNameList, problemWithTestCases);
    } finally {
      serverTask.shutdown();
      serverThread.join();
      serverSocket.close();
    }
  }
Ejemplo n.º 2
0
  @Test
  public void testReadProblemAndTestCaseData() throws Exception {
    InputStream in = this.getClass().getResourceAsStream("testdata/exercise.json");
    try {
      Reader reader = new InputStreamReader(in, Charset.forName("UTF-8"));

      RepoProblemAndTestCaseList exercise = new RepoProblemAndTestCaseList();

      JSONConversion.readProblemAndTestCaseData(
          exercise,
          ReflectionFactory.forClass(RepoProblem.class),
          ReflectionFactory.forClass(RepoTestCase.class),
          reader);

      assertEquals(ProblemType.C_PROGRAM, exercise.getProblem().getProblemType());
      assertEquals("hello", exercise.getProblem().getTestname());
      assertEquals("Print hello, world", exercise.getProblem().getBriefDescription());
      assertTrue(
          exercise
              .getProblem()
              .getDescription()
              .startsWith("<p>Print a line with the following text:"));
      assertTrue(exercise.getProblem().getSkeleton().startsWith("#include <stdio.h>"));
      assertEquals(0, exercise.getProblem().getSchemaVersion());
      assertEquals("A. User", exercise.getProblem().getAuthorName());
      assertEquals("*****@*****.**", exercise.getProblem().getAuthorEmail());
      assertEquals("http://cs.unseen.edu/~auser", exercise.getProblem().getAuthorWebsite());
      assertEquals(1345230040466L, exercise.getProblem().getTimestampUtc());
      assertEquals(ProblemLicense.CC_ATTRIB_SHAREALIKE_3_0, exercise.getProblem().getLicense());

      assertEquals(1, exercise.getTestCaseData().size());
      RepoTestCase testCase = exercise.getTestCaseData().get(0);

      assertEquals("hello", testCase.getTestCaseName());
      assertEquals("", testCase.getInput());
      assertEquals("^\\s*Hello\\s*,\\s*world\\s*$i", testCase.getOutput());
      assertEquals(false, testCase.isSecret());
    } finally {
      in.close();
    }
  }
  @Override
  public ProblemAndTestCaseList importExercise(Course course, String exerciseHash)
      throws CloudCoderAuthenticationException {
    if (course == null || exerciseHash == null) {
      throw new IllegalArgumentException();
    }

    // Make sure a user is authenticated
    User user =
        ServletUtil.checkClientIsAuthenticated(
            getThreadLocalRequest(), GetCoursesAndProblemsServiceImpl.class);

    // Find user's registration in the course: if user is not instructor,
    // import is not allowed
    CourseRegistrationList reg = Database.getInstance().findCourseRegistrations(user, course);
    if (!reg.isInstructor()) {
      throw new CloudCoderAuthenticationException(
          "Only an instructor can import a problem in a course");
    }

    // Attempt to load the problem from the exercise repository.
    ConfigurationSetting repoUrlSetting =
        Database.getInstance().getConfigurationSetting(ConfigurationSettingName.PUB_REPOSITORY_URL);
    if (repoUrlSetting == null) {
      logger.error("Repository URL configuration setting is not set");
      return null;
    }

    // GET the exercise from the repository
    HttpGet get = new HttpGet(repoUrlSetting.getValue() + "/exercisedata/" + exerciseHash);
    ProblemAndTestCaseList exercise = null;

    HttpClient client = new DefaultHttpClient();
    try {
      HttpResponse response = client.execute(get);

      HttpEntity entity = response.getEntity();

      ContentType contentType = ContentType.getOrDefault(entity);
      Reader reader = new InputStreamReader(entity.getContent(), contentType.getCharset());

      exercise = new ProblemAndTestCaseList();
      exercise.setTestCaseList(new TestCase[0]);
      JSONConversion.readProblemAndTestCaseData(
          exercise,
          ReflectionFactory.forClass(Problem.class),
          ReflectionFactory.forClass(TestCase.class),
          reader);

      // Set the course id
      exercise.getProblem().setCourseId(course.getId());
    } catch (IOException e) {
      logger.error("Error importing exercise from repository", e);
      return null;
    } finally {
      client.getConnectionManager().shutdown();
    }

    // Set "when assigned" and "when due" to reasonable default values
    long now = System.currentTimeMillis();
    exercise.getProblem().setWhenAssigned(now);
    exercise.getProblem().setWhenDue(now + 48L * 60L * 60L * 1000L);

    // Set problem authorship as IMPORTED
    exercise.getProblem().setProblemAuthorship(ProblemAuthorship.IMPORTED);

    // For IMPORTED problems, parent_hash is actually the hash of the problem
    // itself.  If the problem is modified (and the authorship changed
    // to IMPORTED_AND_MODIFIED), then the (unchanged) parent_hash value
    // really does reflect the "parent" problem.
    exercise.getProblem().setParentHash(exerciseHash);

    // Store the exercise in the database
    exercise = Database.getInstance().storeProblemAndTestCaseList(exercise, course, user);

    return exercise;
  }