@@ -105,6 +105,78 @@ public void testExceedMaxEntriesWithMultiplePorts() {
105105 assertTrue (containsEntry (entries , "test.com" , 8080 ));
106106 assertTrue (containsEntry (entries , "newsite.com" , 3000 ));
107107 }
108+ @ Test
109+ public void testAddArrayWithNewEntries () {
110+ Hostnames .HostnameEntry [] entriesToAdd = {
111+ new Hostnames .HostnameEntry ("example.com" , 80 ),
112+ new Hostnames .HostnameEntry ("test.com" , 443 )
113+ };
114+ entriesToAdd [0 ].incrementHits ();
115+ entriesToAdd [1 ].incrementHits ();
116+
117+ hostnames .addArray (entriesToAdd );
118+
119+ Hostnames .HostnameEntry [] entries = hostnames .asArray ();
120+ assertEquals (2 , entries .length );
121+ assertTrue (containsEntry (entries , "example.com" , 80 ));
122+ assertTrue (containsEntry (entries , "test.com" , 443 ));
123+ }
124+
125+ @ Test
126+ public void testAddArrayWithExistingEntries () {
127+ hostnames .add ("example.com" , 80 ); // Initial entry
128+ Hostnames .HostnameEntry [] entriesToAdd = {
129+ new Hostnames .HostnameEntry ("example.com" , 80 ), // Same entry, should merge hits
130+ new Hostnames .HostnameEntry ("test.com" , 443 ) // New entry
131+ };
132+ entriesToAdd [0 ].incrementHits ();
133+ entriesToAdd [1 ].incrementHits ();
134+
135+ hostnames .addArray (entriesToAdd );
136+
137+ Hostnames .HostnameEntry [] entries = hostnames .asArray ();
138+ assertEquals (2 , entries .length );
139+ assertEquals (2 , getHits (entries , "example.com" , 80 )); // Hits should be 2
140+ assertEquals (1 , getHits (entries , "test.com" , 443 )); // Hits should be 1
141+ }
142+
143+ @ Test
144+ public void testAddArrayExceedMaxEntries () {
145+ hostnames .add ("example.com" , 80 );
146+ hostnames .add ("test.com" , 443 );
147+ hostnames .add ("localhost" , 3000 );
148+
149+ Hostnames .HostnameEntry [] entriesToAdd = {
150+ new Hostnames .HostnameEntry ("newsite.com" , 8080 ), // This should cause an eviction
151+ new Hostnames .HostnameEntry ("example.com" , 80 ) // Should merge hits
152+ };
153+ entriesToAdd [0 ].incrementHits ();
154+ entriesToAdd [1 ].incrementHits (10 );
155+
156+ hostnames .addArray (entriesToAdd );
157+
158+ Hostnames .HostnameEntry [] entries = hostnames .asArray ();
159+ assertEquals (3 , entries .length );
160+ assertFalse (containsEntry (entries , "test.com" , 443 )); // "test.com" should be evicted
161+ assertTrue (containsEntry (entries , "localhost" , 3000 ));
162+ assertTrue (containsEntry (entries , "newsite.com" , 8080 ));
163+ assertEquals (10 , getHits (entries , "example.com" , 80 )); // Hits should be 2
164+ }
165+
166+ @ Test
167+ public void testAddArrayWithZeroPort () {
168+ Hostnames .HostnameEntry [] entriesToAdd = {
169+ new Hostnames .HostnameEntry ("example.com" , 0 ),
170+ new Hostnames .HostnameEntry ("test.com" , 0 )
171+ };
172+
173+ hostnames .addArray (entriesToAdd );
174+
175+ Hostnames .HostnameEntry [] entries = hostnames .asArray ();
176+ assertEquals (2 , entries .length );
177+ assertTrue (containsEntry (entries , "example.com" , 0 ));
178+ assertTrue (containsEntry (entries , "test.com" , 0 ));
179+ }
108180
109181 private boolean containsEntry (Hostnames .HostnameEntry [] entries , String hostname , int port ) {
110182 for (Hostnames .HostnameEntry entry : entries ) {
@@ -114,4 +186,12 @@ private boolean containsEntry(Hostnames.HostnameEntry[] entries, String hostname
114186 }
115187 return false ;
116188 }
189+ private int getHits (Hostnames .HostnameEntry [] entries , String hostname , int port ) {
190+ for (Hostnames .HostnameEntry entry : entries ) {
191+ if (entry .getHostname ().equals (hostname ) && entry .getPort () == port ) {
192+ return entry .getHits ();
193+ }
194+ }
195+ return 0 ; // Not found
196+ }
117197}
0 commit comments