11/*
2- * Copyright 2024-2024 the original author or authors.
2+ * Copyright 2024-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1818
1919import java .io .IOException ;
2020import java .net .MalformedURLException ;
21+ import java .net .URI ;
2122import java .net .URL ;
2223import java .util .UUID ;
2324
@@ -49,6 +50,22 @@ void testMediaBuilderWithByteArrayResource() {
4950 assertThat (media .getName ()).isEqualTo (name );
5051 }
5152
53+ @ Test
54+ void testMediaBuilderWithUri () {
55+ MimeType mimeType = MimeType .valueOf ("image/png" );
56+ URI uri = URI .create ("http://example.com/image.png" );
57+ String id = "123" ;
58+ String name = "test-media" ;
59+
60+ Media media = Media .builder ().mimeType (mimeType ).data (uri ).id (id ).name (name ).build ();
61+
62+ assertThat (media .getMimeType ()).isEqualTo (mimeType );
63+ assertThat (media .getData ()).isInstanceOf (String .class );
64+ assertThat (media .getData ()).isEqualTo (uri .toString ());
65+ assertThat (media .getId ()).isEqualTo (id );
66+ assertThat (media .getName ()).isEqualTo (name );
67+ }
68+
5269 @ Test
5370 void testMediaBuilderWithURL () throws MalformedURLException {
5471 MimeType mimeType = MimeType .valueOf ("image/png" );
@@ -91,6 +108,13 @@ void testGetDataAsByteArrayWithInvalidData() {
91108 .hasMessageContaining ("Media data is not a byte[]" );
92109 }
93110
111+ @ Test
112+ void testMediaBuilderWithNullUri () {
113+ assertThatThrownBy (() -> Media .builder ().mimeType (MimeType .valueOf ("image/png" )).data ((URI ) null ).build ())
114+ .isInstanceOf (IllegalArgumentException .class )
115+ .hasMessageContaining ("URI must not be null" );
116+ }
117+
94118 @ Test
95119 void testMediaBuilderWithNullURL () {
96120 assertThatThrownBy (() -> Media .builder ().mimeType (MimeType .valueOf ("image/png" )).data ((URL ) null ).build ())
@@ -162,6 +186,20 @@ void testLastDataMethodWins() throws MalformedURLException {
162186 assertThat (media .getData ()).isSameAs (bytes );
163187 }
164188
189+ @ Test
190+ void testMediaConstructorWithUri () {
191+ MimeType mimeType = MimeType .valueOf ("image/png" );
192+ URI uri = URI .create ("http://example.com/image.png" );
193+
194+ Media media = new Media (mimeType , uri );
195+
196+ assertThat (media .getMimeType ()).isEqualTo (mimeType );
197+ assertThat (media .getData ()).isInstanceOf (String .class );
198+ assertThat (media .getData ()).isEqualTo (uri .toString ());
199+ assertThat (media .getId ()).isNull ();
200+ assertValidMediaName (media .getName (), "png" );
201+ }
202+
165203 @ Test
166204 void testMediaConstructorWithUrl () throws MalformedURLException {
167205 MimeType mimeType = MimeType .valueOf ("image/png" );
@@ -195,7 +233,7 @@ private void assertValidMediaName(String name, String expectedMimeSubtype) {
195233 }
196234
197235 @ Test
198- void testMediaConstructorWithResource () throws IOException {
236+ void testMediaConstructorWithResource () {
199237 MimeType mimeType = MimeType .valueOf ("image/png" );
200238 byte [] data = new byte [] { 1 , 2 , 3 };
201239 Resource resource = new ByteArrayResource (data );
@@ -210,7 +248,7 @@ void testMediaConstructorWithResource() throws IOException {
210248 }
211249
212250 @ Test
213- void testMediaConstructorWithResourceAndId () throws IOException {
251+ void testMediaConstructorWithResourceAndId () {
214252 MimeType mimeType = MimeType .valueOf ("image/png" );
215253 byte [] data = new byte [] { 1 , 2 , 3 };
216254 Resource resource = new ByteArrayResource (data );
@@ -256,6 +294,30 @@ public byte[] getContentAsByteArray() throws IOException {
256294
257295 /// Tests to ensure two arg ctors behave identically to the builder
258296
297+ @ Test
298+ void testUriConstructorMatchesBuilder () {
299+ // Given
300+ MimeType mimeType = MimeType .valueOf ("image/png" );
301+ URI uri = URI .create ("http://example.com/image.png" );
302+
303+ // When
304+ Media mediaFromCtor = new Media (mimeType , uri );
305+ Media mediaFromBuilder = Media .builder ().mimeType (mimeType ).data (uri ).build ();
306+
307+ // Then - verify all properties match
308+ assertThat (mediaFromCtor .getMimeType ()).isEqualTo (mediaFromBuilder .getMimeType ());
309+ assertThat (mediaFromCtor .getData ()).isEqualTo (mediaFromBuilder .getData ());
310+ assertThat (mediaFromCtor .getId ()).isEqualTo (mediaFromBuilder .getId ());
311+
312+ // Verify name structure for both instances
313+ assertValidMediaName (mediaFromCtor .getName (), "png" );
314+ assertValidMediaName (mediaFromBuilder .getName (), "png" );
315+
316+ // Data type consistency
317+ assertThat (mediaFromCtor .getData ()).isInstanceOf (String .class );
318+ assertThat (mediaFromBuilder .getData ()).isInstanceOf (String .class );
319+ }
320+
259321 @ Test
260322 void testURLConstructorMatchesBuilder () throws MalformedURLException {
261323 // Given
@@ -305,6 +367,30 @@ void testResourceConstructorMatchesBuilder() throws IOException {
305367 assertThat (mediaFromBuilder .getData ()).isInstanceOf (byte [].class );
306368 }
307369
370+ @ Test
371+ void testUriConstructorNullValidation () {
372+ MimeType mimeType = MimeType .valueOf ("image/png" );
373+
374+ // Test null mimeType
375+ assertThatThrownBy (() -> new Media (null , URI .create ("http://example.com/image.png" )))
376+ .isInstanceOf (IllegalArgumentException .class )
377+ .hasMessage ("MimeType must not be null" );
378+
379+ // Test null URL
380+ assertThatThrownBy (() -> new Media (mimeType , (URI ) null )).isInstanceOf (IllegalArgumentException .class )
381+ .hasMessage ("URI must not be null" );
382+
383+ // Compare with builder validation
384+ assertThatThrownBy (
385+ () -> Media .builder ().mimeType (null ).data (URI .create ("http://example.com/image.png" )).build ())
386+ .isInstanceOf (IllegalArgumentException .class )
387+ .hasMessage ("MimeType must not be null" );
388+
389+ assertThatThrownBy (() -> Media .builder ().mimeType (mimeType ).data ((URI ) null ).build ())
390+ .isInstanceOf (IllegalArgumentException .class )
391+ .hasMessage ("URI must not be null" );
392+ }
393+
308394 @ Test
309395 void testURLConstructorNullValidation () {
310396 MimeType mimeType = MimeType .valueOf ("image/png" );
@@ -375,7 +461,7 @@ public byte[] getContentAsByteArray() throws IOException {
375461 }
376462
377463 @ Test
378- void testDifferentMimeTypesNameFormat () throws IOException {
464+ void testDifferentMimeTypesNameFormat () {
379465 // Test constructor name generation
380466 Media jpegMediaCtor = new Media (Media .Format .IMAGE_JPEG , new ByteArrayResource (new byte [] { 1 , 2 , 3 }));
381467 assertValidMediaName (jpegMediaCtor .getName (), "jpeg" );
0 commit comments