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: TestContent.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf;
009:
010: import java.util.HashMap;
011: import junit.framework.TestCase;
012:
013: public class TestContent extends TestCase {
014: public TestContent(String name) {
015: super (name);
016: }
017:
018: public void testInstantiation() {
019: Content content = new Content(MimeType.APPLICATION_XHTML,
020: "<html></html>");
021: assertNotNull(content);
022:
023: assertSame(content.getMimeType(), MimeType.APPLICATION_XHTML);
024: assertEquals(content.getData(), "<html></html>");
025: assertFalse(content.isFragment());
026: assertNull(content.getAttributes());
027: assertFalse(content.hasName());
028: assertNull(content.getName());
029: assertFalse(content.hasAttributes());
030: assertFalse(content.hasAttribute("attr1"));
031: assertNull(content.getAttribute("attr1"));
032: assertFalse(content.hasCachedLoadedData());
033: assertNull(content.getCachedLoadedData());
034: assertFalse(content.hasProperties());
035: assertNull(content.getProperties());
036: assertFalse(content.hasProperty("some prop"));
037: assertNull(content.getProperty("some prop"));
038: }
039:
040: public void testInstantiationNullData() {
041: Content content = new Content(MimeType.APPLICATION_XHTML, null);
042: assertNotNull(content);
043:
044: assertSame(content.getMimeType(), MimeType.APPLICATION_XHTML);
045: assertNull(content.getData());
046: assertFalse(content.isFragment());
047: assertFalse(content.hasName());
048: assertNull(content.getName());
049: assertNull(content.getAttributes());
050: assertFalse(content.hasAttributes());
051: assertFalse(content.hasAttribute("attr1"));
052: assertNull(content.getAttribute("attr1"));
053: assertFalse(content.hasCachedLoadedData());
054: assertNull(content.getCachedLoadedData());
055: assertFalse(content.hasProperties());
056: assertNull(content.getProperties());
057: assertFalse(content.hasProperty("some prop"));
058: assertNull(content.getProperty("some prop"));
059: }
060:
061: public void testInvalidMimeType() {
062: Content content = null;
063: try {
064: content = new Content(null, "<html></html>");
065: fail();
066: } catch (IllegalArgumentException e) {
067: assertTrue(e.getMessage().indexOf("mimeType") != -1);
068: }
069:
070: assertNull(content);
071: }
072:
073: public void testFragment() {
074: Content content = new Content(MimeType.APPLICATION_XHTML,
075: "<html></html>");
076: content.setFragment(true);
077: assertTrue(content.isFragment());
078: assertSame(content, content.fragment(false));
079: assertFalse(content.isFragment());
080: }
081:
082: public void testName() {
083: Content content = new Content(MimeType.APPLICATION_XHTML,
084: "<html></html>");
085: content.setName("myname.gif");
086: assertTrue(content.hasName());
087: assertEquals("myname.gif", content.getName());
088: assertSame(content, content.name("anothername.png"));
089: assertTrue(content.hasName());
090: assertEquals("anothername.png", content.getName());
091: content.name(null);
092: assertFalse(content.hasName());
093: assertNull(content.getName());
094: }
095:
096: public void testSetAttributes() {
097: Content content = new Content(MimeType.APPLICATION_XHTML,
098: "<html></html>");
099: HashMap<String, String> attrs = new HashMap<String, String>();
100: attrs.put("attr1", "val1");
101: attrs.put("attr2", "val2");
102: attrs.put("attr3", "val3");
103:
104: content.setAttributes(attrs);
105: assertTrue(content.hasAttributes());
106: assertNotSame(content.getAttributes(), attrs);
107: assertEquals(content.getAttributes().size(), attrs.size());
108: assertTrue(content.getAttributes().containsKey("attr1"));
109: assertTrue(content.getAttributes().containsKey("attr2"));
110: assertTrue(content.getAttributes().containsKey("attr3"));
111: assertTrue(content.hasAttribute("attr1"));
112: assertTrue(content.hasAttribute("attr2"));
113: assertTrue(content.hasAttribute("attr3"));
114: assertEquals("val1", content.getAttribute("attr1"));
115: assertEquals("val2", content.getAttribute("attr2"));
116: assertEquals("val3", content.getAttribute("attr3"));
117:
118: attrs.remove("attr2");
119:
120: content.attributes(attrs);
121: assertTrue(content.hasAttributes());
122: assertNotSame(content.getAttributes(), attrs);
123: assertEquals(content.getAttributes().size(), 2);
124: assertTrue(content.getAttributes().containsKey("attr1"));
125: assertFalse(content.getAttributes().containsKey("attr2"));
126: assertTrue(content.getAttributes().containsKey("attr3"));
127: assertTrue(content.hasAttribute("attr1"));
128: assertFalse(content.hasAttribute("attr2"));
129: assertTrue(content.hasAttribute("attr3"));
130: assertEquals("val1", content.getAttribute("attr1"));
131: assertNull(content.getAttribute("attr2"));
132: assertEquals("val3", content.getAttribute("attr3"));
133:
134: content.setAttributes(null);
135: assertFalse(content.hasAttributes());
136: assertNull(content.getAttributes());
137: }
138:
139: public void testSetAttribute() {
140: Content content = new Content(MimeType.APPLICATION_XHTML,
141: "<html></html>");
142:
143: assertSame(content, content.attribute("attr1", false)
144: .attribute("attr2", 'O').attribute("attr3", (byte) 89)
145: .attribute("attr4", (short) 120).attribute("attr5",
146: 19843).attribute("attr6", 3847934L).attribute(
147: "attr7", 343.9f).attribute("attr8", 3487.343d)
148: .attribute("attr9", "value"));
149:
150: assertTrue(content.hasAttributes());
151: assertEquals(content.getAttributes().size(), 9);
152: assertTrue(content.hasAttribute("attr1"));
153: assertTrue(content.hasAttribute("attr2"));
154: assertTrue(content.hasAttribute("attr3"));
155: assertTrue(content.hasAttribute("attr4"));
156: assertTrue(content.hasAttribute("attr5"));
157: assertTrue(content.hasAttribute("attr6"));
158: assertTrue(content.hasAttribute("attr7"));
159: assertTrue(content.hasAttribute("attr8"));
160: assertTrue(content.hasAttribute("attr9"));
161: assertEquals("false", content.getAttribute("attr1"));
162: assertEquals("O", content.getAttribute("attr2"));
163: assertEquals("89", content.getAttribute("attr3"));
164: assertEquals("120", content.getAttribute("attr4"));
165: assertEquals("19843", content.getAttribute("attr5"));
166: assertEquals("3847934", content.getAttribute("attr6"));
167: assertEquals("343.9", content.getAttribute("attr7"));
168: assertEquals("3487.343", content.getAttribute("attr8"));
169: assertEquals("value", content.getAttribute("attr9"));
170: }
171:
172: public void testLoadedDataCache() {
173: Content content = new Content(MimeType.APPLICATION_XHTML,
174: "<html></html>");
175: Object object = new Object();
176: content.setCachedLoadedData(object);
177: assertTrue(content.hasCachedLoadedData());
178: assertSame(object, content.getCachedLoadedData());
179: content.cachedLoadedData(null);
180: assertFalse(content.hasCachedLoadedData());
181: assertNull(content.getCachedLoadedData());
182: }
183:
184: public void testSetProperties() {
185: Content content = new Content(MimeType.APPLICATION_XHTML,
186: "<html></html>");
187: HashMap<String, String> props = new HashMap<String, String>();
188: props.put("prop1", "val1");
189: props.put("prop2", "val2");
190: props.put("prop3", "val3");
191:
192: content.setProperties(props);
193: assertTrue(content.hasProperties());
194: assertNotSame(content.getProperties(), props);
195: assertEquals(content.getProperties().size(), props.size());
196: assertTrue(content.getProperties().containsKey("prop1"));
197: assertTrue(content.getProperties().containsKey("prop2"));
198: assertTrue(content.getProperties().containsKey("prop3"));
199: assertTrue(content.hasProperty("prop1"));
200: assertTrue(content.hasProperty("prop2"));
201: assertTrue(content.hasProperty("prop3"));
202: assertEquals("val1", content.getProperty("prop1"));
203: assertEquals("val2", content.getProperty("prop2"));
204: assertEquals("val3", content.getProperty("prop3"));
205:
206: props.remove("prop2");
207:
208: content.properties(props);
209: assertTrue(content.hasProperties());
210: assertNotSame(content.getProperties(), props);
211: assertEquals(content.getProperties().size(), 2);
212: assertTrue(content.getProperties().containsKey("prop1"));
213: assertFalse(content.getProperties().containsKey("prop2"));
214: assertTrue(content.getProperties().containsKey("prop3"));
215: assertTrue(content.hasProperty("prop1"));
216: assertFalse(content.hasProperty("prop2"));
217: assertTrue(content.hasProperty("prop3"));
218: assertEquals("val1", content.getProperty("prop1"));
219: assertNull(content.getProperty("prop2"));
220: assertEquals("val3", content.getProperty("prop3"));
221:
222: content.setProperties(null);
223: assertFalse(content.hasProperties());
224: assertNull(content.getProperties());
225: }
226:
227: public void testSetProperty() {
228: Content content = new Content(MimeType.APPLICATION_XHTML,
229: "<html></html>");
230:
231: assertSame(content, content.property("prop1", false).property(
232: "prop2", 'O').property("prop3", (byte) 89).property(
233: "prop4", (short) 120).property("prop5", 19843)
234: .property("prop6", 3847934L).property("prop7", 343.9f)
235: .property("prop8", 3487.343d)
236: .property("prop9", "value"));
237:
238: assertTrue(content.hasProperties());
239: assertEquals(content.getProperties().size(), 9);
240: assertTrue(content.hasProperty("prop1"));
241: assertTrue(content.hasProperty("prop2"));
242: assertTrue(content.hasProperty("prop3"));
243: assertTrue(content.hasProperty("prop4"));
244: assertTrue(content.hasProperty("prop5"));
245: assertTrue(content.hasProperty("prop6"));
246: assertTrue(content.hasProperty("prop7"));
247: assertTrue(content.hasProperty("prop8"));
248: assertTrue(content.hasProperty("prop9"));
249: assertEquals("false", content.getProperty("prop1"));
250: assertEquals("O", content.getProperty("prop2"));
251: assertEquals("89", content.getProperty("prop3"));
252: assertEquals("120", content.getProperty("prop4"));
253: assertEquals("19843", content.getProperty("prop5"));
254: assertEquals("3847934", content.getProperty("prop6"));
255: assertEquals("343.9", content.getProperty("prop7"));
256: assertEquals("3487.343", content.getProperty("prop8"));
257: assertEquals("value", content.getProperty("prop9"));
258: }
259: }
|