@Test public void testAnalyze() throws Exception { final String prgXml = "i am CCL xml"; final StringReader stringReader = mock(StringReader.class); whenNew(StringReader.class).withArguments(prgXml).thenReturn(stringReader); final SAXBuilder saxBuilder = mock(SAXBuilder.class); whenNew(SAXBuilder.class).withNoArguments().thenReturn(saxBuilder); final Document document = mock(Document.class); when(saxBuilder.build(stringReader)).thenReturn(document); final Violation violation = mock(Violation.class); final Delegate delegate = mock(Delegate.class); when(delegate.analyze()).thenReturn(Collections.singleton(violation)); @SuppressWarnings("unchecked") final Enumeration<Delegate> delegates = mock(Enumeration.class); when(delegates.hasMoreElements()).thenReturn(Boolean.TRUE, Boolean.FALSE); when(delegates.nextElement()).thenReturn(delegate); mockStatic(Service.class); when(Service.providers(ArgumentMatchers.<SPInterface> any(), ArgumentMatchers.<ClassLoaders> any())) .thenReturn(delegates); Set<Violation> violations = new JdomAnalysisRule().analyze(prgXml); assertThat(violations).containsOnly(violation); }
@Override @SuppressWarnings("unchecked") public Set<Violation> analyze(final String prgXml) { final Set<Violation> violations = new HashSet<Violation>(); try { final ClassLoaders classLoaders = new ClassLoaders(); final Class<Document>[] classes = new Class[] { Document.class }; final Document[] objects = new Document[] { new SAXBuilder().build(new StringReader(prgXml)) }; classLoaders.put(Document.class.getClassLoader()); final Enumeration<Delegate> delegates = Service.providers(new SPInterface(Delegate.class, classes, objects), classLoaders); if (delegates != null) { String rulesToSkip = System.getProperty("rulesToSkip"); if (rulesToSkip != null) { rulesToSkip = ".*(" + rulesToSkip.replace(",", "|") + ")"; } while (delegates.hasMoreElements()) { Delegate delegate = delegates.nextElement(); if (rulesToSkip == null || !delegate.getClass().getName().matches(rulesToSkip)) { violations.addAll(delegate.analyze()); } } } } catch (final JDOMException | IOException e) { throw new JdomException("Failed to analyze XML.", e); } return violations; }
JdomAnalysisRule implements AnalysisRule { @Override @SuppressWarnings("unchecked") public Set<Violation> analyze(final String prgXml) { final Set<Violation> violations = new HashSet<Violation>(); try { final ClassLoaders classLoaders = new ClassLoaders(); final Class<Document>[] classes = new Class[] { Document.class }; final Document[] objects = new Document[] { new SAXBuilder().build(new StringReader(prgXml)) }; classLoaders.put(Document.class.getClassLoader()); final Enumeration<Delegate> delegates = Service.providers(new SPInterface(Delegate.class, classes, objects), classLoaders); if (delegates != null) { String rulesToSkip = System.getProperty("rulesToSkip"); if (rulesToSkip != null) { rulesToSkip = ".*(" + rulesToSkip.replace(",", "|") + ")"; } while (delegates.hasMoreElements()) { Delegate delegate = delegates.nextElement(); if (rulesToSkip == null || !delegate.getClass().getName().matches(rulesToSkip)) { violations.addAll(delegate.analyze()); } } } } catch (final JDOMException | IOException e) { throw new JdomException("Failed to analyze XML.", e); } return violations; } }
JdomAnalysisRule implements AnalysisRule { @Override @SuppressWarnings("unchecked") public Set<Violation> analyze(final String prgXml) { final Set<Violation> violations = new HashSet<Violation>(); try { final ClassLoaders classLoaders = new ClassLoaders(); final Class<Document>[] classes = new Class[] { Document.class }; final Document[] objects = new Document[] { new SAXBuilder().build(new StringReader(prgXml)) }; classLoaders.put(Document.class.getClassLoader()); final Enumeration<Delegate> delegates = Service.providers(new SPInterface(Delegate.class, classes, objects), classLoaders); if (delegates != null) { String rulesToSkip = System.getProperty("rulesToSkip"); if (rulesToSkip != null) { rulesToSkip = ".*(" + rulesToSkip.replace(",", "|") + ")"; } while (delegates.hasMoreElements()) { Delegate delegate = delegates.nextElement(); if (rulesToSkip == null || !delegate.getClass().getName().matches(rulesToSkip)) { violations.addAll(delegate.analyze()); } } } } catch (final JDOMException | IOException e) { throw new JdomException("Failed to analyze XML.", e); } return violations; }  }
JdomAnalysisRule implements AnalysisRule { @Override @SuppressWarnings("unchecked") public Set<Violation> analyze(final String prgXml) { final Set<Violation> violations = new HashSet<Violation>(); try { final ClassLoaders classLoaders = new ClassLoaders(); final Class<Document>[] classes = new Class[] { Document.class }; final Document[] objects = new Document[] { new SAXBuilder().build(new StringReader(prgXml)) }; classLoaders.put(Document.class.getClassLoader()); final Enumeration<Delegate> delegates = Service.providers(new SPInterface(Delegate.class, classes, objects), classLoaders); if (delegates != null) { String rulesToSkip = System.getProperty("rulesToSkip"); if (rulesToSkip != null) { rulesToSkip = ".*(" + rulesToSkip.replace(",", "|") + ")"; } while (delegates.hasMoreElements()) { Delegate delegate = delegates.nextElement(); if (rulesToSkip == null || !delegate.getClass().getName().matches(rulesToSkip)) { violations.addAll(delegate.analyze()); } } } } catch (final JDOMException | IOException e) { throw new JdomException("Failed to analyze XML.", e); } return violations; }  @Override @SuppressWarnings("unchecked") Set<Violation> analyze(final String prgXml); @Override @SuppressWarnings("unchecked") Set<Violation> getCheckedViolations(); }
JdomAnalysisRule implements AnalysisRule { @Override @SuppressWarnings("unchecked") public Set<Violation> analyze(final String prgXml) { final Set<Violation> violations = new HashSet<Violation>(); try { final ClassLoaders classLoaders = new ClassLoaders(); final Class<Document>[] classes = new Class[] { Document.class }; final Document[] objects = new Document[] { new SAXBuilder().build(new StringReader(prgXml)) }; classLoaders.put(Document.class.getClassLoader()); final Enumeration<Delegate> delegates = Service.providers(new SPInterface(Delegate.class, classes, objects), classLoaders); if (delegates != null) { String rulesToSkip = System.getProperty("rulesToSkip"); if (rulesToSkip != null) { rulesToSkip = ".*(" + rulesToSkip.replace(",", "|") + ")"; } while (delegates.hasMoreElements()) { Delegate delegate = delegates.nextElement(); if (rulesToSkip == null || !delegate.getClass().getName().matches(rulesToSkip)) { violations.addAll(delegate.analyze()); } } } } catch (final JDOMException | IOException e) { throw new JdomException("Failed to analyze XML.", e); } return violations; }  @Override @SuppressWarnings("unchecked") Set<Violation> analyze(final String prgXml); @Override @SuppressWarnings("unchecked") Set<Violation> getCheckedViolations();  }