Skip to content

Commit 45d4011

Browse files
add examples to study next
1 parent 0ece5da commit 45d4011

File tree

24 files changed

+718
-8
lines changed

24 files changed

+718
-8
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package test.currentlyTesting;
2+
3+
import java.io.PrintWriter;
4+
5+
/**
6+
* This class simulates the HttpServletResponse object, which handles writing to the response and sending redirects.
7+
* Main class to simulate and test the commit and redirect behavior.
8+
*/
9+
public class CommitAndRedirectSimulation {
10+
11+
public static void main(String[] args) {
12+
// Create a simulated HTTP response
13+
HttpServletResponseMock response = new HttpServletResponseMock();
14+
// Call the logic that mimics a real servlet behavior
15+
new CommitAndRedirectExample().doGet(response);
16+
}
17+
}
18+
19+
20+
class CommitAndRedirectExample {
21+
22+
/**
23+
* Simulates the logic of handling an HTTP GET request, including writing content to
24+
* the response and attempting a redirect. If the response has been committed,
25+
* a redirection will throw an {@link IllegalStateException}.
26+
*
27+
* @param response The simulated HTTP response.
28+
*/
29+
public void doGet(HttpServletResponseMock response) {
30+
// Write some content to the response (this will commit the response)
31+
PrintWriter out = response.getWriter();
32+
// out.println("This is some content that will be sent to the client.");
33+
34+
// At this point, the response is committed (content is already sent to the client)
35+
36+
// Try to send a redirect after the response has been committed
37+
38+
response.sendRedirect("http://example.com");
39+
System.out.println("Redirect attempted.");
40+
}
41+
}
42+
43+
/**
44+
* This class simulates the HttpServletResponse object, which handles writing to the response and sending redirects.
45+
* Simulates the {@link HttpServletResponse} object for testing purposes.
46+
* It mimics the behavior of response commitment and the ability to perform redirection.
47+
*/
48+
class HttpServletResponseMock {
49+
private boolean committed = false;
50+
private PrintWriter writer;
51+
52+
/**
53+
* Constructor that initializes the mock response writer to print to the console.
54+
*/
55+
public HttpServletResponseMock() {
56+
this.writer = new PrintWriter(System.out); // Write to standard output for simulation
57+
}
58+
59+
/**
60+
* Returns a {@link PrintWriter} for writing content to the response.
61+
* If the response hasn't been committed before, it marks the response as committed.
62+
*
63+
* @return The {@link PrintWriter} for writing content.
64+
*/
65+
public PrintWriter getWriter() {
66+
if (!committed) {
67+
committed = true; // Once content is written, the response is considered committed
68+
}
69+
return writer;
70+
}
71+
72+
/**
73+
* Simulates sending a redirect response to the client.
74+
* If the response is already committed, an {@link IllegalStateException} is thrown.
75+
*
76+
* @param url The URL to redirect the client to.
77+
* @throws IllegalStateException if the response is already committed.
78+
*/
79+
public void sendRedirect(String url) {
80+
if (committed) {
81+
throw new IllegalStateException("Cannot call sendRedirect() after the response has been committed.");
82+
}
83+
// In a real servlet, it would set the HTTP status and Location header here
84+
System.out.println("Redirecting to: " + url);
85+
}
86+
87+
/**
88+
* Returns whether the response has already been committed (i.e., content has been written).
89+
*
90+
* @return True if the response has been committed, false otherwise.
91+
*/
92+
public boolean isCommitted() {
93+
return committed;
94+
}
95+
}

liquidjava-example/src/main/java/testSuite/classes/stack_overflow/iterator_not_tested/IteratorRefinements.java renamed to liquidjava-example/src/main/java/test/currentlyTesting/IteratorRefinements.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package testSuite.classes.stack_overflow.iterator_not_tested;
1+
package test.currentlyTesting;
22

33
import liquidjava.specification.ExternalRefinementsFor;
44
import liquidjava.specification.StateRefinement;

liquidjava-example/src/main/java/testSuite/classes/stack_overflow/iterator_not_tested/LinkedListRefinements.java renamed to liquidjava-example/src/main/java/test/currentlyTesting/LinkedListRefinements.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package testSuite.classes.stack_overflow.iterator_not_tested;
1+
package test.currentlyTesting;
22

33
import java.util.Iterator;
44

liquidjava-example/src/main/java/testSuite/classes/stack_overflow/file_input_stream/Test.java renamed to liquidjava-example/src/main/java/test/currentlyTesting/TestIterator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package testSuite.classes.stack_overflow.file_input_stream;
1+
package test.currentlyTesting;
22

