Example usage for org.apache.commons.lang StringUtils countMatches

List of usage examples for org.apache.commons.lang StringUtils countMatches

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils countMatches.

Prototype

public static int countMatches(String str, String sub) 

Source Link

Document

Counts how many times the substring appears in the larger String.

Usage

From source file:org.openmrs.module.atomfeed.web.AtomFeedDownloadServletTest.java

/**
 * @see {@link AtomFeedDownloadServlet#doGet(HttpServletRequest,HttpServletResponse)}
 *///from   w ww .  j a v  a 2s  .  c o m
@Test
@Verifies(value = "should include all entries if no valid asOfDate is specified", method = "doGet(HttpServletRequest,HttpServletResponse)")
public void doGet_shouldIncludeAllEntriesIfNoValidAsOfDateIsSpecified() throws Exception {
    //ensures that at least we have an entry to exclude for testing purposes
    AtomFeedUtil.objectCreated(new Encounter());

    AtomFeedDownloadServlet atomFeedDownloadServlet = new AtomFeedDownloadServlet();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/atomfeed");

    Thread.sleep(1000);

    request.setParameter("asOfDate", "");
    MockHttpServletResponse response = new MockHttpServletResponse();

    AtomFeedUtil.objectCreated(new Encounter());
    AtomFeedUtil.objectCreated(new Concept());

    Thread.sleep(2000);//wait for 2 seconds for the feed to get updated
    atomFeedDownloadServlet.service(request, response);

    //should have returned all entries
    Assert.assertTrue(StringUtils.countMatches(response.getContentAsString(), "<entry>") > 2);
}

From source file:org.openmrs.module.htmlformentry.EncounterTypeTagTest.java

@Test
public void encounterTypeTag_shouldIncludeOnlyTheSpecifiedEncounterTypesOptions() throws Exception {
    String htmlform = "<htmlform><encounterType types=\"1\" /></htmlform>";
    FormEntrySession session = new FormEntrySession(null, htmlform, null);
    Assert.assertTrue(session.getHtmlToDisplay()
            .indexOf("<option value=\"\">htmlformentry.chooseEncounterType</option>") > -1);
    Assert.assertEquals(2, StringUtils.countMatches(session.getHtmlToDisplay(), "<option value=\""));
}

From source file:org.openmrs.module.htmlformentry.EncounterTypeTagTest.java

@Test
public void encounterTypeTag_shouldIncludeAllEncounterTypesIfNoneAreSpecified() throws Exception {
    String htmlform = "<htmlform><encounterType/></htmlform>";
    FormEntrySession session = new FormEntrySession(null, htmlform, null);
    Assert.assertTrue(session.getHtmlToDisplay()
            .indexOf("<option value=\"\">htmlformentry.chooseEncounterType</option>") > -1);
    Assert.assertEquals(1 + Context.getEncounterService().getAllEncounterTypes().size(),
            StringUtils.countMatches(session.getHtmlToDisplay(), "<option value=\""));
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.PersonControllerTest.java

/**
 * Tests if voided names are not shown in full representation. Adds a name and shows full
 * representation. Then voids the name and sees decrease in number of names in full
 * representation./* www. j ava  2 s . c o  m*/
 * 
 * @see PersonController#getProperty(Person,String)
 * @verifies do not show voided names in full representation
 * @throws Exception
 */
@Test
public void shouldNotShowVoidedNamesInFullRepresentation() throws Exception {
    Person p = new PersonResource().getByUniqueId("5946f880-b197-400b-9caa-a3c661d23041");
    PersonName pn = new PersonName("SUNNY", "TEST", "PURKAYASTHA");
    p.addName(pn);
    Context.getPersonService().savePerson(p);
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);
    Object result = new PersonController().retrieve("5946f880-b197-400b-9caa-a3c661d23041", req);
    Assert.assertEquals(2,
            StringUtils.countMatches(PropertyUtils.getProperty(result, "names").toString(), "givenName"));
    pn.setVoided(true);
    Context.getPersonService().savePerson(p);
    result = new PersonController().retrieve("5946f880-b197-400b-9caa-a3c661d23041", req);
    Assert.assertEquals(1,
            StringUtils.countMatches(PropertyUtils.getProperty(result, "names").toString(), "givenName"));
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.PersonControllerTest.java

/**
 * Tests if voided attributes are not shown in representation. Voids all the person attributes
 * and checks if they are not shown/*www  .ja v  a2 s  . c o  m*/
 * 
 * @see PersonController#getProperty(Person,String)
 * @verifies do not show voided attributes
 * @throws Exception
 */
@Test
public void shouldNotShowVoidedAttributesInRepresentation() throws Exception {
    Person p = new PersonResource().getByUniqueId("5946f880-b197-400b-9caa-a3c661d23041");
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);
    Object result = new PersonController().retrieve("5946f880-b197-400b-9caa-a3c661d23041", req);
    Assert.assertEquals(3, ((Collection<?>) PropertyUtils.getProperty(result, "attributes")).size());
    Set<PersonAttribute> attributes = p.getAttributes();
    for (PersonAttribute pa : attributes) {
        pa.setVoided(true);
    }
    p.setAttributes(attributes);
    Context.getPersonService().savePerson(p);
    result = new PersonController().retrieve("5946f880-b197-400b-9caa-a3c661d23041", req);
    Assert.assertEquals(0,
            StringUtils.countMatches(PropertyUtils.getProperty(result, "attributes").toString(), "value"));
}

