1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.model.reference

File AttachmentReference.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

10
20
9
1
151
63
15
0.75
2.22
9
1.67

Classes

Class Line # Actions
AttachmentReference 33 20 0% 15 8
0.794871879.5%
 

Contributing tests

This file is covered by 132 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.model.reference;
21   
22    import java.beans.Transient;
23   
24    import org.xwiki.model.EntityType;
25   
26    /**
27    * Represents a reference to an Attachment (document reference and file name). Note that an attachment is always
28    * attached to a document.
29    *
30    * @version $Id: 43f1caf0d2c155539fac54ad659bc0c4fdec23ed $
31    * @since 2.2M1
32    */
 
33    public class AttachmentReference extends EntityReference
34    {
35    /**
36    * Special constructor that transforms a generic entity reference into an {@link AttachmentReference}. It checks the
37    * validity of the passed reference (ie correct type and correct parent).
38    *
39    * @param reference the reference to be transformed
40    * @exception IllegalArgumentException if the passed reference is not a valid attachment reference
41    */
 
42  10136 toggle public AttachmentReference(EntityReference reference)
43    {
44  10136 super(reference);
45    }
46   
47    /**
48    * Clone an AttachmentReference, but replace one of the parent in the chain by a new one.
49    *
50    * @param reference the reference that is cloned
51    * @param oldReference the old parent that will be replaced
52    * @param newReference the new parent that will replace oldReference in the chain
53    * @since 3.3M2
54    */
 
55  0 toggle protected AttachmentReference(EntityReference reference, EntityReference oldReference, EntityReference newReference)
56    {
57  0 super(reference, oldReference, newReference);
58    }
59   
60    /**
61    * Clone an AttachmentReference, but use the specified parent for its new parent.
62    *
63    * @param reference the reference to clone
64    * @param parent the new parent to use
65    * @since 10.8RC1
66    */
 
67  1 toggle public AttachmentReference(EntityReference reference, EntityReference parent)
68    {
69  1 super(reference, parent);
70    }
71   
72    /**
73    * Create a new attachment reference based on the attachment name and the parent document reference.
74    *
75    * @param fileName the name of the attachment
76    * @param parent the reference of the document
77    */
 
78  18305 toggle public AttachmentReference(String fileName, DocumentReference parent)
79    {
80  18305 super(fileName, EntityType.ATTACHMENT, parent);
81    }
82   
83    /**
84    * {@inheritDoc}
85    *
86    * Overridden in order to verify the validity of the passed parent.
87    *
88    * @exception IllegalArgumentException if the passed parent is not a valid attachment reference parent (ie an
89    * attachment reference)
90    */
 
91  28443 toggle @Override
92    protected void setParent(EntityReference parent)
93    {
94  28444 if (parent instanceof DocumentReference) {
95  22289 super.setParent(parent);
96  22289 return;
97    }
98   
99  6155 if (parent == null || parent.getType() != EntityType.DOCUMENT) {
100  2 throw new IllegalArgumentException("Invalid parent reference [" + parent + "] in an attachment reference");
101    }
102   
103  6153 super.setParent(new DocumentReference(parent));
104    }
105   
106    /**
107    * {@inheritDoc}
108    *
109    * Overridden in order to verify the validity of the passed type.
110    *
111    * @exception IllegalArgumentException if the passed type is not an attachment type
112    */
 
113  28444 toggle @Override
114    protected void setType(EntityType type)
115    {
116  28444 if (type != EntityType.ATTACHMENT) {
117  1 throw new IllegalArgumentException("Invalid type [" + type + "] for an attachment reference");
118    }
119   
120  28443 super.setType(EntityType.ATTACHMENT);
121    }
122   
123    /**
124    * @return the document reference contained in this attachment reference
125    */
 
126  27396 toggle @Transient
127    public DocumentReference getDocumentReference()
128    {
129  27396 return (DocumentReference) extractReference(EntityType.DOCUMENT);
130    }
131   
 
132  0 toggle @Override
133    public AttachmentReference replaceParent(EntityReference oldParent, EntityReference newParent)
134    {
135  0 if (newParent == oldParent) {
136  0 return this;
137    }
138   
139  0 return new AttachmentReference(this, oldParent, newParent);
140    }
141   
 
142  2 toggle @Override
143    public AttachmentReference replaceParent(EntityReference newParent)
144    {
145  2 if (newParent == getParent()) {
146  1 return this;
147    }
148   
149  1 return new AttachmentReference(this, newParent);
150    }
151    }