Example usage for org.springframework.core.io ClassPathResource ClassPathResource

List of usage examples for org.springframework.core.io ClassPathResource ClassPathResource

Introduction

In this page you can find the example usage for org.springframework.core.io ClassPathResource ClassPathResource.

Prototype

public ClassPathResource(String path) 

Source Link

Document

Create a new ClassPathResource for ClassLoader usage.

Usage

From source file:com.manning.siia.kitchen.RecipeReadingTest.java

@Test
public void shouldReadFileInDirectoryToMessage() throws IOException {
    File resource = new ClassPathResource("/pilav.xml").getFile();
    //copy//  w  w  w .  java 2  s . com
    File recipeWriting = recipeBookLocation.newFile("pilav.xml.writing");
    FileUtils.copyFile(resource, recipeWriting);
    //then rename
    final File outFile = recipeBookLocation.newFile("pilav.xml");
    recipeWriting.renameTo(outFile);

    final Message<File> message = (Message<File>) test.receive(2000);
    assertThat(message, is(notNullValue()));
    final File payload = message.getPayload();
    assertThat(payload.getPath(), is(outFile.getPath()));
}

From source file:org.hisp.dhis.dxf2.importsummary.ImportSummaryTest.java

@Test
public void unMarshallImportSummary() throws Exception {
    ClassPathResource resource = new ClassPathResource("importSummary.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(ImportSummary.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    ImportSummary importSummary = (ImportSummary) jaxbUnmarshaller.unmarshal(resource.getInputStream());
    assertEquals(3, importSummary.getDataValueCount().getImported());
    assertEquals(0, importSummary.getDataValueCount().getUpdated());
    assertEquals(1, importSummary.getDataValueCount().getIgnored());
}

From source file:net.javacrumbs.springws.test.common.MessageGeneratorTest.java

@Test
public void testDetectCreateEnvelope1() throws IOException {
    MessageGenerator generator = new MessageGenerator();
    assertTrue(generator.shouldCreateSoapEnvelope(
            new ClassPathResource("mock-responses/test/default-response-payload.xml")));
}

From source file:org.flywaydb.core.internal.util.XMysqlToH2SqlReplacerTests.java

/**
 * Read class path file//  w  w w .ja v a  2s . c  om
 * 
 * @param path path
 * @param joiner joiner
 * @return string in file
 */
private String readClassPathFile(String path, String joiner) {

    try {

        return Files.lines(Paths.get(new ClassPathResource(path).getURI()), StandardCharsets.UTF_8)
                .collect(Collectors.joining(joiner));
    } catch (IOException e) {

        throw new IllegalStateException("Failed to read class path file", e);
    }
}

From source file:com.example.journal.env.JournalEnvironmentPostProcessor.java

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    Map<String, Object> map = new LinkedHashMap<>();
    contributeDefaults(map, new ClassPathResource("journal.yml"));
    map.put("spring.cloud.stream.bindings." + JournalSink.INPUT + ".content-type",
            "application/x-java-object;type=" + JournalEntry.class.getName());
    addOrReplace(environment.getPropertySources(), map);
}

From source file:com.tacitknowledge.flip.spring.config.InterceptHandlerParser.java

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    XmlBeanFactory factory = new XmlBeanFactory(
            new ClassPathResource("com/tacitknowledge/flip/spring/context.xml"));

    BeanDefinitionBuilder beanBuilder = BeanDefinitionBuilder.rootBeanDefinition(FlipSpringAspect.class);
    String defaultUrlValue = element.getAttribute("default-url");
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.addPropertyValue("defaultValue", defaultUrlValue);
    propertyValues.addPropertyValue(FlipSpringAspect.FEATURE_SERVICE_BEAN_NAME,
            new RuntimeBeanReference(FlipSpringAspect.FEATURE_SERVICE_BEAN_NAME));
    beanBuilder.getRawBeanDefinition().setPropertyValues(propertyValues);

    for (String name : factory.getBeanDefinitionNames()) {
        parserContext.getRegistry().registerBeanDefinition(name, factory.getBeanDefinition(name));
    }//from ww  w. j  av a  2s . c  om
    parserContext.getRegistry().registerBeanDefinition(FlipSpringAspect.ASPECT_BEAN_NAME,
            beanBuilder.getBeanDefinition());

    return null;
}

From source file:com.octo.captcha.engine.bufferedengine.manager.QuartzBufferedEngineManagerTest.java

protected void setUp() throws Exception {
    super.setUp();
    Resource ressource = new ClassPathResource("testQuartzBufferedEngine.xml");
    ConfigurableBeanFactory bf = new XmlBeanFactory(ressource);
    container = (BufferedEngineContainer) bf.getBean("container");
    scheduler = bf.getBean("quartz");

    manager = (QuartzBufferedEngineManager) bf.getBean("manager");
}

From source file:net.sf.jsog.spring.ns.UrlBeanDefinitionParserTest.java

@Test
public void test() {
    ApplicationContext ac = new GenericXmlApplicationContext(
            new ClassPathResource("net/sf/jsog/spring/ns/url.xml"));

    UrlBuilder url = ac.getBean(UrlBuilder.class);
    assertNotNull(url);/*from  w ww  .  j a  v  a2s  .com*/
    assertEquals("http://www.example.com/?foo=bar&foo=baz&qux=quux", url.toString());
}

From source file:com.hippikon.io.FileUtil.java

/**
 * Returns a File object located on the system classpath by a specified
 * filename. If the file is not found in a classpath directory, the "fileutil.search.path"
 * environment variable is checked if set.<p>
 *
 * The filename passed as a method parameter should and not 
 * contain any path information (e.g., "myfile.txt").<p>
 *
 * @param filename a relative filename (e.g., myfile.txt)
 *
 * @exception FileNotFoundException thrown if the file could not be
 * found or a file reference obtained//from  www  .  j  a  va 2 s  .  c om
 */
public static final File findFileInClasspath(String filename) throws IOException {
    ClassPathResource res = new ClassPathResource(filename);
    return res.getFile();
}

From source file:com.create.batch.TicketReaderFactoryTest.java

@Test
public void testCreateReaderAndReadCorrectData() throws Exception {
    // given/*from   w w  w.jav  a 2 s . com*/
    final ExecutionContext executionContext = mock(ExecutionContext.class);
    final LocalDate date = LocalDate.of(2015, 12, 20);

    // when
    final ItemStreamReader<Ticket> reader = factory.createReader(new ClassPathResource("tickets.csv"));
    final Ticket ticket;

    try {
        reader.open(executionContext);
        ticket = reader.read();
    } finally {
        reader.close();
    }

    // then
    assertThat(ticket, notNullValue());
    assertThat(ticket.getTag(), equalTo("Ticket_0"));
    assertThat(ticket.getDate(), equalTo(Date.valueOf(date)));
    assertThat(ticket.getContent(), equalTo("Test ticket"));
}