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:example.springdata.mongodb.textsearch.MongoTestConfiguration.java

public @Bean Jackson2RepositoryPopulatorFactoryBean repositoryPopulator() {

    Jackson2RepositoryPopulatorFactoryBean factoryBean = new Jackson2RepositoryPopulatorFactoryBean();
    factoryBean.setResources(new Resource[] { new ClassPathResource("spring-blog.atom.json") });
    return factoryBean;
}

From source file:com.dvidder.service.AccountService.java

@Transactional
public byte[] getProfilePictureForUsername(String username) throws IOException {
    Account account = accountRepository.findByUsername(username);
    if (account.getProfilePicture().isDefaultPic()) {

        byte[] imgData = new byte[5000];
        Resource resource = new ClassPathResource("static/img/default-profile-pic.png");
        if (!resource.exists()) {
            System.out.println("RESOURCE DOESN'T EXIST");
        } else {/*  ww  w.  j av  a  2 s  .  c o  m*/
            System.out.println("YES, RESOURCE EXISTS");

            System.out.println("resource content length: " + resource.contentLength());
        }

        imgData = IOUtils.toByteArray(resource.getInputStream());
        System.out.println("byte array length: " + imgData.length);
        return imgData;
    }
    return accountRepository.findByUsername(username).getProfilePicture().getImageData();
}

From source file:org.atomserver.core.validators.RelaxNGContentValidatorTest.java

public void testWidgetSchema() throws Exception {
    RelaxNGValidator validator = new RelaxNGValidator();
    ClassPathResource schemaLocation = new ClassPathResource("widgets-1.0.rnc");
    validator.setSchemaLocation(schemaLocation);
    checkValidity(validator, VALID_WIDGET_XML, false);
    checkValidity(validator, INVALID_WIDGET_XML, true);
}

From source file:com.biendltb.main_server.TripMapServer.java

private static ServletContextHandler getServletContextHandler(WebApplicationContext context)
        throws IOException {
    ServletContextHandler contextHandler = new ServletContextHandler();
    contextHandler.setErrorHandler(null);
    contextHandler.setContextPath(CONTEXT_PATH);
    contextHandler.addServlet(new ServletHolder("default", new DispatcherServlet(context)), MAPPING_URL);
    contextHandler.addEventListener(new ContextLoaderListener(context));
    contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
    return contextHandler;
}

From source file:org.geomajas.gwt.server.mvc.GwtResourceControllerTest.java

@Test
public void testResourceInClassPath() throws ServletException, IOException {
    // create mock context that loads from the classpath
    MockServletContext context = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(context);
    request.setPathInfo("/org/geomajas/gwt/server/mvc/geomajas_logo.png");
    request.setMethod("GET");
    MockHttpServletResponse response = new MockHttpServletResponse();
    GwtResourceController resourceController = new GwtResourceController();
    resourceController.setServletContext(context);
    resourceController.getResource(request, response);
    Resource resource = new ClassPathResource("/org/geomajas/gwt/server/mvc/geomajas_logo.png");
    Assert.assertArrayEquals(IOUtils.toByteArray(resource.getInputStream()), response.getContentAsByteArray());
}

From source file:com.graphaware.test.util.TestUtils.java

/**
 * Convert a JSON file to String./* ww w.  j a v  a 2 s  . co m*/
 *
 * @param packagePath path to package. The file is expected to be in the resources directory in the same
 *                    package.
 * @param fileName    name of the file present in the resources directory in the package defined above.
 * @return JSON as String.
 */
public static String jsonAsString(String packagePath, String fileName) {
    try {
        return IOUtils.toString(new ClassPathResource(packagePath + fileName + ".json").getInputStream(),
                Charset.forName("UTF-8"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.deeplearning4j.spark.models.embeddings.glove.GloveTest.java

@Test
public void testGlove() throws Exception {
    Glove glove = new Glove(true, 5, 100);
    JavaRDD<String> corpus = sc.textFile(new ClassPathResource("raw_sentences.txt").getFile().getAbsolutePath())
            .map(new Function<String, String>() {
                @Override//from  w  w w. j a va  2  s .  c o  m
                public String call(String s) throws Exception {
                    return s.toLowerCase();
                }
            }).cache();

    Pair<VocabCache, GloveWeightLookupTable> table = glove.train(corpus);
    WordVectors vectors = WordVectorSerializer
            .fromPair(new Pair<>((WeightLookupTable) table.getSecond(), table.getFirst()));
    Collection<String> words = vectors.wordsNearest("day", 20);
    assertTrue(words.contains("week"));
}

From source file:net.javacrumbs.springws.test.generator.DefaultResponseGeneratorTest.java

@Test
public void testDefaultResponse() throws IOException {
    WebServiceMessage request = createMessage("xml/valid-message.xml");
    ClassPathResource responseResource = new ClassPathResource("mock-responses/test/default-response.xml");

    DefaultResponseGenerator generator = new DefaultResponseGenerator();
    ResourceLookup resourceLookup = createMock(ResourceLookup.class);
    expect(resourceLookup.lookupResource(null, request)).andReturn(responseResource);

    generator.setResourceLookup(resourceLookup);

    replay(resourceLookup);/*from ww  w .j  av  a  2  s .  co  m*/

    WebServiceMessage response = generator.processRequest(null, messageFactory, request);
    assertNotNull(response);

    Document controlDocument = loadDocument(responseResource);
    Diff diff = new Diff(controlDocument, loadDocument(response));
    assertTrue(diff.toString(), diff.similar());

    verify(resourceLookup);
}

From source file:org.canova.nd4j.nlp.reader.TfidfRecordReaderTest.java

@Test
public void testReader() throws Exception {
    TfidfVectorizer vectorizer = new TfidfVectorizer();
    Configuration conf = new Configuration();
    conf.setInt(TfidfVectorizer.MIN_WORD_FREQUENCY, 1);
    conf.setBoolean(RecordReader.APPEND_LABEL, true);
    vectorizer.initialize(conf);//from w  w w. j  av a2 s .co m
    TfidfRecordReader reader = new TfidfRecordReader();
    reader.initialize(conf, new FileSplit(new ClassPathResource("labeled").getFile()));
    int count = 0;
    while (reader.hasNext()) {
        Collection<Writable> record = reader.next();
        count++;
    }
    assertEquals(3, reader.getLabels().size());
    assertEquals(3, count);
}

From source file:reactivity.ResourceController.java

/**
 * Redirects to HTML page.// w w  w.j ava  2 s . co  m
 *
 * @return the resource
 */
@GetMapping(path = "/", produces = MediaType.TEXT_HTML_VALUE)
Resource index() {
    return new ClassPathResource("static/index.html");
}