001: /*******************************************************************************
002: * Copyright (c) 2007 BEA Systems, Inc.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * wharley@bea.com - initial API and implementation
010: *
011: *******************************************************************************/package org.eclipse.jdt.compiler.apt.tests;
012:
013: import java.io.File;
014: import java.io.IOException;
015: import java.io.StringWriter;
016: import java.util.ArrayList;
017: import java.util.List;
018:
019: import javax.tools.JavaCompiler;
020: import javax.tools.ToolProvider;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * Tests for the implementation of javax.annotation.processing.Messager
026: * @since 3.3
027: */
028: public class MessagerTests extends TestCase {
029: // See corresponding usages in the MessagerProc class
030: private static final String MESSAGERPROCNAME = "org.eclipse.jdt.compiler.apt.tests.processors.messager.MessagerProc";
031:
032: @Override
033: protected void setUp() throws Exception {
034: super .setUp();
035: BatchTestUtils.init();
036: }
037:
038: /**
039: * Validate the testMessager test against the javac compiler.
040: * @throws IOException
041: */
042: public void testMessagerWithSystemCompiler() throws IOException {
043: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
044: internalTestMessager(compiler);
045: }
046:
047: /**
048: * Attempt to report errors on various elements, using the Eclipse compiler.
049: * @throws IOException
050: */
051: public void testMessagerWithEclipseCompiler() throws IOException {
052: JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();
053: internalTestMessager(compiler);
054: }
055:
056: /**
057: * Attempt to report errors on various elements.
058: * @throws IOException
059: */
060: private void internalTestMessager(JavaCompiler compiler)
061: throws IOException {
062: System.clearProperty(MESSAGERPROCNAME);
063: File targetFolder = TestUtils.concatPath(BatchTestUtils
064: .getSrcFolderName(), "targets", "errors");
065: BatchTestUtils.copyResources("targets/errors", targetFolder);
066:
067: // Turn on the MessagerProc - without this, it will just return without doing anything
068: List<String> options = new ArrayList<String>();
069: options.add("-A" + MESSAGERPROCNAME);
070:
071: // Invoke processing by compiling the targets.model resources
072: StringWriter errors = new StringWriter();
073: boolean success = BatchTestUtils.compileTreeWithErrors(
074: compiler, options, targetFolder, errors);
075:
076: assertTrue("errors should not be empty", errors.getBuffer()
077: .length() != 0);
078: assertTrue(
079: "Compilation should have failed due to expected errors, but it didn't",
080: !success);
081:
082: // If it succeeded, the processor will have set this property to "succeeded";
083: // if not, it will set it to an error value.
084: String property = System.getProperty(MESSAGERPROCNAME);
085: assertNotNull("No property", property);
086: assertEquals("succeeded", property);
087:
088: // TODO: check "errors" against expected values to ensure that the problems were correctly reported
089: }
090:
091: /* (non-Javadoc)
092: * @see junit.framework.TestCase#tearDown()
093: */
094: @Override
095: protected void tearDown() throws Exception {
096: System.clearProperty(MESSAGERPROCNAME);
097: super.tearDown();
098: }
099:
100: }
|