Skip to content

Instantly share code, notes, and snippets.

package se.uhr.nya.application;
/*
<repositories>
<repository>
<id>alfresco</id>
<url>https://artifacts.alfresco.com/nexus/content/groups/public</url>
</repository>
<repository>
</repositories>
/*
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
*/
package se.uhr.nya.application;
@jonananas
jonananas / ExcelCompare.java
Last active January 27, 2016 13:22
Compare two Excel xlsx files or streams by unzipping and comparing content, except docProps/core.xml which contain date of creation.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ExcelCompare {
@jonananas
jonananas / exceptionInJava8.java
Created December 14, 2015 16:16
Example testing for exception
@Test
public void shouldThrow()
}
Exception exception = assertThrowsWithMessage(x -> statistikRest.excel(req));
assertThat(exception.getMessage()).isEqualTo("the message from exception");
}
private Exception assertThrowsWithMessage(Consumer<String> body) {
try {
body.accept("foo");
@jonananas
jonananas / fileOnClasspathContents.java
Created December 11, 2015 13:17
Get contents from file on classpath
private String fileOnClasspathContents(String filename) {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(filename);
return toString(in);
}
static String toString(InputStream is) {
try (Scanner s = new Scanner(is)) {
s.useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
@RunWith(CdiRunner.class)
public class RepositoryJDBCTest extends RepositoryTest {
@Produces
DataSource createDataSource() {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL("jdbc:h2:mem:test;MODE=DB2;DB_CLOSE_DELAY=1;INIT=runscript from 'create.sql'");
dataSource.setUser("sa");
dataSource.setPassword("sa");
@jonananas
jonananas / jdbc_connection_handling.java
Created November 30, 2015 17:39
JDBC - Extraction of connection and exception handling
@FunctionalInterface
interface SQLFunction<R> {
R apply(PreparedStatement t) throws SQLException;
}
public static Object runQuery(String sql, DataSource ds, SQLFunction f) {
logger.debug(sql);
Connection connection;
try {
@jonananas
jonananas / hasTransaction.java
Created November 26, 2015 16:27
Check transaction status in JEE
private String hasTransaction() {
try {
TransactionSynchronizationRegistry tsr =
(TransactionSynchronizationRegistry) new InitialContext().lookup("java:comp/TransactionSynchronizationRegistry");
int status = tsr.getTransactionStatus();
return (status == Status.STATUS_NO_TRANSACTION) ? "no" : "yes:" + status;
} catch (Exception ex) {
logger.info("Failed deciding transaction status", ex);
return "unknown";
}
@jonananas
jonananas / shouldReportNonISO-8859-1
Last active August 29, 2015 14:15
Distinguish between ISO-8859-1 and UTF-8
import static org.fest.assertions.Assertions.assertThat;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import org.junit.Test;
@jonananas
jonananas / IOS_waitForTimeoutOrUntil
Created December 5, 2013 20:41
Awesome testmethod for #ios when testing stuff with async operations. Add as method to your test,
- (void)wait:(NSTimeInterval)timeout secondsOrUntil:(BOOL(^)())operationDone
{
NSTimeInterval idle = 0.01; // Number of seconds to pause within loop
BOOL timedOut = NO;
NSDate *timeoutDate = [[NSDate alloc] initWithTimeIntervalSinceNow:timeout];
while (!timedOut && !operationDone())
{
NSDate *tick = [[NSDate alloc] initWithTimeIntervalSinceNow:idle];
[[NSRunLoop currentRunLoop] runUntilDate:tick];