Skip to content

Commit b844c52

Browse files
neerajbhattrozza
authored andcommitted
Fixing NumberFormatException when parsing http:// address
https://jira.mongodb.org/browse/JAVA-913 Fixed, When http:// address is given to MongoClient, it now throws a MongoException as told by Jeff Refs JAVA-1357
1 parent 0ca1f13 commit b844c52

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/main/com/mongodb/ServerAddress.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,15 @@ public ServerAddress( String host , int port )
7979
}
8080
else {
8181
int idx = host.indexOf( ":" );
82-
if ( idx > 0 ){
83-
if ( port != defaultPort() )
84-
throw new IllegalArgumentException( "can't specify port in construct and via host" );
85-
port = Integer.parseInt( host.substring( idx + 1 ) );
86-
host = host.substring( 0 , idx ).trim();
82+
if (idx > 0) {
83+
if (port != defaultPort())
84+
throw new IllegalArgumentException("can't specify port in construct and via host");
85+
try {
86+
port = Integer.parseInt(host.substring(idx + 1));
87+
} catch (NumberFormatException e) {
88+
throw new MongoException("host and port should be specified in host:port format");
89+
}
90+
host = host.substring(0, idx).trim();
8791
}
8892
}
8993

src/test/com/mongodb/ServerAddressTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,9 @@ public void testParseWithPortWhenEquivalentPortIsAlsoSpecified() throws UnknownH
129129
public void testParseWithPortWhenNonEquivalentPortIsAlsoSpecified() throws UnknownHostException {
130130
new ServerAddress("somewhere:80", 1000);
131131
}
132-
}
132+
133+
@Test(expected = MongoException.class)
134+
public void testParseUrlWithMissingPort() throws UnknownHostException {
135+
new ServerAddress("mongodb://somewhere/");
136+
}
137+
}

0 commit comments

Comments
 (0)