Project

General

Profile

Revision d5168f4d

IDd5168f4db3cb2e1768b14bcd428dd3f5115b094d
Parent 99b798f1
Child fc89fba2

Added by Andreas Kohlbecker over 8 years ago

prevent media from being corrupted - fixing bugs in test and utils class

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/media/MediaUtils.java
47 47
        }
48 48

  
49 49
    /**
50
     * Filters the given List of Media by the supplied filter parameters <code>representationPartType</code>,
51
     * <code>mimeTypes</code>, <code>sizeTokens</code>, <code>widthOrDuration</code>, <code>height</code>, <code>size</code>.
52
     * Only best matching MediaRepresentation remains attached to the Media entities.
53
     * A Media entity may be completely omitted in the resulting list if  {@link #filterAndOrderMediaRepresentations(Set, Class, String[], Integer, Integer, Integer)}
54
     * is not returning any matching representation. This can be the case if a <code>representationPartType</code> is supplied.
55
     * <p>
56
     * In order to prevent the media entities returned by this method from being persisted accidentally the resulting list contains cloned versions of the originally
57
     * supplied media entities, which have the same UUIDs as the original ones.
50 58
     *
51 59
     * @param mediaList
52
     * @param representationPartType TODO
60
     * @param representationPartType any subclass of {@link MediaRepresentationPart}
53 61
     * @param mimeTypes
54 62
     * @param sizeTokens
55 63
     * @param widthOrDuration
......
93 101

  
94 102
        List<Media> returnMediaList = new ArrayList<Media>(mediaList.size());
95 103
        if(mediaList != null){
104
            Media mediaClone = null;
96 105
            for(Media media : mediaList){
97 106

  
98
                // media objects will modifies by this method, so
107
                // media objects will be modified by this method, so
99 108
                // we clone the medias in order to prevent them
100 109
                // from being stores accidentally
101 110
                // cloning will remove the id
102 111
                try {
103
                    media = (Media) media.clone();
112
                    mediaClone = (Media) media.clone();
113
                    mediaClone.setUuid(media.getUuid());
104 114
                } catch (CloneNotSupportedException e) {
105 115
                    // should never happen
106 116
                    logger.error(e);
117
                    continue;
107 118
                }
108 119

  
109 120
                Set<MediaRepresentation> candidateRepresentations = new HashSet<MediaRepresentation>();
......
117 128
                        // so it cannot retain the sorting which has been found by filterAndOrderMediaRepresentations()
118 129
                        // thus we take first one and remove all other representations
119 130

  
120
                        media.getRepresentations().clear();
121
                        media.addRepresentation(prefRepresentations.get(prefRepresentations.firstKey()));
122
                        returnMediaList.add(media);
131
                        mediaClone.getRepresentations().clear();
132
                        mediaClone.addRepresentation(prefRepresentations.get(prefRepresentations.firstKey()));
133
                        returnMediaList.add(mediaClone);
123 134
                    }
124 135
                } catch (NoSuchElementException nse) {
125 136
                    logger.debug(nse);
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/media/MediaUtilsTest.java
3 3
import java.net.URI;
4 4
import java.util.ArrayList;
5 5
import java.util.List;
6
import java.util.UUID;
6 7

  
7 8
import org.junit.Assert;
8 9
import org.junit.Before;
......
60 61

  
61 62
    }
62 63

  
64
    private Media findMediaByUUID(List<Media> mediaList, UUID uuid){
65
        for(Media media : mediaList){
66
            if(media.getUuid().equals(uuid)){
67
                return media;
68
            }
69
        }
70
        return null;
71
    }
72

  
63 73
    @Test
64 74
    public void testFindPreferredMedia(){
65 75

  
......
69 79
        imageList.add(mediaImage3);
70 80

  
71 81
        List<Media> filteredList = MediaUtils.findPreferredMedia(imageList, ImageFile.class, null, null, null, null, null);
72
        Assert.assertTrue(filteredList.contains(mediaImage1));
73
        Assert.assertTrue(filteredList.contains(mediaImage2));
74
        Assert.assertTrue(filteredList.contains(mediaImage3));
75 82

  
76
        ArrayList<Media> mixedMediaList = (ArrayList<Media>) imageList.clone();
83
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaImage1.getUuid()));
84
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaImage2.getUuid()));
85
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaImage3.getUuid()));
86

  
87
        ArrayList<Media> mixedMediaList =  new ArrayList<Media>();
88
        mixedMediaList.add(mediaImage1);
89
        mixedMediaList.add(mediaImage2);
90
        mixedMediaList.add(mediaImage3);
77 91
        mixedMediaList.add(mediaAudio1);
92
        filteredList = MediaUtils.findPreferredMedia(mixedMediaList, null, null, null, null, null, null);
93
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaImage1.getUuid()));
94
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaImage2.getUuid()));
95
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaImage3.getUuid()));
96
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaAudio1.getUuid()));
97

  
98
        filteredList = MediaUtils.findPreferredMedia(mixedMediaList, AudioFile.class, null, null, null, null, null);
99
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaAudio1.getUuid()));
100

  
78 101
        filteredList = MediaUtils.findPreferredMedia(mixedMediaList, ImageFile.class, null, null, null, null, null);
79
        Assert.assertTrue(filteredList.contains(mediaImage1));
80
        Assert.assertTrue(filteredList.contains(mediaImage2));
81
        Assert.assertTrue(filteredList.contains(mediaImage3));
82
        Assert.assertFalse(filteredList.contains(mediaAudio1));
102
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaImage1.getUuid()));
103
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaImage2.getUuid()));
104
        Assert.assertNotNull(findMediaByUUID(filteredList, mediaImage3.getUuid()));
83 105

  
84 106
    }
85 107

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)