Fix the build for app-props-metadata and basic unit tests

This commit is contained in:
BoykoAlex
2016-10-12 17:17:09 -04:00
parent ff23aca698
commit c444aa0fad
4 changed files with 283 additions and 8 deletions

View File

@@ -11,6 +11,14 @@
<description>Builds metadata for boot application properties based on project's classpath contents</description>
<dependencies>
<!-- Local modified JSON lib packaged to support order in maps -->
<dependency>
<groupId>libs</groupId>
<artifactId>json</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/org.springframework.ide.eclipse.org.json-20140107-repackaged.jar</systemPath>
</dependency>
<!-- Guava collections -->
<dependency>
<groupId>com.google.guava</groupId>
@@ -38,14 +46,34 @@
<artifactId>util-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Eclipse JDT -->
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>core</artifactId>
<version>3.3.0-v_771</version>
</dependency>
</dependencies>
<!--
<build>
<plugins>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<setupIncludes>
<setupInclude>src/test/resources/demo-1/pom.xml</setupInclude>
</setupIncludes>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
-->
</project>

View File

@@ -0,0 +1,177 @@
/*******************************************************************************
* Copyright (c) 2015 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.properties.metadata;
import static org.junit.Assert.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.Test;
import org.springframework.ide.vscode.boot.properties.util.FuzzyMap;
import org.springframework.ide.vscode.boot.properties.util.FuzzyMap.Match;
import org.springframework.ide.vscode.util.FuzzyMatcher;
public class FuzzyMapTests {
@Test
public void testMatches() {
assertMatch(true, "", "");
assertMatch(true, "", "abc");
assertMatch(true, "server.port", "server.port");
assertMatch(true, "port", "server.port");
assertMatch(true, "sport", "server.port");
assertMatch(false, "spox", "server.port");
}
@Test
public void testOrder() {
assertMatchOrder("port",
"port",
"server.port",
"server.port-mapping",
"piano.sorting"
);
}
@Test
public void testPrefixAlfaOrder() {
//all matches are prefix matches so should come in alpha order
assertMatchOrder("spring",
"spring.abracdabra",
"spring.boot",
"spring.candel",
"spring.shoe",
"spring.springer"
);
}
@Test
public void testPrefixMixedOrder() {
assertMatchOrder("spring",
//prefix matches first
"spring.abracdabra",
"spring.boot",
"spring.candel",
"spring.shoe",
"spring.springer",
//non prefix matches after prefix matches in 'similarity order'
"zspring",
"asprouting"
);
}
public class TestMap extends FuzzyMap<String> {
public TestMap(String... entries) {
for (String e : entries) {
add(e);
}
}
protected String getKey(String entry) {
return entry;
}
}
@Test
public void testCommonPrefix() {
String[] entries = {
"a",
"archipel",
"aardappel",
"aardbei",
"aardvark",
"zoroaster"
};
String[] expectPrefix = {
"a",
"a",
"aard",
"aard",
"aard",
""
};
for (int focusOn = 0; focusOn < entries.length; focusOn++) {
TestMap map = new TestMap();
for (int other = 0; other < entries.length; other++) {
if (focusOn!=other) {
map.add(entries[other]);
}
}
String prefix = map.findValidPrefix(entries[focusOn]);
String prefixEntry = map.findLongestCommonPrefixEntry(entries[focusOn]);
assertEquals(expectPrefix[focusOn], prefix);
assertTrue(prefixEntry.startsWith(prefixEntry));
assertTrue(prefixEntry.length()>prefix.length());
}
}
@Test
public void testCommonPrefixWithExactMatch() {
String[] entries = {
"a",
"archipel",
"aardappel",
"aardbei",
"aardvark",
"zoroaster"
};
TestMap map = new TestMap(entries);
for (String find : entries) {
String found = map.findLongestCommonPrefixEntry(find);
assertEquals(find, found);
}
}
@Test
public void testCommonPrefixEmptyMap() {
TestMap empty = new TestMap();
assertEquals(null, empty.findValidPrefix("foo"));
assertEquals(null, empty.findValidPrefix(""));
assertEquals(null, empty.findLongestCommonPrefixEntry("aaa"));
assertEquals(null, empty.findLongestCommonPrefixEntry(""));
}
private void assertMatchOrder(String pattern, String... datas) {
TestMap map = new TestMap(datas);
List<Match<String>> found = map.find(pattern);
//Note that found elements are scored but not sorted.
Collections.sort(found, new Comparator<Match<String>>() {
public int compare(Match<String> o1, Match<String> o2) {
return Double.valueOf(o2.score).compareTo(o1.score);
}
});
//all the datas should be found and be in the order given.
assertEquals(found.size(), datas.length);
for (int i = 0; i < datas.length; i++) {
assertEquals(datas[i], found.get(i).data);
}
// also check that scores are decreasing.
double previousScore = found.get(0).score;
assertTrue(previousScore!=0.0);
for (int i = 1; i < datas.length; i++) {
String data = datas[i];
double score = found.get(i).score;
assertTrue("Wrong score order: '"+datas[i-1]+"'["+previousScore+"] '"+data+"' ["+score+"]", previousScore>=score);
previousScore = score;
}
}
private void assertMatch(boolean expect, String pattern, String data) {
boolean actual = FuzzyMatcher.matchScore(pattern, data)!=0.0;
assertEquals(expect, actual);
}
}

View File

@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright (c) 2014-2015 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.properties.metadata;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.ide.vscode.boot.properties.util.StringUtil;
/**
* @author Kris De Volder
*/
public class StringUtilTests {
@Test
public void testUpperCaseToHyphens() throws Exception {
assertEquals("extra-small", StringUtil.upperCaseToHyphens("EXTRA_SMALL"));
assertEquals("extra-small", StringUtil.upperCaseToHyphens("extra-small")); //can be applied to already converted string without any harm
assertEquals("", StringUtil.upperCaseToHyphens(""));
assertNull(StringUtil.upperCaseToHyphens(null));
}
@Test
public void testHasText() throws Exception {
assertFalse(StringUtil.hasText(null));
assertFalse(StringUtil.hasText(""));
assertFalse(StringUtil.hasText(" \t\n\r"));
assertTrue(StringUtil.hasText("something"));
}
@Test
public void testTrim() throws Exception {
assertNull(StringUtil.trim(null));
assertEquals("foo", StringUtil.trim(" foo \n\r\t"));
}
@Test
public void testCommonPrefixLen() throws Exception {
assertEquals("foo".length(), StringUtil.commonPrefixLength("foobarlonger", "fooshort"));
assertEquals("foo".length(), StringUtil.commonPrefixLength("fooshort", "foobarlonger"));
assertEquals("foo".length(), StringUtil.commonPrefixLength("foo", "foobarlonger"));
assertEquals("foo".length(), StringUtil.commonPrefixLength("foobarlonger", "foo"));
assertEquals(0, StringUtil.commonPrefixLength("", ""));
assertEquals(0, StringUtil.commonPrefixLength("", "something"));
assertEquals(0, StringUtil.commonPrefixLength("something", ""));
assertEquals(0, StringUtil.commonPrefixLength("nothing", "in common"));
}
@Test
public void testCommonPrefix() throws Exception {
assertEquals("foo", StringUtil.commonPrefix("foobarlonger", "fooshort"));
assertEquals("foo", StringUtil.commonPrefix("fooshort", "foobarlonger"));
assertEquals("foo", StringUtil.commonPrefix("foo", "foobarlonger"));
assertEquals("foo", StringUtil.commonPrefix("foobarlonger", "foo"));
assertEquals("", StringUtil.commonPrefix("", ""));
assertEquals("", StringUtil.commonPrefix("", "something"));
assertEquals("", StringUtil.commonPrefix("something", ""));
assertEquals("", StringUtil.commonPrefix("nothing", "in common"));
}
}

View File

@@ -15,6 +15,7 @@
<module>yaml-commons</module>
<module>util-commons</module>
<module>java-properties</module>
<module>application-properties-metadata</module>
</modules>
<properties>