aboutsummaryrefslogtreecommitdiffstats
path: root/docs/sandbox/inoculated/src/StubReplace.java
blob: 56bda0367f0d084da749288c64b0e265ae95e3ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * Copyright (c) 1998-2002 PARC Inc.  All rights reserved.
 *
 * Use and copying of this software and preparation of derivative works based
 * upon this software are permitted.  Any distribution of this software or
 * derivative works must comply with all applicable United States export
 * control laws.
 *
 * This software is made available AS IS, and PARC Inc. makes no
 * warranty about the software, its performance or its conformity to any
 * specification.
 */

public class StubReplace {
  public static void main(String[] args) {
      new PrintJob().run();
  } 
} 

/** @author Wes Isberg */
aspect Stubs {

    // article page 76 - stubs

    // START-SAMPLE testing-inoculated-replaceWithProxy Replace object with proxy on constructiono
    /**
     * Replace all PrintStream with our StubStream
     * by replacing the call to any constructor of 
     * PrinterStream or any subclasses.
     */
    PrinterStream around () : within(PrintJob) 
        && call (PrinterStream+.new(..)) && !call (StubStream+.new(..)) {
        return new StubStream(thisJoinPoint.getArgs());
    }  
    // END-SAMPLE testing-inoculated-replaceWithProxy

    // START-SAMPLE testing-inoculated-adviseProxyCallsOnly Advise calls to the proxy object only
    pointcut stubWrite() : printerStreamTestCalls() && target(StubStream);

    pointcut printerStreamTestCalls() : call(* PrinterStream.write());

    before() : stubWrite() {
        System.err.println("picking out stubWrite" );
    }
    // END-SAMPLE testing-inoculated-adviseProxyCallsOnly
}

class PrinterStream {
    public void write() {}
}

class StubStream extends PrinterStream {
    public StubStream(Object[] args) {}
}

class PrintJob {
    public void run() {
        PrinterStream p = new PrinterStream();
        System.err.println("not PrinterStream: " + p);
        System.err.println("now trying call...");
        p.write();
    }
}