org.springextensions.db4o.io.ResourceStorageTest.java Source code

Java tutorial

Introduction

Here is the source code for org.springextensions.db4o.io.ResourceStorageTest.java

Source

/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springextensions.db4o.io;

import com.db4o.io.Bin;
import com.db4o.io.BinConfiguration;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.testng.Assert;
import org.testng.annotations.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * author: olli
 */
public class ResourceStorageTest {

    @Test
    public void testExists() {
        String uri = "resource";

        Resource resource = mock(Resource.class);
        when(resource.exists()).thenReturn(true);

        ResourceLoader resourceLoader = mock(ResourceLoader.class);
        when(resourceLoader.getResource(uri)).thenReturn(resource);

        ResourceStorage resourceStorage = new ResourceStorage();
        resourceStorage.setResourceLoader(resourceLoader);

        Assert.assertTrue(resourceStorage.exists(uri));
    }

    @Test
    public void testExistsNot() {
        String uri = "resource";

        Resource resource = mock(Resource.class);
        when(resource.exists()).thenReturn(false);

        ResourceLoader resourceLoader = mock(ResourceLoader.class);
        when(resourceLoader.getResource(uri)).thenReturn(resource);

        ResourceStorage resourceStorage = new ResourceStorage();
        resourceStorage.setResourceLoader(resourceLoader);

        Assert.assertFalse(resourceStorage.exists(uri));
    }

    @Test
    public void testOpen() {
        String uri = "resource";
        BinConfiguration binConfiguration = new BinConfiguration(uri, false, 0, false);

        byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
        Resource resource = new ByteArrayResource(bytes);

        ResourceLoader resourceLoader = mock(ResourceLoader.class);
        when(resourceLoader.getResource(uri)).thenReturn(resource);

        ResourceStorage resourceStorage = new ResourceStorage();
        resourceStorage.setResourceLoader(resourceLoader);

        Bin bin = resourceStorage.open(binConfiguration);

        Assert.assertEquals(bin.length(), bytes.length);
    }

}