netty5/codec-smtp/src/test/java/io/netty/handler/codec/smtp/SmtpCommandTest.java
Nikolay Fedorovskikh 0234878c4b 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.
2017-08-18 09:46:53 +02:00

46 lines
1.6 KiB
Java

/*
* 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());
}
}