Package org.apache.sling.api.resource

Examples of org.apache.sling.api.resource.Resource


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("first", "first-value");
        map.put("third", "third-value");
        ValueMap vm = spy(new ValueMapDecorator(map));

        Resource res = mock(Resource.class);
        when(res.adaptTo(ValueMap.class)).thenReturn(vm);

        boolean thrown = false;
        try {
            factory.createModel(res, ResourceModelWithRequiredField.class);
        } catch (MissingElementsException e) {
View Full Code Here


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("first", "first-value");
        map.put("third", "third-value");
        ValueMap vm = spy(new ValueMapDecorator(map));

        Resource res = mock(Resource.class);
        when(res.adaptTo(ValueMap.class)).thenReturn(vm);

        ResourceModelWithRequiredFieldOptionalStrategy model = factory.getAdapter(res, ResourceModelWithRequiredFieldOptionalStrategy.class);
        assertNull(model);

        verify(vm).get("optional", String.class);
View Full Code Here

    public void testRequiredPropertyModelOptionalStrategyNotAvailable() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("required", "first-value");
        ValueMap vm = spy(new ValueMapDecorator(map));

        Resource res = mock(Resource.class);
        when(res.adaptTo(ValueMap.class)).thenReturn(vm);

        ResourceModelWithRequiredFieldOptionalStrategy model = factory.getAdapter(res, ResourceModelWithRequiredFieldOptionalStrategy.class);
        assertNotNull(model);

        verify(vm).get("optional", String.class);
View Full Code Here

              DEFAULT_RESOURCE_TYPE, DEFAULT_RESOURCE_TYPE, true);
    }

    public static boolean deleteResource(
            final ResourceResolver resourceResolver, final String path) throws PersistenceException {
        final Resource resource = resourceResolver.getResource(path);
        if (resource==null) {
            return false;
        }
        resourceResolver.delete(resource);
        return true;
View Full Code Here

        verify(vm).get("required", String.class);
    }

    @Test
    public void testChildResource() {
        Resource child = mock(Resource.class);
        Resource secondChild = mock(Resource.class);
        Resource emptyChild = mock(Resource.class);

        Resource firstGrandChild = mock(Resource.class);
        Resource secondGrandChild = mock(Resource.class);
        when(secondChild.listChildren()).thenReturn(Arrays.asList(firstGrandChild, secondGrandChild).iterator());
        when(emptyChild.listChildren()).thenReturn(Collections.<Resource>emptySet().iterator());

        Resource res = mock(Resource.class);
        when(res.getChild("firstChild")).thenReturn(child);
        when(res.getChild("secondChild")).thenReturn(secondChild);
        when(res.getChild("emptyChild")).thenReturn(emptyChild);

        ChildResourceModel model = factory.getAdapter(res, ChildResourceModel.class);
        assertNotNull(model);
        assertEquals(child, model.getFirstChild());
        assertEquals(2, model.getGrandChildren().size());
View Full Code Here

    @Test
    public void testChildValueMap() {
        ValueMap map = ValueMapDecorator.EMPTY;

        Resource child = mock(Resource.class);
        when(child.adaptTo(ValueMap.class)).thenReturn(map);

        Resource res = mock(Resource.class);
        when(res.getChild("firstChild")).thenReturn(child);

        ChildValueMapModel model = factory.getAdapter(res, ChildValueMapModel.class);
        assertNotNull(model);
        assertEquals(map, model.getFirstChild());
    }
View Full Code Here

     * Read the properties from the repository
     */
    private void readProperties(final Resource res) {
        final Map<String, String> props = new HashMap<String, String>();
        if (res != null) {
            final Resource propertiesChild = res.getChild("properties");
            if (propertiesChild != null) {
                final ValueMap properties = propertiesChild.adaptTo(ValueMap.class);
                if (properties != null) {
                    for (Iterator<String> it = properties.keySet().iterator(); it
                            .hasNext();) {
                        String key = it.next();
                        if (!key.equals("jcr:primaryType")) {
View Full Code Here

    @Test
    public void testChildModel() {
        Object firstValue = RandomStringUtils.randomAlphabetic(10);
        ValueMap firstMap = new ValueMapDecorator(Collections.singletonMap("property", firstValue));

        final Resource firstChild = mock(Resource.class);
        when(firstChild.adaptTo(ValueMap.class)).thenReturn(firstMap);
        when(firstChild.adaptTo(ChildModel.class)).thenAnswer(new AdaptToChildModel());

        Object firstGrandChildValue = RandomStringUtils.randomAlphabetic(10);
        ValueMap firstGrandChildMap = new ValueMapDecorator(Collections.singletonMap("property", firstGrandChildValue));
        Object secondGrandChildValue = RandomStringUtils.randomAlphabetic(10);
        ValueMap secondGrandChildMap = new ValueMapDecorator(Collections.singletonMap("property", secondGrandChildValue));

        final Resource firstGrandChild = mock(Resource.class);
        when(firstGrandChild.adaptTo(ValueMap.class)).thenReturn(firstGrandChildMap);
        when(firstGrandChild.adaptTo(ChildModel.class)).thenAnswer(new AdaptToChildModel());

        final Resource secondGrandChild = mock(Resource.class);
        when(secondGrandChild.adaptTo(ValueMap.class)).thenReturn(secondGrandChildMap);
        when(secondGrandChild.adaptTo(ChildModel.class)).thenAnswer(new AdaptToChildModel());

        Resource secondChild = mock(Resource.class);
        when(secondChild.listChildren()).thenReturn(Arrays.asList(firstGrandChild, secondGrandChild).iterator());

        Resource emptyChild = mock(Resource.class);
        when(emptyChild.listChildren()).thenReturn(Collections.<Resource>emptySet().iterator());

        Resource res = mock(Resource.class);
        when(res.getChild("firstChild")).thenReturn(firstChild);
        when(res.getChild("secondChild")).thenReturn(secondChild);
        when(res.getChild("emptyChild")).thenReturn(emptyChild);

        ParentModel model = factory.getAdapter(res, ParentModel.class);
        assertNotNull(model);

        ChildModel childModel = model.getFirstChild();
View Full Code Here

    /** Construct a new established cluster view **/
    public EstablishedClusterView(final Config config, final View view,
            final String localId) {
        super(view.getViewId());

        final Resource viewRes = view.getResource();
        if (viewRes == null) {
            throw new IllegalStateException("viewRes must not be null");
        }
        final ValueMap valueMap = viewRes.adaptTo(ValueMap.class);
        if (valueMap == null) {
            throw new IllegalStateException("valueMap must not be null");
        }
        String leaderId = valueMap.get("leaderId", String.class);
        final Resource members = viewRes.getChild("members");
        if (members == null) {
            throw new IllegalStateException("members must not be null");
        }
        final Iterator<Resource> it1 = members
                .getChildren().iterator();
        final List<Resource> instanceRess = new LinkedList<Resource>();
        while (it1.hasNext()) {
            instanceRess.add(it1.next());
        }

        Collections.sort(instanceRess, new Comparator<Resource>() {
            public int compare(Resource o1, Resource o2) {
                if (o1 == o2) {
                    return 0;
                }
                if (o1 == null) {
                    return 1;
                }
                if (o2 == null) {
                    return -1;
                }
                return o1.getName().compareTo(o2.getName());
            }
        });

        if (leaderId==null || leaderId.length()==0) {
          // fallback to pre-SLING-3253: choose leader based on slingId alone.
          final Resource leader = instanceRess.get(0);
          leaderId = leader.getName();
        }
        InstanceDescription leaderInstance = null;

        for (Iterator<Resource> it2 = instanceRess.iterator(); it2.hasNext();) {
            Resource resource = it2.next();
            Resource instanceResource = resource.getResourceResolver()
                    .getResource(
                            config.getClusterInstancesPath() + "/"
                                    + resource.getName());
            String slingId = resource.getName();
            EstablishedInstanceDescription instance = new EstablishedInstanceDescription(
View Full Code Here

    private ResourceResolverInjector injector = new ResourceResolverInjector();

    @Test
    public void testFromResource() {
        Resource resource = mock(Resource.class);
        ResourceResolver resourceResolver = mock(ResourceResolver.class);
        when(resource.getResourceResolver()).thenReturn(resourceResolver);

        Object result = injector.getValue(resource, "resourceResolver", null, null, null);
        assertEquals(resourceResolver, result);
    }
View Full Code Here

TOP

Related Classes of org.apache.sling.api.resource.Resource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.