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"));
}
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();
}
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);
}
}
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));
}
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));
}
Aggregations