forked from netty/netty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the commands cache and hashCode() in SmtpCommand
Motivation: - A `hashCode` of the SmtpCommand is recalculated on each call of `hashCode()`. Cached hash code value can be just replaced with call of `name.hashCode()`. - The commands cache don't work for strings: `SmtpCommand.valueOf("HELO")` returns a new instance. - Field `contentExpected` is redundant and can be replaced with `equals(DATA)`. Modifications: - Use the `name.hashCode()` as hash code result. - Fix a command cache: use strings as map keys. - Replace field `contentExpected` to using `this.equals(DATA)`. - Add unit tests. Result: More correct and clean code.
- Loading branch information
1 parent
03d89c2
commit 0234878
Showing
2 changed files
with
76 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
codec-smtp/src/test/java/io/netty/handler/codec/smtp/SmtpCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2017 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.handler.codec.smtp; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class SmtpCommandTest { | ||
@Test | ||
public void getCommandFromCache() { | ||
assertSame(SmtpCommand.DATA, SmtpCommand.valueOf("DATA")); | ||
assertSame(SmtpCommand.EHLO, SmtpCommand.valueOf("EHLO")); | ||
assertNotSame(SmtpCommand.EHLO, SmtpCommand.valueOf("ehlo")); | ||
} | ||
|
||
@Test | ||
public void equalsIgnoreCase() { | ||
assertEquals(SmtpCommand.MAIL, SmtpCommand.valueOf("mail")); | ||
assertEquals(SmtpCommand.valueOf("test"), SmtpCommand.valueOf("TEST")); | ||
} | ||
|
||
@Test | ||
public void isContentExpected() { | ||
assertTrue(SmtpCommand.valueOf("DATA").isContentExpected()); | ||
assertTrue(SmtpCommand.valueOf("data").isContentExpected()); | ||
|
||
assertFalse(SmtpCommand.HELO.isContentExpected()); | ||
assertFalse(SmtpCommand.HELP.isContentExpected()); | ||
assertFalse(SmtpCommand.valueOf("DATA2").isContentExpected()); | ||
} | ||
} |