Skip to content

Commit a4f3644

Browse files
committed
add project with license and readme and 100% code coverage
0 parents  commit a4f3644

36 files changed

+1504
-0
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Sebastien Colladon
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Apex Chainable
2+
TBD
3+
4+
## Usage
5+
TBD
6+
7+
```apex
8+
/*TODO*/
9+
```
10+
11+
## Improvement
12+
13+
BatchIterable and ScheduleBatchIterable
14+
15+
## Installation
16+
<a href="https://githubsfdeploy.herokuapp.com?owner=scolladon&repo=apex-chainable">
17+
<img alt="Deploy to Salesforce"
18+
src="https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/deploy.png">
19+
</a>

src/classes/chain_Batch.cls

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public inherited sharing abstract class chain_Batch extends chain_Chainable implements Database.Batchable<sObject> {
2+
private Database.BatchableContext bc;
3+
private List<sObject> scope;
4+
5+
public chain_Batch(){
6+
super();
7+
}
8+
9+
protected override abstract void job();
10+
11+
public abstract Database.QueryLocator start(Database.BatchableContext bc);
12+
13+
public virtual void execute(Database.BatchableContext bd, List<sObject> scope){
14+
this.bc = bc;
15+
this.scope = scope;
16+
try {
17+
this.job();
18+
} catch(Exception ex) {
19+
if(this.canBreak == true) {
20+
throw ex;
21+
}
22+
}
23+
}
24+
25+
public virtual void finish(Database.BatchableContext bc){
26+
this.chain();
27+
}
28+
29+
public virtual override void executeChain() {
30+
Database.executeBatch(this);
31+
}
32+
33+
public override chain_Chainable add(final chain_Chainable aChain) {
34+
if(!(aChain instanceof chain_Future)) {
35+
return super.add(aChain);
36+
}
37+
return this;
38+
}
39+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>44.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

src/classes/chain_Batch_TEST.cls

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@isTest
2+
public class chain_Batch_TEST extends chain_Batch {
3+
public override Database.QueryLocator start(Database.BatchableContext bc) {
4+
return Database.getQueryLocator('select id from account limit 1');
5+
}
6+
7+
public chain_Batch_TEST(){
8+
super();
9+
}
10+
11+
public chain_Batch_TEST(final boolean canBreak){
12+
this();
13+
this.canBreak = canBreak;
14+
}
15+
16+
protected override void job() {
17+
System.Debug('chain_Batch_TEST');
18+
if(this.canBreak == true) {
19+
throw new chain_BatchException();
20+
}
21+
}
22+
23+
public class chain_BatchException extends Exception{}
24+
25+
@testSetup
26+
static void setup(){
27+
insert new Account(name='test');
28+
}
29+
30+
@isTest
31+
static void testFail() {
32+
chain_Batch_TEST aChainable = new chain_Batch_TEST(true);
33+
34+
test.startTest();
35+
try {
36+
aChainable.execute(null,null);
37+
} catch(chain_BatchException ex){}
38+
test.stopTest();
39+
}
40+
41+
@isTest
42+
static void testSuccess() {
43+
chain_Chainable aChainable = new chain_Batch_TEST();
44+
45+
test.startTest();
46+
aChainable.executeChain();
47+
test.stopTest();
48+
}
49+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>44.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

src/classes/chain_Chainable.cls

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public inherited sharing abstract class chain_Chainable {
2+
protected chain_Chainable nextJob;
3+
protected boolean canBreak;
4+
protected abstract void job();
5+
6+
// Deal with add enqueue readding reenqueue
7+
8+
public chain_Chainable() {
9+
this.canBreak = false;
10+
}
11+
12+
public abstract void executeChain();
13+
14+
public virtual chain_Chainable add(final chain_Chainable aChain) {
15+
this.nextJob = aChain;
16+
chain_Chainable tempChain = aChain;
17+
while(tempChain != null && tempChain.nextJob != null) {
18+
tempChain = tempChain.nextJob;
19+
}
20+
return tempChain;
21+
}
22+
23+
protected void chain() {
24+
if(this.nextJob != null) {
25+
this.nextJob.executeChain();
26+
this.nextJob = null;
27+
}
28+
}
29+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>44.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
@isTest
2+
private class chain_Chainable_TEST {
3+
@testSetup
4+
static void setup(){
5+
insert new Account(name='test');
6+
}
7+
8+
@isTest
9+
static void testAll() {
10+
chain_Chainable aChainable = new chain_Method_TEST();
11+
12+
chain_Chainable anotherChainable = new chain_Method_TEST();
13+
anotherChainable.add(new chain_Method_TEST());
14+
15+
aChainable
16+
.Add(anotherChainable)
17+
.Add(new chain_Method_TEST())
18+
.Add(new chain_Batch_TEST())
19+
.Add(new chain_Future_TEST())
20+
.Add(new chain_Schedule_TEST('test','0 0 * * * ?'))
21+
.Add(new chain_Future_TEST())
22+
.Add(new chain_Future_TEST())
23+
.Add(new chain_ScheduleBatch_TEST('test',1,1))
24+
.Add(new chain_Future_TEST())
25+
.Add(new chain_Queue_TEST())
26+
;
27+
28+
test.startTest();
29+
aChainable.executeChain();
30+
test.stopTest();
31+
}
32+
33+
@isTest
34+
static void testQueue() {
35+
chain_Chainable aChainable = new chain_Queue_TEST();
36+
37+
test.startTest();
38+
aChainable.executeChain();
39+
test.stopTest();
40+
}
41+
42+
@isTest
43+
static void testScheduleBatch() {
44+
chain_Chainable aChainable = new chain_ScheduleBatch_TEST('test',1,-1);
45+
46+
test.startTest();
47+
aChainable.executeChain();
48+
test.stopTest();
49+
}
50+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>44.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

0 commit comments

Comments
 (0)