[jbosstools-commits] JBoss Tools SVN: r42578 - in trunk: cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/validation/java and 3 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Jul 13 16:41:32 EDT 2012


Author: vrubezhny
Date: 2012-07-13 16:41:30 -0400 (Fri, 13 Jul 2012)
New Revision: 42578

Modified:
   trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/CDIUIAllTests.java
   trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/validation/java/CDIAsYouTypeInJavaValidationTest.java
   trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/java/JavaDirtyRegionProcessor.java
   trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/validation/AbstractAsYouTypeValidationTest.java
   trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/validation/java/BaseAsYouTypeInJavaValidationTest.java
Log:
JBIDE-10611
As-you-type CDI validation 

Improved detection if CDI Validation is needed

JUnit Test Is added for CDI As-You-Type Validation

Modified: trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/CDIUIAllTests.java
===================================================================
--- trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/CDIUIAllTests.java	2012-07-13 18:33:50 UTC (rev 42577)
+++ trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/CDIUIAllTests.java	2012-07-13 20:41:30 UTC (rev 42578)
@@ -65,13 +65,8 @@
 		suite.addTestSuite(ELReferencesQueryParticipantTest.class);
 		suite.addTestSuite(CATest.class);
 		suite.addTestSuite(OpenCDINamedBeanDialogTest.class);
+		suite.addTestSuite(CDIAsYouTypeInJavaValidationTest.class); 
 
-		/*
-		 * The following test is reserved for CDI As-You-Type Validation:
-		 * 
-		 * suite.addTestSuite(CDIAsYouTypeInJavaValidationTest.class); 
-		 */
-
 		suiteAll.addTest(new CDICoreTestSetup(suite));
 
 		suiteAll.addTestSuite(AddQualifiersToBeanWizardTest.class);

Modified: trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/validation/java/CDIAsYouTypeInJavaValidationTest.java
===================================================================
--- trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/validation/java/CDIAsYouTypeInJavaValidationTest.java	2012-07-13 18:33:50 UTC (rev 42577)
+++ trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/validation/java/CDIAsYouTypeInJavaValidationTest.java	2012-07-13 20:41:30 UTC (rev 42578)
@@ -11,14 +11,12 @@
 package org.jboss.tools.cdi.ui.test.validation.java;
 
 import org.eclipse.core.resources.IProject;
-import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.source.Annotation;
 import org.jboss.tools.cdi.core.test.tck.TCKTest;
+import org.jboss.tools.common.base.test.validation.AbstractAsYouTypeValidationTest;
 import org.jboss.tools.common.base.test.validation.java.BaseAsYouTypeInJavaValidationTest;
