List of usage examples for org.apache.commons.lang StringUtils countMatches
public static int countMatches(String str, String sub)
Counts how many times the substring appears in the larger String.
From source file:de.fhg.iais.asc.transformer.jdom.handler.XmlFieldEditorTest.java
@Test public void testSampler() throws Exception { Document xdoc = XmlProcessor.buildDocumentFrom(XML); XmlFieldEditor s = new XmlFieldEditor(FIELDPATTERN, CONTENTPATTERN, SEARCHTERMS, REPLACEMENT, NEWFIELDNAME); xdoc = this.transform(s, xdoc); String output = XmlUtils.elementToString(true, xdoc.getRootElement()); System.out.println(output);/* w w w. j ava2 s .c o m*/ final int p = StringUtils.countMatches(output, "publisher"); final int c = StringUtils.countMatches(output, "creator"); Assert.assertTrue("Expected 2 creator tags, found " + c, c == 2); Assert.assertTrue("Expected 4 publisher tags, found " + p, p == 4); }
From source file:com.callidusrobotics.droptables.view.ResultListViewTest.java
@Test public void renderFreeMarkerSuccess() throws Exception { // Unit under test renderer.render(view, Locale.ENGLISH, writer); // Verify results String result = writer.toString(); assertEquals("Exactly 1 ReportLog should have a null TTL", 1, StringUtils.countMatches(result, "These results will not be scheduled for deletion from the database")); for (int i = 0; i < results.size(); i++) { ResultEntry entry = results.get(i); assertTrue("Report ID field was not set for #" + i, result.contains(entry.getReport() == null ? "DELETED" : entry.getReport().getId().toString())); assertTrue("ID field was not set for #" + i, result.contains(entry.getId().toString())); for (Entry<String, String> kv : entry.getParameters().entrySet()) { assertTrue("Parameter Key was not set for #" + i, result.contains(kv.getKey())); assertTrue("Parameter Val was not set for #" + i, result.contains(kv.getValue())); }/*from w ww.ja v a 2 s .co m*/ } }
From source file:loaders.LoadQueryTransformerTest.java
@Test public void testParentBandCondition() throws Exception { String query = "select id as id from user where id = ${Root.parentBandParam}"; HashMap<String, Object> params = new HashMap<String, Object>(); AbstractDbDataLoader.QueryPack queryPack = prepareQuery(query, new BandData(""), params); System.out.println(queryPack.getQuery()); Assert.assertFalse(queryPack.getQuery().contains("${")); writeParams(queryPack);/* w w w . ja va 2 s . c o m*/ BandData parentBand = new BandData("Root"); parentBand.setData(new HashMap<String, Object>()); parentBand.addData("parentBandParam", "parentBandParam"); queryPack = prepareQuery(query, parentBand, params); System.out.println(queryPack.getQuery()); Assert.assertEquals(1, StringUtils.countMatches(queryPack.getQuery(), "?")); writeParams(queryPack); }
From source file:de.fhg.iais.asc.transformer.jdom.handler.XmlFieldStripperTest.java
@Test public void testSampler() throws Exception { Document xdoc = XmlProcessor.buildDocumentFrom(XML); XmlFieldStripper s = new XmlFieldStripper(FIELDPATTERN, CONTENTPATTERN); xdoc = this.transform(s, xdoc); String output = XmlUtils.elementToString(true, xdoc.getRootElement()); System.out.println(output);// w w w . ja v a 2 s. co m final int p = StringUtils.countMatches(output, "_"); Assert.assertTrue("Expected 2 strings '_', found " + p, p == 2); final int q = StringUtils.countMatches(output, "`"); Assert.assertTrue("Expected 2 strings '`', found " + q, q == 2); }
From source file:com.hp.autonomy.frontend.find.core.view.AbstractViewController.java
private String getBaseUrl(final HttpServletRequest request) { final String path = request.getRequestURI().replaceFirst(request.getContextPath(), ""); final int depth = StringUtils.countMatches(path, "/") - 1; final String baseUrl; if (depth == 0) { baseUrl = "."; } else {// w w w. j a va 2 s. co m baseUrl = StringUtils.repeat("../", depth); } return baseUrl; }
From source file:de.fhg.iais.asc.transformer.jdom.handler.XmlFieldStatsTest.java
@Test public void testSampler() throws Exception { FieldStatsMap stats = new FieldStatsMap(); Document xdoc = XmlProcessor.buildDocumentFrom(XML); XmlFieldStats s = new XmlFieldStats(stats, FIELDPATTERN); xdoc = this.transform(s, xdoc); String output = stats.toString(); final int p = StringUtils.countMatches(output, "test"); Assert.assertTrue("Expected 0 strings 'test', found " + p, p == 0); System.out.println(output);//w ww . java 2 s . c o m int numberOfDifferentTags = 0; int absoluteNumberOfTags = 0; Map<String, Integer> result = new HashMap<String, Integer>(); while (!output.isEmpty()) { int value = Integer.parseInt(output.charAt(output.indexOf(':') + 2) + ""); absoluteNumberOfTags += value; result.put(output.substring(0, output.indexOf(':')), value); output = output.substring(output.indexOf('\n') + 1); numberOfDifferentTags++; } //Test, whether the number of different tags is correct Assert.assertEquals("Expected 6 different types of tags, found " + numberOfDifferentTags, 6, numberOfDifferentTags); //Test, whether the number of absolute tags is correct Assert.assertEquals("Expected 14 tags, found " + absoluteNumberOfTags, 14, absoluteNumberOfTags); //Tests, whether the same key tags are found, containing the same values then expected for (Entry<String, Integer> es : result.entrySet()) { Assert.assertEquals(es.getValue(), expected.get(es.getKey())); } }
From source file:eionet.cr.dao.helpers.QueryHelper.java
private static int countSpaces(int spaces, String line) { int result = 0; int begin = StringUtils.countMatches(line, "{"); int end = StringUtils.countMatches(line, "}"); result = spaces + begin - end;//from www . ja va2 s . c o m if (result < 0) { return 0; } else { return result; } }
From source file:edu.msoe.se2800.h4.Logger.java
/** * Example usage:// w w w. j a va 2 s. c om * <p/> * <code> * public class PerfectPerson {<br/> * public static final String TAG = "Logger";<br/> * public PerfectPerson() {<br/> * Logger.INSTANCE.log(TAG, "My eyes are %s and my hair is %s", new String[]{"Green", "Blonde"});<br/> * }<br/> * }<br/> * </code><br/> * will produce (PerfectPerson, My eyes are Green and my hair is Blonde). * * @param tag of the class making the call. * @param message to be logged. Use %s to indicate fields. * @param args Strings to populate fields with. These need to be in the order they are found in * the message */ public void log(String tag, String message, String[] args) { synchronized (this) { OutputStreamWriter writer = null; try { writer = new OutputStreamWriter(new FileOutputStream(FILE_NAME, true)); DateFormat format = DateFormat.getInstance(); // Print the date/timestamp writer.write(format.format(new Date())); writer.write(" | "); // Print the tag writer.write(tag); writer.write(" | "); if (StringUtils.countMatches(message, "%s") != args.length) { throw new IllegalArgumentException("The number of placeholders in (" + message + ") was not the same as the number of args (" + args.length + ")."); } for (String s : args) { message = StringUtils.replaceOnce(message, "%s", s); } // Print the message writer.write(message); writer.write("\n"); } catch (FileNotFoundException e1) { System.out.println("The specified file could not be found."); } catch (IOException e) { System.out.println("Unable to connect to the logger. This call to log() will be ignored."); } finally { if (writer != null) { try { Closeables.close(writer, true); } catch (IOException e) { } } } } }
From source file:edu.cornell.mannlib.vitro.webapp.search.documentBuilding.VivoAgentContextNodeFieldsTest2.java
/** * Test how many times history is returned for context nodes * of the History department. /*from w w w.ja v a 2 s . c om*/ */ @Test public void testHistory() { Individual ind = new IndividualImpl(); ind.setURI(HISTORY_DEPT); VivoAgentContextNodeFields vacnf = new VivoAgentContextNodeFields(rdfServiceFactory); StringBuffer sb = vacnf.getValues(ind); assertNotNull(sb); String value = sb.toString(); assertTrue("Expected to get some text back from " + "VivoAgentContextNodeFields but got none", !value.trim().isEmpty()); int count = StringUtils.countMatches(value.toLowerCase(), "history"); System.out.println("histories: " + count); System.out.println("'" + value + "'"); // assertTrue("expected to have jane because SPCA advises jane", hasJane); }
From source file:com.intellij.lang.jsgraphql.endpoint.doc.psi.JSGraphQLEndpointDocPsiUtil.java
/** * Gets whether the specified comment is considered documentation, i.e. that it's placed directly above a type or field definition *//*www . ja v a 2 s.c om*/ public static boolean isDocumentationComment(PsiElement element) { if (element instanceof PsiComment) { PsiElement next = element.getNextSibling(); while (next != null) { final boolean isWhiteSpace = next instanceof PsiWhiteSpace; if (next instanceof PsiComment || isWhiteSpace) { if (isWhiteSpace && StringUtils.countMatches(next.getText(), "\n") > 1) { // a blank line before the next element, so this comment is not directly above it break; } next = next.getNextSibling(); } else { break; } } if (next instanceof JSGraphQLEndpointFieldDefinition || next instanceof JSGraphQLEndpointNamedTypeDefinition) { return true; } } return false; }