use of javax.jcr.Binary in project jackrabbit-oak by apache.
the class TextExtractionQueryTest method assertFileContains.
private void assertFileContains(String name, String type, String... statements) throws Exception {
if (testRootNode.hasNode(nodeName1)) {
testRootNode.getNode(nodeName1).remove();
}
testRootNode.getSession().save();
Node resource = testRootNode.addNode(nodeName1, NodeType.NT_RESOURCE);
resource.setProperty(JcrConstants.JCR_MIMETYPE, type);
InputStream stream = getClass().getResourceAsStream(name);
assertNotNull(stream);
try {
Binary binary = testRootNode.getSession().getValueFactory().createBinary(stream);
resource.setProperty(JcrConstants.JCR_DATA, binary);
} finally {
stream.close();
}
testRootNode.getSession().save();
for (String statement : statements) {
assertContainsQuery(statement, true);
}
}
use of javax.jcr.Binary in project jackrabbit-oak by apache.
the class DefaultSyncContext method createValue.
/**
* Creates a new JCR value of the given object, checking the internal type.
* @param v the value
* @return the JCR value or null
* @throws RepositoryException if an error occurs
*/
@CheckForNull
protected Value createValue(@Nullable Object v) throws RepositoryException {
if (v == null) {
return null;
} else if (v instanceof Boolean) {
return valueFactory.createValue((Boolean) v);
} else if (v instanceof Byte || v instanceof Short || v instanceof Integer || v instanceof Long) {
return valueFactory.createValue(((Number) v).longValue());
} else if (v instanceof Float || v instanceof Double) {
return valueFactory.createValue(((Number) v).doubleValue());
} else if (v instanceof BigDecimal) {
return valueFactory.createValue((BigDecimal) v);
} else if (v instanceof Calendar) {
return valueFactory.createValue((Calendar) v);
} else if (v instanceof Date) {
Calendar cal = Calendar.getInstance();
cal.setTime((Date) v);
return valueFactory.createValue(cal);
} else if (v instanceof byte[]) {
Binary bin = valueFactory.createBinary(new ByteArrayInputStream((byte[]) v));
return valueFactory.createValue(bin);
} else if (v instanceof Binary) {
return valueFactory.createValue((Binary) v);
} else if (v instanceof InputStream) {
return valueFactory.createValue((InputStream) v);
} else if (v instanceof char[]) {
return valueFactory.createValue(new String((char[]) v));
} else {
return valueFactory.createValue(String.valueOf(v));
}
}
use of javax.jcr.Binary in project jackrabbit-oak by apache.
the class DefaultSyncContextTest method testCreateValueFromInputStream.
/**
* @see <a href="https://issues.apache.org/jira/browse/OAK-4231">OAK-4231</a>
*/
@Test
public void testCreateValueFromInputStream() throws Exception {
byte[] bytes = new byte[] { 'a', 'b' };
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
Binary binary = valueFactory.createBinary(is);
Value v = syncCtx.createValue(is);
assertNotNull(v);
assertEquals(PropertyType.BINARY, v.getType());
assertEquals(binary, v.getBinary());
}
use of javax.jcr.Binary in project acs-aem-commons by Adobe-Consulting-Services.
the class EntryNodeWriterTest method testValid.
@Test
public void testValid() throws IOException, RepositoryException {
final EntryNodeWriterMocks.MockArguments arguments = new EntryNodeWriterMocks.MockArguments();
arguments.cacheContentCharEncoding = "UTF-8";
arguments.cacheContentType = "text/html";
arguments.entryNode = mock(Node.class);
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(CACHE_CONTENT_LOCATION);
arguments.cacheContent = inputStream;
List<String> header1Value = Arrays.asList("header-value");
List<String> header2Value = Arrays.asList("another-header-value");
arguments.cacheContentHeaders.put("some-header", header1Value);
arguments.cacheContentHeaders.put("another-header", header2Value);
final EntryNodeWriterMocks mocks = new EntryNodeWriterMocks(arguments);
mocks.getEntryNodeWriter().write();
verify(mocks.getEntryNode(), times(1)).setProperty(Matchers.startsWith(JCRHttpCacheStoreConstants.PN_CACHEKEY), any(Binary.class));
ArgumentCaptor<Binary> argumentCaptor = ArgumentCaptor.forClass(Binary.class);
verify(mocks.getJcrContentNode(), times(1)).setProperty(Matchers.startsWith(JcrConstants.JCR_DATA), argumentCaptor.capture());
Binary savedBinary = argumentCaptor.getValue();
IOUtils.contentEquals(inputStream, savedBinary.getStream());
verify(mocks.getJcrContentNode(), times(1)).setProperty(JcrConstants.JCR_MIMETYPE, arguments.cacheContentType);
// verify(mocks.getHeadersNode().setProperty("some-header", header1Value))
}
use of javax.jcr.Binary in project jackrabbit by apache.
the class GarbageCollectorTest method testGC.
public void testGC() throws Exception {
Node root = testRootNode;
Session session = root.getSession();
deleteMyNodes();
runGC(session, true);
root.addNode("node1");
Node node2 = root.addNode("node2");
Node n = node2.addNode("nodeWithBlob").addNode("sub");
ValueFactory vf = session.getValueFactory();
Binary b = vf.createBinary(new RandomInputStream(20, 1000));
n.setProperty("test", b);
session.save();
n = node2.addNode("nodeWithTemporaryBlob");
n.setProperty("test", vf.createBinary(new RandomInputStream(11, 1000)));
session.save();
n.remove();
session.save();
GarbageCollector gc = ((SessionImpl) session).createDataStoreGarbageCollector();
gc.getDataStore().clearInUse();
gc.setPersistenceManagerScan(false);
gc.setMarkEventListener(this);
if (gc.getDataStore() instanceof FileDataStore) {
// make sure the file is old (access time resolution is 2 seconds)
Thread.sleep(2000);
}
LOG.debug("scanning...");
gc.mark();
int count = listIdentifiers(gc);
LOG.debug("stop scanning; currently " + count + " identifiers");
LOG.debug("deleting...");
gc.getDataStore().clearInUse();
assertTrue(gc.sweep() > 0);
int count2 = listIdentifiers(gc);
assertEquals(count - 1, count2);
// verify the node was moved, and that the binary is still there
n = root.getNode("node1").getNode("nodeWithBlob").getNode("sub");
b = n.getProperty("test").getValue().getBinary();
InputStream in = b.getStream();
InputStream in2 = new RandomInputStream(20, 1000);
verifyInputStream(in, in2);
deleteMyNodes();
gc.close();
}
Aggregations