Example usage for org.springframework.util StringUtils countOccurrencesOf

List of usage examples for org.springframework.util StringUtils countOccurrencesOf

Introduction

In this page you can find the example usage for org.springframework.util StringUtils countOccurrencesOf.

Prototype

public static int countOccurrencesOf(String str, String sub) 

Source Link

Document

Count the occurrences of the substring sub in string str .

Usage

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

/**
 * Restart scenario - content is appended to the output file after restart.
 */// w w  w.  j  ava2 s. c  o  m
@Test
public void testRestart() throws Exception {
    writer.open(executionContext);
    // write item
    writer.write(items);
    writer.update(executionContext);
    writer.close();

    // create new writer from saved restart data and continue writing
    writer = createItemWriter();
    writer.open(executionContext);
    writer.write(items);
    writer.write(items);
    writer.close();

    // check the output is concatenation of 'before restart' and 'after
    // restart' writes.
    String outputFile = getOutputFileContent();
    assertEquals(3, StringUtils.countOccurrencesOf(outputFile, TEST_STRING));
    assertEquals("<root>" + TEST_STRING + TEST_STRING + TEST_STRING + "</root>", outputFile.replace(" ", ""));
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

@Test
public void testTransactionalRestart() throws Exception {
    writer.open(executionContext);//from   w  w  w . j av a  2  s  .  c o  m

    PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();

    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                // write item
                writer.write(items);
            } catch (Exception e) {
                throw new UnexpectedInputException("Could not write data", e);
            }
            // get restart data
            writer.update(executionContext);
            return null;
        }
    });
    writer.close();

    // create new writer from saved restart data and continue writing
    writer = createItemWriter();
    writer.open(executionContext);
    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                writer.write(items);
            } catch (Exception e) {
                throw new UnexpectedInputException("Could not write data", e);
            }
            // get restart data
            writer.update(executionContext);
            return null;
        }
    });
    writer.close();

    // check the output is concatenation of 'before restart' and 'after
    // restart' writes.
    String outputFile = getOutputFileContent();
    assertEquals(2, StringUtils.countOccurrencesOf(outputFile, TEST_STRING));
    assertTrue(outputFile.contains("<root>" + TEST_STRING + TEST_STRING + "</root>"));
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

private void testTransactionalRestartWithMultiByteCharacter(String encoding) throws Exception {
    writer.setEncoding(encoding);//from w  ww  .j a  v a  2 s . c  o m
    writer.open(executionContext);

    PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();

    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                // write item
                writer.write(itemsMultiByte);
            } catch (Exception e) {
                throw new UnexpectedInputException("Could not write data", e);
            }
            // get restart data
            writer.update(executionContext);
            return null;
        }
    });
    writer.close();

    // create new writer from saved restart data and continue writing
    writer = createItemWriter();
    writer.setEncoding(encoding);
    writer.open(executionContext);
    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                writer.write(itemsMultiByte);
            } catch (Exception e) {
                throw new UnexpectedInputException("Could not write data", e);
            }
            // get restart data
            writer.update(executionContext);
            return null;
        }
    });
    writer.close();

    // check the output is concatenation of 'before restart' and 'after
    // restart' writes.
    String outputFile = getOutputFileContent(encoding);
    assertEquals(2, StringUtils.countOccurrencesOf(outputFile, TEST_STRING_MULTI_BYTE));
    assertTrue(outputFile.contains("<root>" + TEST_STRING_MULTI_BYTE + TEST_STRING_MULTI_BYTE + "</root>"));
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

@Test
public void testTransactionalRestartFailOnFirstWrite() throws Exception {

    PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();

    writer.open(executionContext);// ww w  .j  av  a2 s  . co  m
    try {
        new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
            @Override
            public Void doInTransaction(TransactionStatus status) {
                try {
                    writer.write(items);
                } catch (Exception e) {
                    throw new IllegalStateException("Could not write data", e);
                }
                throw new UnexpectedInputException("Could not write data");
            }
        });
    } catch (UnexpectedInputException e) {
        // expected
    }
    writer.close();
    String outputFile = getOutputFileContent();
    assertEquals("<root></root>", outputFile);

    // create new writer from saved restart data and continue writing
    writer = createItemWriter();
    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            writer.open(executionContext);
            try {
                writer.write(items);
            } catch (Exception e) {
                throw new UnexpectedInputException("Could not write data", e);
            }
            // get restart data
            writer.update(executionContext);
            return null;
        }
    });
    writer.close();

    // check the output is concatenation of 'before restart' and 'after
    // restart' writes.
    outputFile = getOutputFileContent();
    assertEquals(1, StringUtils.countOccurrencesOf(outputFile, TEST_STRING));
    assertTrue(outputFile.contains("<root>" + TEST_STRING + "</root>"));
    assertEquals("<root><StaxEventItemWriter-testString/></root>", outputFile);
}

