ItEr24S08CUAsignacionGrupoRecursosAPlanificacionItEr23S10: Adding support for declaring via class the scripts required.
This commit is contained in:
parent
49d7989fdb
commit
59dd6d0d2f
5 changed files with 192 additions and 0 deletions
|
|
@ -0,0 +1,67 @@
|
|||
package org.zkoss.ganttz.util.script;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class ScriptExtractor {
|
||||
|
||||
public static List<ScriptDependency> extractFrom(Class<?> classWithScripts) {
|
||||
ScriptsRequiredDeclaration annotation = classWithScripts
|
||||
.getAnnotation(ScriptsRequiredDeclaration.class);
|
||||
if (annotation == null)
|
||||
throw new IllegalArgumentException(classWithScripts
|
||||
+ " must be annotated with "
|
||||
+ ScriptsRequiredDeclaration.class.getName());
|
||||
List<ScriptDependency> dependsOn = getDependencies(annotation);
|
||||
List<ScriptDependency> result = new ArrayList<ScriptDependency>();
|
||||
for (Field field : getStringFields(getStaticFields(classWithScripts
|
||||
.getFields()))) {
|
||||
result.add(new ScriptDependency(getValueFromStringField(field),
|
||||
dependsOn));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ArrayList<ScriptDependency> getDependencies(
|
||||
ScriptsRequiredDeclaration declaration) {
|
||||
Class<?>[] dependsOn = declaration.dependsOn();
|
||||
ArrayList<ScriptDependency> result = new ArrayList<ScriptDependency>();
|
||||
for (Class<?> klass : dependsOn) {
|
||||
result.addAll(extractFrom(klass));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private static String getValueFromStringField(Field stringField) {
|
||||
try {
|
||||
return (String) stringField.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Field> getStaticFields(Field[] fields) {
|
||||
List<Field> result = new ArrayList<Field>();
|
||||
for (Field field : fields) {
|
||||
if (Modifier.isStatic(field.getModifiers())) {
|
||||
result.add(field);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<Field> getStringFields(Collection<Field> fields) {
|
||||
List<Field> stringFields = new ArrayList<Field>();
|
||||
for (Field field : fields) {
|
||||
if (field.getType().equals(String.class)) {
|
||||
stringFields.add(field);
|
||||
}
|
||||
}
|
||||
return stringFields;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package org.zkoss.ganttz.util.script;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface ScriptsRequiredDeclaration {
|
||||
|
||||
public Class<?>[] dependsOn() default {};
|
||||
}
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
package org.zkoss.ganttz.util.script;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.matchers.JUnitMatchers.each;
|
||||
import static org.junit.matchers.JUnitMatchers.hasItem;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ScriptExtractorTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void aClassWithoutScriptRequiredAnnotationIsNotIncluded() {
|
||||
ScriptExtractor.extractFrom(String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onlyPublicStringFieldsAreIncluded() {
|
||||
List<ScriptDependency> scripts = ScriptExtractor
|
||||
.extractFrom(ScriptsDeclarationsExample.class);
|
||||
assertThat(scripts.size(), equalTo(2));
|
||||
assertThat(scripts,
|
||||
hasItem(withURL(ScriptsDeclarationsExample.EXAMPLE_A)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncludesDependencies() {
|
||||
List<ScriptDependency> scripts = ScriptExtractor
|
||||
.extractFrom(ScriptsDeclarationsExample.class);
|
||||
assertThat(scripts, each(withDependencies(ScriptIncludedExample.base,
|
||||
ScriptIncludedExample.other)));
|
||||
|
||||
}
|
||||
|
||||
private Matcher<ScriptDependency> withDependencies(final String... urls) {
|
||||
final Set<String> urlsSet = new HashSet<String>(Arrays.asList(urls));
|
||||
return new BaseMatcher<ScriptDependency>() {
|
||||
|
||||
@Override
|
||||
public boolean matches(Object object) {
|
||||
if (object instanceof ScriptDependency) {
|
||||
ScriptDependency dependency = (ScriptDependency) object;
|
||||
Set<String> urls = new HashSet<String>();
|
||||
for (ScriptDependency s : dependency.getDependsOn()) {
|
||||
urls.add(s.getURL());
|
||||
}
|
||||
return urlsSet.equals(urls);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("depends on "
|
||||
+ Arrays.toString(urls));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private Matcher<ScriptDependency> withURL(final String url) {
|
||||
return new BaseMatcher<ScriptDependency>() {
|
||||
|
||||
@Override
|
||||
public boolean matches(Object dependency) {
|
||||
if (dependency instanceof ScriptDependency) {
|
||||
ScriptDependency d = (ScriptDependency) dependency;
|
||||
return d.getURL().equals(url);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("it has url:" + url);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package org.zkoss.ganttz.util.script;
|
||||
|
||||
@ScriptsRequiredDeclaration
|
||||
public class ScriptIncludedExample {
|
||||
|
||||
public static final String base = "blabla/bla/base.js";
|
||||
public static final String other = "othare/other.js";
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package org.zkoss.ganttz.util.script;
|
||||
|
||||
@ScriptsRequiredDeclaration(dependsOn = ScriptIncludedExample.class)
|
||||
public class ScriptsDeclarationsExample {
|
||||
|
||||
public static final String EXAMPLE_A = "/project-a/blabla/a.js";
|
||||
|
||||
public static final String EXAMPLE_B = "/project-a/blabla/b.js";
|
||||
|
||||
private static String EXAMPLE_NOT_INCLUDED = "/project-a/blablaadsf/a.js";
|
||||
|
||||
public static int NOT_INCLUDED_BECAUSE_IS_NOT_STRING = 4;
|
||||
|
||||
private String NOT_INCLUDED_BECAUSE_IS_NOT_STATIC = "balbla/bladsfafa/ba.js";
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue