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.
Allow to get existing ChannelOption / AttributeKey from String
Motivation: We should allow to get a ChannelOption/AttributeKey from a String. This will make it a lot easier to make use of configuration files in applications. Modifications: - Add exists(...), newInstance(...) method to ChannelOption and AttributeKey and alter valueOf(...) to return an existing instance for a String or create one. - Add unit tests. Result: Much more flexible usage of ChannelOption and AttributeKey.
- Loading branch information
1 parent
6e508e6
commit 99bd43e
Showing
5 changed files
with
192 additions
and
1 deletion.
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
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
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,59 @@ | ||
/* | ||
* Copyright 2015 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.util; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class AttributeKeyTest { | ||
|
||
@Test | ||
public void testExists() { | ||
String name = "test"; | ||
assertFalse(AttributeKey.exists(name)); | ||
AttributeKey<String> attr = AttributeKey.valueOf(name); | ||
|
||
assertTrue(AttributeKey.exists(name)); | ||
assertNotNull(attr); | ||
} | ||
|
||
@Test | ||
public void testValueOf() { | ||
String name = "test1"; | ||
assertFalse(AttributeKey.exists(name)); | ||
AttributeKey<String> attr = AttributeKey.valueOf(name); | ||
AttributeKey<String> attr2 = AttributeKey.valueOf(name); | ||
|
||
assertSame(attr, attr2); | ||
} | ||
|
||
@Test | ||
public void testNewInstance() { | ||
String name = "test2"; | ||
assertFalse(AttributeKey.exists(name)); | ||
AttributeKey<String> attr = AttributeKey.newInstance(name); | ||
assertTrue(AttributeKey.exists(name)); | ||
assertNotNull(attr); | ||
|
||
try { | ||
AttributeKey.<String>newInstance(name); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
// expected | ||
} | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
transport/src/test/java/io/netty/channel/ChannelOptionTest.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,64 @@ | ||
/* | ||
* Copyright 2015 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.channel; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertSame; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
|
||
public class ChannelOptionTest { | ||
|
||
@Test | ||
public void testExists() { | ||
String name = "test"; | ||
assertFalse(ChannelOption.exists(name)); | ||
ChannelOption<String> option = ChannelOption.valueOf(name); | ||
|
||
assertTrue(ChannelOption.exists(name)); | ||
assertNotNull(option); | ||
} | ||
|
||
@Test | ||
public void testValueOf() { | ||
String name = "test1"; | ||
assertFalse(ChannelOption.exists(name)); | ||
ChannelOption<String> option = ChannelOption.valueOf(name); | ||
ChannelOption<String> option2 = ChannelOption.valueOf(name); | ||
|
||
assertSame(option, option2); | ||
} | ||
|
||
@Test | ||
public void testCreateOrFail() { | ||
String name = "test2"; | ||
assertFalse(ChannelOption.exists(name)); | ||
ChannelOption<String> option = ChannelOption.newInstance(name); | ||
assertTrue(ChannelOption.exists(name)); | ||
assertNotNull(option); | ||
|
||
try { | ||
ChannelOption.<String>newInstance(name); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
// expected | ||
} | ||
} | ||
} |