return this.doc.edit(true);
return this.doc.edit(true);
protected Properties datasourceConfig() { Properties datasourceCfg = new Properties(); EditableDocument localCopy = config.edit(true); // remove the generic configuration fields ALL_FIELDS.forEach(localCopy::remove); // convert each of the properties to their Hikari names datasourceCfg.setProperty("jdbcUrl", connectionUrl); datasourceCfg.setProperty("driverClassName", config.getString(DRIVER, DEFAULT_DRIVER)); datasourceCfg.setProperty("username", config.getString(USERNAME, DEFAULT_USERNAME)); datasourceCfg.setProperty("password", config.getString(PASSWORD, DEFAULT_PASSWORD)); datasourceCfg.setProperty("maximumPoolSize", propertyAsString(config, POOL_SIZE, DEFAULT_MAX_POOL_SIZE)); datasourceCfg.setProperty("minimumIdle", DEFAULT_MIN_IDLE); datasourceCfg.setProperty("idleTimeout", DEFAULT_IDLE_TIMEOUT); // pass all the other fields as they are (this will also overwrite any of the previous values if they are explicitly configured) localCopy.fields().forEach(field -> datasourceCfg.setProperty(field.getName(), field.getValue().toString())); return datasourceCfg; }
protected Document dbPersistenceConfiguration() { EditableDocument config = super.defaultPersistenceConfiguration().edit(false); //use a random mem db each time, to avoid possible conflicts.... config.setString(RepositoryConfiguration.FieldName.TYPE, RelationalDbConfig.ALIAS1); config.setString(RelationalDbConfig.CONNECTION_URL, "jdbc:h2:mem:" + UUID.randomUUID().toString() + ";DB_CLOSE_DELAY=0"); config.setBoolean(RelationalDbConfig.DROP_ON_EXIT, true); return config; } }
@Test public void shouldGetAndPut() throws Exception { List<SchematicEntry> dbEntries = randomEntries(3); //simulate the start of a transaction db.txStarted("0"); //write some entries without committing dbEntries.forEach(dbEntry -> db.put(dbEntry.id(), dbEntry.content())); Set<String> expectedIds = dbEntries.stream().map(SchematicEntry::id).collect(Collectors.toCollection(TreeSet::new)); // check that the same connection is used and the entries are still there assertTrue(db.keys().containsAll(expectedIds)); // simulate a commit for the write db.txCommitted("0"); // check that the entries are still there assertTrue(db.keys().containsAll(expectedIds)); // check that for each entry the content is correctly stored dbEntries.stream().forEach(entry -> assertEquals(entry.content(), db.getEntry(entry.id()).content())); // update one of the documents and check the update is correct SchematicEntry firstEntry = dbEntries.get(0); String idToUpdate = firstEntry.id(); EditableDocument updatedDocument = firstEntry.content().edit(true); updatedDocument.setNumber(VALUE_FIELD, 2); //simulate a new transaction db.txStarted("1"); db.get(idToUpdate); db.put(idToUpdate, updatedDocument); assertEquals(updatedDocument, db.getEntry(idToUpdate).content()); db.txCommitted("1"); assertEquals(updatedDocument, db.getEntry(idToUpdate).content()); }
@Test public void shouldPutIfAbsent() throws Exception { SchematicEntry entry = writeSingleEntry(); EditableDocument editableDocument = entry.content().edit(true); editableDocument.setNumber(VALUE_FIELD, 100); SchematicEntry updatedEntry = simulateTransaction(() -> db.putIfAbsent(entry.id(), entry.content())); assertNotNull(updatedEntry); assertEquals(1, (int) updatedEntry.content().getInteger(VALUE_FIELD)); SchematicEntry newEntry = SchematicEntry.create(UUID.randomUUID().toString(), DEFAULT_CONTENT); assertNull(simulateTransaction(() -> db.putIfAbsent(newEntry.id(), newEntry.content()))); updatedEntry = db.getEntry(newEntry.id()); assertNotNull(updatedEntry); }