@Override
 public String getMessage() {
   StringBuilder fullMessage = new StringBuilder("Compilation failed");
   int i = 1;
   for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
     fullMessage.append("\n    ").append(i++).append(") ");
     Diagnostics.appendTo(fullMessage, diagnostic, 8);
   }
   return fullMessage.toString();
 }
예제 #2
0
  @Test
  public void test() {
    String str;

    /*
     * DiagnosisType.
     */

    DiagnosisType dt;

    dt = DiagnosisType.DUPLICATE;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.EMPTY;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.ERROR;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.ERROR_EXPECTED;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.INVALID;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.INVALID_DATA;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.INVALID_ENCODING;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.INVALID_EXPECTED;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.RECOMMENDED;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.RECOMMENDED_MISSING;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.REQUIRED_INVALID;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.RESERVED;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.UNDESIRED_DATA;
    str = dt.toString();
    Assert.assertNotNull(str);

    dt = DiagnosisType.UNKNOWN;
    str = dt.toString();
    Assert.assertNotNull(str);

    /*
     * Diagnosis.
     */

    Diagnosis d;
    Object[] messageArgs;

    try {
      new Diagnosis(null, null);
      Assert.fail("Exception expected!");
    } catch (IllegalArgumentException e) {
    }

    try {
      new Diagnosis(DiagnosisType.INVALID, null);
      Assert.fail("Exception expected!");
    } catch (IllegalArgumentException e) {
    }

    d = new Diagnosis(DiagnosisType.EMPTY, "Void");
    Assert.assertNotNull(d);
    str = d.toString();
    Assert.assertNotNull(str);
    Assert.assertEquals(DiagnosisType.EMPTY, d.type);
    Assert.assertEquals("Void", d.entity);
    Assert.assertEquals(0, d.information.length);

    messageArgs = d.getMessageArgs();
    Assert.assertEquals(1, messageArgs.length);
    Assert.assertEquals("Void", messageArgs[0]);

    d = new Diagnosis(DiagnosisType.UNKNOWN, "help", "me");
    Assert.assertNotNull(d);
    str = d.toString();
    Assert.assertNotNull(str);
    Assert.assertEquals(DiagnosisType.UNKNOWN, d.type);
    Assert.assertEquals("help", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("me", d.information[0]);

    messageArgs = d.getMessageArgs();
    Assert.assertEquals(2, messageArgs.length);
    Assert.assertEquals("help", messageArgs[0]);
    Assert.assertEquals("me", messageArgs[1]);

    d = new Diagnosis(DiagnosisType.RECOMMENDED, "help", "me", "NOW!");
    Assert.assertNotNull(d);
    str = d.toString();
    Assert.assertNotNull(str);
    Assert.assertEquals(DiagnosisType.RECOMMENDED, d.type);
    Assert.assertEquals("help", d.entity);
    Assert.assertEquals(2, d.information.length);
    Assert.assertEquals("me", d.information[0]);
    Assert.assertEquals("NOW!", d.information[1]);

    messageArgs = d.getMessageArgs();
    Assert.assertEquals(3, messageArgs.length);
    Assert.assertEquals("help", messageArgs[0]);
    Assert.assertEquals("me", messageArgs[1]);
    Assert.assertEquals("NOW!", messageArgs[2]);

    d = new Diagnosis(DiagnosisType.EMPTY, "entity", (String[]) null);
    Assert.assertNotNull(d);
    d = new Diagnosis(DiagnosisType.EMPTY, "entity", new String[] {});
    Assert.assertNotNull(d);
    d = new Diagnosis(DiagnosisType.EMPTY, "entity", "info1");
    Assert.assertNotNull(d);
    d = new Diagnosis(DiagnosisType.EMPTY, "entity", "info1", "info2");
    Assert.assertNotNull(d);

    try {
      d = new Diagnosis(DiagnosisType.ERROR, "entity", (String[]) null);
      Assert.fail("Exception expected!");
    } catch (IllegalArgumentException e) {
    }
    try {
      d = new Diagnosis(DiagnosisType.ERROR, "entity", new String[] {});
      Assert.fail("Exception expected!");
    } catch (IllegalArgumentException e) {
    }
    d = new Diagnosis(DiagnosisType.ERROR, "entity", "info1");
    Assert.assertNotNull(d);
    d = new Diagnosis(DiagnosisType.ERROR, "entity", "info1", "info2");
    Assert.assertNotNull(d);

    /*
     * Diagnostics.
     */

    Diagnostics<Diagnosis> ds;
    Diagnostics<Diagnosis> ds2;

    ds = new Diagnostics<Diagnosis>();
    Assert.assertFalse(ds.hasErrors());
    Assert.assertFalse(ds.hasWarnings());
    Assert.assertEquals(0, ds.getErrors().size());
    Assert.assertEquals(0, ds.getWarnings().size());

    d = new Diagnosis(DiagnosisType.EMPTY, "Void");
    Assert.assertNotNull(d);
    ds.addError(d);
    d = new Diagnosis(DiagnosisType.UNKNOWN, "help", "me");
    Assert.assertNotNull(d);
    ds.addWarning(d);

    Assert.assertTrue(ds.hasErrors());
    Assert.assertTrue(ds.hasWarnings());
    Assert.assertEquals(1, ds.getErrors().size());
    Assert.assertEquals(1, ds.getWarnings().size());

    d = ds.getErrors().get(0);
    Assert.assertNotNull(d);
    str = d.toString();
    Assert.assertNotNull(str);
    Assert.assertEquals(DiagnosisType.EMPTY, d.type);
    Assert.assertEquals("Void", d.entity);
    Assert.assertEquals(0, d.information.length);

    d = ds.getWarnings().get(0);
    Assert.assertNotNull(d);
    str = d.toString();
    Assert.assertNotNull(str);
    Assert.assertEquals(DiagnosisType.UNKNOWN, d.type);
    Assert.assertEquals("help", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("me", d.information[0]);

    d = new Diagnosis(DiagnosisType.RECOMMENDED, "help", "me", "NOW!");
    Assert.assertNotNull(d);
    ds.addWarning(d);

    Assert.assertTrue(ds.hasErrors());
    Assert.assertTrue(ds.hasWarnings());
    Assert.assertEquals(1, ds.getErrors().size());
    Assert.assertEquals(2, ds.getWarnings().size());

    d = ds.getWarnings().get(1);
    Assert.assertNotNull(d);
    str = d.toString();
    Assert.assertNotNull(str);
    Assert.assertEquals(DiagnosisType.RECOMMENDED, d.type);
    Assert.assertEquals("help", d.entity);
    Assert.assertEquals(2, d.information.length);
    Assert.assertEquals("me", d.information[0]);
    Assert.assertEquals("NOW!", d.information[1]);

    /*
     * addAll.
     */

    ds = new Diagnostics<Diagnosis>();
    ds2 = new Diagnostics<Diagnosis>();

    Assert.assertFalse(ds.hasErrors());
    Assert.assertFalse(ds.hasWarnings());
    Assert.assertEquals(0, ds.getErrors().size());
    Assert.assertEquals(0, ds.getWarnings().size());

    Assert.assertFalse(ds2.hasErrors());
    Assert.assertFalse(ds2.hasWarnings());
    Assert.assertEquals(0, ds2.getErrors().size());
    Assert.assertEquals(0, ds2.getWarnings().size());

    d = new Diagnosis(DiagnosisType.ERROR, "e_one_entity", "e_one_info");
    Assert.assertNotNull(d);
    ds.addError(d);

    d = new Diagnosis(DiagnosisType.UNKNOWN, "w_one_entity", "w_one_info");
    Assert.assertNotNull(d);
    ds.addWarning(d);

    d = new Diagnosis(DiagnosisType.INVALID, "e_two_entity", "e_two_info");
    Assert.assertNotNull(d);
    ds2.addError(d);

    d = new Diagnosis(DiagnosisType.RESERVED, "w_two_entity", "w_two_info");
    Assert.assertNotNull(d);
    ds2.addWarning(d);

    Assert.assertTrue(ds.hasErrors());
    Assert.assertTrue(ds.hasWarnings());
    Assert.assertEquals(1, ds.getErrors().size());
    Assert.assertEquals(1, ds.getWarnings().size());

    Assert.assertTrue(ds2.hasErrors());
    Assert.assertTrue(ds2.hasWarnings());
    Assert.assertEquals(1, ds2.getErrors().size());
    Assert.assertEquals(1, ds2.getWarnings().size());

    ds.addAll(null);
    ds2.addAll(null);
    ds.addAll(ds);
    ds2.addAll(ds2);

    Assert.assertTrue(ds.hasErrors());
    Assert.assertTrue(ds.hasWarnings());
    Assert.assertEquals(1, ds.getErrors().size());
    Assert.assertEquals(1, ds.getWarnings().size());

    Assert.assertTrue(ds2.hasErrors());
    Assert.assertTrue(ds2.hasWarnings());
    Assert.assertEquals(1, ds2.getErrors().size());
    Assert.assertEquals(1, ds2.getWarnings().size());

    d = ds.getErrors().get(0);
    Assert.assertEquals(DiagnosisType.ERROR, d.type);
    Assert.assertEquals("e_one_entity", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("e_one_info", d.information[0]);

    d = ds.getWarnings().get(0);
    Assert.assertEquals(DiagnosisType.UNKNOWN, d.type);
    Assert.assertEquals("w_one_entity", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("w_one_info", d.information[0]);

    Assert.assertTrue(ds2.hasErrors());
    Assert.assertTrue(ds2.hasWarnings());
    Assert.assertEquals(1, ds2.getErrors().size());
    Assert.assertEquals(1, ds2.getWarnings().size());

    d = ds2.getErrors().get(0);
    Assert.assertEquals(DiagnosisType.INVALID, d.type);
    Assert.assertEquals("e_two_entity", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("e_two_info", d.information[0]);

    d = ds2.getWarnings().get(0);
    Assert.assertEquals(DiagnosisType.RESERVED, d.type);
    Assert.assertEquals("w_two_entity", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("w_two_info", d.information[0]);

    ds.addAll(ds2);

    Assert.assertTrue(ds2.hasErrors());
    Assert.assertTrue(ds2.hasWarnings());
    Assert.assertEquals(1, ds2.getErrors().size());
    Assert.assertEquals(1, ds2.getWarnings().size());

    d = ds2.getErrors().get(0);
    Assert.assertEquals(DiagnosisType.INVALID, d.type);
    Assert.assertEquals("e_two_entity", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("e_two_info", d.information[0]);

    d = ds2.getWarnings().get(0);
    Assert.assertEquals(DiagnosisType.RESERVED, d.type);
    Assert.assertEquals("w_two_entity", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("w_two_info", d.information[0]);

    Assert.assertTrue(ds.hasErrors());
    Assert.assertTrue(ds.hasWarnings());
    Assert.assertEquals(2, ds.getErrors().size());
    Assert.assertEquals(2, ds.getWarnings().size());

    d = ds.getErrors().get(0);
    Assert.assertEquals(DiagnosisType.ERROR, d.type);
    Assert.assertEquals("e_one_entity", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("e_one_info", d.information[0]);

    d = ds.getWarnings().get(0);
    Assert.assertEquals(DiagnosisType.UNKNOWN, d.type);
    Assert.assertEquals("w_one_entity", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("w_one_info", d.information[0]);

    d = ds.getErrors().get(1);
    Assert.assertEquals(DiagnosisType.INVALID, d.type);
    Assert.assertEquals("e_two_entity", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("e_two_info", d.information[0]);

    d = ds.getWarnings().get(1);
    Assert.assertEquals(DiagnosisType.RESERVED, d.type);
    Assert.assertEquals("w_two_entity", d.entity);
    Assert.assertEquals(1, d.information.length);
    Assert.assertEquals("w_two_info", d.information[0]);

    /*
     * reset.
     */

    Assert.assertTrue(ds.hasErrors());
    Assert.assertTrue(ds.hasWarnings());
    Assert.assertEquals(2, ds.getErrors().size());
    Assert.assertEquals(2, ds.getWarnings().size());

    ds.reset();

    Assert.assertFalse(ds.hasErrors());
    Assert.assertFalse(ds.hasWarnings());
    Assert.assertEquals(0, ds.getErrors().size());
    Assert.assertEquals(0, ds.getWarnings().size());

    Assert.assertTrue(ds2.hasErrors());
    Assert.assertTrue(ds2.hasWarnings());
    Assert.assertEquals(1, ds2.getErrors().size());
    Assert.assertEquals(1, ds2.getWarnings().size());

    ds2.reset();

    Assert.assertFalse(ds2.hasErrors());
    Assert.assertFalse(ds2.hasWarnings());
    Assert.assertEquals(0, ds2.getErrors().size());
    Assert.assertEquals(0, ds2.getWarnings().size());
  }
예제 #3
0
 /**
  * Report bit field errors as diagnoses.
  *
  * @param bfErrors bit field with indicated errors
  * @param diagnostics diagnostics object used to report diagnoses
  */
 public static void report_error(int bfErrors, Diagnostics<Diagnosis> diagnostics) {
   if (diagnostics == null) {
     throw new IllegalArgumentException("'diagnostics' argument is null");
   }
   if ((bfErrors & E_BIT_EOF) != 0) {
     diagnostics.addError(new Diagnosis(DiagnosisType.ERROR, "header/line", "Unexpected EOF"));
   }
   if ((bfErrors & E_BIT_MISPLACED_CR) != 0) {
     diagnostics.addError(new Diagnosis(DiagnosisType.ERROR, "header/line", "Misplaced CR"));
   }
   if ((bfErrors & E_BIT_MISSING_CR) != 0) {
     diagnostics.addError(new Diagnosis(DiagnosisType.ERROR, "header/line", "Missing CR"));
   }
   if ((bfErrors & E_BIT_UNEXPECTED_CR) != 0) {
     diagnostics.addError(new Diagnosis(DiagnosisType.ERROR, "header/line", "Unexpected CR"));
   }
   if ((bfErrors & E_BIT_INVALID_UTF8_ENCODING) != 0) {
     diagnostics.addError(
         new Diagnosis(DiagnosisType.ERROR, "header/line", "Invalid UTF-8 encoded character"));
   }
   if ((bfErrors & E_BIT_INVALID_US_ASCII_CHAR) != 0) {
     diagnostics.addError(
         new Diagnosis(DiagnosisType.ERROR, "header/line", "Invalid US-ASCII character"));
   }
   if ((bfErrors & E_BIT_INVALID_CONTROL_CHAR) != 0) {
     diagnostics.addError(
         new Diagnosis(DiagnosisType.ERROR, "header/line", "Invalid control character"));
   }
   if ((bfErrors & E_BIT_INVALID_SEPARATOR_CHAR) != 0) {
     diagnostics.addError(
         new Diagnosis(DiagnosisType.ERROR, "header/line", "Invalid separator character"));
   }
   if ((bfErrors & E_BIT_MISSING_QUOTE) != 0) {
     diagnostics.addError(
         new Diagnosis(DiagnosisType.ERROR, "header/line", "Missing quote character"));
   }
   if ((bfErrors & E_BIT_MISSING_QUOTED_PAIR_CHAR) != 0) {
     diagnostics.addError(
         new Diagnosis(DiagnosisType.ERROR, "header/line", "Missing quoted pair character"));
   }
   if ((bfErrors & E_BIT_INVALID_QUOTED_PAIR_CHAR) != 0) {
     diagnostics.addError(
         new Diagnosis(DiagnosisType.ERROR, "header/line", "Invalid quoted pair character"));
   }
 }