PageRenderTime 40ms CodeModel.GetById 8ms app.highlight 24ms RepoModel.GetById 5ms app.codeStats 0ms

/maven-amps-plugin/src/test/java/com/atlassian/maven/plugins/amps/osgi/TestPackageImportRangeValidator.java

Relevant Search: With Applications for Solr and Elasticsearch

For more in depth reading about search, ranking and generally everything you could ever want to know about how lucene, elasticsearch or solr work under the hood I highly suggest this book. Easily one of the most interesting technical books I have read in a long time. If you are tasked with solving search relevance problems even if not in Solr or Elasticsearch it should be your first reference. Amazon Affiliate Link
https://bitbucket.org/mmeinhold/amps
Java | 259 lines | 233 code | 22 blank | 4 comment | 7 complexity | e3fe456e1569e4fe73d511c9caaf5986 MD5 | raw file
  1package com.atlassian.maven.plugins.amps.osgi;
  2
  3import junit.framework.TestCase;
  4
  5import org.apache.maven.plugin.MojoFailureException;
  6import org.apache.maven.plugin.logging.Log;
  7import org.apache.maven.project.MavenProject;
  8
  9import static org.mockito.Matchers.anyObject;
 10import static org.mockito.Matchers.anyString;
 11import static org.mockito.Mockito.mock;
 12import static org.mockito.Mockito.never;
 13import static org.mockito.Mockito.verify;
 14
 15import java.util.Iterator;
 16import java.util.Map;
 17import java.util.HashMap;
 18
 19/**
 20 *
 21 */
 22public class TestPackageImportRangeValidator extends TestCase
 23{
 24    private MavenProject project;
 25    private Log log;
 26    private PackageImportVersionValidator validator;
 27
 28    @Override
 29    protected void setUp() throws Exception
 30    {
 31        super.setUp();
 32        project = mock(MavenProject.class);
 33        log = mock(Log.class);
 34        validator = new PackageImportVersionValidator(project, log, "foo");
 35    }
 36
 37    public void testValidateWithProperVersion() throws MojoFailureException
 38    {
 39
 40        validator.validate("foo.bar;version=\"[1.0,2.0)\"");
 41        verify(log, never()).warn(anyString());
 42    }
 43
 44    public void testValidateNull()
 45    {
 46        validator.validate(null);
 47        verify(log, never()).warn(anyString());
 48    }
 49
 50    public void testValidateEmpty()
 51    {
 52        validator.validate("");
 53        verify(log, never()).warn(anyString());
 54    }
 55
 56    public void testValidateJustMinimum()
 57    {
 58        validator.validate("foo.bar;version=\"1.0\"");
 59        verify(log, never()).warn(anyString());
 60    }
 61
 62    public void testValidateNoVersion()
 63    {
 64        validator.validate("foo.bar");
 65        verify(log).warn(anyString());
 66    }
 67
 68    public void testValidateEmptyVersion()
 69    {
 70        validator.validate("foo.bar;version=\"\"");
 71        verify(log).warn(anyString());
 72    }
 73
 74    public void testValidateNoVersionOneAttribute()
 75    {
 76        validator.validate("foo.bar;bar=foo");
 77        verify(log).warn(anyString());
 78    }
 79
 80    public void testCompressImports()
 81    {
 82        Map<String, String> imports = PackageImportVersionValidator.compressPackages(new HashMap<String, String>()
 83        {{
 84                put("foo.bar.baz.jim", "1.0");
 85                put("foo", "1.0");
 86                put("foo.bar.baz.sara", "1.0");
 87                put("jim", "2.0");
 88            }});
 89        assertEquals(new HashMap<String, String>()
 90        {{
 91                put("foo.bar.baz.*", "1.0");
 92                put("foo", "1.0");
 93                put("jim", "2.0");
 94            }}, imports);
 95    }
 96
 97    public void testCompressImportsNotTooShort()
 98    {
 99        Map<String, String> imports = PackageImportVersionValidator.compressPackages(new HashMap<String, String>()
100        {{
101                put("foo.bar", "1.0");
102                put("faz.bar", "1.0");
103                put("jim", "2.0");
104            }});
105        assertEquals(new HashMap<String, String>()
106        {{
107                put("foo.bar", "1.0");
108                put("faz.bar", "1.0");
109                put("jim", "2.0");
110            }}, imports);
111    }
112
113    public void testCompressImportsNotTooShort2()
114    {
115        Map<String, String> imports = PackageImportVersionValidator.compressPackages(new HashMap<String, String>()
116        {{
117                put("foo.bar", "1.0");
118                put("fos.bar", "1.0");
119                put("jim", "2.0");
120            }});
121        assertEquals(new HashMap<String, String>()
122        {{
123                put("foo.bar", "1.0");
124                put("fos.bar", "1.0");
125                put("jim", "2.0");
126            }}, imports);
127    }
128
129    public void testCompressImportsNotIncludeOutcast()
130    {
131        Map<String, String> imports = PackageImportVersionValidator.compressPackages(new HashMap<String, String>()
132        {{
133                put("foo.bar.sara.jim", "1.0");
134                put("foo", "1.0");
135                put("foo.bar.sara.jim.baz", "1.0");
136                put("foo.jim", "2.0");
137            }});
138        assertEquals(new HashMap<String, String>()
139        {{
140                put("foo", "1.0");
141                put("foo.bar.sara.jim*", "1.0");
142                put("foo.jim", "2.0");
143            }}, imports);
144    }
145
146    public void testCompressImportsNotAlwaysFullPackage()
147    {
148        Map<String, String> imports = PackageImportVersionValidator.compressPackages(new HashMap<String, String>()
149        {{
150                put("foo.bar", "1.0");
151                put("foo.bar.baz", "1.0");
152                put("foo.bara", "2.0");
153            }});
154        assertEquals(new HashMap<String, String>()
155        {{
156                put("foo.bar", "1.0");
157                put("foo.bar.baz", "1.0");
158                put("foo.bara", "2.0");
159            }}, imports);
160    }
161
162    public void testCompressImportsRealWorld()
163    {
164        // note in this example, most of the packages were not on the classpath, and therefore, 
165        Map<String, String> imports = PackageImportVersionValidator.compressPackages(new HashMap<String, String>()
166        {{
167                put("com.atlassian.sal.api", "2.0");
168                put("com.mchange.v2.c3p0", "0.0.0");
169                put("javax.naming", "0.0.0");
170                put("javax.naming.spi", "0.0.0");
171                put("javax.net", "0.0.0");
172                put("javax.net.ssl", "0.0.0");
173                put("javax.security.cert", "0.0.0");
174                put("javax.servlet", "0.0.0");
175                put("javax.servlet.http", "0.0.0");
176                put("javax.sql", "0.0.0");
177                put("javax.swing", "0.0.0");
178                put("javax.swing.border", "0.0.0");
179                put("javax.swing.event", "0.0.0");
180                put("javax.swing.table", "0.0.0");
181                put("javax.swing.tree", "0.0.0");
182                put("org.apache.commons.dbcp", "0.0.0");
183                put("org.apache.lucene.analysis", "0.0.0");
184                put("org.apache.lucene.document", "0.0.0");
185                put("org.apache.lucene.index", "0.0.0");
186                put("org.apache.lucene.queryParser", "0.0.0");
187                put("org.apache.lucene.search", "0.0.0");
188                put("org.apache.lucene.search.similar", "0.0.0");
189                put("org.apache.lucene.store", "0.0.0");
190                put("org.apache.tools.ant", "0.0.0");
191                put("org.apache.tools.ant.types", "0.0.0");
192                put("org.logicalcobwebs.proxool", "0.0.0");
193                put("org.osgi.framework", "1.5");
194                put("org.osgi.util.tracker", "1.4");
195                put("org.slf4j", "1.5");
196                put("snaq.db", "0.0.0");
197                put("sun.security.action", "0.0.0");
198                put("org.apache.lucene.queryParser", "0.0.0");
199                put("org.apache.tools.ant.types", "0.0.0");
200                put("org.apache.lucene.document", "0.0.0");
201            }});
202        assertMapEquals(new HashMap<String, String>()
203        {{
204                put("com.atlassian.sal.api", "2.0");
205                put("com.mchange.v2.c3p0", "0.0.0");
206                put("javax.naming", "0.0.0");
207                put("javax.naming.spi", "0.0.0");
208                put("javax.net", "0.0.0");
209                put("javax.net.ssl", "0.0.0");
210                put("javax.security.cert", "0.0.0");
211                put("javax.servlet", "0.0.0");
212                put("javax.servlet.http", "0.0.0");
213                put("javax.sql", "0.0.0");
214                put("javax.swing", "0.0.0");
215                put("javax.swing.border", "0.0.0");
216                put("javax.swing.event", "0.0.0");
217                put("javax.swing.table", "0.0.0");
218                put("javax.swing.tree", "0.0.0");
219                put("org.apache.commons.dbcp", "0.0.0");
220                put("org.apache.lucene.*", "0.0.0");
221                put("org.apache.tools.ant*", "0.0.0");
222                put("org.logicalcobwebs.proxool", "0.0.0");
223                put("org.osgi.framework", "1.5");
224                put("org.osgi.util.tracker", "1.4");
225                put("org.slf4j", "1.5");
226                put("snaq.db", "0.0.0");
227                put("sun.security.action", "0.0.0");
228            }}, imports);
229    }
230
231    private void assertMapEquals(HashMap<String, String> expected, Map<String, String> actual)
232    {
233        for (Iterator<Map.Entry<String,String>> i = expected.entrySet().iterator(); i.hasNext(); )
234        {
235            Map.Entry<String,String> entry = i.next();
236            if (actual.containsKey(entry.getKey()) && actual.get(entry.getKey()).equals(entry.getValue()))
237            {
238                i.remove();
239                actual.remove(entry.getKey());
240            }
241        }
242
243        if (!expected.isEmpty() || !actual.isEmpty())
244        {
245            StringBuilder sb = new StringBuilder();
246            sb.append("Missing expected entries:\n");
247            for (Map.Entry<String,String> entry : expected.entrySet())
248            {
249                sb.append(entry.getKey()).append("=").append(entry.getValue()).append(",");
250            }
251            sb.append("\nUnique actual entries:\n");
252            for (Map.Entry<String,String> entry : actual.entrySet())
253            {
254                sb.append(entry.getKey()).append("=").append(entry.getValue()).append(",");
255            }
256            fail(sb.toString());
257        }
258    }
259}