Skip to content

Commit

Permalink
Fix issue #42
Browse files Browse the repository at this point in the history
  • Loading branch information
christ66 committed Jul 16, 2013
1 parent 006cc9b commit faa6fe3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cobertura/src/main/javacc/Java1.1.jj
Expand Up @@ -2365,7 +2365,7 @@ void EqualityExpression() :
void EqualityExpression() :
{}
{
AssigmentExpression() ( ( "==" | "!=" ) AssigmentExpression() )*
AssigmentExpression() ( ( "==" | "!=" | "+=" | "-=" | "*=" | "/=" | "&=" | "|=" | "^=" | "%=" | "<<=" | ">>=" | ">>>=") AssigmentExpression() )*
}

void AssigmentExpression() :
Expand Down
@@ -0,0 +1,29 @@
package net.sourceforge.cobertura.bugs;

import java.io.IOException;

import org.junit.Test;

import net.sourceforge.cobertura.test.AbstractCoberturaTestCase;

public class GithubIssue42 extends AbstractCoberturaTestCase {

@Test
public void testPlusEqualsParsing() throws IOException {
String imports = "";
String method = "\n public void foo() {" + "\n int yyn = 0;"
+ "\n int yychar = 1;" + "\n if ((yyn += yychar) >= 0)"
+ "\n {} " + "\n if ((yyn -= yychar) >= 0)" + "\n {} "
+ "\n if ((yyn *= yychar) >= 0)" + "\n {} "
+ "\n if ((yyn /= yychar) >= 0)" + "\n {} "
+ "\n if ((yyn &= yychar) >= 0)" + "\n {} "
+ "\n if ((yyn |= yychar) >= 0)" + "\n {} "
+ "\n if ((yyn ^= yychar) >= 0)" + "\n {} "
+ "\n if ((yyn %= yychar) >= 0)" + "\n {} "
+ "\n if ((yyn <<= yychar) >= 0)" + "\n {} "
+ "\n if ((yyn >>= yychar) >= 0)" + "\n {} "
+ "\n if ((yyn >>>= yychar) >= 0)" + "\n {} " + "\n }";

super.parseIssueTester(imports, method);
}
}
Expand Up @@ -7,23 +7,17 @@
import net.sourceforge.cobertura.ant.ReportTask;
import net.sourceforge.cobertura.test.util.TestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.DirSet;
import org.apache.tools.ant.types.Path;
import org.junit.After;
import org.junit.Before;
import org.xml.sax.SAXException;

import com.sun.corba.se.impl.oa.poa.AOMEntry;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

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

/**
* @author schristou88
Expand All @@ -35,7 +29,7 @@ public class AbstractCoberturaTestCase {
public static File reportDir;
public static File instrumentDir;
File mainSourceFile;
File datafile;
public static File datafile;

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -107,4 +101,51 @@ public Node createAndExecuteMainMethod(String packageName, String fileName,
return TestUtils.getXMLReportDOM(srcDir.getAbsolutePath()
+ "/coverage.xml");
}

/**
*
* @param method method with the parsing issue
* @throws IOException
*/
public static void parseIssueTester(String imports, String method)
throws IOException {
String wrapper = "\n package mypackage;" + "\n " + imports
+ "\n public class FooMain {" + method + "\n }";

FileUtils.write(new File(srcDir, "mypackage/FooMain.java"), wrapper);

TestUtils.compileSource(TestUtils.antBuilder, srcDir);

TestUtils.instrumentClasses(TestUtils.antBuilder, srcDir, datafile,
instrumentDir);

DefaultLogger fileLogger = new DefaultLogger();
fileLogger.setErrorPrintStream(new PrintStream(new File(reportDir,
"error.log")));
fileLogger.setOutputPrintStream(new PrintStream(new File(reportDir,
"std.log")));
fileLogger.setMessageOutputLevel(Project.MSG_INFO);
TestUtils.project.addBuildListener(fileLogger);

ReportTask reportTask = new ReportTask();
reportTask.setProject(TestUtils.project);
reportTask.setDataFile(datafile.getAbsolutePath());
reportTask.setFormat("xml");
reportTask.setSrcDir(srcDir.getAbsolutePath());
reportTask.setDestDir(reportDir);
reportTask.setFailonerror(true);
reportTask.execute();

TestUtils.project.removeBuildListener(fileLogger);

if (FileUtils.readFileToString(new File(reportDir, "error.log"))
.contains("JavaNCSS got an error while parsing the java file"))
fail("JavaNCSS Error, see console output or file: "
+ new File(reportDir, "error.log").getAbsolutePath());

if (FileUtils.readFileToString(new File(reportDir, "std.log"))
.contains("JavaNCSS got an error while parsing the java file"))
fail("JavaNCSS Error, see console output or file: "
+ new File(reportDir, "std.log").getAbsolutePath());
}
}

0 comments on commit faa6fe3

Please sign in to comment.