Skip to content

Commit

Permalink
Basic GradleModelLoader tests done.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Jul 4, 2013
1 parent 082b23c commit d62802b
Show file tree
Hide file tree
Showing 14 changed files with 1,460 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.jboss.forge.addon.gradle.projects.model;

import org.jboss.forge.addon.gradle.projects.util.Preconditions;

/**
* @author Adam Wyłuda
*/
Expand All @@ -15,16 +17,34 @@ public class GradleDependencyBuilder
private String group;
private String version;
private String configuration;

private GradleDependencyBuilder()
{
}

public static GradleDependencyBuilder create()
{
return new GradleDependencyBuilder();
}

/**
* Creates gradle dependency using given configuration and parsing gradleString in format:
* {@code group:name:version}
*/
public static GradleDependencyBuilder fromGradleString(String configuration, String gradleString)
{
String[] split = gradleString.split(":");
Preconditions.checkArgument(split.length == 3, "Invalid gradle string format");
String group = split[0];
String name = split[1];
String version = split[2];
return create()
.setName(name)
.setGroup(group)
.setVersion(version)
.setConfiguration(configuration);
}

public String getName()
{
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public interface GradleModel

List<GradleRepository> getRepositories();

boolean hasTask(GradleTaskBuilder builder);

boolean hasDependency(GradleDependencyBuilder builder);

boolean hasManagedDependency(GradleDependencyBuilder builder);

boolean hasProfile(String name);

boolean hasPlugin(String name);

boolean hasRepository(String url);

void setProjectName(String name) throws UnremovableElementException;

void setVersion(String version) throws UnremovableElementException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.addon.gradle.projects.util;

/**
* @author Adam Wyłuda
*/
public class Preconditions
{

public static void checkArgument(boolean condition, String message)
{
if (!condition)
{
throw new IllegalArgumentException(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.jboss.forge.addon.gradle.parser;

import org.jboss.forge.addon.gradle.projects.util.Preconditions;

/**
* @author Adam Wyłuda
*/
Expand All @@ -31,17 +33,17 @@ public static String insertString(String source, String string, int lineNumber,
*/
public static int positionInSource(String source, int lineNumber, int columnNumber)
{
checkArgument(lineNumber >= 1, "Line number must be greater than 0");
checkArgument(columnNumber >= 1, "Column number must be greater than 0");
Preconditions.checkArgument(lineNumber >= 1, "Line number must be greater than 0");
Preconditions.checkArgument(columnNumber >= 1, "Column number must be greater than 0");

// Position is indexed from 1, arrays are indexed from 0, so we fix it
lineNumber--;
columnNumber--;

String[] sourceLines = source.split("\n");

checkArgument(lineNumber < sourceLines.length, "Given line number exceeds line count");
checkArgument(columnNumber <= sourceLines[lineNumber].length(),
Preconditions.checkArgument(lineNumber < sourceLines.length, "Given line number exceeds line count");
Preconditions.checkArgument(columnNumber <= sourceLines[lineNumber].length(),
"Given column number exceeds column count in line");

int precedingCharactersCount = 0;
Expand All @@ -63,12 +65,4 @@ public static String removeSourceFragment(String source, int lineNumber, int col
int endingPosition = positionInSource(source, lastLineNumber, lastColumnNumber);
return source.substring(0, begginingPosition) + source.substring(endingPosition);
}

static void checkArgument(boolean condition, String message)
{
if (!condition)
{
throw new IllegalArgumentException(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,48 @@ public List<GradleRepository> getRepositories()
return repositories;
}

@Override
public boolean hasTask(GradleTaskBuilder builder)
{
// TODO Auto-generated method stub
return false;
}

@Override
public boolean hasDependency(GradleDependencyBuilder builder)
{
// TODO Auto-generated method stub
return false;
}

@Override
public boolean hasManagedDependency(GradleDependencyBuilder builder)
{
// TODO Auto-generated method stub
return false;
}

@Override
public boolean hasProfile(String name)
{
// TODO Auto-generated method stub
return false;
}

@Override
public boolean hasPlugin(String name)
{
// TODO Auto-generated method stub
return false;
}

@Override
public boolean hasRepository(String url)
{
// TODO Auto-generated method stub
return false;
}

@Override
public void setProjectName(String name) throws UnremovableElementException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
@Exported
public class GradleModelLoader
{
/**
* Parses XML source into Gradle model, setting given file resource as Gradle resource.
*/
public GradleModel loadFromXML(FileResource<?> fileResource, String source)
{
// TODO load gradle model from xml
Expand Down
11 changes: 11 additions & 0 deletions impl/src/main/resources/forgeOutput.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

// @author Adam Wyłuda

task forgeOutput << {
def indentLevel = 0
def outputFile = new File('forge-output.xml').newPrintWriter()
Expand Down Expand Up @@ -114,6 +123,7 @@ task forgeOutput << {
outputInc '<project>'
outputProject project
outputDec '</project>'
outputInc '<profiles>'
projectDir.eachFileMatch(groovy.io.FileType.FILES, {
it.matches('^[a-zA-Z0-9]+-profile\\.gradle$')
}, {
Expand All @@ -134,6 +144,7 @@ task forgeOutput << {
outputProject project
outputDec '</profile>'
})
outputDec '</profiles>'
outputDec '</forgeOutput>'

outputFile.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,146 @@
*/
package org.jboss.forge.addon.gradle.projects.model;

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

import java.io.IOException;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

/**
* @author Adam Wyłuda
*/
public class GradleModelLoaderTest
{
private static GradleModel model;

@BeforeClass
public static void init() throws IOException
{
GradleModelLoader loader = new GradleModelLoader();
String source = IOUtils.toString(GradleModelLoaderTest.class.getResourceAsStream("/loader/forge-output.xml"));
model = loader.loadFromXML(null, source);
}

@Test
public void testProjectName()
{
assertEquals("Gradle Test Project", model.getProjectName());
}

@Test
public void testVersion()
{
assertEquals("0.1-SNAPSHOT", model.getVersion());
}

@Test
public void testTasks()
{
boolean zxyzSet = false;
for (GradleTask task : model.getTasks())
{
if (task.getName().equals("zxyz"))
{
zxyzSet = true;
Set<GradleTask> dependsOnSet = task.getDependsOn();

boolean buildSet = false, testSet = false;
for (GradleTask dependsOn : dependsOnSet)
{
if (dependsOn.getName().equals("build"))
{
buildSet = true;
}
else if (dependsOn.getName().equals("test"))
{
testSet = true;
}
}
assertTrue("No dependency on build", buildSet);
assertTrue("No dependency on test", testSet);
}
}
assertTrue("zxyz task not found", zxyzSet);
}

@Test
public void testDependencies()
{
boolean gradleToolingSet = false, junitSet = false;
for (GradleDependency dep : model.getDependencies())
{
if (dep.getName().equals("gradle-tooling-api"))
{
gradleToolingSet = true;
assertEquals("org.gradle", dep.getGroup());
assertEquals("1.6", dep.getVersion());
assertEquals(GradleDependencyConfiguration.COMPILE, dep.getConfiguration());
}
else if (dep.getName().equals("junit"))
{
junitSet = true;
assertEquals("junit", dep.getGroup());
assertEquals("4.11", dep.getVersion());
assertEquals(GradleDependencyConfiguration.TEST_COMPILE, dep.getConfiguration());
}
}
assertTrue("gradle-tooling-api dependency not found", gradleToolingSet);
assertTrue("junit dependency not found", junitSet);
}

@Test
@Ignore
public void testManagedDependencies()
{
// TODO test managed dependencies
}

@Test
public void testProfiles()
{
assertEquals("There are more or less than 2 profiles", 2, model.getProfiles().size());
boolean glassfishSet = false, wildflySet = false;
for (GradleProfile profile : model.getProfiles())
{
if (profile.getName().equals("glassfish"))
{
glassfishSet = true;
assertTrue("Glassfish profile doesn't contain runApplicationServer task",
profile.getModel().hasTask(GradleTaskBuilder.create().setName("runApplicationServer")));
assertTrue("Glassfish profile doesn't contain specified dependency",
profile.getModel().hasDependency(GradleDependencyBuilder.fromGradleString("compile", "javax.annotation:jsr250-api:1.0")));
}
else if (profile.getName().equals("wildfly"))
{
wildflySet = true; glassfishSet = true;
assertTrue("Wildfly profile doesn't contain runApplicationServer task",
profile.getModel().hasTask(GradleTaskBuilder.create().setName("runApplicationServer")));
assertTrue("Glassfish profile doesn't contain specified dependency",
profile.getModel().hasDependency(GradleDependencyBuilder.fromGradleString("compile", "log4j:log4j:1.2.17")));
}
}
assertTrue("glassfish profile not found", glassfishSet);
assertTrue("wildfly profile not found", wildflySet);
}

@Test
public void testPlugins()
{
assertTrue("There is no java plugin", model.hasPlugin(GradlePluginType.JAVA.getClazz()));
assertTrue("There is no groovy plugin", model.hasPlugin(GradlePluginType.GROOVY.getClazz()));
assertTrue("There is no scala plugin", model.hasPlugin(GradlePluginType.SCALA.getClazz()));
assertTrue("There is no eclipse plugin", model.hasPlugin(GradlePluginType.ECLIPSE.getClazz()));
}

@Test
public void testRepositories()
{
assertTrue("There is no Gradle repository", model.hasRepository("http://repo.gradle.org/gradle/libs-releases-local/"));
}
}
Empty file.
Loading

0 comments on commit d62802b

Please sign in to comment.