33
import java.io.File;
44
import java.io.FileInputStream;
@@ -20,12 +20,12 @@
2020
* Can we still relate them? Does it make sense?
2121
*/
2222
@SuppressWarnings("unused")
23-
public class Test {
23+
public class TestIterator {
2424

2525
public static void main(String[] args) {
2626

2727
// Create a test file
28-
File testFile = new File("liquidjava-example/src/main/java/testSuite/classes/stack_overflow/file_input_stream/testFile.txt");
28+
File testFile = new File("liquidjava-example/src/main/java/test/currentlyTesting/testFile.txt");
2929

3030
// Simulate opening the file and forgetting to close it
3131
FileInputStream inputStream = null;

liquidjava-example/src/main/java/testSuite/classes/stack_overflow/file_input_stream/testFile.txt renamed to liquidjava-example/src/main/java/test/currentlyTesting/testFile.txt

File renamed without changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package testSuite.in_progress.searching_state_space.resultset_forwardonly;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
7+
/**
8+
* ResultSet
9+
*/
10+
public class Test {
11+
12+
/*
13+
* Error ResultSet is FORWARD_ONLY and we try to get a value before
14+
*/
15+
public static void example6367737(Connection con, String username, String password ) throws Exception {
16+
17+
// Step 1) Prepare the statement
18+
PreparedStatement pstat =
19+
con.prepareStatement("select typeid from users where username=? and password=?");
20+
21+
// Step 2) Set parameters for the statement
22+
pstat.setString(1, username);
23+
pstat.setString(2, password);
24+
25+
// Step 3) Execute the query
26+
ResultSet rs = pstat.executeQuery();
27+
28+
// Step 4) Process the result
29+
int rowCount=0;
30+
while(rs.next()){
31+
rowCount++;
32+
}
33+
34+
// ERROR! because it is FORWARD_ONLY, we cannot go back and check beforeFirst
35+
rs.beforeFirst();
36+
37+
int typeID = 0;
38+
if(rowCount>=1) {
39+
while(rs.next()){
40+
typeID=rs.getInt(1);
41+
}
42+
}
43+
44+
// To be correct we need to change the resultset to be scrollable
45+
/*PreparedStatement pstat =
46+
con.prepareStatement("select typeid from users where username=? and password=?",
47+
ResultSet.TYPE_SCROLL_SENSITIVE,
48+
ResultSet.CONCUR_UPDATABLE);
49+
*/
50+
}
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package testSuite.in_progress.searching_state_space.resultset_read_after_end;
2+
3+
import java.sql.ResultSet;
4+
5+
/**
6+
* URLConnection
7+
*/
8+
public class Test {
9+
10+
/*
11+
* Error cannot reschedule a timer
12+
*/
13+
public static Object example1801324(ResultSet rs) throws Exception {
14+
Object count = null;
15+
if (rs != null) {
16+
while (rs.next()) {
17+
count = rs.getInt(1);
18+
}
19+
count = rs.getInt(1); //this will throw Exhausted resultset
20+
}
21+
22+
return count;
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package testSuite.in_progress.searching_state_space.resultset_read_before_next;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
7+
/**
8+
* ResultSet
9+
*/
10+
public class Test {
11+
12+
/*
13+
* Error - in ResultSet, after executing the query we need to call next() before getting a value
14+
*/
15+
public static void example6367737(Connection con, String username, String password ) throws Exception {
16+
17+
// Step 1) Prepare the statement
18+
PreparedStatement pstat =
19+
con.prepareStatement("select typeid from users where username=? and password=?");
20+
21+
// Step 2) Set parameters for the statement
22+
pstat.setString(1, username);
23+
pstat.setString(2, password);
24+
25+
// Step 3) Execute the query
26+
ResultSet parentMessage = pstat.executeQuery("SELECT SUM(IMPORTANCE) AS IMPAVG FROM MAIL");
27+
28+
float avgsum = parentMessage.getFloat("IMPAVG"); // Error because we are trying to get a value before next
29+
30+
// To be correct we need to call next() before the getter
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package testSuite.in_progress.searching_state_space.timertask_cannot_reschedule;
2+
3+
import java.util.Map;
4+
import java.util.Timer;
5+
import java.util.TimerTask;
6+
7+
/**
8+
* URLConnection
9+
*
10+
* -> STATE_OPENED -> STATE_SETTER -> STATE_CONNECTED
11+
*/
12+
public class Test {
13+
14+
/*
15+
* Error cannot reschedule a timer
16+
*/
17+
public static void example1801324( Map<String, Timer> timers, String sessionKey) {
18+
19+
// Step 1) Get the timer
20+
Timer timer = timers.get(sessionKey);
21+
22+
// Step 2) Cancel the timer
23+
timer.cancel();
24+
25+
// Step 3) Schedule a new task for this timer -> ERROR Cannot reschedule a Timer
26+
timer.schedule(new TimerTask() {
27+
@Override
28+
public void run() {
29+
System.out.println("Timer task completed.");
30+
}
31+
}, 1000);
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package testSuite.in_progress.searching_state_space.url_connection_reuse_connection;
2+
3+
import java.io.IOException;
4+
import java.net.URL;
5+
import java.net.URLConnection;
6+
7+
/**
8+
* URLConnection
9+
*
10+
* -> STATE_OPENED -> STATE_SETTER -> STATE_CONNECTED
11+
*/
12+
public class Test {
13+
/*
14+
* Error cannot set property before opening connection
15+
*/
16+
public static void example4278917(URL address) {
17+
try {
18+
19+
// Step 1) Open the connection
20+
URLConnection connection = address.openConnection();
21+
22+
// Step 2) Connect
23+
connection.connect();
24+
25+
/* Other code in original question */
26+
27+
// Step 3) Setup parameters and connection properties after connection == ERROR
28+
connection.setAllowUserInteraction(true);
29+
connection.addRequestProperty("AUTHENTICATION_REQUEST_PROPERTY", "authorizationRequest");
30+
connection.getHeaderFields();
31+
32+
} catch (IOException e) {
33+
// Handle exceptions related to network or stream issues
34+
System.err.println("Error: " + e.getMessage());
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)