View Javadoc

1   /**
2    * Copyright 2008 WebPhotos
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.webphotos.util.legacy;
17  
18  import java.util.*;
19  import org.apache.log4j.Logger;
20  
21  /**
22   *
23   * @author Guilherme
24   */
25  public class CacheFTPTest extends MassaTesteClassesFTP {
26      
27      private static Logger log = Logger.getLogger(CacheFTPTest.class);
28      private CacheFTP instancia;
29  
30      private Iterator iListaArq;
31      private ComandoFTP cmdFTPAtual, cmdFTPNovo;
32      private boolean achou;
33  
34      /**
35       * {@inheritDoc}
36       */
37      public CacheFTPTest(String testName) {
38          super(testName);
39      }
40  
41      /**
42       * {@inheritDoc}
43       */
44      @Override
45      protected void setUp() throws Exception {
46          instancia = CacheFTP.getCache();
47          super.setUp();
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      protected void tearDown() throws Exception {
55          instancia.clear();
56          instancia.saveFile();
57      }
58  
59      /**
60       * Teste do método addCommand, da classe webfotos.util.CacheFTP.
61       * Verifica se todos e apenas comandos válidos são inseridos.
62       */
63      public void testAddCommand() {
64          log.info("addCommand");
65  
66          instancia.clear();
67  
68          /** 
69           * Fase 1: tenta colocar primeiro os comandos reduntantes e depois 
70           * sobrepõe as com os comandos genéricos (foto = 0).
71           * testa em conjunto se comandos invalidos são inseridos
72           */
73          this.carregaComandos(comandosRedundantes);
74          this.carregaComandos(comandosCertos);
75          this.carregaComandos(comandosErrados);
76          
77          this.verificaPresenca();
78          this.verificaAusencia();
79  
80          instancia.clear();
81  
82          /** 
83           * Fase 2: tenta colocar primeiro os comandos certos e depois sobrepor
84           * com as redundancias. Procurando saber se a sobreposição está errada.
85           */
86          this.carregaComandos(comandosCertos);
87          this.carregaComandos(comandosRedundantes);
88          
89          this.verificaPresenca();
90          this.verificaAusencia();
91      }
92  
93      /**
94       * Teste do método add, da classe webfotos.util.CacheFTP.
95       */
96      public void testAdd() {
97          log.info("add");
98          
99          instancia.clear();
100 
101         /** Tenta acrescentar todos os comandos certos */
102         for(int i = 0; i < comandosCertos.length; i++) {
103             instancia.add(new ComandoFTP(comandosCertos[i][0], comandosCertos[i][1], comandosCertos[i][2]));
104         }
105         
106         /** Tenta acrescentar todos os comandos errados */
107         for(int i = 0; i < comandosErrados.length; i++) {
108             instancia.add(new ComandoFTP(comandosCertos[i][0], comandosCertos[i][1], comandosCertos[i][2]));
109         }
110         
111         this.verificaPresenca();
112         this.verificaAusencia();
113     }
114 
115     /**
116      * Procura na lista e verifica se todos os comandos certos foram inseridos
117      */
118     private void verificaPresenca () {
119         for(int i = 0; i < comandosCertos.length; i++) {
120             iListaArq = instancia.iterator();
121             achou = false;
122             cmdFTPNovo = new ComandoFTP(comandosCertos[i][0], comandosCertos[i][1], comandosCertos[i][2]);
123             while(iListaArq.hasNext()) {
124                 cmdFTPAtual = (ComandoFTP) iListaArq.next();
125                 achou = cmdFTPAtual.equals(cmdFTPNovo);
126                 if(achou) {
127                     break;
128                 }
129             }
130             assertTrue("Não encontrou o Comando: " + cmdFTPNovo.toString(), achou);
131         }
132     }
133     
134     /**
135      * Procura na lista e verifica se algum comando invalido foi inserido
136      */
137     private void verificaAusencia() {
138         for(int i = 0; i < comandosErrados.length; i++) {
139             iListaArq = instancia.iterator();
140             achou = false;
141             cmdFTPNovo = new ComandoFTP(comandosErrados[i][0], comandosErrados[i][1], comandosErrados[i][2]);
142             while(iListaArq.hasNext()) {
143                 cmdFTPAtual = (ComandoFTP) iListaArq.next();
144                 achou = cmdFTPAtual.equals(cmdFTPNovo);
145                 if(achou) {
146                     break;
147                 }
148             }
149             assertFalse("Encontrou o Comando Invalido: " + cmdFTPNovo.toString(), achou);
150         }
151     }
152     
153     /**
154      * Tenta acrescentar todos os comandos da lista
155      * @param arrayComandos Lista de comandos para execucao
156      */
157     private void carregaComandos(int[][] arrayComandos) {
158         
159         for(int i = 0; i < arrayComandos.length; i++) {
160             instancia.addCommand(arrayComandos[i][0], arrayComandos[i][1], arrayComandos[i][2]);
161         }
162         
163     }
164 }