1 17 18 package javax.activation; 19 20 import java.io.IOException; 21 import java.io.ObjectInput; 22 import java.io.ObjectOutput; 23 24 import junit.framework.TestCase; 25 26 27 31 public class MimeTypeTest extends TestCase { 32 private MimeType mimeType; 33 34 public MimeTypeTest(String name) { 35 super(name); 36 } 37 38 public void setUp() throws Exception { 39 super.setUp(); 40 mimeType = new MimeType(); 41 } 42 43 public void testDefaultConstructor() throws MimeTypeParseException { 44 assertEquals("application/*", mimeType.getBaseType()); 45 assertEquals("application", mimeType.getPrimaryType()); 46 assertEquals("*", mimeType.getSubType()); 48 49 assertTrue(mimeType.match(new MimeType())); 50 assertTrue(mimeType.match(new MimeType("application/*"))); 51 52 assertNull(mimeType.getParameter("foo")); 53 assertEquals(0, mimeType.getParameters().size()); 54 assertTrue(mimeType.getParameters().isEmpty()); 55 } 56 57 public void testMimeTypeConstructor() throws MimeTypeParseException { 58 mimeType = new MimeType("text/plain"); 59 assertEquals("text/plain", mimeType.getBaseType()); 60 assertEquals("text", mimeType.getPrimaryType()); 61 assertEquals("plain", mimeType.getSubType()); 62 assertEquals("text/plain", mimeType.toString()); 63 } 64 65 public void testTypeConstructor() throws MimeTypeParseException { 66 mimeType = new MimeType("text", "plain"); 67 assertEquals("text/plain", mimeType.getBaseType()); 68 assertEquals("text", mimeType.getPrimaryType()); 69 assertEquals("plain", mimeType.getSubType()); 70 assertEquals("text/plain", mimeType.toString()); 71 } 72 73 public void testConstructorWithParams() throws MimeTypeParseException { 74 mimeType = new MimeType("text/plain; charset=\"iso-8859-1\""); 75 assertEquals("text/plain", mimeType.getBaseType()); 76 assertEquals("text", mimeType.getPrimaryType()); 77 assertEquals("plain", mimeType.getSubType()); 78 MimeTypeParameterList params = mimeType.getParameters(); 79 assertEquals(1, params.size()); 80 assertEquals("iso-8859-1", params.get("charset")); 81 assertEquals("text/plain; charset=iso-8859-1", mimeType.toString()); 82 } 83 84 public void testConstructorWithQuotableParams() throws MimeTypeParseException { 85 mimeType = new MimeType("text/plain; charset=\"iso(8859)\""); 86 assertEquals("text/plain", mimeType.getBaseType()); 87 assertEquals("text", mimeType.getPrimaryType()); 88 assertEquals("plain", mimeType.getSubType()); 89 MimeTypeParameterList params = mimeType.getParameters(); 90 assertEquals(1, params.size()); 91 assertEquals("iso(8859)", params.get("charset")); 92 assertEquals("text/plain; charset=\"iso(8859)\"", mimeType.toString()); 93 } 94 95 public void testWriteExternal() throws MimeTypeParseException, IOException { 96 mimeType = new MimeType("text/plain; charset=iso8859-1"); 97 mimeType.writeExternal(new ObjectOutput() { 98 public void writeUTF(String str) { 99 assertEquals("text/plain; charset=iso8859-1", str); 100 } 101 102 public void close() { 103 fail(); 104 } 105 106 public void flush() { 107 } 108 109 public void write(int b) { 110 fail(); 111 } 112 113 public void write(byte b[]) { 114 fail(); 115 } 116 117 public void write(byte b[], int off, int len) { 118 fail(); 119 } 120 121 public void writeObject(Object obj) { 122 fail(); 123 } 124 125 public void writeDouble(double v) { 126 fail(); 127 } 128 129 public void writeFloat(float v) { 130 fail(); 131 } 132 133 public void writeByte(int v) { 134 fail(); 135 } 136 137 public void writeChar(int v) { 138 fail(); 139 } 140 141 public void writeInt(int v) { 142 fail(); 143 } 144 145 public void writeShort(int v) { 146 fail(); 147 } 148 149 public void writeLong(long v) { 150 fail(); 151 } 152 153 public void writeBoolean(boolean v) { 154 fail(); 155 } 156 157 public void writeBytes(String s) { 158 fail(); 159 } 160 161 public void writeChars(String s){ 162 fail(); 163 } 164 }); 165 } 166 167 public void testReadExternal() throws IOException, ClassNotFoundException { 168 mimeType.readExternal(new ObjectInput() { 169 public String readUTF() { 170 return "text/plain; charset=iso-8859-1"; 171 } 172 173 public int available() { 174 fail(); 175 throw new AssertionError(); 176 } 177 178 public int read() { 179 fail(); 180 throw new AssertionError(); 181 } 182 183 public void close() { 184 fail(); 185 throw new AssertionError(); 186 } 187 188 public long skip(long n) { 189 fail(); 190 throw new AssertionError(); 191 } 192 193 public int read(byte b[]) { 194 fail(); 195 throw new AssertionError(); 196 } 197 198 public int read(byte b[], int off, int len) { 199 fail(); 200 throw new AssertionError(); 201 } 202 203 public Object readObject() { 204 fail(); 205 throw new AssertionError(); 206 } 207 208 public byte readByte() { 209 fail(); 210 throw new AssertionError(); 211 } 212 213 public char readChar() { 214 fail(); 215 throw new AssertionError(); 216 } 217 218 public double readDouble() { 219 fail(); 220 throw new AssertionError(); 221 } 222 223 public float readFloat() { 224 fail(); 225 throw new AssertionError(); 226 } 227 228 public int readInt() { 229 fail(); 230 throw new AssertionError(); 231 } 232 233 public int readUnsignedByte() { 234 fail(); 235 throw new AssertionError(); 236 } 237 238 public int readUnsignedShort() { 239 fail(); 240 throw new AssertionError(); 241 } 242 243 public long readLong() { 244 fail(); 245 throw new AssertionError(); 246 } 247 248 public short readShort() { 249 fail(); 250 throw new AssertionError(); 251 } 252 253 public boolean readBoolean() { 254 fail(); 255 throw new AssertionError(); 256 } 257 258 public int skipBytes(int n) { 259 fail(); 260 throw new AssertionError(); 261 } 262 263 public void readFully(byte b[]) { 264 fail(); 265 } 266 267 public void readFully(byte b[], int off, int len) { 268 fail(); 269 } 270 271 public String readLine() { 272 fail(); 273 throw new AssertionError(); 274 } 275 }); 276 assertEquals("text/plain", mimeType.getBaseType()); 277 assertEquals("text", mimeType.getPrimaryType()); 278 assertEquals("plain", mimeType.getSubType()); 279 MimeTypeParameterList params = mimeType.getParameters(); 280 assertEquals(1, params.size()); 281 assertEquals("iso-8859-1", params.get("charset")); 282 assertEquals("text/plain; charset=iso-8859-1", mimeType.toString()); 283 } 284 } 285 | Popular Tags |