From source file:org.springframework.batch.item.xml.TransactionalStaxEventItemWriterTests.java

/**
 * Item is written to the output file only after flush.
 *///from   www .j  av a  2 s. co m
@Test
public void testWriteWithHeaderAfterRollback() throws Exception {
    writer.setHeaderCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "header"));
                writer.add(factory.createEndElement("", "", "header"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.open(executionContext);
    try {
        new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
            @Override
            public Void doInTransaction(TransactionStatus status) {
                try {
                    writer.write(items);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                throw new RuntimeException("Planned");
            }
        });
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        // expected
    }
    writer.close();
    writer.open(executionContext);
    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                writer.write(items);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return null;
        }
    });
    writer.close();
    String content = outputFileContent();
    assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, ("<header/>")));
    assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, TEST_STRING));
}

From source file:org.springframework.batch.item.xml.TransactionalStaxEventItemWriterTests.java

/**
 * Item is written to the output file only after flush.
 *//* w  w w . j av a  2 s.  c  o  m*/
@Test
public void testWriteWithHeaderAfterFlushAndRollback() throws Exception {
    writer.setHeaderCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "header"));
                writer.add(factory.createEndElement("", "", "header"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.open(executionContext);
    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                writer.write(items);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return null;
        }
    });
    writer.update(executionContext);
    writer.close();
    writer.open(executionContext);
    try {
        new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
            @Override
            public Void doInTransaction(TransactionStatus status) {
                try {
                    writer.write(items);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                throw new RuntimeException("Planned");
            }
        });
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        // expected
    }
    writer.close();
    String content = outputFileContent();
    assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, ("<header/>")));
    assertEquals("Wrong content: " + content, 1, StringUtils.countOccurrencesOf(content, TEST_STRING));
}

From source file:org.springframework.boot.logging.java.JavaLoggerSystemTests.java

@Test
public void setLevel() throws Exception {
    this.loggingSystem.initialize();
    this.logger.debug("Hello");
    this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG);
    this.logger.debug("Hello");
    assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello"), equalTo(1));
}

From source file:org.springframework.boot.logging.java.JavaLoggingSystemTests.java

@Test
public void setLevel() throws Exception {
    this.loggingSystem.beforeInitialize();
    this.loggingSystem.initialize(null, null, null);
    this.logger.debug("Hello");
    this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG);
    this.logger.debug("Hello");
    assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello"), equalTo(1));
}

From source file:org.springframework.boot.logging.logback.LogbackLoggingSystemTests.java

@Test
public void setLevel() throws Exception {
    this.loggingSystem.beforeInitialize();
    this.loggingSystem.initialize(this.initializationContext, null, null);
    this.logger.debug("Hello");
    this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG);
    this.logger.debug("Hello");
    assertThat(StringUtils.countOccurrencesOf(this.output.toString(), "Hello"), equalTo(1));
}

From source file:org.springframework.cloud.netflix.turbine.stream.TurbineStreamTests.java

private ResponseEntity<String> extract(ClientHttpResponse response) throws IOException {
    // The message has to be sent after the endpoint is activated, so this is a
    // convenient place to put it
    stubTrigger.trigger("metrics");
    byte[] bytes = new byte[1024];
    StringBuilder builder = new StringBuilder();
    int read = 0;
    while (read >= 0 && StringUtils.countOccurrencesOf(builder.toString(), "\n") < 2) {
        read = response.getBody().read(bytes, 0, bytes.length);
        if (read > 0) {
            latch.countDown();//from  www  .  j  a v  a2s . com
            builder.append(new String(bytes, 0, read));
        }
        log.debug("Building: " + builder);
    }
    log.debug("Done: " + builder);
    return ResponseEntity.status(response.getStatusCode()).headers(response.getHeaders())
            .body(builder.toString());
}