Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Commit

Permalink
First unit test working
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas C. Zakas committed Oct 30, 2009
1 parent 030efa6 commit bf1adfb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions ant.properties
Expand Up @@ -3,6 +3,7 @@ tests.dir = tests
lib.dir = lib
doc.dir = doc
build.dir = build
res.dir = res
product.name = cssembed
version.number = 0.1.0
jar.name = ${product.name}-${version.number}.jar
Expand Down
14 changes: 10 additions & 4 deletions build.xml
Expand Up @@ -28,9 +28,9 @@
</target>

<target name="build.tests" depends="-init">
<mkdir dir="${build.dir}/classes"/>
<mkdir dir="${build.dir}/testclasses"/>
<javac srcdir="${tests.dir}"
destdir="${build.dir}/classes"
destdir="${build.dir}/testclasses"
includes="**/*.java"
deprecation="off"
debug="on"
Expand All @@ -41,6 +41,9 @@
<pathelement location="${build.dir}/classes"/>
</classpath>
</javac>
<copy todir="${build.dir}/testclasses/net/nczonline/web/cssembed">
<fileset dir="${tests.dir}" includes="*.png"/>
</copy>
</target>

<target name="build.jar" depends="build.classes">
Expand Down Expand Up @@ -76,11 +79,14 @@
</target>

<target name="test.classes" depends="build.classes,build.tests">
<junit printsummary="yes" fork="yes" errorproperty="junit.failure" failureproperty="junit.failure" showoutput="true">
<junit printsummary="yes" fork="yes" errorproperty="junit.failure" failureproperty="junit.failure" showoutput="true">

<jvmarg value="-Duser.dir=${res.dir}"/>

<!-- classpath must include all jar dependencies and classes -->
<classpath>
<pathelement location="${build.dir}/classes"/>
<pathelement location="${build.dir}/testclasses"/>
<pathelement location="${lib.dir}/${datauri.jar}"/>
<pathelement location="${lib.dir}/${junit.jar}"/>
</classpath>
Expand All @@ -90,7 +96,7 @@

<!-- fully qualified classname of testsuite -->
<batchtest>
<fileset dir="${build.dir}/classes">
<fileset dir="${build.dir}/testclasses">
<include name="**/*Test.class" />
</fileset>
</batchtest>
Expand Down
Binary file added lib/junit-4.1.jar
Binary file not shown.
13 changes: 12 additions & 1 deletion src/net/nczonline/web/cssembed/CSSURLEmbedder.java
Expand Up @@ -96,6 +96,10 @@ public static void embedImages(Reader in, Writer out, String root) throws IOExce
int pos = line.indexOf("url(", start);
int npos;

if (lineNum > 1){
builder.append("\n");
}

if (pos > -1){
while (pos > -1){
pos += 4;
Expand Down Expand Up @@ -128,7 +132,6 @@ public static void embedImages(Reader in, Writer out, String root) throws IOExce
builder.append(line);
}

builder.append("\n");
lineNum++;
}

Expand All @@ -153,6 +156,8 @@ private static String getImageURIString(String url, String originalUrl) throws I
//it's an image, so encode it
if (imageTypes.contains(fileType)){

DataURIGenerator.setVerbose(verbose);

StringWriter writer = new StringWriter();

if (url.startsWith("http://")){
Expand All @@ -167,6 +172,12 @@ private static String getImageURIString(String url, String originalUrl) throws I
System.err.println("[INFO] Opening file '" + url + "' to generate data URI.");
}

File file = new File(url);

if (verbose && !file.isFile()){
System.err.println("[INFO] Could not find file '" + file.getCanonicalPath() + "'.");
}

DataURIGenerator.generate(new File(url), writer);
}

Expand Down
Binary file added tests/folder.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 20 additions & 8 deletions tests/net/nczonline/web/cssembed/CSSURLEmbedderTest.java
Expand Up @@ -31,17 +31,29 @@ public void tearDown() {
}

@Test
public void testRelativeLocalFile() throws IOException {
System.out.println("HERE");
String code = "background: url(folder.png)";
StringWriter writer = new StringWriter();
public void testAbsoluteLocalFile() throws IOException {
String filename = CSSURLEmbedderTest.class.getResource("folder.png").getPath().replace("%20", " ");
String code = "background: url(folder.png);";

CSSURLEmbedder.embedImages(new StringReader(code), writer);
StringWriter writer = new StringWriter();
CSSURLEmbedder.setVerbose(true);
CSSURLEmbedder.embedImages(new StringReader(code), writer, filename.substring(0, filename.lastIndexOf("/")+1));

String result = writer.toString();

assertEquals("", result);

assertEquals("background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACIAAAAbCAMAAAAu7K2VAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAwUExURWxsbNbW1v/rhf/ge//3kf/Ub9/f3/b29oeHh/7LZv/0juazTktLS8WSLf//mf/////BPrAAAAB4SURBVHja3NLdCoAgDIbhqbXZz2f3f7eZWUpMO67nQEReBqK0vaLPJohYegnSYqSdYAtRGvUYVpJhPpx7z2piLSqsJQ73oY1ztGREuEwBpCUTwpAt7cRmncRlnWTMoCdcXxmrdiMxngpvtDcSNkX9AvTnv9uyCzAAgzAw+dNAwOQAAAAASUVORK5CYII=);", result);
}

@Test
public void testLocalFileWithBase() throws IOException {
String code = "background: url(folder.png);";
StringWriter writer = new StringWriter();

CSSURLEmbedder.embedImages(new StringReader(code), writer, Class.class.getResource("folder.png").getPath());

String result = writer.toString();


}


}

0 comments on commit bf1adfb

Please sign in to comment.