Deprecated as of 4.3.13, in favor of selective use of ClassPathResource.ClassPathResource(String, ClassLoader) vs ClassPathResource.ClassPathResource(String, Class)
private long resourceLastModified(String resourceName) throws IOException { return new ClassPathResource(resourceName, getClass()).getFile().lastModified(); }
private long resourceLastModified(String resourceName) throws IOException { return new ClassPathResource(resourceName, getClass()).getFile().lastModified(); }
private long resourceLastModifiedDate(String resourceName) throws IOException { return new ClassPathResource(resourceName, getClass()).getFile().lastModified(); }
@Test public void testResourceArrayPropertyEditor() throws IOException { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CONTEXT_WILDCARD); Service service = (Service) ctx.getBean("service"); assertEquals(3, service.getResources().length); List<Resource> resources = Arrays.asList(service.getResources()); assertTrue(resources.contains(new FileSystemResource(new ClassPathResource(FQ_CONTEXT_A).getFile()))); assertTrue(resources.contains(new FileSystemResource(new ClassPathResource(FQ_CONTEXT_B).getFile()))); assertTrue(resources.contains(new FileSystemResource(new ClassPathResource(FQ_CONTEXT_C).getFile()))); ctx.close(); }
private String resource(String suffix) throws IOException { String name = this.getClass().getSimpleName() + "-" + suffix; ClassPathResource resource = new ClassPathResource(name, this.getClass()); try ( BufferedReader reader = new BufferedReader(new FileReader(resource.getFile())) ) { return reader.lines().collect(Collectors.joining()); } } }
/** * This will check to ensure that the expected number of xsd documents are found to ensure that we are validating * against the current xsd document. If this test fails, all that is needed is to update the schemaDocument * and the expected size for this test. * @return */ @Test public void sizeWhenReadingFilesystemThenIsCorrectNumberOfSchemaFiles() throws IOException { ClassPathResource resource = new ClassPathResource(this.schemaDocumentLocation); String[] schemas = resource.getFile().getParentFile().list((dir, name) -> name.endsWith(".xsd")); assertThat(schemas.length).isEqualTo(14) .withFailMessage("the count is equal to 14, if not then schemaDocument needs updating"); }
@Test public void composeResourceLookupFunction() throws Exception { ClassPathResource defaultResource = new ClassPathResource("response.txt", getClass()); Function<ServerRequest, Mono<Resource>> lookupFunction = new PathResourceLookupFunction("/resources/**", new ClassPathResource("org/springframework/web/reactive/function/server/")); Function<ServerRequest, Mono<Resource>> customLookupFunction = lookupFunction.andThen(resourceMono -> resourceMono .switchIfEmpty(Mono.just(defaultResource))); MockServerRequest request = MockServerRequest.builder() .uri(new URI("http://localhost/resources/foo")) .build(); Mono<Resource> result = customLookupFunction.apply(request); StepVerifier.create(result) .expectNextMatches(resource -> { try { return defaultResource.getFile().equals(resource.getFile()); } catch (IOException ex) { return false; } }) .expectComplete() .verify(); }
@Test public void normal() throws Exception { ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/"); PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location); MockServerRequest request = MockServerRequest.builder() .uri(new URI("http://localhost/resources/response.txt")) .build(); Mono<Resource> result = function.apply(request); File expected = new ClassPathResource("response.txt", getClass()).getFile(); StepVerifier.create(result) .expectNextMatches(resource -> { try { return expected.equals(resource.getFile()); } catch (IOException ex) { return false; } }) .expectComplete() .verify(); }
@Test public void subPath() throws Exception { ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/"); PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location); MockServerRequest request = MockServerRequest.builder() .uri(new URI("http://localhost/resources/child/response.txt")) .build(); Mono<Resource> result = function.apply(request); String path = "org/springframework/web/reactive/function/server/child/response.txt"; File expected = new ClassPathResource(path).getFile(); StepVerifier.create(result) .expectNextMatches(resource -> { try { return expected.equals(resource.getFile()); } catch (IOException ex) { return false; } }) .expectComplete() .verify(); }
@Test public void fileSystemXmlApplicationContext() throws IOException { ClassPathResource xml = new ClassPathResource(XML_PATH); File tmpFile = File.createTempFile("test", "xml"); FileCopyUtils.copy(xml.getFile(), tmpFile); // strange - FSXAC strips leading '/' unless prefixed with 'file:' ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
@Override public String getSystemId() { try { return new ClassPathResource("org/springframework/integration/xml/customSource.data") .getFile() .getPath(); } catch (IOException e) { throw new IllegalStateException(e); } }
@Test public void testValidation() throws IOException { File file = new ClassPathResource("/validation/script_1time.py").getFile(); Condition m_eventSync = new Condition(); File log = validationService.doValidate(file.getParentFile(), file, m_eventSync, false, GRINDER_SECURITY_LEVEL_NORMAL, ""); assertThat(log.length(), greaterThan(1000L)); }
/** * Locate dumped user1 repo into tempdir. * * @throws IOException */ @Before public void before() throws IOException { File file = new File(System.getProperty("java.io.tmpdir"), "repo"); FileUtils.deleteQuietly(file); CompressionUtils.unzip(new ClassPathResource("TEST_USER.zip").getFile(), file); repo.setUserRepository(new File(file, getTestUser().getUserId())); }
@Test public void testInt3067OutputFileAsString() throws IOException { MessageChannel input = applicationContext.getBean("outputFileAsStringChannel", MessageChannel.class); Message<?> message = MessageBuilder.withPayload(new ClassPathResource("org/springframework/integration/xml/transformer/xsl-text-file.xml").getFile()).build(); input.send(message); Message<?> resultMessage = output.receive(); assertEquals("Wrong payload type", String.class, resultMessage.getPayload().getClass()); String stringPayload = (String) resultMessage.getPayload(); assertEquals("Wrong content of payload", "hello world text", stringPayload.trim()); } }
@Before public void before() throws IOException { repoDir = new File(System.getProperty("java.io.tmpdir"), "repo"); FileUtils.deleteQuietly(repoDir); CompressionUtils.unzip(new ClassPathResource("TEST_USER.zip").getFile(), repoDir); repo.setUserRepository(new File(repoDir, getTestUser().getUserId())); }
/** * Locate dumped user1 repo into tempdir * * @throws IOException */ @Before public void before() throws IOException { File file = new File(System.getProperty("java.io.tmpdir"), "repo"); FileUtils.deleteQuietly(file); CompressionUtils.unzip(new ClassPathResource("TEST_USER.zip").getFile(), file); repo.setUserRepository(new File(file, getTestUser().getUserId())); }
@Test public void testConvertFileToDocument() throws Exception { File file = new ClassPathResource("org/springframework/integration/xml/customSource.data").getFile(); Document doc = converter.convertToDocument(file); assertThat(XmlTestUtil.docToString(doc)).isXmlEqualTo(TEST_DOCUMENT_AS_STRING); }
@Test public void testGetResourcesOnScriptFolder() throws IOException { File file = new File(System.getProperty("java.io.tmpdir"), "repo"); FileUtils.deleteQuietly(file); CompressionUtils.unzip(new ClassPathResource("TEST_USER.zip").getFile(), file); repo.setUserRepository(new File(file, getTestUser().getUserId())); controller.getResources(getTestUser(), "filefilter.txt", null); }
@Test public void outboundChannelAdapter() throws Exception { DirectFieldAccessor accessor = new DirectFieldAccessor( new DirectFieldAccessor(adapter).getPropertyValue("handler")); File expected = new ClassPathResource("").getFile(); Expression destinationDirectoryExpression = (Expression) accessor.getPropertyValue("destinationDirectoryExpression"); File actual = new File(destinationDirectoryExpression.getExpressionString()); assertEquals("'destinationDirectory' should be set", expected, actual); }
@Test public void outboundGateway() throws Exception { DirectFieldAccessor accessor = new DirectFieldAccessor( new DirectFieldAccessor(gateway).getPropertyValue("handler")); File expected = new ClassPathResource("").getFile(); Expression destinationDirectoryExpression = (Expression) accessor.getPropertyValue("destinationDirectoryExpression"); File actual = new File(destinationDirectoryExpression.getExpressionString()); assertEquals("'destinationDirectory' should be set", expected, actual); }