001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestContentInfo.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf;
009:
010: import java.sql.Timestamp;
011: import java.util.HashMap;
012: import junit.framework.TestCase;
013:
014: public class TestContentInfo extends TestCase {
015: public TestContentInfo(String name) {
016: super (name);
017: }
018:
019: public void testInstantiation() {
020: ContentInfo contentinfo = new ContentInfo();
021: assertNotNull(contentinfo);
022:
023: assertNull(contentinfo.getPath());
024: assertEquals(-1, contentinfo.getVersion());
025: assertNull(contentinfo.getCreated());
026: assertNull(contentinfo.getMimeType());
027: assertFalse(contentinfo.isFragment());
028: assertFalse(contentinfo.hasName());
029: assertNull(contentinfo.getName());
030: assertNull(contentinfo.getAttributes());
031: assertFalse(contentinfo.hasAttributes());
032: assertFalse(contentinfo.hasAttribute("attr1"));
033: assertNull(contentinfo.getAttribute("attr1"));
034: assertEquals(-1, contentinfo.getSize());
035: assertFalse(contentinfo.hasProperties());
036: assertNull(contentinfo.getProperties());
037: assertFalse(contentinfo.hasProperty("some prop"));
038: assertNull(contentinfo.getProperty("some prop"));
039: }
040:
041: public void testPath() {
042: ContentInfo contentinfo = new ContentInfo();
043: contentinfo.setPath("/my/path");
044: assertEquals("/my/path", contentinfo.getPath());
045: }
046:
047: public void testVersion() {
048: ContentInfo contentinfo = new ContentInfo();
049: contentinfo.setVersion(5);
050: assertEquals(5, contentinfo.getVersion());
051: }
052:
053: public void testMimeType() {
054: ContentInfo contentinfo = new ContentInfo();
055: contentinfo.setMimeType(MimeType.IMAGE_GIF.toString());
056: assertEquals(MimeType.IMAGE_GIF.toString(), contentinfo
057: .getMimeType());
058: }
059:
060: public void testFragment() {
061: ContentInfo contentinfo = new ContentInfo();
062: contentinfo.setFragment(true);
063: assertTrue(contentinfo.isFragment());
064: contentinfo.setFragment(false);
065: assertFalse(contentinfo.isFragment());
066: }
067:
068: public void testCreated() {
069: ContentInfo contentinfo = new ContentInfo();
070: Timestamp ts = new Timestamp(System.currentTimeMillis());
071: contentinfo.setCreated(ts);
072: assertEquals(ts, contentinfo.getCreated());
073: }
074:
075: public void testName() {
076: ContentInfo contentinfo = new ContentInfo();
077: contentinfo.setName("myname.gif");
078: assertTrue(contentinfo.hasName());
079: assertEquals("myname.gif", contentinfo.getName());
080: contentinfo.setName(null);
081: assertFalse(contentinfo.hasName());
082: assertNull(contentinfo.getName());
083: }
084:
085: public void testOptimalPath() {
086: ContentInfo contentinfo = new ContentInfo();
087: contentinfo.setPath("/some/path");
088: assertEquals("/some/path", contentinfo.getOptimalPath());
089: contentinfo.setName("myname.gif");
090: assertEquals("/some/path/myname.gif", contentinfo
091: .getOptimalPath());
092: contentinfo.setPath(null);
093: assertNull(contentinfo.getOptimalPath());
094: }
095:
096: public void testSetAttributes() {
097: ContentInfo contentinfo = new ContentInfo();
098: HashMap<String, String> attrs = new HashMap<String, String>();
099: attrs.put("attr1", "val1");
100: attrs.put("attr2", "val2");
101: attrs.put("attr3", "val3");
102:
103: contentinfo.setAttributes(attrs);
104: assertTrue(contentinfo.hasAttributes());
105: assertSame(contentinfo.getAttributes(), attrs);
106: assertEquals(contentinfo.getAttributes().size(), attrs.size());
107: assertTrue(contentinfo.getAttributes().containsKey("attr1"));
108: assertTrue(contentinfo.getAttributes().containsKey("attr2"));
109: assertTrue(contentinfo.getAttributes().containsKey("attr3"));
110: assertTrue(contentinfo.hasAttribute("attr1"));
111: assertTrue(contentinfo.hasAttribute("attr2"));
112: assertTrue(contentinfo.hasAttribute("attr3"));
113: assertEquals("val1", contentinfo.getAttribute("attr1"));
114: assertEquals("val2", contentinfo.getAttribute("attr2"));
115: assertEquals("val3", contentinfo.getAttribute("attr3"));
116:
117: attrs.remove("attr2");
118:
119: contentinfo.setAttributes(attrs);
120: assertTrue(contentinfo.hasAttributes());
121: assertSame(contentinfo.getAttributes(), attrs);
122: assertEquals(contentinfo.getAttributes().size(), 2);
123: assertTrue(contentinfo.getAttributes().containsKey("attr1"));
124: assertFalse(contentinfo.getAttributes().containsKey("attr2"));
125: assertTrue(contentinfo.getAttributes().containsKey("attr3"));
126: assertTrue(contentinfo.hasAttribute("attr1"));
127: assertFalse(contentinfo.hasAttribute("attr2"));
128: assertTrue(contentinfo.hasAttribute("attr3"));
129: assertEquals("val1", contentinfo.getAttribute("attr1"));
130: assertNull(contentinfo.getAttribute("attr2"));
131: assertEquals("val3", contentinfo.getAttribute("attr3"));
132:
133: contentinfo.setAttributes(null);
134: assertFalse(contentinfo.hasAttributes());
135: assertNull(contentinfo.getAttributes());
136: }
137:
138: public void testSize() {
139: ContentInfo contentinfo = new ContentInfo();
140: contentinfo.setSize(2434);
141: assertEquals(2434, contentinfo.getSize());
142: }
143:
144: public void testFormattedSize() {
145: ContentInfo contentinfo = new ContentInfo();
146: contentinfo.setSize(90);
147: assertEquals("0.09KB", contentinfo.getFormattedSize());
148: contentinfo.setSize((int) (12.78 * 1024));
149: assertEquals("12.78KB", contentinfo.getFormattedSize());
150: contentinfo.setSize((int) (101.23 * 1024));
151: assertEquals("101KB", contentinfo.getFormattedSize());
152: contentinfo.setSize((int) (278.15 * 1024 * 1024));
153: assertEquals("278.15MB", contentinfo.getFormattedSize());
154: contentinfo.setSize((int) (1.76 * 1024 * 1024 * 1024));
155: assertEquals("1.76GB", contentinfo.getFormattedSize());
156: }
157:
158: public void testProperties() {
159: ContentInfo contentinfo = new ContentInfo();
160: HashMap<String, String> props = new HashMap<String, String>();
161: props.put("prop1", "val1");
162: props.put("prop2", "val2");
163: props.put("prop3", "val3");
164:
165: contentinfo.setProperties(props);
166: assertTrue(contentinfo.hasProperties());
167: assertSame(contentinfo.getProperties(), props);
168: assertEquals(contentinfo.getProperties().size(), props.size());
169: assertTrue(contentinfo.getProperties().containsKey("prop1"));
170: assertTrue(contentinfo.getProperties().containsKey("prop2"));
171: assertTrue(contentinfo.getProperties().containsKey("prop3"));
172: assertTrue(contentinfo.hasProperty("prop1"));
173: assertTrue(contentinfo.hasProperty("prop2"));
174: assertTrue(contentinfo.hasProperty("prop3"));
175: assertEquals("val1", contentinfo.getProperty("prop1"));
176: assertEquals("val2", contentinfo.getProperty("prop2"));
177: assertEquals("val3", contentinfo.getProperty("prop3"));
178:
179: props.remove("prop2");
180:
181: contentinfo.setProperties(props);
182: assertTrue(contentinfo.hasProperties());
183: assertSame(contentinfo.getProperties(), props);
184: assertEquals(contentinfo.getProperties().size(), 2);
185: assertTrue(contentinfo.getProperties().containsKey("prop1"));
186: assertFalse(contentinfo.getProperties().containsKey("prop2"));
187: assertTrue(contentinfo.getProperties().containsKey("prop3"));
188: assertTrue(contentinfo.hasProperty("prop1"));
189: assertFalse(contentinfo.hasProperty("prop2"));
190: assertTrue(contentinfo.hasProperty("prop3"));
191: assertEquals("val1", contentinfo.getProperty("prop1"));
192: assertNull(contentinfo.getProperty("prop2"));
193: assertEquals("val3", contentinfo.getProperty("prop3"));
194:
195: contentinfo.setProperties(null);
196: assertFalse(contentinfo.hasProperties());
197: assertNull(contentinfo.getProperties());
198: }
199:
200: public void testValidation() {
201: ContentInfo contentinfo = new ContentInfo();
202:
203: contentinfo.resetValidation();
204: assertFalse(contentinfo.validate());
205: assertFalse(contentinfo.isSubjectValid("path"));
206: assertFalse(contentinfo.isSubjectValid("mimeType"));
207: assertFalse(contentinfo.isSubjectValid("version"));
208: assertFalse(contentinfo.isSubjectValid("created"));
209:
210: contentinfo.resetValidation();
211: contentinfo.setPath("/some/other/path");
212: assertFalse(contentinfo.validate());
213: assertTrue(contentinfo.isSubjectValid("path"));
214:
215: contentinfo.resetValidation();
216: contentinfo.setMimeType("");
217: assertFalse(contentinfo.validate());
218: assertFalse(contentinfo.isSubjectValid("mimeType"));
219: contentinfo.resetValidation();
220: contentinfo
221: .setMimeType("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
222: assertFalse(contentinfo.validate());
223: assertFalse(contentinfo.isSubjectValid("mimeType"));
224: contentinfo.resetValidation();
225: contentinfo.setMimeType(MimeType.APPLICATION_XHTML.toString());
226: assertFalse(contentinfo.validate());
227: assertTrue(contentinfo.isSubjectValid("mimeType"));
228:
229: contentinfo.resetValidation();
230: contentinfo.setVersion(5);
231: assertFalse(contentinfo.validate());
232: assertTrue(contentinfo.isSubjectValid("version"));
233:
234: contentinfo.resetValidation();
235: contentinfo
236: .setCreated(new Timestamp(System.currentTimeMillis()));
237: assertTrue(contentinfo.validate());
238: assertTrue(contentinfo.isSubjectValid("created"));
239:
240: contentinfo.resetValidation();
241: assertTrue(contentinfo.validate());
242: assertTrue(contentinfo.isSubjectValid("path"));
243: assertTrue(contentinfo.isSubjectValid("mimeType"));
244: assertTrue(contentinfo.isSubjectValid("version"));
245: assertTrue(contentinfo.isSubjectValid("created"));
246: }
247: }
|