@@ -834,6 +834,218 @@ func TestComprehensiveXMLPreservation(t *testing.T) {
834834 t .Logf (" - Element counts correct: %d servers, %d mirrors, %d profiles" , serverCount , mirrorCount , profileCount )
835835}
836836
837+ func TestRemoveArtifactoryRepository (t * testing.T ) {
838+ tempDir := t .TempDir ()
839+ settingsPath := filepath .Join (tempDir , "settings.xml" )
840+
841+ // First configure Artifactory
842+ manager , err := NewSettingsXmlManagerWithPath (settingsPath )
843+ require .NoError (t , err )
844+
845+ err = manager .ConfigureArtifactoryRepository ("https://artifactory.example.com" , "maven-virtual" , "admin" , "secret123" )
846+ require .NoError (t , err )
847+
848+ // Verify configuration was added
849+ content , err := os .ReadFile (settingsPath )
850+ require .NoError (t , err )
851+ xmlContent := string (content )
852+ assert .Contains (t , xmlContent , "artifactory-mirror" )
853+ assert .Contains (t , xmlContent , "artifactory-deploy" )
854+ assert .Contains (t , xmlContent , "admin" )
855+
856+ // Now remove the configuration
857+ manager , err = NewSettingsXmlManagerWithPath (settingsPath )
858+ require .NoError (t , err )
859+
860+ err = manager .RemoveArtifactoryRepository ("https://artifactory.example.com" , "maven-virtual" )
861+ require .NoError (t , err )
862+
863+ // Verify configuration was removed
864+ content , err = os .ReadFile (settingsPath )
865+ require .NoError (t , err )
866+ xmlContent = string (content )
867+ assert .NotContains (t , xmlContent , "artifactory-mirror" )
868+ assert .NotContains (t , xmlContent , "artifactory-deploy" )
869+ assert .NotContains (t , xmlContent , "admin" )
870+ assert .NotContains (t , xmlContent , "secret123" )
871+
872+ // Should still have valid XML structure
873+ manager , err = NewSettingsXmlManagerWithPath (settingsPath )
874+ require .NoError (t , err )
875+ root := manager .doc .SelectElement (xmlElementSettings )
876+ assert .NotNil (t , root )
877+ }
878+
879+ func TestRemoveArtifactoryRepository_WithoutURL (t * testing.T ) {
880+ tempDir := t .TempDir ()
881+ settingsPath := filepath .Join (tempDir , "settings.xml" )
882+
883+ // First configure Artifactory
884+ manager , err := NewSettingsXmlManagerWithPath (settingsPath )
885+ require .NoError (t , err )
886+
887+ err = manager .ConfigureArtifactoryRepository ("https://artifactory.example.com" , "maven-virtual" , "admin" , "secret123" )
888+ require .NoError (t , err )
889+
890+ // Remove without URL verification (should still work)
891+ manager , err = NewSettingsXmlManagerWithPath (settingsPath )
892+ require .NoError (t , err )
893+
894+ err = manager .RemoveArtifactoryRepository ("" , "" )
895+ require .NoError (t , err )
896+
897+ // Verify configuration was removed
898+ content , err := os .ReadFile (settingsPath )
899+ require .NoError (t , err )
900+ xmlContent := string (content )
901+ assert .NotContains (t , xmlContent , "artifactory-mirror" )
902+ assert .NotContains (t , xmlContent , "artifactory-deploy" )
903+ }
904+
905+ func TestRemoveArtifactoryRepository_URLMismatch (t * testing.T ) {
906+ tempDir := t .TempDir ()
907+ settingsPath := filepath .Join (tempDir , "settings.xml" )
908+
909+ // Configure with one URL
910+ manager , err := NewSettingsXmlManagerWithPath (settingsPath )
911+ require .NoError (t , err )
912+
913+ err = manager .ConfigureArtifactoryRepository ("https://artifactory.example.com" , "maven-virtual" , "admin" , "secret123" )
914+ require .NoError (t , err )
915+
916+ // Try to remove with different URL - should fail
917+ manager , err = NewSettingsXmlManagerWithPath (settingsPath )
918+ require .NoError (t , err )
919+
920+ err = manager .RemoveArtifactoryRepository ("https://different.example.com" , "maven-virtual" )
921+ assert .Error (t , err )
922+ assert .Contains (t , err .Error (), "mirror URL mismatch" )
923+
924+ // Configuration should still be present
925+ content , err := os .ReadFile (settingsPath )
926+ require .NoError (t , err )
927+ xmlContent := string (content )
928+ assert .Contains (t , xmlContent , "artifactory-mirror" )
929+ }
930+
931+ func TestRemoveArtifactoryRepository_PreservesOtherConfig (t * testing.T ) {
932+ tempDir := t .TempDir ()
933+ settingsPath := filepath .Join (tempDir , "settings.xml" )
934+
935+ // Create settings with existing configuration
936+ existingXML := `<?xml version="1.0" encoding="UTF-8"?>
937+ <settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
938+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
939+ xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
940+ <servers>
941+ <server>
942+ <id>my-server</id>
943+ <username>myuser</username>
944+ <password>mypass</password>
945+ </server>
946+ </servers>
947+ <mirrors>
948+ <mirror>
949+ <id>my-mirror</id>
950+ <url>https://my-mirror.com</url>
951+ <mirrorOf>central</mirrorOf>
952+ </mirror>
953+ </mirrors>
954+ <profiles>
955+ <profile>
956+ <id>my-profile</id>
957+ <activation>
958+ <activeByDefault>true</activeByDefault>
959+ </activation>
960+ </profile>
961+ </profiles>
962+ </settings>`
963+
964+ err := os .WriteFile (settingsPath , []byte (existingXML ), 0o644 )
965+ require .NoError (t , err )
966+
967+ // Add Artifactory config
968+ manager , err := NewSettingsXmlManagerWithPath (settingsPath )
969+ require .NoError (t , err )
970+
971+ err = manager .ConfigureArtifactoryRepository ("https://artifactory.example.com" , "maven-virtual" , "admin" , "secret123" )
972+ require .NoError (t , err )
973+
974+ // Remove Artifactory config
975+ manager , err = NewSettingsXmlManagerWithPath (settingsPath )
976+ require .NoError (t , err )
977+
978+ err = manager .RemoveArtifactoryRepository ("" , "" )
979+ require .NoError (t , err )
980+
981+ // Verify original configuration is preserved
982+ content , err := os .ReadFile (settingsPath )
983+ require .NoError (t , err )
984+ xmlContent := string (content )
985+
986+ // Original config should be present
987+ assert .Contains (t , xmlContent , "my-server" )
988+ assert .Contains (t , xmlContent , "myuser" )
989+ assert .Contains (t , xmlContent , "my-mirror" )
990+ assert .Contains (t , xmlContent , "my-profile" )
991+
992+ // Artifactory config should be removed
993+ assert .NotContains (t , xmlContent , "artifactory-mirror" )
994+ assert .NotContains (t , xmlContent , "artifactory-deploy" )
995+ assert .NotContains (t , xmlContent , "admin" )
996+ }
997+
998+ func TestRemoveArtifactoryRepository_Idempotent (t * testing.T ) {
999+ tempDir := t .TempDir ()
1000+ settingsPath := filepath .Join (tempDir , "settings.xml" )
1001+
1002+ // Create empty settings
1003+ manager , err := NewSettingsXmlManagerWithPath (settingsPath )
1004+ require .NoError (t , err )
1005+
1006+ // Try to remove from empty file - should not error
1007+ err = manager .RemoveArtifactoryRepository ("" , "" )
1008+ assert .NoError (t , err )
1009+
1010+ // Remove again - should still not error (idempotent)
1011+ manager , err = NewSettingsXmlManagerWithPath (settingsPath )
1012+ require .NoError (t , err )
1013+
1014+ err = manager .RemoveArtifactoryRepository ("" , "" )
1015+ assert .NoError (t , err )
1016+ }
1017+
1018+ func TestRemoveArtifactoryRepository_RemovesEmptyContainers (t * testing.T ) {
1019+ tempDir := t .TempDir ()
1020+ settingsPath := filepath .Join (tempDir , "settings.xml" )
1021+
1022+ // Configure Artifactory only (no other servers/mirrors/profiles)
1023+ manager , err := NewSettingsXmlManagerWithPath (settingsPath )
1024+ require .NoError (t , err )
1025+
1026+ err = manager .ConfigureArtifactoryRepository ("https://artifactory.example.com" , "maven-virtual" , "admin" , "secret123" )
1027+ require .NoError (t , err )
1028+
1029+ // Remove configuration
1030+ manager , err = NewSettingsXmlManagerWithPath (settingsPath )
1031+ require .NoError (t , err )
1032+
1033+ err = manager .RemoveArtifactoryRepository ("" , "" )
1034+ require .NoError (t , err )
1035+
1036+ // Verify empty container elements are removed
1037+ content , err := os .ReadFile (settingsPath )
1038+ require .NoError (t , err )
1039+ xmlContent := string (content )
1040+
1041+ assert .NotContains (t , xmlContent , "<servers>" )
1042+ assert .NotContains (t , xmlContent , "<mirrors>" )
1043+ assert .NotContains (t , xmlContent , "<profiles>" )
1044+
1045+ // Should still have valid root structure
1046+ assert .Contains (t , xmlContent , "<settings" )
1047+ }
1048+
8371049// Helper function to set home directory for cross-platform testing
8381050func setTestHomeDir (t * testing.T , tempDir string ) {
8391051 originalHome , err := os .UserHomeDir ()
0 commit comments