Skip to content

Commit

Permalink
basic unit tests application changes
Browse files Browse the repository at this point in the history
  • Loading branch information
petrkunc committed May 25, 2013
1 parent 5b32ac3 commit ee59bc8
Show file tree
Hide file tree
Showing 189 changed files with 924 additions and 21,154 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -4,4 +4,5 @@
/images
/main.lesscache
/urltester
/helper.lesscache
/helper.lesscache
/target/surefire-reports
1 change: 1 addition & 0 deletions src/main/java/cz/muni/fi/lessappcache/Main.java
Expand Up @@ -55,6 +55,7 @@ public static void main(String[] args) {
System.out.println(s);
}
} catch (IOException ex) {
System.err.println("File not found "+ ex.getMessage());
}
}

Expand Down
17 changes: 7 additions & 10 deletions src/main/java/cz/muni/fi/lessappcache/filesystem/PathUtils.java
Expand Up @@ -35,19 +35,16 @@ public class PathUtils {
* in the same folder it relativizes as "../"
* In future this method could be replaced by absolutization and the relativizing.
*
* @param basePath the path to be relativized against
* @param otherPath the path to be relativized
* @param relativized the path to be relativized
* @param base the path to be relativized against
* @return Path describing the relative path from one folder to other one.
*/
public static Path relativizeFolders(Path basePath, Path otherPath) {
Path difference = basePath.relativize(otherPath).normalize();
difference = difference.getParent();
if (difference == null || difference.getNameCount() <= 1) {
difference = Paths.get("");
} else {
difference = difference.subpath(1, difference.getNameCount());
public static Path getParent(Path path) {
Path parent = path.getParent();
if (parent == null || parent.getNameCount() < 1) {
parent = Paths.get("");
}
return difference;
return parent;
}

/**
Expand Down
Expand Up @@ -50,6 +50,7 @@ public List<String> execute(final String[] args, Path context) throws FilterExec
List<String> result = new ArrayList<>();
String pathName = args[1];
pathName = PathUtils.processResource(pathName, context);
logger.fatal(pathName);

final Path path = Paths.get(pathName);
Path pathRelative = Paths.get(args[1]);
Expand Down
Expand Up @@ -50,7 +50,9 @@ public abstract class AbstractWalkTree implements Filter {
@Override
public List<String> execute(String[] args, Path context) throws FilterExecutionException {
List<String> result = new ArrayList<>();
// pattern in args[2]
Finder f = new Finder(args[2]);
// directory path in args[1]
String pathName = args[1];
pathName = PathUtils.processResource(pathName, context);

Expand Down Expand Up @@ -83,7 +85,7 @@ public List<Path> result() {
return results;
}

// Compares the glob pattern against
// Compares the pattern against
// the file name.
void find(Path file) {
Path name = file.getFileName();
Expand All @@ -92,16 +94,14 @@ void find(Path file) {
}
}

// Invoke the pattern matching
// method on each file.
// Invoke the pattern matching method on each file.
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
find(file);
return CONTINUE;
}

// Invoke the pattern matching
// method on each directory.
// Ignores directory names
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
return CONTINUE;
Expand Down
Expand Up @@ -41,12 +41,8 @@ public static Filter getFilterInstance(String filterName) throws FilterException
try {
Class filter = loader.loadClass(filterName);
filters.put(filterName, (Filter) filter.newInstance());
} catch (ClassNotFoundException ex) {
throw new FilterException("Filter " + filterName + " not found!", ex);
} catch (InstantiationException ex) {
throw new FilterException("Could not instantiate " + filterName, ex);
} catch (IllegalAccessException ex) {
throw new FilterException("Illegal access to filter " + filterName, ex);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
throw new FilterException("There was an error when calling filter " + filterName, ex);
}
}
return filters.get(filterName);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/cz/muni/fi/lessappcache/importer/ImportedFile.java
Expand Up @@ -24,10 +24,19 @@
*
* @author Petr Kunc
*/
public class ImportedFile {
public final class ImportedFile {
private List<String> lines;
private Path filePath;

/**
* Contstructor of ImportedFile definig resource to be imported.
*
* @param filePath of file to be imported
*/
public ImportedFile(Path filePath) {
setFilePath(filePath);
}

/**
* Getter for lines
*
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/cz/muni/fi/lessappcache/importer/Importer.java
Expand Up @@ -32,7 +32,7 @@
public class Importer {

private final static Logger logger = Logger.getLogger(Importer.class.getName());
private final static Set<Path> importedFiles = new HashSet<>();
private final static Set<ImportedFile> importedFiles = new HashSet<>();

/**
* Method serves to detect whether given file is already imported
Expand All @@ -41,7 +41,7 @@ public class Importer {
* @return true if file is already imported, false otherwise
*/
public static boolean isImported(Path filePath) {
return importedFiles.contains(filePath.normalize());
return importedFiles.contains(new ImportedFile(filePath));
}

/**
Expand Down Expand Up @@ -75,11 +75,10 @@ public static ImportedFile importFile(String fileName) throws IOException {
*/
public static ImportedFile importFile(Path path) throws IOException {
logger.info("Importing file: "+ path);
ImportedFile imported = new ImportedFile();
imported.setFilePath(path);
ImportedFile imported = new ImportedFile(path);
imported.setLines(FileUtils.readFile(path));
logger.info("Importing complete");
importedFiles.add(imported.getFilePath());
importedFiles.add(imported);
return imported;
}
}
29 changes: 9 additions & 20 deletions src/main/java/cz/muni/fi/lessappcache/parser/ManifestParser.java
Expand Up @@ -143,19 +143,18 @@ public List<String> execute() throws IOException {
}

/**
* Processes lesscache file. Given context describes the relative path between this file and file which imported this file
* Processes lesscache file.
*
* @param context
* @return lines of processed manifest file in given context
* @throws IOException when accessing of any file or resource stated in the lesscache file failed and application was not able to continue parsing
*/
public List<String> processFileInContextOf(Path context) throws IOException {
public List<String> processFile() throws IOException {
List<String> processed = new ArrayList<>();
//returned Imported File has loaded lines and normalized path saved
ImportedFile imported = Importer.importFile(filePath);
Path relative = PathUtils.relativizeFolders(context, imported.getFilePath());
Path pathToImport = relative.resolve(imported.getFilePath().getFileName());
processed.add("# Imported file: " + pathToImport);
Path parent = PathUtils.getParent(filePath);
processed.add("# Imported file: " + filePath);
//make sure that imported file starts with mode set to CACHE:
String oldMode = mode;
if (!"CACHE:".equals(mode)) {
Expand All @@ -166,30 +165,20 @@ public List<String> processFileInContextOf(Path context) throws IOException {
for (String line : imported.getLines()) {
lineNumber++;
try {
processed.addAll(processLine(line, relative, lineNumber));
processed.addAll(processLine(line, parent, lineNumber));
} catch (ModuleException ex) {
logger.error("Error while processing " + imported.getFilePath() + " on line " + lineNumber + ": " + ex.getMessage());
}
}
processed.add("# End of imported file: " + pathToImport);
processed.add("# End of imported file: " + filePath);
//make sure to get back to mode we got while importing
if (!mode.equals(oldMode) && !filePath.equals(context)) {
if (!mode.equals(oldMode)) {
mode = oldMode;
processed.add(oldMode);
}
return processed;
}

/**
* Processes the file set in construstor in context of its own directory
*
* @return lines of processed manifest file
* @throws IOException when accessing of any file or resource stated in the lesscache file failed and application was not able to continue parsing
*/
public List<String> processFile() throws IOException {
return processFileInContextOf(filePath);
}

/**
* Processes line of the manifest file by executing module parsers.
*
Expand All @@ -207,7 +196,7 @@ public List<String> processLine(String line, Path context, int lineNumber) throw
ModuleOutput mo = m.parse(line, new ParsingContext(loadedResources, mode, context));

for (Map.Entry<String, String> entry : mo.getLoadedResources().entrySet()) {
getLoadedResources().put(entry.getKey(), filePath.toString() + ", line: " + lineNumber + ", info: " + entry.getValue() + "\n");
getLoadedResources().put(entry.getKey(), filePath.toString() + ", line: " + lineNumber + ", info: " + entry.getValue());
}

if (mo.getMode() != null) {
Expand All @@ -223,7 +212,7 @@ public List<String> processLine(String line, Path context, int lineNumber) throw
}
break;
} else {
//TODO: add?
// DEFINE: to add or not to add?
//output.addAll(mo.getOutput());
}
}
Expand Down
Expand Up @@ -32,7 +32,7 @@ public class ExplicitModule extends AbstractModule implements Module {
* Constructs module and sets priority
*/
public ExplicitModule() {
setPriority(1.3);
setPriority(1.0);
}

@Override
Expand Down
Expand Up @@ -36,7 +36,7 @@ public class FallbackModule extends AbstractModule implements Module {
* Constructs module and sets priority
*/
public FallbackModule() {
setPriority(1.1);
setPriority(1.0);
}

@Override
Expand Down
Expand Up @@ -52,7 +52,7 @@ public ModuleOutput parse(String line, ParsingContext pc) throws ModuleException
output.getOutput().add(s);
}
} catch (FilterException ex) {
throw new ModuleException("Filter could not be loader! "+ line, ex);
throw new ModuleException("Filter could not be loaded! "+ line, ex);
} catch (FilterExecutionException ex) {
throw new ModuleException("Error during filter execution.", ex);
}
Expand All @@ -62,7 +62,7 @@ public ModuleOutput parse(String line, ParsingContext pc) throws ModuleException

private List<String> loadFilter(String line, Path context) throws FilterException, FilterExecutionException {
List<String> output = new ArrayList<>();
String[] split = line.split(" ");
String[] split = line.split("\\s+");
//singleton FilterFactory to ensure that each filter has only one instance to support variables and so on in the future
Filter filterInstance = FilterFactory.getFilterInstance(split[0]);
output.addAll(filterInstance.execute(split, context));
Expand Down
Expand Up @@ -20,6 +20,8 @@
import cz.muni.fi.lessappcache.parser.ManifestParser;
import cz.muni.fi.lessappcache.parser.ParsingContext;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.log4j.Logger;

/**
Expand All @@ -45,7 +47,8 @@ public ModuleOutput parse(String line, ParsingContext pc) throws ModuleException
if (line.startsWith("@import")) {
output.setControl(ModuleControl.STOP);
String url = line.replaceAll("(?i)^@import\\s+(.*)$", "$1");
String file = (PathUtils.isAbsoluteOrRemote(url) ? "" : pc.getContext()) + url;
Path base = PathUtils.isAbsoluteOrRemote(url) ? Paths.get("") : pc.getContext();
Path file = base.resolve(Paths.get(url));
if (Importer.isImported(file)) {
logger.warn("File "+file+" already imported. Skipping...");
return output;
Expand All @@ -54,7 +57,7 @@ public ModuleOutput parse(String line, ParsingContext pc) throws ModuleException
try {
mp.getLoadedResources().putAll(pc.getLoadedResources());
mp.setMode(pc.getMode());
output.getOutput().addAll(mp.processFileInContextOf(pc.getContext()));
output.getOutput().addAll(mp.processFile());
output.setLoadedResources(mp.getLoadedResources());
output.setMode(mp.getMode());
} catch (IOException ex) {
Expand Down
@@ -1,3 +1,4 @@

/*
* Copyright 2013 Petr Kunc.
*
Expand All @@ -19,6 +20,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Class representing the output of modules. Contains generated lines, newly loaded resources and mode the parser should switch to.
Expand Down Expand Up @@ -95,4 +97,45 @@ public String getMode() {
public void setMode(String mode) {
this.mode = mode;
}

@Override
public int hashCode() {
int hash = 7;
hash = 37 * hash + Objects.hashCode(this.output);
hash = 37 * hash + Objects.hashCode(this.loadedResources);
hash = 37 * hash + (this.control != null ? this.control.hashCode() : 0);
hash = 37 * hash + Objects.hashCode(this.mode);
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ModuleOutput other = (ModuleOutput) obj;
if (!Objects.equals(this.output, other.output)) {
return false;
}
if (!Objects.equals(this.loadedResources.keySet(), other.loadedResources.keySet())) {
return false;
}
if (this.control != other.control) {
return false;
}
if (!Objects.equals(this.mode, other.mode)) {
return false;
}
return true;
}

@Override
public String toString() {
return "ModuleOutput{" + "output=" + output + ", loadedResources=" + loadedResources + ", control=" + control + ", mode=" + mode + '}';
}


}
Expand Up @@ -29,7 +29,7 @@ public class NetworkModule extends AbstractModule implements Module {
* Constructs module and sets priority
*/
public NetworkModule() {
setPriority(1.2);
setPriority(1.0);
}

@Override
Expand Down

0 comments on commit ee59bc8

Please sign in to comment.