|
1 | 1 | /*
|
2 |
| - * Copyright 2023 the original author or authors. |
| 2 | + * Copyright 2023-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 |
|
18 | 18 | import java.util.Collections;
|
19 | 19 |
|
| 20 | +import org.junit.jupiter.api.Nested; |
20 | 21 | import org.junit.jupiter.api.Test;
|
21 | 22 |
|
22 | 23 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
@@ -375,4 +376,186 @@ private <T> ApplicationContextRunner configCommon(Class<T> type, T bean, String
|
375 | 376 | bd.getPropertyValues().add(CommandRegistrationFactoryBean.COMMAND_METHOD_PARAMETERS, parameters);
|
376 | 377 | });
|
377 | 378 | }
|
| 379 | + |
| 380 | + @Nested |
| 381 | + class Aliases { |
| 382 | + |
| 383 | + @Test |
| 384 | + void aliasOnlyOnMethod() { |
| 385 | + configCommon(AliasOnlyOnMethod.class, new AliasOnlyOnMethod(), "command1", new Class[] { }) |
| 386 | + .run((context) -> { |
| 387 | + CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF, |
| 388 | + CommandRegistrationFactoryBean.class); |
| 389 | + assertThat(fb).isNotNull(); |
| 390 | + CommandRegistration registration = fb.getObject(); |
| 391 | + assertThat(registration).isNotNull(); |
| 392 | + assertThat(registration.getCommand()).isEqualTo("one two"); |
| 393 | + assertThat(registration.getAliases()).hasSize(1); |
| 394 | + assertThat(registration.getAliases().get(0).getCommand()).isEqualTo("four"); |
| 395 | + }); |
| 396 | + } |
| 397 | + |
| 398 | + @Command(command = "one") |
| 399 | + private static class AliasOnlyOnMethod { |
| 400 | + |
| 401 | + @Command(command = "two", alias = "four") |
| 402 | + void command1(){ |
| 403 | + } |
| 404 | + } |
| 405 | + |
| 406 | + @Test |
| 407 | + void aliasOnlyOnClass() { |
| 408 | + configCommon(AliasOnlyOnClass.class, new AliasOnlyOnClass(), "command1", new Class[] { }) |
| 409 | + .run((context) -> { |
| 410 | + CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF, |
| 411 | + CommandRegistrationFactoryBean.class); |
| 412 | + assertThat(fb).isNotNull(); |
| 413 | + CommandRegistration registration = fb.getObject(); |
| 414 | + assertThat(registration).isNotNull(); |
| 415 | + assertThat(registration.getCommand()).isEqualTo("one two"); |
| 416 | + assertThat(registration.getAliases()).hasSize(0); |
| 417 | + }); |
| 418 | + } |
| 419 | + |
| 420 | + @Command(command = "one", alias = "three") |
| 421 | + private static class AliasOnlyOnClass { |
| 422 | + |
| 423 | + @Command(command = "two") |
| 424 | + void command1(){ |
| 425 | + } |
| 426 | + } |
| 427 | + |
| 428 | + @Test |
| 429 | + void aliasOnlyOnMethodMultiCommandString() { |
| 430 | + configCommon(AliasOnlyOnMethodMultiCommandString.class, new AliasOnlyOnMethodMultiCommandString(), "command1", new Class[] { }) |
| 431 | + .run((context) -> { |
| 432 | + CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF, |
| 433 | + CommandRegistrationFactoryBean.class); |
| 434 | + assertThat(fb).isNotNull(); |
| 435 | + CommandRegistration registration = fb.getObject(); |
| 436 | + assertThat(registration).isNotNull(); |
| 437 | + assertThat(registration.getCommand()).isEqualTo("one two"); |
| 438 | + assertThat(registration.getAliases()).hasSize(1); |
| 439 | + assertThat(registration.getAliases().get(0).getCommand()).isEqualTo("four five"); |
| 440 | + }); |
| 441 | + } |
| 442 | + |
| 443 | + @Command(command = "one") |
| 444 | + private static class AliasOnlyOnMethodMultiCommandString { |
| 445 | + |
| 446 | + @Command(command = "two", alias = "four five") |
| 447 | + void command1(){ |
| 448 | + } |
| 449 | + } |
| 450 | + |
| 451 | + @Test |
| 452 | + void aliasOnlyOnMethodMultiCommandArray() { |
| 453 | + configCommon(AliasOnlyOnMethodMultiCommandArray.class, new AliasOnlyOnMethodMultiCommandArray(), "command1", new Class[] { }) |
| 454 | + .run((context) -> { |
| 455 | + CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF, |
| 456 | + CommandRegistrationFactoryBean.class); |
| 457 | + assertThat(fb).isNotNull(); |
| 458 | + CommandRegistration registration = fb.getObject(); |
| 459 | + assertThat(registration).isNotNull(); |
| 460 | + assertThat(registration.getCommand()).isEqualTo("one two"); |
| 461 | + assertThat(registration.getAliases()).hasSize(2); |
| 462 | + assertThat(registration.getAliases().get(0).getCommand()).isEqualTo("four"); |
| 463 | + assertThat(registration.getAliases().get(1).getCommand()).isEqualTo("five"); |
| 464 | + }); |
| 465 | + } |
| 466 | + |
| 467 | + @Command(command = "one") |
| 468 | + private static class AliasOnlyOnMethodMultiCommandArray { |
| 469 | + |
| 470 | + @Command(command = "two", alias = {"four", "five"}) |
| 471 | + void command1(){ |
| 472 | + } |
| 473 | + } |
| 474 | + |
| 475 | + @Test |
| 476 | + void aliasOnBothMethodStringEmpty() { |
| 477 | + configCommon(AliasOnBothMethodStringEmpty.class, new AliasOnBothMethodStringEmpty(), "command1", new Class[] { }) |
| 478 | + .run((context) -> { |
| 479 | + CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF, |
| 480 | + CommandRegistrationFactoryBean.class); |
| 481 | + assertThat(fb).isNotNull(); |
| 482 | + CommandRegistration registration = fb.getObject(); |
| 483 | + assertThat(registration).isNotNull(); |
| 484 | + assertThat(registration.getCommand()).isEqualTo("one two"); |
| 485 | + assertThat(registration.getAliases()).hasSize(1); |
| 486 | + assertThat(registration.getAliases().get(0).getCommand()).isEqualTo("three"); |
| 487 | + }); |
| 488 | + } |
| 489 | + |
| 490 | + @Command(command = "one", alias = "three") |
| 491 | + private static class AliasOnBothMethodStringEmpty { |
| 492 | + |
| 493 | + @Command(command = "two", alias = "") |
| 494 | + void command1(){ |
| 495 | + } |
| 496 | + } |
| 497 | + |
| 498 | + @Test |
| 499 | + void aliasOnBoth() { |
| 500 | + configCommon(AliasOnBoth.class, new AliasOnBoth(), "command1", new Class[] { }) |
| 501 | + .run((context) -> { |
| 502 | + CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF, |
| 503 | + CommandRegistrationFactoryBean.class); |
| 504 | + assertThat(fb).isNotNull(); |
| 505 | + CommandRegistration registration = fb.getObject(); |
| 506 | + assertThat(registration).isNotNull(); |
| 507 | + assertThat(registration.getCommand()).isEqualTo("one two"); |
| 508 | + assertThat(registration.getAliases()).hasSize(1); |
| 509 | + assertThat(registration.getAliases().get(0).getCommand()).isEqualTo("three four"); |
| 510 | + }); |
| 511 | + } |
| 512 | + |
| 513 | + @Command(command = "one", alias = "three") |
| 514 | + private static class AliasOnBoth { |
| 515 | + |
| 516 | + @Command(command = "two", alias = "four") |
| 517 | + void command1(){ |
| 518 | + } |
| 519 | + } |
| 520 | + |
| 521 | + @Test |
| 522 | + void aliasWithCommandOnBothMethodStringEmpty() { |
| 523 | + configCommon(AliasWithCommandOnBothMethodStringEmpty.class, new AliasWithCommandOnBothMethodStringEmpty(), "command1", new Class[] { }) |
| 524 | + .run((context) -> { |
| 525 | + CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF, |
| 526 | + CommandRegistrationFactoryBean.class); |
| 527 | + assertThat(fb).isNotNull(); |
| 528 | + CommandRegistration registration = fb.getObject(); |
| 529 | + assertThat(registration).isNotNull(); |
| 530 | + assertThat(registration.getCommand()).isEqualTo("one"); |
| 531 | + assertThat(registration.getAliases()).hasSize(1); |
| 532 | + assertThat(registration.getAliases().get(0).getCommand()).isEqualTo("ten"); |
| 533 | + }); |
| 534 | + configCommon(AliasWithCommandOnBothMethodStringEmpty.class, new AliasWithCommandOnBothMethodStringEmpty(), "command2", new Class[] { }) |
| 535 | + .run((context) -> { |
| 536 | + CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF, |
| 537 | + CommandRegistrationFactoryBean.class); |
| 538 | + assertThat(fb).isNotNull(); |
| 539 | + CommandRegistration registration = fb.getObject(); |
| 540 | + assertThat(registration).isNotNull(); |
| 541 | + assertThat(registration.getCommand()).isEqualTo("one two"); |
| 542 | + assertThat(registration.getAliases()).hasSize(1); |
| 543 | + assertThat(registration.getAliases().get(0).getCommand()).isEqualTo("ten twelve"); |
| 544 | + }); |
| 545 | + } |
| 546 | + |
| 547 | + @Command(command = "one", alias = "ten") |
| 548 | + private static class AliasWithCommandOnBothMethodStringEmpty { |
| 549 | + |
| 550 | + @Command(command = "", alias = "") |
| 551 | + void command1(){ |
| 552 | + } |
| 553 | + |
| 554 | + @Command(command = "two", alias = "twelve") |
| 555 | + void command2(){ |
| 556 | + } |
| 557 | + } |
| 558 | + |
| 559 | + } |
| 560 | + |
378 | 561 | }
|
0 commit comments