-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from jvasiljevich/accept-url-sources
Accept URL sources
- Loading branch information
Showing
18 changed files
with
328 additions
and
56 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
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
51 changes: 51 additions & 0 deletions
51
jsonschema2pojo-cli/src/main/java/org/jsonschema2pojo/cli/UrlConverter.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,51 @@ | ||
/** | ||
* Copyright © 2010-2014 Nokia | ||
* | ||
* Licensed 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 org.jsonschema2pojo.cli; | ||
|
||
import com.beust.jcommander.ParameterException; | ||
import com.beust.jcommander.converters.BaseConverter; | ||
import org.jsonschema2pojo.util.URLUtil; | ||
|
||
import java.net.URL; | ||
|
||
import static org.apache.commons.lang3.StringUtils.isBlank; | ||
|
||
|
||
/** | ||
* Convert a string into a url. | ||
* | ||
* @author jvasiljevich | ||
*/ | ||
public class UrlConverter extends BaseConverter<URL> { | ||
|
||
public UrlConverter(String optionName) { | ||
super(optionName); | ||
} | ||
|
||
public URL convert(String value) { | ||
if (isBlank(value)) { | ||
throw new ParameterException(getErrorString("a blank value", "a valid URL")); | ||
} | ||
|
||
try { | ||
return URLUtil.parseURL(value); | ||
} catch (IllegalArgumentException e) { | ||
throw new ParameterException(getErrorString(value, "a valid URL")); | ||
} | ||
|
||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
jsonschema2pojo-cli/src/test/java/org/jsonschema2pojo/cli/UrlConverterTest.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,57 @@ | ||
/** | ||
* Copyright © 2010-2014 Nokia | ||
* | ||
* Licensed 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 org.jsonschema2pojo.cli; | ||
|
||
import static org.hamcrest.Matchers.*; | ||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
import com.beust.jcommander.ParameterException; | ||
import org.jsonschema2pojo.Annotator; | ||
|
||
import java.net.URL; | ||
|
||
public class UrlConverterTest { | ||
|
||
private UrlConverter converter = new UrlConverter("--source"); | ||
|
||
@Test | ||
public void urlIsCreatedFromFilePath() { | ||
URL url = converter.convert("/path/to/something"); | ||
|
||
assertThat(url.getPath(), is("/path/to/something")); | ||
} | ||
|
||
@Test | ||
public void urlIsCreatedFromFileUrl() { | ||
URL url = converter.convert("file:/path/to/something"); | ||
|
||
assertThat(url.toString(), is("file:/path/to/something")); | ||
} | ||
|
||
@Test(expected = ParameterException.class) | ||
public void invalidUrlThrowsParameterException() { | ||
converter.convert("http:total nonsense"); | ||
} | ||
|
||
@Test(expected = ParameterException.class) | ||
public void nullValueThrowsParameterException() { | ||
converter.convert(null); | ||
} | ||
|
||
} |
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
Oops, something went wrong.