View Javadoc
1   package org.apache.maven.index.cli;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0    
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.util.Random;
26  import org.codehaus.plexus.util.FileUtils;
27  import org.eclipse.sisu.launch.InjectedTest;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertFalse;
32  import static org.junit.Assert.assertTrue;
33  
34  public abstract class AbstractNexusIndexerCliTest
35          extends InjectedTest
36  {
37  
38      private static final long rand = new Random().nextLong();
39  
40      /*
41       * private static final String DEST_DIR = new File( getBasedir(), "target/tests/clitest/output-"+rand
42       * ).getAbsolutePath(); private static final String INDEX_DIR = new File( getBasedir(),
43       * "target/tests/clitest/index-"+rand ).getAbsolutePath(); private static final String UNPACK_DIR = new File(
44       * getBasedir(), "target/tests/clitest/unpack-"+rand ).getAbsolutePath(); private static final String TEST_REPO =
45       * new File( getBasedir(), "src/test/repo" ).getAbsolutePath();
46       */
47      private final String DEST_DIR =
48          new File( getBasedir(), "target/tests/clitest-" + rand + "/output" ).getAbsolutePath();
49  
50      private final String INDEX_DIR =
51          new File( getBasedir(), "target/tests/clitest-" + rand + "/index" ).getAbsolutePath();
52  
53      private final String UNPACK_DIR =
54          new File( getBasedir(), "target/tests/clitest-" + rand + "/unpack" ).getAbsolutePath();
55  
56      private final String TEST_REPO = new File( getBasedir(), "src/test/repo" ).getAbsolutePath();
57  
58      protected OutputStream out;
59  
60      @Override
61      public void setUp()
62          throws Exception
63      {
64          super.setUp();
65  
66          out = new OutputStream()
67          {
68  
69              private StringBuilder buf = new StringBuilder();
70  
71              @Override
72              public void write( int b )
73              {
74                  byte[] bytes = new byte[1];
75                  bytes[0] = (byte) b;
76                  buf.append( new String( bytes ) );
77              }
78  
79              @Override
80              public String toString()
81              {
82                  String string = buf.toString();
83                  buf = new StringBuilder();
84                  return string;
85              }
86          };
87  
88          FileUtils.deleteDirectory( INDEX_DIR );
89          FileUtils.deleteDirectory( DEST_DIR );
90          FileUtils.deleteDirectory( UNPACK_DIR );
91  
92      }
93  
94      @Override
95      public void tearDown()
96          throws Exception
97      {
98          super.tearDown();
99  
100     }
101 
102     protected File getTestFile( String path )
103     {
104         return new File( new File( getBasedir() ), path );
105     }
106 
107     @Test
108     public void testNoArgs()
109     {
110         int code = execute();
111         String output = out.toString();
112         assertEquals( output, 1, code );
113         assertTrue( "Should print usage", output.contains( "usage: nexus-indexer [options]" ) );
114     }
115 
116     @Test
117     public void testRequiredArgs()
118         throws Exception
119     {
120         int code = execute( "--repository", TEST_REPO, "--index", INDEX_DIR, "-d", DEST_DIR );
121         String output = out.toString();
122         assertEquals( output, 0, code );
123     }
124 
125     @Test
126     public void testUnpack()
127         throws Exception
128     {
129         // first create an index, in the destination dir
130         execute( "--repository", TEST_REPO, "--index", INDEX_DIR, "-d", DEST_DIR );
131         // then unpack it
132         int code = execute( "--unpack", "--index", DEST_DIR, "-d", UNPACK_DIR );
133         String output = out.toString();
134         assertEquals( output, 0, code );
135         
136         //FIXME: Looks strange that a newly generated index can not be reopened.
137         //assertIndexFiles( UNPACK_DIR );
138     }
139 
140     @Test
141     public void testMissingArgs()
142         throws IOException
143     {
144         String usage = "usage: nexus-indexer";
145 
146         int code = execute( "--repository", "--index", INDEX_DIR, "-d", DEST_DIR );
147         String output = out.toString();
148         assertEquals( output, 1, code );
149         assertTrue( "Should print bad usage", output.contains( usage ) );
150 
151         code = execute( "--repository", TEST_REPO, "--index", "-d", DEST_DIR );
152         output = out.toString();
153         assertEquals( output, 1, code );
154         assertTrue( "Should print bad usage", output.contains( usage ) );
155 
156         code = execute( "--repository", TEST_REPO, "--index", INDEX_DIR, "-d" );
157         output = out.toString();
158         assertEquals( output, 1, code );
159         assertTrue( "Should print bad usage", output.contains( usage ) );
160 
161         code = execute( "--repository", "--index", "-d" );
162         output = out.toString();
163         assertEquals( output, 1, code );
164         assertTrue( "Should print bad usage but '" + output + "'", output.contains( usage ) );
165 
166         assertFalse( "Index file was generated", new File( INDEX_DIR ).exists() );
167     }
168 
169     @Test
170     public void testAbrvsRequiredArgs()
171         throws Exception
172     {
173         int code = execute( "-r", TEST_REPO, "-i", INDEX_DIR, "-d", DEST_DIR );
174         String output = out.toString();
175         assertEquals( output, 0, code );
176     }
177 
178     @Test
179     public void testInvalidRepo()
180         throws Exception
181     {
182         int code =
183             execute( "-r", new File( "target/undexinting/repo/to/try/what/will/happen/here" ).getCanonicalPath(), "-i",
184                      INDEX_DIR, "-d", DEST_DIR );
185         String output = out.toString();
186         assertEquals( output, 1, code );
187     }
188 
189     protected abstract int execute( String... args );
190 
191 }