Search in sources :

Example 96 with ClassPathResource

use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testPersistenceUnitRootUrl.

@Test
public void testPersistenceUnitRootUrl() throws Exception {
    URL url = PersistenceUnitReader.determinePersistenceUnitRootUrl(new ClassPathResource("/org/springframework/orm/jpa/persistence-no-schema.xml"));
    assertNull(url);
    url = PersistenceUnitReader.determinePersistenceUnitRootUrl(new ClassPathResource("/org/springframework/orm/jpa/META-INF/persistence.xml"));
    assertTrue("the containing folder should have been returned", url.toString().endsWith("/org/springframework/orm/jpa"));
}
Also used : URL(java.net.URL) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 97 with ClassPathResource

use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.

the class DefaultPersistenceUnitManagerTests method defaultDomainWithIndex.

@Test
public void defaultDomainWithIndex() {
    this.manager.setPackagesToScan("org.springframework.orm.jpa.domain");
    this.manager.setResourceLoader(new DefaultResourceLoader(CandidateComponentsTestClassLoader.index(getClass().getClassLoader(), new ClassPathResource("spring.components", Person.class))));
    testDefaultDomain();
}
Also used : Person(org.springframework.orm.jpa.domain.Person) ClassPathResource(org.springframework.core.io.ClassPathResource) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Test(org.junit.Test)

Example 98 with ClassPathResource

use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.

the class SqlScriptsTestExecutionListener method detectDefaultScript.

/**
	 * Detect a default SQL script by implementing the algorithm defined in
	 * {@link Sql#scripts}.
	 */
private String detectDefaultScript(TestContext testContext, boolean classLevel) {
    Class<?> clazz = testContext.getTestClass();
    Method method = testContext.getTestMethod();
    String elementType = (classLevel ? "class" : "method");
    String elementName = (classLevel ? clazz.getName() : method.toString());
    String resourcePath = ClassUtils.convertClassNameToResourcePath(clazz.getName());
    if (!classLevel) {
        resourcePath += "." + method.getName();
    }
    resourcePath += ".sql";
    String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath;
    ClassPathResource classPathResource = new ClassPathResource(resourcePath);
    if (classPathResource.exists()) {
        if (logger.isInfoEnabled()) {
            logger.info(String.format("Detected default SQL script \"%s\" for test %s [%s]", prefixedResourcePath, elementType, elementName));
        }
        return prefixedResourcePath;
    } else {
        String msg = String.format("Could not detect default SQL script for test %s [%s]: " + "%s does not exist. Either declare statements or scripts via @Sql or make the " + "default SQL script available.", elementType, elementName, classPathResource);
        logger.error(msg);
        throw new IllegalStateException(msg);
    }
}
Also used : Method(java.lang.reflect.Method) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 99 with ClassPathResource

use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.

the class Jaxb2MarshallerTests method marshalAttachments.

@Test
public void marshalAttachments() throws Exception {
    marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(BinaryObject.class);
    marshaller.setMtomEnabled(true);
    marshaller.afterPropertiesSet();
    MimeContainer mimeContainer = mock(MimeContainer.class);
    Resource logo = new ClassPathResource("spring-ws.png", getClass());
    DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
    given(mimeContainer.convertToXopPackage()).willReturn(true);
    byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
    BinaryObject object = new BinaryObject(bytes, dataHandler);
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, new StreamResult(writer), mimeContainer);
    assertTrue("No XML written", writer.toString().length() > 0);
    verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) ClassPathResource(org.springframework.core.io.ClassPathResource) MimeContainer(org.springframework.oxm.mime.MimeContainer) Test(org.junit.Test)

Example 100 with ClassPathResource

use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.

the class CastorUnmarshallerTests method setBothTargetClassesAndMapping.

@Test
public void setBothTargetClassesAndMapping() throws IOException {
    CastorMarshaller unmarshaller = new CastorMarshaller();
    unmarshaller.setMappingLocation(new ClassPathResource("order-mapping.xml", CastorMarshaller.class));
    unmarshaller.setTargetClasses(new Class[] { Order.class });
    unmarshaller.afterPropertiesSet();
    String xml = "<order>" + "<order-item id=\"1\" quantity=\"15\"/>" + "<order-item id=\"3\" quantity=\"20\"/>" + "</order>";
    Order order = (Order) unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
    assertEquals("Invalid amount of items", 2, order.getOrderItemCount());
    OrderItem item = order.getOrderItem(0);
    assertEquals("Invalid items", "1", item.getId());
    assertThat("Invalid items", item.getQuantity(), equalTo(15));
    item = order.getOrderItem(1);
    assertEquals("Invalid items", "3", item.getId());
    assertThat("Invalid items", item.getQuantity(), equalTo(20));
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

ClassPathResource (org.springframework.core.io.ClassPathResource)558 Test (org.junit.Test)373 Resource (org.springframework.core.io.Resource)170 DhisSpringTest (org.hisp.dhis.DhisSpringTest)85 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)77 List (java.util.List)62 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)59 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)54 Before (org.junit.Before)53 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)39 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)36 ArrayList (java.util.ArrayList)33 DataElement (org.hisp.dhis.dataelement.DataElement)32 File (java.io.File)27 UrlResource (org.springframework.core.io.UrlResource)27 TestBean (org.springframework.tests.sample.beans.TestBean)27 FileSystemResource (org.springframework.core.io.FileSystemResource)20 IOException (java.io.IOException)19 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)19 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)17