From source file:org.openmrs.module.xforms.formentry.FormEntryQueueProcessorTest.java

/**
 * @see {@link FormEntryQueueProcessor#transformFormEntryQueue(FormEntryQueue,null)}
 *//*from   ww  w  . j  av a 2s . co m*/
@Test
@Verifies(value = "should transform xml data with a serialized complex obs", method = "transformFormEntryQueue(FormEntryQueue,null)")
public void transformFormEntryQueue_shouldTransformXmlDataWithASerializedComplexObs() throws Exception {
    String handlerName = "NeighborHandler";

    Context.getObsService().registerHandler(handlerName, new NeighborObsHandler());
    try {

        String xml = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(TEST_XFORM_FILE));
        FormEntryQueue formEntryQueue = new FormEntryQueue();
        formEntryQueue.setCreator(Context.getAuthenticatedUser());
        formEntryQueue.setDateCreated(new Date());
        formEntryQueue.setFormData(xml);
        formEntryQueue.setFileSystemUrl("/path");

        HL7InQueue hl7inQueue = new FormEntryQueueProcessor().transformFormEntryQueue(formEntryQueue, true);
        Assert.assertEquals(2, StringUtils.countMatches(hl7inQueue.getHL7Data(), "OBX"));
        Assert.assertTrue(hl7inQueue.getHL7Data()
                .indexOf("{\"firstname\":\"Horatio\", \"lastname\":\"Hornblower\"}") > 0);
        Assert.assertTrue(
                hl7inQueue.getHL7Data().indexOf("{\"firstname\":\"John\", \"lastname\":\"Doe\"}") > 0);
    } finally {
        Context.getObsService().removeHandler(handlerName);
    }
}

From source file:org.openmrs.Obs.java

/**
 * Sets the namespace and path of the form field that was used to capture the obs details in the
 * form.<br>/*from  w  w w  .  j a  va  2 s.c  om*/
 * <b>Note:</b> Namespace and formFieldPath together must not exceed 254 characters in length,
 * form applications can subtract the length of their namespace from 254 to determine the
 * maximum length they can use for a form field path.
 * 
 * @param namespace the namespace of the form field
 * @param formFieldPath the path of the form field
 * @since 1.11
 * @should set the underlying formNamespaceAndPath in the correct pattern
 * @should reject a namepace containing the separator
 * @should reject a path containing the separator
 * @should reject a namepace and path combination longer than the max length
 */
