### Question:
RadlValidatingVisitor implements IResourceDeltaVisitor, IResourceVisitor { @Override public boolean visit(IResourceDelta delta) throws CoreException { IResource resource = delta.getResource(); switch (delta.getKind()) { case IResourceDelta.ADDED: case IResourceDelta.CHANGED: validateRadl(resource); break; default: break; } return true; } RadlValidatingVisitor(Validator validator); @Override boolean visit(IResourceDelta delta); @Override boolean visit(IResource resource); static final String MARKER_TYPE; }### Answer:
@SuppressWarnings("unchecked") @Test public void createsMarkersForRadlIssues() throws CoreException { StringStream contents = new StringStream(RANDOM.string()); IResourceDelta delta = someResourceDelta(); IFile file = (IFile)delta.getResource(); when(file.getContents()).thenReturn(contents); final String error = RANDOM.string(); final int line1 = RANDOM.integer(); final int column1 = RANDOM.integer(); final String warning = RANDOM.string(); final int line2 = RANDOM.integer(); final int column2 = RANDOM.integer(); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Collection<Issue> issues = (Collection<Issue>)invocation.getArguments()[1]; issues.add(new Issue(Validator.class, Level.ERROR, line1, column1, error)); issues.add(new Issue(Validator.class, Level.WARNING, line2, column2, warning)); return null; } }).when(validator).validate(eq(contents), any(Collection.class)); final AtomicInteger numMarkers = new AtomicInteger(); final Marker errorMarker = mock(Marker.class); final Marker warningMarker = mock(Marker.class); when(file.createMarker(RadlValidatingVisitor.MARKER_TYPE)).thenAnswer(new Answer<Marker>() { @Override public Marker answer(InvocationOnMock invocation) throws Throwable { return numMarkers.incrementAndGet() == 1 ? errorMarker : warningMarker; } }); visitor.visit(delta); verify(errorMarker).setAttribute(Marker.SEVERITY, Marker.SEVERITY_ERROR); verify(errorMarker).setAttribute(Marker.LINE_NUMBER, line1); verify(errorMarker).setAttribute(Marker.MESSAGE, error); verify(warningMarker).setAttribute(Marker.SEVERITY, Marker.SEVERITY_WARNING); verify(warningMarker).setAttribute(Marker.LINE_NUMBER, line2); verify(warningMarker).setAttribute(Marker.MESSAGE, warning); }
@SuppressWarnings("unchecked") @Test public void validatesNewlyAddedRadlFile() throws CoreException { IResourceDelta delta = someResourceDelta(); when(delta.getKind()).thenReturn(IResourceDelta.ADDED); visitor.visit(delta); verify(validator).validate(any(InputStream.class), any(Collection.class)); }
@SuppressWarnings("unchecked") @Test public void doesntValidateDeletedRadlFile() throws CoreException { IResourceDelta delta = someResourceDelta(); when(delta.getKind()).thenReturn(IResourceDelta.REMOVED); visitor.visit(delta); verify(validator, never()).validate(any(InputStream.class), any(Collection.class)); }
@SuppressWarnings("unchecked") @Test public void doesntValidateNonRadlFile() throws CoreException { IResourceDelta delta = someResourceDelta(); when(delta.getResource().getFileExtension()).thenReturn(RANDOM.string(3)); visitor.visit(delta); verify(validator, never()).validate(any(InputStream.class), any(Collection.class)); }
@SuppressWarnings("unchecked") @Test public void doesntValidateNonFile() throws CoreException { IResourceDelta delta = someResourceDelta(); when(delta.getResource()).thenReturn(mock(IResource.class)); visitor.visit(delta); verify(validator, never()).validate(any(InputStream.class), any(Collection.class)); }
@SuppressWarnings("unchecked") @Test public void validatesDuringFullBuild() throws CoreException { IResource resource = someResourceDelta().getResource(); visitor.visit(resource); verify(validator).validate(any(InputStream.class), any(Collection.class)); }