-import org.jboss.tools.common.preferences.SeverityPreferences;
-import org.jboss.tools.jst.web.kb.WebKbPlugin;
-import org.jboss.tools.jst.web.kb.preferences.ELSeverityPreferences;
 
 /**
  * 
@@ -26,47 +24,123 @@
  *
  */
 public class CDIAsYouTypeInJavaValidationTest extends TCKTest {
-	private static final String PAGE_NAME = "JavaSource/org/jboss/jsr299/tck/tests/jbt/validation/el/TestBean.java";
-
+	private static final String PAGE_NAME = "JavaSource/org/jboss/jsr299/tck/tests/jbt/validation/NPEValidation.java";
+	private static final String RESOURCE_MARKER_TYPE = "org.jboss.tools.cdi.core.cdiproblem";
+	
 	private BaseAsYouTypeInJavaValidationTest baseTest = null;
 	protected IProject project;
 	
-	private static final String [][] EL2VALIDATE = 
+	private static final String [][] ANNOTATIONS2VALIDATE = 
 		{ 
-			{"#{namedBean.foos}", "\"foos\" cannot be resolved"}, 
-			{"#{snamedBean.foo}", "\"snamedBean\" cannot be resolved"},
-			{"#{['}", "EL syntax error: Expecting expression."}
+			{"@Inject", "Multiple beans are eligible for injection to the injection point [JSR-299 §5.2.1]"}, 
+			{"@Produces", "Producer cannot be declared in a decorator [JSR-299 §3.3.2]"}
 		};
 
 	public void setUp() throws Exception {
 		project = TCKTest.findTestProject();
 		if (baseTest == null) {
-			baseTest = new BaseAsYouTypeInJavaValidationTest(project);
+			baseTest = new BaseAsYouTypeInJavaValidationTest(project, RESOURCE_MARKER_TYPE);
 		}
 	}
 
-	public void testAsYouTypeInJavaValidation() throws JavaModelException, BadLocationException {
-/*
- * Reserved for a future test
- *
+	
+	public void testAsYouTypeInJavaValidation() throws BadLocationException, CoreException {
  		assertNotNull("Test project '" + TCKTest.MAIN_PROJECT_NAME + "' is not prepared", project);
-		baseTest.openEditor(PAGE_NAME);
-		IPreferenceStore store = WebKbPlugin.getDefault().getPreferenceStore();
-		String defaultValidateUnresolvedEL = SeverityPreferences.ENABLE;
-		String defaultUnknownELVariableName = SeverityPreferences.IGNORE;
-		try {
-			defaultValidateUnresolvedEL = store.getString(ELSeverityPreferences.RE_VALIDATE_UNRESOLVED_EL);
-			defaultUnknownELVariableName = store.getString(ELSeverityPreferences.UNKNOWN_EL_VARIABLE_NAME);
-			store.setValue(ELSeverityPreferences.RE_VALIDATE_UNRESOLVED_EL, SeverityPreferences.ENABLE);
-			store.setValue(ELSeverityPreferences.UNKNOWN_EL_VARIABLE_NAME, SeverityPreferences.ERROR);
-			for (int i = 0; i < EL2VALIDATE.length; i++) {
-				baseTest.doAsYouTipeInJavaValidationTest(EL2VALIDATE[i][0], EL2VALIDATE[i][1]);
+			for (int i = 0; i < ANNOTATIONS2VALIDATE.length; i++) {
+				baseTest.openEditor(PAGE_NAME);
+				try {
+					doAsYouTypeValidationMarkerAnnotationsRemovalTest(ANNOTATIONS2VALIDATE[i][0], ANNOTATIONS2VALIDATE[i][1]);
+				} finally {
+					baseTest.closeEditor();
+				}
 			}
-		} finally {
-			store.setValue(ELSeverityPreferences.RE_VALIDATE_UNRESOLVED_EL, defaultValidateUnresolvedEL);
-			store.setValue(ELSeverityPreferences.UNKNOWN_EL_VARIABLE_NAME, defaultUnknownELVariableName);
-			baseTest.closeEditor();
+	}
+	
+	
+	/**
+	 * The test procedure steps:
+	 * - Find EL by a given number
+	 * - Set up a broken EL and save the document => see problem marker appearance on that EL
+	 * - Set up a another broken EL => see annotation appearance on that EL instead of a problem marker 
+	 *   (an old problem marker has to disappear)
+	 * - Set up a good EL again => see annotation to disappear on that EL
+	 * 
+	 * @param goodEL
+	 * @param elToValidate
+	 * @param errorMessage
+	 * @param numberOfRegionToTest
+	 * @throws BadLocationException
+	 * @throws CoreException 
+	 */
+	public void doAsYouTypeValidationMarkerAnnotationsRemovalTest(String annotation, String errorMessage) throws BadLocationException, CoreException {
+
+		//============================
+		// The test procedure steps:
+		// - Find wrong annotation
+		//============================
+		
+		String documentContent = baseTest.getDocument().get();
+	
+		int start = (documentContent == null ? -1 : documentContent
+					.indexOf(annotation, 0));
+		assertFalse("No annotation " + annotation + " found in document", (start == -1));
+		int length = annotation.length();
+
+		// do check marker and marker annotation appeared here
+		int line = baseTest.getDocument().getLineOfOffset(start);
+		baseTest.assertResourceMarkerIsCreated(baseTest.getFile(), toRegex(errorMessage), line + 1);
+
+		Annotation problemAnnotation = baseTest.waitForAnnotation(
+				start, start + length, errorMessage, AbstractAsYouTypeValidationTest.MAX_SECONDS_TO_WAIT, true, true);
+		assertNotNull("Problem Marker Annotation for " + annotation + " not found!", problemAnnotation);
+		
+		String message = problemAnnotation.getText();
+		assertEquals(
+				"Not expected error message found in ProblemAnnotation. Expected: ["
+						+ errorMessage + "], Found: [" + message + "]",
+				errorMessage, message);
+		
+		//=================================================================================================
+		// - Remove broken Annotation => see error annotation to disappear 
+		//   (an old problem marker annotation has to disappear)
+		//=================================================================================================
+
+		baseTest.getDocument().replace(start, length, "");
+		
+		problemAnnotation = baseTest.waitForAnnotation(
+				start, start + length, null, AbstractAsYouTypeValidationTest.MAX_SECONDS_TO_WAIT, true, false); // Still use the same length (Just to have a place to look in)
+		assertNull("Problem Annotation has not disappeared!", problemAnnotation);
+
+		//=================================================================================================
+		// - Restore broken Annotation => see error annotation appearance 
+		//=================================================================================================
+
+		baseTest.getDocument().replace(start, 0, annotation);
+		
+		problemAnnotation = baseTest.waitForAnnotation(
+				start, start + length, errorMessage, AbstractAsYouTypeValidationTest.MAX_SECONDS_TO_WAIT, false, true);
+		
+		message = problemAnnotation.getText();
+		assertEquals(
+				"Not expected error message found in ProblemAnnotation. Expected: ["
+						+ errorMessage + "], Found: [" + message + "]",
+				errorMessage, message);
+
+		assertNotNull("No Problem Annotation found for wrong annotation " + annotation + "!", problemAnnotation);
+	}
+	
+	private String toRegex(String text) {
+		StringBuilder result = new StringBuilder(text);
+		
+		int i = -1;
+		while ((i = result.indexOf("[", i+1)) != -1) {
+			result.insert(i++, '\\');
 		}
- */
+		i = -1;
+		while ((i = result.indexOf("]", i+1)) != -1) {
+			result.insert(i++, '\\');
+		}
+		
+		return result.toString();
 	}
 }

Modified: trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/java/JavaDirtyRegionProcessor.java
===================================================================
--- trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/java/JavaDirtyRegionProcessor.java	2012-07-13 18:33:50 UTC (rev 42577)
+++ trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/java/JavaDirtyRegionProcessor.java	2012-07-13 20:41:30 UTC (rev 42578)
@@ -17,6 +17,8 @@
 import org.eclipse.core.resources.IFile;
 import org.eclipse.jdt.core.ICompilationUnit;
 import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IMember;
+import org.eclipse.jdt.core.ISourceRange;
 import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.ui.text.IJavaPartitions;
 import org.eclipse.jface.text.BadLocationException;
@@ -163,9 +165,7 @@
 		/**
 		 * This method removes from annotation model each annotation stored 
 		 * in JavaELProblemReporter.fAnnotations(Annotation, Position) that
-		 * 1) either has position inside [start,end] region;
-		 * 2) or exists in fAlwaysRemoveAnnotations
-		 * that indicates it should be removed without regard to its actual position.
+		 * has position inside [start,end] region;
 		 */
 		public void clearAnnotations(int start, int end) {
 			if (fAnnotations.isEmpty()) {
@@ -174,18 +174,37 @@
 			Annotation[] annotations = fAnnotations.toArray(new Annotation[0]);
 			for (Annotation annotation : annotations) {
 				Position position = getAnnotationModel().getPosition(annotation);
-				if (fAlwaysRemoveAnnotations.contains(annotation) || ( position != null && position.getOffset() >= start && 
+				if (!fAlwaysRemoveAnnotations.contains(annotation) && (position != null && position.getOffset() >= start && 
 						position.getOffset() <= end)) {
 					// remove annotation from managed annotations map as well as from the model
 					fAnnotations.remove(annotation);
-					if (fAlwaysRemoveAnnotations.contains(annotation))
-						fAlwaysRemoveAnnotations.remove(annotation);
 					getAnnotationModel().removeAnnotation(annotation);
 				}
 			}
 		}
 	
 		/**
+		 * This method removes from annotation model each annotation stored 
+		 * in JavaELProblemReporter.fAnnotations(Annotation, Position) that
+ 		 * or exists in fAlwaysRemoveAnnotations
+		 * that indicates it should be removed without regard to its actual position.
+		 */
+		public void clearAlwaysRemoveAnnotations() {
+			if (fAnnotations.isEmpty()) {
+				return;
+			}
+			Annotation[] annotations = fAnnotations.toArray(new Annotation[0]);
+			for (Annotation annotation : annotations) {
+				if (fAlwaysRemoveAnnotations.contains(annotation)) {
+					// remove annotation from managed annotations map as well as from the model
+					fAnnotations.remove(annotation);
+					fAlwaysRemoveAnnotations.remove(annotation);
+					getAnnotationModel().removeAnnotation(annotation);
+				}
+			}
+		}
+	
+		/**
 		 * Adds annotation to the annotation model, and stores it in fAnnotations (when cleanAllAnnotations = true, that indicates that 
 		 * the annotation should be removed without regard to the modified region, the annotation is stored in fAlwaysRemoveAnnotations as well). 
 		 * 
@@ -417,6 +436,7 @@
 //			} catch (BadLocationException e) {
 //				e.printStackTrace();
 //			}
+			fReporter.clearAlwaysRemoveAnnotations();
 			fValidatorManager.validateJavaElement(new Region(fStartRegionToProcess, fEndRegionToProcess - fStartRegionToProcess), fHelper, fReporter);			
 		}
 	}
@@ -456,14 +476,28 @@
 					continue;
 				}
 
-				element = unit.getElementAt(position++);
+				element = unit.getElementAt(position);
 				if (element == null)
 					continue;
 				
 				atLeastOneElementIsProcessed = true;
-				if (element.getElementType() != IJavaElement.METHOD) {
+				boolean doSkipThisElement = false;
+				if (element instanceof IMember && element.getElementType() == IJavaElement.METHOD) { 
+					ISourceRange range = ((IMember)element).getSourceRange();
+					if (position >= range.getOffset()) {
+						try {
+							String text = fDocument.get(range.getOffset(), position - range.getOffset() + 1); 
+							if (text.indexOf('{') != -1 && !text.endsWith("}")) {
+								doSkipThisElement = true;
+								position = range.getOffset() + range.getLength();
+							}
+						} catch (BadLocationException e) {
+							// Ignore it and do not skip validation
+						}
+						position++;
+					}
+				} else {
 					IJavaElement parent = element.getParent();
-					boolean doSkipThisElement = false;
 					while (parent != null && parent.getElementType() != IJavaElement.COMPILATION_UNIT) {
 						if (parent.getElementType() == IJavaElement.METHOD) {
 							doSkipThisElement = true;
@@ -471,10 +505,11 @@
 						}
 						parent = parent.getParent();
 					}
+					position++;
+				}
 					
-					if (!doSkipThisElement) 
-						return true;
-				}
+				if (!doSkipThisElement) 
+					return true;
 			}
 		} catch (JavaModelException e) {
 			LogHelper.logError(CommonValidationPlugin.getDefault(), e);

Modified: trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/validation/AbstractAsYouTypeValidationTest.java
===================================================================
--- trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/validation/AbstractAsYouTypeValidationTest.java	2012-07-13 18:33:50 UTC (rev 42577)
+++ trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/validation/AbstractAsYouTypeValidationTest.java	2012-07-13 20:41:30 UTC (rev 42578)
@@ -45,7 +45,7 @@
  */
 @SuppressWarnings("restriction")
 public abstract class AbstractAsYouTypeValidationTest extends TestCase {
-	private static final int MAX_SECONDS_TO_WAIT = 10;
+	public static final int MAX_SECONDS_TO_WAIT = 5;
 
 	protected String fileName;
 	protected IProject project = null;
@@ -104,6 +104,13 @@
 		return documentProvider.getAnnotationModel(textEditor.getEditorInput());
 	}
 
+	public IDocument getDocument() {
+		return document;
+	}
+	public IFile getFile() {
+		return file;
+	}
+	
 	protected abstract ISourceViewer getTextViewer();
 
 	/**
@@ -316,7 +323,7 @@
 		return true;
 	}
 	
-	private Annotation waitForAnnotation(final int start, final int end, final String errorMessage, final int seconds, final boolean markerAnnotation, final boolean waitForAppearance) {
+	public Annotation waitForAnnotation(final int start, final int end, final String errorMessage, final int seconds, final boolean markerAnnotation, final boolean waitForAppearance) {
 		final Annotation[] result = new Annotation[] { null };
 
 		Display.getDefault().syncExec(new Runnable() {

Modified: trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/validation/java/BaseAsYouTypeInJavaValidationTest.java
===================================================================
--- trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/validation/java/BaseAsYouTypeInJavaValidationTest.java	2012-07-13 18:33:50 UTC (rev 42577)
+++ trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/validation/java/BaseAsYouTypeInJavaValidationTest.java	2012-07-13 20:41:30 UTC (rev 42578)
@@ -34,11 +34,14 @@
 public class BaseAsYouTypeInJavaValidationTest extends AbstractAsYouTypeValidationTest {
 	public static final String MARKER_TYPE = "org.jboss.tools.common.validation.temp"; //$NON-NLS-1$
 	public static final String RESOURCE_MARKER_TYPE = "org.jboss.tools.jst.web.kb.elproblem"; //$NON-NLS-1$
+	private String fResourceMarkerType = null;
 
-	public BaseAsYouTypeInJavaValidationTest(IProject project) {
+	public BaseAsYouTypeInJavaValidationTest(IProject project, String resourceMarkerType) {
 		this.project = project;
+		this.fResourceMarkerType = resourceMarkerType;
 	}
 	public BaseAsYouTypeInJavaValidationTest() {
+		this.fResourceMarkerType = RESOURCE_MARKER_TYPE;
 	}
 	
 	@Override
@@ -87,20 +90,20 @@
 		String type;
 		try {
 			type = marker.getType();
-			return RESOURCE_MARKER_TYPE.equals(type);
+			return fResourceMarkerType.equals(type);
 		} catch (CoreException e) {
 			e.printStackTrace();
 		}
 		return false;
 	}
 	@Override
-	protected void assertResourceMarkerIsCreated(IFile file,
+	public void assertResourceMarkerIsCreated(IFile file,
 			String errorMessage, int line) throws CoreException {
 		IMarker[] markers = AbstractResourceMarkerTest.findMarkers(
-				file, RESOURCE_MARKER_TYPE, errorMessage, true);
+				file, fResourceMarkerType, errorMessage, true);
 
-		assertNotNull("Resource Marker not found for type: " + RESOURCE_MARKER_TYPE + ", message: [" + errorMessage + "] at line: " + line, markers);
-		assertFalse("Resource Marker not found for type: " + RESOURCE_MARKER_TYPE + ", message: [" + errorMessage + "] at line: " + line, markers.length == 0);
+		assertNotNull("Resource Marker not found for type: " + fResourceMarkerType + ", message: [" + errorMessage + "] at line: " + line, markers);
+		assertFalse("Resource Marker not found for type: " + fResourceMarkerType + ", message: [" + errorMessage + "] at line: " + line, markers.length == 0);
 
 		for (IMarker m : markers) {
 			Integer l = m.getAttribute(IMarker.LINE_NUMBER, -1);
@@ -109,6 +112,6 @@
 			}
 		}
 	
-		fail("Resource Marker not found for type: " + RESOURCE_MARKER_TYPE + ", message: [" + errorMessage + "] at line: " + line);
+		fail("Resource Marker not found for type: " + fResourceMarkerType + ", message: [" + errorMessage + "] at line: " + line);
 	}
 }



More information about the jbosstools-commits mailing list