|
| 1 | +/* |
| 2 | + * Created on 17.08.2015 |
| 3 | + */ |
| 4 | +package com.github.dockerjava.core; |
| 5 | + |
| 6 | +import org.apache.commons.lang.StringUtils; |
| 7 | +import org.testng.annotations.Test; |
| 8 | + |
| 9 | +import static org.testng.Assert.assertEquals; |
| 10 | + |
| 11 | +import com.github.dockerjava.api.model.AuthConfig; |
| 12 | +import com.github.dockerjava.core.NameParser.HostnameReposName; |
| 13 | +import com.github.dockerjava.core.NameParser.ReposTag; |
| 14 | + |
| 15 | +/** |
| 16 | + * |
| 17 | + * |
| 18 | + * @author marcus |
| 19 | + * |
| 20 | + */ |
| 21 | +public class NameParserTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testValidateRepoName() throws Exception { |
| 25 | + NameParser.validateRepoName("repository"); |
| 26 | + NameParser.validateRepoName("namespace/repository"); |
| 27 | + NameParser.validateRepoName("namespace-with-dashes/repository"); |
| 28 | + NameParser.validateRepoName("namespace/repository-with-dashes"); |
| 29 | + NameParser.validateRepoName("namespace.with.dots/repository"); |
| 30 | + NameParser.validateRepoName("namespace/repository.with.dots"); |
| 31 | + NameParser.validateRepoName("namespace_with_underscores/repository"); |
| 32 | + NameParser.validateRepoName("namespace/repository_with_underscores"); |
| 33 | + } |
| 34 | + |
| 35 | + @Test(expectedExceptions = InvalidRepositoryNameException.class) |
| 36 | + public void testValidateRepoNameEmpty() throws Exception { |
| 37 | + NameParser.validateRepoName(""); |
| 38 | + } |
| 39 | + |
| 40 | + @Test(expectedExceptions = InvalidRepositoryNameException.class) |
| 41 | + public void testValidateRepoNameExceedsMaxLength() throws Exception { |
| 42 | + NameParser.validateRepoName(StringUtils.repeat("repository", 255)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test(expectedExceptions = InvalidRepositoryNameException.class) |
| 46 | + public void testValidateRepoNameEndWithDash() throws Exception { |
| 47 | + NameParser.validateRepoName("repository-"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test(expectedExceptions = InvalidRepositoryNameException.class) |
| 51 | + public void testValidateRepoNameStartWithDash() throws Exception { |
| 52 | + NameParser.validateRepoName("-repository"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test(expectedExceptions = InvalidRepositoryNameException.class) |
| 56 | + public void testValidateRepoNameEndWithDot() throws Exception { |
| 57 | + NameParser.validateRepoName("repository."); |
| 58 | + } |
| 59 | + |
| 60 | + @Test(expectedExceptions = InvalidRepositoryNameException.class) |
| 61 | + public void testValidateRepoNameStartWithDot() throws Exception { |
| 62 | + NameParser.validateRepoName(".repository"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test(expectedExceptions = InvalidRepositoryNameException.class) |
| 66 | + public void testValidateRepoNameEndWithUnderscore() throws Exception { |
| 67 | + NameParser.validateRepoName("repository_"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test(expectedExceptions = InvalidRepositoryNameException.class) |
| 71 | + public void testValidateRepoNameStartWithUnderscore() throws Exception { |
| 72 | + NameParser.validateRepoName("_repository"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test(expectedExceptions = InvalidRepositoryNameException.class) |
| 76 | + public void testValidateRepoNameWithColon() throws Exception { |
| 77 | + NameParser.validateRepoName("repository:with:colon"); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testResolveSimpleRepositoryName() throws Exception { |
| 82 | + HostnameReposName resolved = NameParser.resolveRepositoryName("repository"); |
| 83 | + assertEquals(resolved, new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, "repository")); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testResolveRepositoryNameWithNamespace() throws Exception { |
| 88 | + HostnameReposName resolved = NameParser.resolveRepositoryName("namespace/repository"); |
| 89 | + assertEquals(resolved, new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, "namespace/repository")); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testResolveRepositoryNameWithNamespaceAndHostname() throws Exception { |
| 94 | + HostnameReposName resolved = NameParser.resolveRepositoryName("localhost:5000/namespace/repository"); |
| 95 | + assertEquals(resolved, new HostnameReposName("localhost:5000", "namespace/repository")); |
| 96 | + } |
| 97 | + |
| 98 | + @Test(expectedExceptions = InvalidRepositoryNameException.class) |
| 99 | + public void testResolveRepositoryNameWithIndex() throws Exception { |
| 100 | + NameParser.resolveRepositoryName("index.docker.io/repository"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testResolveReposTagWithoutTagSimple() throws Exception { |
| 105 | + ReposTag resolved = NameParser.parseRepositoryTag("repository"); |
| 106 | + assertEquals(resolved, new ReposTag("repository", "")); |
| 107 | + |
| 108 | + resolved = NameParser.parseRepositoryTag("namespace/repository"); |
| 109 | + assertEquals(resolved, new ReposTag("namespace/repository", "")); |
| 110 | + |
| 111 | + resolved = NameParser.parseRepositoryTag("localhost:5000/namespace/repository"); |
| 112 | + assertEquals(resolved, new ReposTag("localhost:5000/namespace/repository", "")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testResolveReposTagWithTag() throws Exception { |
| 117 | + ReposTag resolved = NameParser.parseRepositoryTag("repository:tag"); |
| 118 | + assertEquals(resolved, new ReposTag("repository", "tag")); |
| 119 | + |
| 120 | + resolved = NameParser.parseRepositoryTag("namespace/repository:tag"); |
| 121 | + assertEquals(resolved, new ReposTag("namespace/repository", "tag")); |
| 122 | + |
| 123 | + resolved = NameParser.parseRepositoryTag("localhost:5000/namespace/repository:tag"); |
| 124 | + assertEquals(resolved, new ReposTag("localhost:5000/namespace/repository", "tag")); |
| 125 | + } |
| 126 | +} |
0 commit comments