Skip to content

Commit f50826c

Browse files
committed
Added WebPageChangedDetector
1 parent aa18a1b commit f50826c

File tree

8 files changed

+277
-2
lines changed

8 files changed

+277
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.class
2+
/webPageImages
23
/bin
34
*.received.*
45
# Package Files #
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.approvaltests.webpages;
2+
3+
import java.io.File;
4+
import java.net.URI;
5+
6+
import org.approvaltests.Approvals;
7+
8+
import com.spun.util.ObjectUtils;
9+
import com.spun.util.io.FileUtils;
10+
11+
public class WebPageApproval
12+
{
13+
public static void verifyRenderedPage(URI uri)
14+
{
15+
String imageFile = convertToLegalFileName(uri, "png");
16+
captureWebPage(uri, imageFile);
17+
Approvals.verify(new File(imageFile));
18+
}
19+
public static void captureWebPage(URI uri, String imageFile) throws Error
20+
{
21+
try
22+
{
23+
File jsFile = createPhantomjsCommand(uri, imageFile);
24+
Process exec = Runtime.getRuntime().exec(String.format("C:\\tools\\PhantomJS\\phantomjs.exe %s", jsFile));
25+
int waitFor = exec.waitFor();
26+
}
27+
catch (Throwable e)
28+
{
29+
throw ObjectUtils.throwAsError(e);
30+
}
31+
}
32+
private static File createPhantomjsCommand(URI uri, String imageFile) throws Throwable
33+
{
34+
File jsFile = File.createTempFile("capture", ".js");
35+
String template = "var page = require('webpage').create();\n" + "page.open('%s', function() {\n"
36+
+ " page.render('%s');\n" + " phantom.exit();\n" + "});";
37+
String js = String.format(template, uri, imageFile);
38+
FileUtils.writeFile(jsFile, js);
39+
return jsFile;
40+
}
41+
public static String convertToLegalFileName(URI uri, String extentionWithoutDot)
42+
{
43+
return uri.toString().replaceAll("[^a-zA-Z0-9\\.\\-]", "_") + "." + extentionWithoutDot;
44+
}
45+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package org.approvaltests.webpages;
2+
3+
import java.awt.Color;
4+
import java.awt.event.ActionEvent;
5+
import java.awt.event.ActionListener;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.net.URI;
9+
import java.net.URISyntaxException;
10+
11+
import javax.swing.JFrame;
12+
13+
import org.approvaltests.approvers.FileApprover;
14+
import org.approvaltests.reporters.DiffReporter;
15+
16+
import com.spun.util.Colors;
17+
import com.spun.util.ObjectUtils;
18+
import com.spun.util.WindowUtils;
19+
20+
public class WebPageChangeDetector implements ActionListener
21+
{
22+
private boolean keyPressed = false;
23+
private URI url;
24+
private boolean validUrl;
25+
private Boolean filesMatched;
26+
private WebPageChangeDetectorGui gui;
27+
public WebPageChangeDetector()
28+
{
29+
gui = new WebPageChangeDetectorGui(this);
30+
}
31+
@Override
32+
public void actionPerformed(ActionEvent e)
33+
{
34+
if (keyPressed) { return; }
35+
try
36+
{
37+
keyPressed = true;
38+
if (e.getSource() == gui.lock)
39+
{
40+
doLock();
41+
}
42+
else if (e.getSource() == gui.check)
43+
{
44+
doCheck();
45+
}
46+
}
47+
finally
48+
{
49+
keyPressed = false;
50+
}
51+
}
52+
private void doCheck()
53+
{
54+
updateModel();
55+
WebPageApproval.captureWebPage(url, getRecievedFile());
56+
filesMatched = verifyFiles(getApprovedFile(), getRecievedFile());
57+
if (!filesMatched)
58+
{
59+
try
60+
{
61+
new DiffReporter().report(getApprovedFile(), getRecievedFile());
62+
}
63+
catch (Exception e)
64+
{
65+
throw ObjectUtils.throwAsError(e);
66+
}
67+
}
68+
updateScreen();
69+
}
70+
private boolean verifyFiles(String approvedFile, String recievedFile)
71+
{
72+
try
73+
{
74+
return FileApprover.approveTextFile(new File(approvedFile), new File(recievedFile));
75+
}
76+
catch (IOException e)
77+
{
78+
throw ObjectUtils.throwAsError(e);
79+
}
80+
}
81+
private String getRecievedFile()
82+
{
83+
return "webPageImages/" + WebPageApproval.convertToLegalFileName(url, "recieved.png");
84+
}
85+
private void doLock()
86+
{
87+
updateModel();
88+
WebPageApproval.captureWebPage(url, getApprovedFile());
89+
updateScreen();
90+
}
91+
private String getApprovedFile()
92+
{
93+
return "webPageImages/" + WebPageApproval.convertToLegalFileName(url, "png");
94+
}
95+
private void updateModel()
96+
{
97+
try
98+
{
99+
url = new URI(gui.urlTextField.getText());
100+
}
101+
catch (URISyntaxException e)
102+
{
103+
validUrl = false;
104+
url = null;
105+
}
106+
}
107+
private void updateScreen()
108+
{
109+
gui.urlTextField.setText(url.toString());
110+
Color color = null;
111+
if (filesMatched == null)
112+
{
113+
color = Colors.Yellows.Yellow;
114+
}
115+
else if (filesMatched == true)
116+
{
117+
color = Colors.Greens.Green;
118+
}
119+
else if (filesMatched == false)
120+
{
121+
color = Colors.Reds.FireBrick;
122+
}
123+
gui.check.setBackground(color);
124+
}
125+
public static void main(String[] args) throws URISyntaxException
126+
{
127+
WebPageChangeDetector panel = new WebPageChangeDetector();
128+
String url = args.length == 0 ? "http://cosmoquest.org" : args[0];
129+
panel.url = new URI(url);
130+
panel.updateScreen();
131+
panel.showFrame();
132+
}
133+
private void showFrame()
134+
{
135+
JFrame test = new JFrame("CSS/HTML Refactor Locker");
136+
test.getContentPane().add(gui);
137+
WindowUtils.testFrame(test, true);
138+
}
139+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.approvaltests.webpages;
2+
3+
import java.awt.Dimension;
4+
import java.awt.GridBagConstraints;
5+
import java.awt.GridBagLayout;
6+
7+
import javax.swing.JButton;
8+
import javax.swing.JLabel;
9+
import javax.swing.JPanel;
10+
import javax.swing.JTextField;
11+
12+
public class WebPageChangeDetectorGui extends JPanel
13+
{
14+
private JLabel urlLabel;
15+
public JTextField urlTextField;
16+
public JButton lock;
17+
public JButton check;
18+
private WebPageChangeDetector controller;
19+
public WebPageChangeDetectorGui(WebPageChangeDetector controller)
20+
{
21+
this.controller = controller;
22+
initialize();
23+
}
24+
private void initialize()
25+
{
26+
this.setPreferredSize(new Dimension(400, 50));
27+
JButton button;
28+
this.setLayout(new GridBagLayout());
29+
GridBagConstraints c = new GridBagConstraints();
30+
c.fill = GridBagConstraints.HORIZONTAL;
31+
urlLabel = new JLabel("URL");
32+
c.weightx = 0.5;
33+
c.fill = GridBagConstraints.HORIZONTAL;
34+
c.gridx = 0;
35+
c.gridy = 0;
36+
this.add(urlLabel, c);
37+
urlTextField = new JTextField();
38+
c.fill = GridBagConstraints.HORIZONTAL;
39+
c.weightx = 0.5;
40+
c.gridwidth = 3;
41+
c.gridx = 1;
42+
c.gridy = 0;
43+
this.add(urlTextField, c);
44+
lock = new JButton("Lock");
45+
lock.addActionListener(controller);
46+
c.fill = GridBagConstraints.HORIZONTAL;
47+
c.weightx = 0.5;
48+
c.gridwidth = 1;
49+
c.gridx = 2;
50+
c.gridy = 1;
51+
this.add(lock, c);
52+
check = new JButton("Check");
53+
check.addActionListener(controller);
54+
c.fill = GridBagConstraints.HORIZONTAL;
55+
c.weightx = 0.5;
56+
c.gridwidth = 1;
57+
c.gridx = 3;
58+
c.gridy = 1;
59+
this.add(check, c);
60+
}
61+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.approvaltests.webpages.tests;
2+
3+
import java.net.URI;
4+
5+
import junit.framework.TestCase;
6+
7+
import org.approvaltests.Approvals;
8+
import org.approvaltests.reporters.ClipboardReporter;
9+
import org.approvaltests.reporters.DiffReporter;
10+
import org.approvaltests.reporters.UseReporter;
11+
import org.approvaltests.webpages.WebPageApproval;
12+
import org.approvaltests.webpages.WebPageChangeDetector;
13+
14+
@UseReporter({DiffReporter.class, ClipboardReporter.class})
15+
public class WebPageTest extends TestCase
16+
{
17+
public void ptestWikipedia() throws Exception
18+
{
19+
WebPageApproval.verifyRenderedPage(new URI("http://cosmoquest.org"));
20+
}
21+
public void testChangeDetectorUI() throws Exception
22+
{
23+
Approvals.verify(new WebPageChangeDetector());
24+
}
25+
}
1.48 KB
Loading
Binary file not shown.

java/org/approvaltests/writers/FileApprovalWriter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
import org.approvaltests.core.ApprovalWriter;
66

7+
import com.spun.util.io.FileUtils;
8+
79
public class FileApprovalWriter implements ApprovalWriter
810
{
911
private final File newFile;
12+
private String extensionWithDot;
1013
public FileApprovalWriter(File newFile)
1114
{
1215
this.newFile = newFile;
16+
extensionWithDot = FileUtils.getExtensionWithDot(newFile.getName());
1317
}
1418
@Override
1519
public String writeReceivedFile(String received) throws Exception
@@ -19,11 +23,11 @@ public String writeReceivedFile(String received) throws Exception
1923
@Override
2024
public String getApprovalFilename(String base)
2125
{
22-
return base + Writer.approved + ".html";
26+
return base + Writer.approved + extensionWithDot;
2327
}
2428
@Override
2529
public String getReceivedFilename(String base)
2630
{
27-
return base + Writer.received + ".html";
31+
return base + Writer.received + extensionWithDot;
2832
}
2933
}

0 commit comments

Comments
 (0)