public void setFormField(String namespace, String formFieldPath) {
    if (namespace == null && formFieldPath == null) {
        return;
    }

    String nsAndPathTemp = "";
    if (StringUtils.isNotBlank(namespace) && StringUtils.isNotBlank(formFieldPath)) {
        nsAndPathTemp = namespace + FORM_NAMESPACE_PATH_SEPARATOR + formFieldPath;
    } else if (StringUtils.isNotBlank(namespace)) {
        nsAndPathTemp = namespace + FORM_NAMESPACE_PATH_SEPARATOR;
    } else if (StringUtils.isNotBlank(formFieldPath)) {
        nsAndPathTemp = FORM_NAMESPACE_PATH_SEPARATOR + formFieldPath;
    }

    if (nsAndPathTemp.length() > FORM_NAMESPACE_PATH_MAX_LENGTH) {
        throw new APIException("Obs.namespaceAndPathTooLong", (Object[]) null);
    }
    if (StringUtils.countMatches(nsAndPathTemp, FORM_NAMESPACE_PATH_SEPARATOR) > 1) {
        throw new APIException("Obs.namespaceAndPathNotContainSeparator", (Object[]) null);
    }

    formNamespaceAndPath = nsAndPathTemp;
}

From source file:org.opennms.features.topology.app.internal.IpLikeSearchProvider.java

/**
 * Validate if the query string has any chance of being an IPLIKE query.
 * /*from w  ww  .  j  a  v  a  2 s. co m*/
 * FIXME: validates iplike strings that have an octet ending in '-' such as:
 * 10.7-.*.*
 * 
 * @param queryString
 * @return true if the string contains a '*' or '-' or ',' ".
 */
private boolean isIpLikeQuery(String queryString) {

    //        Matcher iplikeMatcher = m_iplikePattern.matcher(queryString);
    //        return iplikeMatcher.matches();

    boolean validity = false;

    int ipv4delimCnt = StringUtils.countMatches(queryString, ".");
    int ipv6delimCnt = StringUtils.countMatches(queryString, ":");

    if ((ipv4delimCnt == 3 || ipv6delimCnt == 7) && !StringUtils.endsWith(queryString, "-")) {
        validity = true;
    } else {
        validity = false;
    }

    return validity;

}

From source file:org.opennms.netmgt.provision.service.dns.DnsRequisitionUrlConnection.java

/**
 * Validate the format is://w  ww.  ja v  a  2  s.  c  o  m
 *   dns://<host>/<zone>/?expression=<regex>
 *
 *   there should be only one arguement in the path
 *   there should only be one query parameter
 *
 * @param url a {@link java.net.URL} object.
 * @throws java.net.MalformedURLException if any.
 */
protected static void validateDnsUrl(URL url) throws MalformedURLException {

    String path = url.getPath();
    path = StringUtils.removeStart(path, "/");
    path = StringUtils.removeEnd(path, "/");

    if (path == null || StringUtils.countMatches(path, "/") > 1) {
        throw new MalformedURLException("The specified DNS URL contains invalid path: " + url);
    }

    String query = url.getQuery();

    if ((query != null) && (determineExpressionFromUrl(url) == null) && (getArgs().get(SERVICES_ARG) == null)
            && (getArgs().get(FID_HASH_SRC_ARG) == null)) {
        throw new MalformedURLException("The specified DNS URL contains an invalid query string: " + url);
    }

}

From source file:org.opennms.netmgt.provision.service.dns.DnsRequisitionUrlConnection.java

/**
 * Zone should be the first path entity/*from   w ww. j av  a  2 s .c o  m*/
 *
 *   dns://<host>/<zone>[/<foreign source>][/<?expression=<regex>>
 *
 * @param url a {@link java.net.URL} object.
 * @return a {@link java.lang.String} object.
 */
protected static String parseZone(URL url) {

    String path = url.getPath();

    path = StringUtils.removeStart(path, "/");
    path = StringUtils.removeEnd(path, "/");

    String zone = path;

    if (path != null && StringUtils.countMatches(path, "/") == 1) {
        String[] paths = path.split("/");
        zone = paths[0];
    }

    return zone;
}