Ignore:
Timestamp:
08/20/13 15:32:37 (11 years ago)
Author:
George.Georgovassilis@mpi.nl
Message:

#360 Refactoring of transaction handling, tests and application context

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ConcurrentRestServiceTest.java

    r1861 r3449  
    1717import org.junit.Before;
    1818import org.junit.Test;
    19 import org.junit.runner.RunWith;
    2019import org.slf4j.Logger;
    2120import org.slf4j.LoggerFactory;
    2221import org.springframework.beans.factory.annotation.Autowired;
    2322import org.springframework.jdbc.core.JdbcTemplate;
    24 import org.springframework.test.context.ContextConfiguration;
    25 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    2623
    2724import static clarin.cmdi.componentregistry.rest.ComponentRegistryRestService.USERSPACE_PARAM;
    2825import static org.junit.Assert.*;
    2926
    30 @RunWith(SpringJUnit4ClassRunner.class)
    31 @ContextConfiguration(locations = {"/applicationContext.xml"})
    32 public class ConcurrentRestServiceTest extends ComponentRegistryRestServiceTestCase {
    33 
    34     private final static Logger LOG = LoggerFactory.getLogger(ConcurrentRestServiceTest.class);
     27/**
     28 *
     29 * @author george.georgovassilis@mpi.nl
     30 *
     31 */
     32public class ConcurrentRestServiceTest extends
     33        ComponentRegistryRestServiceTestCase {
     34
     35    private final static Logger LOG = LoggerFactory
     36            .getLogger(ConcurrentRestServiceTest.class);
    3537    private int NR_OF_PROFILES = 20;
    3638    private int NR_OF_COMPONENTS = 20;
     
    4446    }
    4547
    46     @Override
    47     protected String getApplicationContextFile() {
    48         return "classpath:applicationContext.xml";
    49     }
    50 
    5148    @Test
    5249    public void testConcurrentRegisterProfile() throws Exception {
    53         List<String> errors = new ArrayList();
     50        List<String> errors = new ArrayList<String>();
    5451        List<Thread> ts = new ArrayList<Thread>();
    5552
     
    7370    private void assertProfiles(int nrOfProfiles, String userSpace) {
    7471        List<ProfileDescription> response = getAuthenticatedResource(
    75                 getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, userSpace)).
    76                 accept(MediaType.APPLICATION_XML).get(
    77                 PROFILE_LIST_GENERICTYPE);
     72                getResource().path("/registry/profiles").queryParam(
     73                        USERSPACE_PARAM, userSpace)).accept(
     74                MediaType.APPLICATION_XML).get(PROFILE_LIST_GENERICTYPE);
    7875        Collections.sort(response, descriptionComparator);
    79         assertEquals("half should be deleted", nrOfProfiles / 2, response.size());
     76        assertEquals("half should be deleted", nrOfProfiles / 2,
     77                response.size());
    8078        for (int i = 0; i < nrOfProfiles / 2; i++) {
    8179            ProfileDescription desc = response.get(i);
    8280            assertEquals("Test Profile" + (i * 2 + 1000), desc.getName());
    83             assertEquals("Test Profile" + (i * 2 + 1000) + " Description", desc.getDescription());
    84         }
    85     }
     81            assertEquals("Test Profile" + (i * 2 + 1000) + " Description",
     82                    desc.getDescription());
     83        }
     84    }
     85
    8686    private Comparator<AbstractDescription> descriptionComparator = new Comparator<AbstractDescription>() {
    8787
     
    9494    private void assertComponents(int nrOfComponents, String userSpace) {
    9595        List<ComponentDescription> cResponse = getAuthenticatedResource(
    96                 getResource().path("/registry/components").queryParam(USERSPACE_PARAM, userSpace)).
    97                 accept(MediaType.APPLICATION_XML).get(
    98                 COMPONENT_LIST_GENERICTYPE);
     96                getResource().path("/registry/components").queryParam(
     97                        USERSPACE_PARAM, userSpace)).accept(
     98                MediaType.APPLICATION_XML).get(COMPONENT_LIST_GENERICTYPE);
    9999        Collections.sort(cResponse, descriptionComparator);
    100         assertEquals("half should be deleted", nrOfComponents / 2, cResponse.size());
     100        assertEquals("half should be deleted", nrOfComponents / 2,
     101                cResponse.size());
    101102        for (int i = 0; i < nrOfComponents / 2; i++) {
    102103            ComponentDescription desc = cResponse.get(i);
    103104            assertEquals("Test Component" + (i * 2 + 1000), desc.getName());
    104             assertEquals("Test Component" + (i * 2 + 1000) + " Description", desc.getDescription());
     105            assertEquals("Test Component" + (i * 2 + 1000) + " Description",
     106                    desc.getDescription());
    105107        }
    106108    }
     
    112114        }
    113115        for (Thread thread : ts) {
    114             thread.join(); //Wait till all are finished
    115         }
    116     }
    117 
    118     private void registerProfiles(List<Thread> ts, int size, final List<String> errors, String userSpace) throws InterruptedException {
     116            thread.join(); // Wait till all are finished
     117        }
     118    }
     119
     120    private void registerProfiles(List<Thread> ts, int size,
     121            final List<String> errors, String userSpace)
     122            throws InterruptedException {
    119123        for (int i = 0; i < size; i++) {
    120124            final boolean shouldDelete = (i % 2) == 1;
    121             LOG.debug("Profile {} should be registered in {} and {}", new Object[]{i + 1000, Boolean.parseBoolean(userSpace) ? "user space" : "public space", shouldDelete ? "ALSO DELETED" : "not deleted"});
    122             Thread thread = createThread("/registry/profiles/", userSpace, "Test Profile" + (i + 1000), shouldDelete, RegistryTestHelper.getTestProfileContent(), errors);
     125            LOG.debug("Profile {} should be registered in {} and {}",
     126                    new Object[] {
     127                            i + 1000,
     128                            Boolean.parseBoolean(userSpace) ? "user space"
     129                                    : "public space",
     130                            shouldDelete ? "ALSO DELETED" : "not deleted" });
     131            Thread thread = createThread("/registry/profiles/", userSpace,
     132                    "Test Profile" + (i + 1000), shouldDelete,
     133                    RegistryTestHelper.getTestProfileContent(), errors);
    123134            ts.add(thread);
    124135        }
    125136    }
    126137
    127     private void registerComponents(List<Thread> ts, int size, final List<String> errors, String userSpace) throws InterruptedException {
     138    private void registerComponents(List<Thread> ts, int size,
     139            final List<String> errors, String userSpace)
     140            throws InterruptedException {
    128141        for (int i = 0; i < size; i++) {
    129142            final boolean shouldDelete = (i % 2) == 1;
    130             LOG.debug("Component {} should be registered in {} and {}", new Object[]{i + 1000, Boolean.parseBoolean(userSpace) ? "user space" : "public space", shouldDelete ? "ALSO DELETED" : "not deleted"});
    131             Thread thread = createThread("/registry/components/", userSpace, "Test Component" + (i + 1000), shouldDelete,
     143            LOG.debug("Component {} should be registered in {} and {}",
     144                    new Object[] {
     145                            i + 1000,
     146                            Boolean.parseBoolean(userSpace) ? "user space"
     147                                    : "public space",
     148                            shouldDelete ? "ALSO DELETED" : "not deleted" });
     149            Thread thread = createThread("/registry/components/", userSpace,
     150                    "Test Component" + (i + 1000), shouldDelete,
    132151                    RegistryTestHelper.getComponentTestContent(), errors);
    133152            ts.add(thread);
     
    135154    }
    136155
    137     private Thread createThread(final String path, final String userSpace, final String name, final boolean alsoDelete,
    138             InputStream content, final List<String> errors) throws InterruptedException {
     156    private Thread createThread(final String path, final String userSpace,
     157            final String name, final boolean alsoDelete, InputStream content,
     158            final List<String> errors) throws InterruptedException {
    139159        final FormDataMultiPart form = new FormDataMultiPart();
    140         form.field(ComponentRegistryRestService.DATA_FORM_FIELD, content, MediaType.APPLICATION_OCTET_STREAM_TYPE);
    141         form.field(ComponentRegistryRestService.NAME_FORM_FIELD, name);
    142         form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, name + " Description");
     160        form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content,
     161                MediaType.APPLICATION_OCTET_STREAM_TYPE);
     162        form.field(IComponentRegistryRestService.NAME_FORM_FIELD, name);
     163        form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD, name
     164                + " Description");
    143165        Thread t = new Thread(new Runnable() {
    144166
    145167            @Override
    146168            public void run() {
    147                 //                System.out.println("THREAD STARTED"+Thread.currentThread().getName());
     169                // System.out.println("THREAD STARTED"+Thread.currentThread().getName());
    148170                RegisterResponse registerResponse = getAuthenticatedResource(
    149                         getResource().path(path).queryParam(USERSPACE_PARAM, userSpace)).
    150                         type(MediaType.MULTIPART_FORM_DATA).post(
    151                         RegisterResponse.class, form);
     171                        getResource().path(path).queryParam(USERSPACE_PARAM,
     172                                userSpace)).type(MediaType.MULTIPART_FORM_DATA)
     173                        .post(RegisterResponse.class, form);
    152174                if (!registerResponse.isRegistered()) {
    153                     errors.add("Failed to register " + Arrays.toString(registerResponse.getErrors().toArray()));
     175                    errors.add("Failed to register "
     176                            + Arrays.toString(registerResponse.getErrors()
     177                                    .toArray()));
    154178                }
    155                 LOG.debug(">>>>>>>>>>>>>>>> [Thread " + hashCode() + "] REGISTERING DESCRIPTION " + name + " " + registerResponse.getDescription().getId() + (Boolean.valueOf(userSpace) ? " userspace" : "") + (alsoDelete ? " alsoDelete" : ""));
     179                LOG.debug(">>>>>>>>>>>>>>>> [Thread " + hashCode()
     180                        + "] REGISTERING DESCRIPTION " + name + " "
     181                        + registerResponse.getDescription().getId()
     182                        + (Boolean.valueOf(userSpace) ? " userspace" : "")
     183                        + (alsoDelete ? " alsoDelete" : ""));
    156184                if (alsoDelete) {
    157                     LOG.debug(">>>>>>>>>>>>>>>> [Thread " + hashCode() + "] DELETING DESCRIPTION " + name + " " + registerResponse.getDescription().getId() + (Boolean.valueOf(userSpace) ? " userspace " : "") + (alsoDelete ? " alsoDelete" : ""));
     185                    LOG.debug(">>>>>>>>>>>>>>>> [Thread " + hashCode()
     186                            + "] DELETING DESCRIPTION " + name + " "
     187                            + registerResponse.getDescription().getId()
     188                            + (Boolean.valueOf(userSpace) ? " userspace " : "")
     189                            + (alsoDelete ? " alsoDelete" : ""));
    158190                    ClientResponse response = getAuthenticatedResource(
    159                             getResource().path(path + registerResponse.getDescription().getId()).queryParam(USERSPACE_PARAM, userSpace)).
    160                             delete(ClientResponse.class);
     191                            getResource().path(
     192                                    path
     193                                            + registerResponse.getDescription()
     194                                                    .getId()).queryParam(
     195                                    USERSPACE_PARAM, userSpace)).delete(
     196                            ClientResponse.class);
    161197                    if (response.getStatus() != 200) {
    162                         errors.add("Failed to delete " + registerResponse.getDescription());
     198                        errors.add("Failed to delete "
     199                                + registerResponse.getDescription());
    163200                    }
    164201                }
    165                 //                System.out.println("THREAD FINISHED"+Thread.currentThread().getName());
     202                // System.out.println("THREAD FINISHED"+Thread.currentThread().getName());
    166203            }
    167204        });
Note: See TracChangeset for help on using the changeset viewer.