Skip to content

Commit

Permalink
[#1719] fix moduleOrder to handle Dynamic revisions and "id" options …
Browse files Browse the repository at this point in the history
…or with/without version number

[#1719] Fix Configuration by adding jpa. and hibernate configuration
* update ivy to version 2.3.0
[#1719] fix retrieving list of modules (filter lib, modules)
[#1719] Add tests for retrieving modules
  • Loading branch information
xael-fry committed Mar 12, 2014
1 parent c7fc5e9 commit 2cef465
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 33 deletions.
1 change: 1 addition & 0 deletions framework/build.xml
Expand Up @@ -361,6 +361,7 @@
<fileset dir="test-src">
<include name="**/*.properties"/>
<include name="**/*.sql"/>
<include name="**/*.yml"/>
<include name="**/*.xml"/>
<include name="**/*.plugins"/>
</fileset>
Expand Down
2 changes: 1 addition & 1 deletion framework/dependencies.yml
Expand Up @@ -41,7 +41,7 @@ require: &allDependencies
- mysql -> mysql-connector-java 5.1.20
- oauth.signpost -> signpost-core 1.2
- org.apache.geronimo.specs -> geronimo-servlet_2.5_spec 1.2
- org.apache.ivy -> ivy 2.2.0
- org.apache.ivy -> ivy 2.3.0
- org.bouncycastle -> bcprov-jdk15 1.45
- org.codehaus.groovy -> groovy-all 1.8.6
- org.eclipse.jdt.core 3.7.1.v_B76_R37x
Expand Down
Binary file removed framework/lib/ivy-2.2.0.jar
Binary file not shown.
Binary file added framework/lib/ivy-2.3.0.jar
Binary file not shown.
7 changes: 4 additions & 3 deletions framework/src/play/Play.java
Expand Up @@ -736,11 +736,12 @@ public static void loadModules() {
if (moduleName.contains("-")) {
moduleName = moduleName.substring(0, moduleName.indexOf("-"));
}

if (module.isDirectory()) {

if(module == null || !module.exists()){
Logger.error("Module %s will not be loaded because %s does not exist", moduleName, module.getAbsolutePath());
} else if (module.isDirectory()) {
addModule(moduleName, module);
} else {

File modulePath = new File(IO.readContentAsString(module).trim());
if (!modulePath.exists() || !modulePath.isDirectory()) {
Logger.error("Module %s will not be loaded because %s does not exist", moduleName, modulePath.getAbsolutePath());
Expand Down
88 changes: 59 additions & 29 deletions framework/src/play/deps/YamlParser.java
@@ -1,6 +1,8 @@
package play.deps;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
Expand All @@ -12,10 +14,12 @@
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.ivy.core.module.descriptor.Configuration;
import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
import org.apache.ivy.core.module.descriptor.DefaultExcludeRule;
import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
import org.apache.ivy.core.module.descriptor.ExcludeRule;
import org.apache.ivy.core.module.descriptor.MDArtifact;
import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
Expand All @@ -30,6 +34,7 @@
import org.apache.ivy.plugins.repository.Resource;
import org.yaml.snakeyaml.Yaml;


public class YamlParser extends AbstractModuleDescriptorParser {

static class Oops extends Exception {
Expand All @@ -42,15 +47,18 @@ public Oops(String message) {
public boolean accept(Resource rsrc) {
return rsrc.exists() && rsrc.getName().endsWith(".yml");
}

public ModuleDescriptor parseDescriptor(ParserSettings ps, URL url, Resource rsrc, boolean bln) throws ParseException, IOException {
return parseDescriptor( ps, url, rsrc.openStream(), rsrc.getLastModified(), bln);
}

public ModuleDescriptor parseDescriptor(ParserSettings ps, URL url, InputStream srcStream, long lastModified, boolean bln) throws ParseException, IOException {
try {
Yaml yaml = new Yaml();
Object o = null;

// Try to parse the yaml
try {
o = yaml.load(rsrc.openStream());
o = yaml.load(srcStream);
} catch (Exception e) {
throw new Oops(e.toString().replace("\n", "\n~ \t"));
}
Expand Down Expand Up @@ -93,7 +101,7 @@ public ModuleDescriptorParser getParser() {
};
descriptor.addConfiguration(new Configuration("default"));
descriptor.addArtifact("default", new MDArtifact(descriptor, id.getName(), "jar", "zip"));
descriptor.setLastModified(rsrc.getLastModified());
descriptor.setLastModified(lastModified);

boolean transitiveDependencies = get(data, "transitiveDependencies", boolean.class, true);

Expand Down Expand Up @@ -240,37 +248,59 @@ <T> T get(Map data, String key, Class<T> type, T defaultValue) {
return o;
}

public static List<String> getOrderedModuleList(File file) throws Oops {
Yaml yaml = new Yaml();
Object o = null;

// Try to parse the yaml
try {
o = yaml.load(new FileInputStream(file));
} catch (Exception e) {
throw new Oops(e.toString().replace("\n", "\n~ \t"));
public static List<String> getOrderedModuleList(File file) throws FileNotFoundException, ParseException, IOException {
List<String> modules = new ArrayList<String>();
if (file == null || !file.exists()) {
throw new FileNotFoundException("There was a problem to find the file");
}
System.setProperty("application.path", Play.applicationPath.getAbsolutePath());

// We expect a Map here
if (!(o instanceof Map)) {
throw new Oops("Unexpected format -> " + o);
}
YamlParser parser = new YamlParser();

Map data = (Map) o;
ModuleRevisionId id = null;
ModuleDescriptor md = parser.parseDescriptor(null, null, new FileInputStream(file), 0, true);



if (data.containsKey("require")) {
if (data.get("require") instanceof List) {

List dependencies = (List) data.get("require");
// filter out play
dependencies.remove("play");
System.out.println("loading modules " + dependencies);
return dependencies;
DependencyDescriptor[] rules = md.getDependencies();
for (DependencyDescriptor dep : rules) {
ModuleRevisionId rev = dep.getDependencyRevisionId();
String moduleName = filterModuleName(rev);
if (moduleName != null) {
modules.add(moduleName);
}
}
return new ArrayList<String>();
return modules;
}


private static String filterModuleName(ModuleRevisionId rev) {
if (!"play".equals(rev.getName())) {
File moduleDir = new File(Play.applicationPath, "modules");
// create new filename filter to check if it is a module (lib will
// be skipped)
File[] filterFiles = moduleDir.listFiles(new ModuleFilter(rev));
if (filterFiles != null && filterFiles.length > 0) {
return filterFiles[0].getName();
}
}

return null;

}

private static class ModuleFilter implements FilenameFilter {

private ModuleRevisionId moduleRevision;

public ModuleFilter(ModuleRevisionId moduleRevision) {
this.moduleRevision = moduleRevision;
}
@Override
public boolean accept(File dir, String name) {
// Accept module with the same name or with a version number
if (name.equals(moduleRevision.getName())
|| name.equals(moduleRevision.getName() + "-" + moduleRevision.getRevision())
|| name.startsWith(moduleRevision.getName() + "-")) {
return true;
}
return false;

}
1 change: 1 addition & 0 deletions framework/test-src/play/PlayBuilder.java
Expand Up @@ -35,6 +35,7 @@ public PlayBuilder withConfiguration(Properties config){
@SuppressWarnings({"deprecation"})
public void build(){

Play.version = "localbuild";
Play.configuration = configuration;
Play.classes = new ApplicationClasses();
Play.javaPath = new ArrayList<VirtualFile>();
Expand Down
100 changes: 100 additions & 0 deletions framework/test-src/play/deps/YamlParserTest.java
@@ -0,0 +1,100 @@
package play.deps;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import play.Play;
import play.PlayBuilder;

public class YamlParserTest {

@BeforeClass
public static void setUp(){
// Play
new PlayBuilder().build();
System.setProperty("play.version", Play.version);

// We will create a "tmp/modules" directory to simulate the play dependencies
File moduleDir = new File(Play.applicationPath, "modules");
moduleDir.mkdirs();

String[] moduleNames = {"crud", "deadbolt-1.5.4", "pdf-1.5"};
try {
for (String module : moduleNames) {
File moduleFile = new File(moduleDir, module);
moduleFile.createNewFile();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@AfterClass
public static void cleanUp(){
File moduleDir = new File(Play.applicationPath, "modules");
try {
FileUtils.deleteDirectory(moduleDir);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


@Test(expected = FileNotFoundException.class)
public void fileNotFoundTest() throws Exception {
List<String> modules = null;
try {
modules = YamlParser.getOrderedModuleList(new File(Play.applicationPath, "fakeFile.yml"));
} catch (Exception e) {
assertTrue(e.getMessage().startsWith("There was a problem to find the file"));
throw e;
}
}


@Test
public void retrieveModulesTest() {
List<String> modules = null;
try {
modules = YamlParser.getOrderedModuleList(new File(getClass().getResource("/play/deps/dependencies_test1.yml").toURI()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertNotNull(modules);
assertEquals("crud", modules.get(0));
assertEquals("deadbolt-1.5.4", modules.get(1));
assertEquals("pdf-1.5", modules.get(2));
}

@Test
public void retrieveModulesTest2() {
List<String> modules = null;
try {
modules = YamlParser.getOrderedModuleList(new File(getClass().getResource("/play/deps/dependencies_test2.yml").toURI()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertNotNull(modules);
assertEquals("pdf-1.5", modules.get(0));
assertEquals("deadbolt-1.5.4", modules.get(1));
assertEquals("crud", modules.get(2));
}


}
12 changes: 12 additions & 0 deletions framework/test-src/play/deps/dependencies_test1.yml
@@ -0,0 +1,12 @@
# Application dependencies

require:
- play
- crud
- play -> deadbolt 1.5.4
- play -> cobertura 2.4:
id: test
- play -> pdf [0.9,)
- commons-lang -> commons-lang 2.5
- commons-codec 1.7
- net.sf.json-lib -> json-lib 2.4 jdk15
12 changes: 12 additions & 0 deletions framework/test-src/play/deps/dependencies_test2.yml
@@ -0,0 +1,12 @@
# Application dependencies

require:
- play
- play -> pdf [0.9,)
- play -> deadbolt 1.5.4
- play -> cobertura 2.4:
id: test
- commons-lang -> commons-lang 2.5
- commons-codec 1.7
- net.sf.json-lib -> json-lib 2.4 jdk15
- crud

0 comments on commit 2cef465

Please sign in to comment.