Permalink
Browse files

First unit test working

  • Loading branch information...
1 parent 030efa6 commit bf1adfbf407e1ddb6ad76044b196004f3fa6f20e Nicholas C. Zakas committed Oct 30, 2009
View
@@ -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
View
@@ -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"
@@ -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">
@@ -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>
@@ -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>
View
Binary file not shown.
@@ -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;
@@ -128,7 +132,6 @@ public static void embedImages(Reader in, Writer out, String root) throws IOExce
builder.append(line);
}
- builder.append("\n");
lineNum++;
}
@@ -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://")){
@@ -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);
}
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -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.